primitive.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef PRIMITIVE_H
  2. #define PRIMITIVE_H
  3. #include "../utils/types.h"
  4. #include "value.h"
  5. namespace binom {
  6. class Primitive {
  7. union types {
  8. void* ptr;
  9. VarType* type;
  10. byte* bytes;
  11. types(void* ptr) : ptr(ptr) {}
  12. } data;
  13. inline ui64 msize() const {
  14. switch (*data.type) {
  15. case VarType::byte: return 2;
  16. case VarType::word: return 3;
  17. case VarType::dword: return 5;
  18. case VarType::qword: return 9;
  19. default: throw Exception(ErrCode::binom_invalid_type);
  20. }
  21. }
  22. void destroy();
  23. void* clone() const;
  24. friend class Variable;
  25. friend class ValueRef;
  26. Primitive(void* buffer) : data(buffer) {}
  27. public:
  28. Primitive(VarType type);
  29. Primitive() : data(nullptr) {}
  30. // Bool init
  31. Primitive(bool value);
  32. // Primitive init
  33. Primitive(ui8 value);
  34. Primitive(ui16 value);
  35. Primitive(ui32 value);
  36. Primitive(ui64 value);
  37. Primitive(i8 value);
  38. Primitive(i16 value);
  39. Primitive(i32 value);
  40. Primitive(i64 value);
  41. Primitive(f32 value);
  42. Primitive(f64 value);
  43. Primitive(ValType type, void* value_ptr);
  44. Primitive(const Primitive& other);
  45. Primitive(Primitive&& other);
  46. Primitive(ValueRef value);
  47. ~Primitive();
  48. inline VarType getType() const {return *data.type;}
  49. inline ValType getValType() const {return toValueType(*data.type);}
  50. inline bool isByte() const {return getType() == VarType::byte_array;}
  51. inline bool isWord() const {return getType() == VarType::word_array;}
  52. inline bool isDWord() const {return getType() == VarType::dword_array;}
  53. inline bool isQWord() const {return getType() == VarType::qword_array;}
  54. ByteArray serialize() const;
  55. static Primitive deserialize(ByteArray::iterator& it);
  56. inline ui8 getMemberSize() const {
  57. switch (*data.type) {
  58. case VarType::byte: return 1;
  59. case VarType::word: return 2;
  60. case VarType::dword: return 4;
  61. case VarType::qword: return 8;
  62. default: throw Exception(ErrCode::binom_invalid_type);
  63. }
  64. }
  65. static inline ui8 getMemberSize(VarType type) {
  66. switch (type) {
  67. case VarType::byte: return 1;
  68. case VarType::word: return 2;
  69. case VarType::dword: return 4;
  70. case VarType::qword: return 8;
  71. default: throw Exception(ErrCode::binom_invalid_type);
  72. }
  73. }
  74. inline ValueRef getValue() const {return ValueRef(*data.type, data.bytes + 1);}
  75. inline ValueRef operator*() const {return getValue();}
  76. inline bool operator=(const bool value) {return (**this).setBool(value);}
  77. inline ui64 operator=(const ui64 value) {return (**this).setUnsigned(value);}
  78. inline i64 operator=(const i64 value) {return (**this).setSigned(value);}
  79. inline f64 operator=(const f64 value) {return (**this).setFloat(value);}
  80. Primitive& operator=(Primitive other);
  81. bool operator==(Primitive other) const;
  82. bool operator!=(Primitive other) const;
  83. bool operator>(Primitive other) const;
  84. bool operator>=(Primitive other) const;
  85. bool operator<(Primitive other) const;
  86. bool operator<=(Primitive other) const;
  87. i8 getCompare(Primitive other) const;
  88. Variable& asVar() {return *reinterpret_cast<Variable*>(this);}
  89. BufferArray toBufferArray();
  90. void* getDataPtr() {return data.bytes + 1;}
  91. };
  92. }
  93. #endif