From 1418709bdfce0b6b7aaece41f7295348930e802b Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 3 May 2022 01:13:03 +0300 Subject: [PATCH] struct 1 --- CMakeLists.txt | 2 ++ struct.c | 16 ++++++++++++++++ struct.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 struct.c create mode 100644 struct.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 198451e..e1e629a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(HEADERS array.h bitmap.h endian.h + struct.h ) set(SOURCES @@ -16,6 +17,7 @@ set(SOURCES array.c bitmap.c endian.c + struct.c ) add_library(cutil STATIC ${SOURCES} ${HEADERS}) diff --git a/struct.c b/struct.c new file mode 100644 index 0000000..0780756 --- /dev/null +++ b/struct.c @@ -0,0 +1,16 @@ +#include "struct.h" + +struct cu_value_s test_struct[] = { + {.type = Sequence, .sequence = {.size = 6}}, + {.type = Array, .array = {.item = 4, .count = 12, .index = 0}}, + {.type = Int16}, + {.type = Array, .array = {.item = 14, .count = 3, .index = 1}}, + {.type = NoValue} +}; + +/*CU_STRUCT_BEGIN(test) +CU_VALUE_SEQUENCE(6) +CU_VALUE_ARRAY(4, 12) +CU_VALUE_INT16() +CU_VALUE_ARRAY_INDEX(14, 3) +CU_STRUCT_END()*/ diff --git a/struct.h b/struct.h new file mode 100644 index 0000000..6d5399b --- /dev/null +++ b/struct.h @@ -0,0 +1,48 @@ +#ifndef __STRUCT_H +#define __STRUCT_H + +#include "cutypes.h" + +enum cu_value_type_e { + NoValue, + Sequence, + Array, + Int8, + Int16, + Int32, + Int64 +}; + +struct cu_value_s { + enum cu_value_type_e type; + union { + struct { + uint size; + } sequence; + struct { + uint item; + uint count : 31; + uint index : 1; + } array; + }; +}; + +#define CU_STRUCT_BEGIN(name) \ + struct cu_value_s cu_struct_##name[] = { +#define CU_STRUCT_END() \ + {.type = NoValue}}; +#define CU_VALUE_INT8() {.type = Int8}, +#define CU_VALUE_INT16() {.type = Int16}, +#define CU_VALUE_INT32() {.type = Int32}, +#define CU_VALUE_INT64() {.type = Int64}, +#define CU_VALUE_SEQUENCE(_size) \ + {.type = Sequence, .sequence = {.size = (_size)}, +#define CU_VALUE_ARRAY(_item, _count) \ + {.type = Array, .array = {.item = (_item), .count = (_count), .index = 0}}, +#define CU_VALUE_ARRAY_INDEX(_item, _index) \ + {.type = Array, .array = {.item = (_item), .count = (_index), .index = 1}}, + +typedef void (*cu_value_operate)(struct cu_value_s* type, + uint8_t** in, uint8_t** out); + +#endif \ No newline at end of file