wut/include/wut_structsize.h
2018-06-20 11:20:43 +01:00

34 lines
1.1 KiB
C

#pragma once
#include <assert.h>
#include <stddef.h>
// Ensure structs are correct size & offsets
#if defined(static_assert)
# define WUT_CHECK_SIZE(Type, Size) \
static_assert(sizeof(Type) == Size, \
#Type " must be " #Size " bytes")
# define WUT_CHECK_OFFSET(Type, Offset, Field) \
static_assert(offsetof(Type, Field) == Offset, \
#Type "::" #Field " must be at offset " #Offset)
#else
# define WUT_CHECK_SIZE(Type, Size)
# define WUT_CHECK_OFFSET(Type, Offset, Field)
#endif
// Workaround weird macro concat ## behaviour
#define WUT_PP_CAT(a, b) WUT_PP_CAT_I(a, b)
#define WUT_PP_CAT_I(a, b) WUT_PP_CAT_II(~, a ## b)
#define WUT_PP_CAT_II(p, res) res
// Allow us to easily add UNKNOWN / PADDING bytes into our structs,
// generates unique variable names using __COUNTER__
#define WUT_UNKNOWN_BYTES(Size) char WUT_PP_CAT(__unk, __COUNTER__) [Size]
#define WUT_PADDING_BYTES(Size) WUT_UNKNOWN_BYTES(Size)
// Unknown struct size
#define WUT_UNKNOWN_SIZE(x)
// Marks a struct as packed
#define WUT_PACKED __attribute__((__packed__))