// Example of X-macros for automatic serialization in C. See file // `binary-serialization.md` in pavnotes2, binmsg_cpp.c, and // binmsg.py. typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef struct { } *stream; // dummy definition to make output compilable #define MESSAGE_TYPE(type) \ DECLARE_STRUCT(type) \ STRUCT_READER(type) \ STRUCT_WRITER(type) #define DECLARE_STRUCT(type) \ typedef struct type { \ type##_fields(STRUCT_FIELD, IGNORE) \ } type; #define IGNORE(type) #define STRUCT_FIELD(type, name) type name; #define STRUCT_READER(type) \ bool read_##type##_big_endian(stream out, type *obj) { \ type##_fields(READ_FIELD_BIG_ENDIAN, READ_PADDING) \ return true; \ } #define READ_FIELD_BIG_ENDIAN(type, name) \ if (!read_##type##_big_endian(out, &obj->name)) return false; #define READ_PADDING(type) \ if (!read_dummy_##type(out)) return false; #define STRUCT_WRITER(type) \ bool write_##type##_big_endian(stream out, type *obj) { \ type##_fields(WRITE_FIELD_BIG_ENDIAN, WRITE_PADDING) \ return true; \ } #define WRITE_FIELD_BIG_ENDIAN(type, name) \ if (!write_##type##_big_endian(out, &obj->name)) return false; #define WRITE_PADDING(type) \ if (!write_dummy_##type(out)) return false; bool read_u8_big_endian(stream out, u8 *obj); bool read_dummy_u8(stream out); bool read_u32_big_endian(stream out, u32 *obj); bool write_u8_big_endian(stream out, u8 *obj); bool write_dummy_u8(stream out); bool write_u32_big_endian(stream out, u32 *obj);