DataType.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef DATATYPE_H
  2. #define DATATYPE_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include "Check.h"
  9. #include "Types.h"
  10. typedef enum {
  11. VT_INVALID = 0,
  12. VT_NOT_SET,
  13. VT_INT,
  14. VT_FLOAT,
  15. VT_POINTER,
  16. VT_ARRAY,
  17. VT_LAST
  18. } ValueType;
  19. #define VALUE_TYPE_BIT_SIZE 4
  20. static_assert(VT_LAST < (1 << VALUE_TYPE_BIT_SIZE));
  21. typedef struct {
  22. uint32 typeAndOffset;
  23. union {
  24. int32 intValue;
  25. float32 floatValue;
  26. } data;
  27. } Value;
  28. typedef enum {
  29. RDT_INVALID = 0,
  30. RDT_INT,
  31. RDT_FLOAT,
  32. RDT_VOID,
  33. RDT_STRUCT,
  34. RDT_LAST
  35. } RawDataType;
  36. typedef struct {
  37. uint32 data;
  38. } DataType;
  39. #define DT_TYPE_BIT_SIZE 4
  40. #define DT_IS_POINTER_BIT_SIZE 1
  41. #define DT_IS_ARRAY_BIT_SIZE 1
  42. #define DT_STRUCT_ID_BIT_SIZE \
  43. (32 - DT_TYPE_BIT_SIZE - DT_IS_POINTER_BIT_SIZE - DT_IS_ARRAY_BIT_SIZE)
  44. static_assert(RDT_LAST < (1 << DT_TYPE_BIT_SIZE));
  45. #define DT_MAX_STRUCT_ID ((1 << DT_STRUCT_ID_BIT_SIZE) - 1)
  46. #define DT_TYPE_OFFSET 0
  47. #define DT_IS_POINTER_OFFSET (DT_TYPE_OFFSET + DT_TYPE_BIT_SIZE)
  48. #define DT_IS_ARRAY_OFFSET (DT_IS_POINTER_OFFSET + DT_IS_POINTER_BIT_SIZE)
  49. #define DT_STRUCT_ID_OFFSET (DT_IS_ARRAY_OFFSET + DT_IS_ARRAY_BIT_SIZE)
  50. typedef struct {
  51. const char* name;
  52. DataType type;
  53. char padding[sizeof(const char*) - sizeof(DataType)];
  54. } StructVariable;
  55. typedef struct {
  56. const char* name;
  57. uint16 id;
  58. char padding[2];
  59. int amount;
  60. StructVariable* vars;
  61. } Struct;
  62. typedef struct {
  63. int capacity;
  64. int entries;
  65. Struct* data;
  66. } Structs;
  67. check_return const char* vtGetName(ValueType vt);
  68. check_return ValueType vGetType(Value v);
  69. check_return bool vSetType(Value* v, ValueType vt);
  70. check_return int32 vGetOffset(Value v);
  71. check_return bool vSetOffset(Value* v, int32 offset);
  72. check_return bool dtGetSize(DataType dt, const Structs* sts, int* size);
  73. check_return DataType dtInt(void);
  74. check_return DataType dtFloat(void);
  75. check_return DataType dtVoid(void);
  76. check_return DataType dtStruct(const Struct* st);
  77. check_return DataType dtText(void);
  78. check_return DataType dtToArray(DataType dt);
  79. check_return DataType dtRemoveArray(DataType dt);
  80. check_return Struct* dtGetStruct(const Structs* sts, DataType dt);
  81. check_return DataType dtToPointer(DataType dt);
  82. bool dtRemovePointer(DataType* dt);
  83. check_return RawDataType dtGetType(DataType dt);
  84. check_return bool dtCompare(DataType a, DataType b);
  85. check_return bool dtIsInt(DataType dt);
  86. check_return bool dtIsFloat(DataType dt);
  87. check_return bool dtIsVoid(DataType dt);
  88. check_return bool dtIsArray(DataType dt);
  89. check_return bool dtIsStruct(DataType dt);
  90. check_return bool dtIsPointer(DataType dt);
  91. check_return const char* dtGetName(const Structs* sts, DataType dt);
  92. void stAddVariable(Struct* st, const char* name, DataType type);
  93. void stsInit(Structs* sts);
  94. void stsDelete(Structs* sts);
  95. check_return Struct* stsSearch(Structs* sts, const char* name);
  96. check_return Struct* stsAdd(Structs* sts, const char* name);
  97. void gstsInit(void);
  98. void gstsDelete(void);
  99. check_return Structs* gstsGet(void);
  100. check_return Struct* gstsAdd(const char* name);
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif