//
// Created by xajhuang on 2023/3/17.
//

#ifndef VCPE_COMMON_H
#define VCPE_COMMON_H
#ifdef __cplusplus
extern "C" {
#endif

#define _STR(s) #s
#define STR(s)  _STR(s)

/**
 * container_of - cast a member of a structure out to the containing structure
 *
 * @ptr:        the pointer to the member.
 * @type:       the type of the container struct this is embedded in.
 * @member:     the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member)                    \
    ({                                                     \
        const typeof(((type *)0)->member) *__mptr = (ptr); \
        (type *)((char *)__mptr - offsetof(type, member)); \
    })

#ifndef MIN
#define MIN(a, b)                                  \
    ({                                             \
        typeof(a) __min1__ = (a);                  \
        typeof(b) __min2__ = (b);                  \
        (void)(&__min1__ == &__min2__);            \
        __min1__ < __min2__ ? __min1__ : __min2__; \
    })
#endif

#ifndef MAX
#define MAX(a, b)                                  \
    ({                                             \
        typeof(a) __max1__ = (a);                  \
        typeof(b) __max2__ = (b);                  \
        (void)(&__max1__ == &__max2__);            \
        __max1__ > __max2__ ? __max1__ : __max2__; \
    })
#endif

#ifndef ARRAY_SIZE
//#ifdef __cplusplus
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
//#else
//#define ARRAY_SIZE_TYPE_CHECK(a) (sizeof(typeof(int[1 - 2 * !!__builtin_types_compatible_p(typeof(a), typeof(&a[0]))])))
//#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + ARRAY_SIZE_TYPE_CHECK(a) * 0)
//#endif
#endif

#ifndef UNUSED
#define UNUSED(x) UNUSED_##x __attribute__((__unused__))
#endif

#ifndef MAX_PATH
#define MAX_PATH (512)
#endif

#ifndef TRUE
#define TRUE (1)
#endif

#ifndef FALSE
#define FALSE (0)
#endif

#define VERIFY_STRING(s)     (((s) != NULL) && (strlen(s) > 0))
#define SAFETY_STR_STRING(s) ((s) == NULL ? "NULL" : (s))

#define MAX_IP_V4_STR        (16)
#define MAX_MAC_ADDR_STR     (18)

#define DEBUG_CODE_LINE()    LOG_MSG(debug, "\n")

typedef unsigned char      U8;
typedef unsigned short     U16;
typedef unsigned int       U32;
typedef unsigned long long U64;
typedef char               S8;
typedef short              S16;
typedef int                S32;
typedef long long          S64;

#ifdef __cplusplus
}
#endif
#endif    //VCPE_COMMON_H