buffer_array.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef BUFFER_ARRAY_H
  2. #define BUFFER_ARRAY_H
  3. #include "../utils/types.h"
  4. #include "../utils/var_mem_mngr.h"
  5. #include "value.h"
  6. namespace binom {
  7. class BufferArray : VarMemoryManager<BufferArray> {
  8. friend class VarMemoryManager<BufferArray>;
  9. union types {
  10. void* ptr;
  11. VarType* type;
  12. byte* bytes;
  13. types(void* ptr) : ptr(ptr) {}
  14. } data;
  15. inline ui64& length() const {return *reinterpret_cast<ui64*>(data.bytes + 1);}
  16. inline byte*& ptrImpl() {return data.bytes;}
  17. inline ui64 msizeImpl() const {return 9 + length() * getMemberSize();}
  18. void* cloneImpl() const;
  19. void destroyImpl();
  20. friend class Variable;
  21. friend class Object;
  22. friend struct NamedVariable;
  23. struct _value_7_1 {ui8 val:7;bool sign:1;};
  24. static ByteArray toChainNumber(ui64 number);
  25. static ui64 fromChainNumber(ByteArray::iterator& it);
  26. BufferArray(void* buffer) : data(buffer) {}
  27. public:
  28. typedef ValueIterator iterator;
  29. typedef const ValueIterator const_iterator;
  30. BufferArray(VarType type);
  31. BufferArray(VarType type, size_t count);
  32. BufferArray(ValType type);
  33. BufferArray(ValType type, size_t count);
  34. BufferArray(const std::string_view str);
  35. BufferArray(const std::u16string_view str);
  36. BufferArray(const std::u32string_view str);
  37. BufferArray(const std::wstring_view wstr);
  38. BufferArray(const std::string str) : BufferArray(std::string_view(str)) {}
  39. BufferArray(const std::u16string str) : BufferArray(std::u16string_view(str)) {}
  40. BufferArray(const std::u32string str) : BufferArray(std::u32string_view(str)) {}
  41. BufferArray(const std::wstring wstr) : BufferArray(std::wstring_view(wstr)) {}
  42. BufferArray(const char* c_str) : BufferArray(std::string_view(c_str)) {}
  43. BufferArray(const char16_t* c_str) : BufferArray(std::u16string_view(c_str)) {}
  44. BufferArray(const char32_t* c_str) : BufferArray(std::u32string_view(c_str)) {}
  45. BufferArray(const wchar_t* c_wstr) : BufferArray(std::wstring_view(c_wstr)) {}
  46. BufferArray(size_t size, ui8 value);
  47. BufferArray(size_t size, ui16 value);
  48. BufferArray(size_t size, ui32 value);
  49. BufferArray(size_t size, ui64 value);
  50. BufferArray(size_t size, i8 value);
  51. BufferArray(size_t size, i16 value);
  52. BufferArray(size_t size, i32 value);
  53. BufferArray(size_t size, i64 value);
  54. BufferArray(ui8* values, size_t count);
  55. BufferArray(ui16* values, size_t count);
  56. BufferArray(ui32* values, size_t count);
  57. BufferArray(ui64* values, size_t count);
  58. BufferArray(i8* values, size_t count);
  59. BufferArray(i16* values, size_t count);
  60. BufferArray(i32* values, size_t count);
  61. BufferArray(i64* values, size_t count);
  62. BufferArray(ValType type, const void* ptr, size_t count);
  63. BufferArray(ui8arr array);
  64. BufferArray(ui16arr array);
  65. BufferArray(ui32arr array);
  66. BufferArray(ui64arr array);
  67. BufferArray(i8arr array);
  68. BufferArray(i16arr array);
  69. BufferArray(i32arr array);
  70. BufferArray(i64arr array);
  71. BufferArray(ByteArray arr);
  72. BufferArray(const BufferArray& other);
  73. BufferArray(BufferArray&& other);
  74. BufferArray(Primitive primitive);
  75. ~BufferArray() {destroy();}
  76. inline VarType getType() const {return *data.type;}
  77. inline ValType getValType() const {return toValueType(getType());}
  78. inline bool inRange(ui64 index) const {return length() > index;}
  79. inline bool isEmpty() const {return !length();}
  80. inline ui64 getMemberCount() const {return *reinterpret_cast<ui64*>(data.bytes + 1);}
  81. inline ui8 getMemberSize() const {return toSize(getValType());}
  82. inline ui64 getDataSize() const {return msize() - 9;}
  83. inline void* getDataPointer() const {return data.bytes + 9;}
  84. inline bool isByte() const {return getType() == VarType::byte_array;}
  85. inline bool isWord() const {return getType() == VarType::word_array;}
  86. inline bool isDWord() const {return getType() == VarType::dword_array;}
  87. inline bool isQWord() const {return getType() == VarType::qword_array;}
  88. bool isPrintable() const;
  89. bool isWPrintable() const;
  90. ByteArray serialize() const;
  91. static BufferArray deserialize(ByteArray::iterator& it);
  92. static ui64 serializedSize(ByteArray::iterator it);
  93. static inline ui8 getMemberSize(VarType type) {
  94. switch (type) {
  95. case VarType::byte_array: return 1;
  96. case VarType::word_array: return 2;
  97. case VarType::dword_array: return 4;
  98. case VarType::qword_array: return 8;
  99. default: throw Exception(ErrCode::binom_invalid_type);
  100. }
  101. }
  102. inline ValueRef getValue(ui64 index) const {
  103. if(index >= getMemberCount()) throw Exception(ErrCode::binom_out_of_range, "Out of buffer array range!");
  104. return begin()[index];
  105. }
  106. ValueRef pushBack(ui64 value);
  107. ValueRef pushBack(i64 value);
  108. ValueRef pushBack(f64 value);
  109. iterator pushBack(const BufferArray& other);
  110. inline ValueRef pushBack(const ValueRef value) {return pushBack(value.asUnsigned());}
  111. ValueRef pushFront(ui64 value);
  112. ValueRef pushFront(i64 value);
  113. ValueRef pushFront(f64 value);
  114. iterator pushFront(const BufferArray& other);
  115. inline ValueRef pushFront(const ValueRef value) {return pushFront(value.asUnsigned());}
  116. ValueRef insert(const ui64 index, const ui64 value);
  117. ValueRef insert(const ui64 index, const i64 value);
  118. ValueRef insert(const ui64 index, const f64 value);
  119. iterator insert(const ui64 index, const BufferArray& other);
  120. inline ValueRef insert(ui64 index, const ValueRef value) {return insert(index, value.asUnsigned());}
  121. void popBack(const ui64 n = 1);
  122. void popFront(const ui64 n = 1);
  123. void remove(ui64 index, ui64 n = 1);
  124. BufferArray subarr(ui64 index, ui64 n);
  125. void clear();
  126. inline ValueRef operator[](ui64 index) const {return getValue(index);}
  127. inline BufferArray& operator+=(const ui64 value) {pushBack(value); return *this;}
  128. inline BufferArray& operator+=(const i64 value) {pushBack(value); return *this;}
  129. inline BufferArray& operator+=(const f64 value) {pushBack(value); return *this;}
  130. inline BufferArray& operator+=(const BufferArray& other) {pushBack(other); return *this;}
  131. inline BufferArray& operator+=(const ValueRef value) {pushBack(value); return *this;}
  132. BufferArray& operator=(BufferArray other);
  133. bool operator==(BufferArray other) const;
  134. inline bool operator!=(BufferArray other) const {return !(*this == std::move(other));}
  135. bool operator>(BufferArray other) const;
  136. bool operator<(BufferArray other) const;
  137. bool operator>=(BufferArray other) const;
  138. bool operator<=(BufferArray other) const;
  139. i8 getCompare(BufferArray other) const;
  140. iterator findMemory(BufferArray data) const;
  141. iterator findValue(BufferArray data) const;
  142. iterator begin() const;
  143. iterator end() const;
  144. const_iterator cbegin() const;
  145. const_iterator cend() const;
  146. inline operator std::string() const { return toString(); }
  147. inline operator std::u16string() const { return toU16String(); }
  148. inline operator std::u32string() const { return toU32String(); }
  149. inline operator std::wstring() const { return toWString(); }
  150. inline operator ByteArray() const { return toByteArray(); }
  151. inline operator ByteArrayView() const { return toByteArrayView(); }
  152. std::string toString() const;
  153. std::string toU8String() const;
  154. std::u16string toU16String() const;
  155. std::u32string toU32String() const;
  156. std::wstring toWString() const;
  157. ByteArray toByteArray() const;
  158. ByteArrayView toByteArrayView() const;
  159. Variable& asVar() {return *reinterpret_cast<Variable*>(this);}
  160. };
  161. }
  162. const binom::BufferArray operator "" _vbfr(const char* c_str, std::size_t);
  163. #endif