value.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef VALUEPTR_H
  2. #define VALUEPTR_H
  3. #include "../utils/types.h"
  4. #include <iostream>
  5. #include <iomanip>
  6. namespace binom {
  7. class ValueRef {
  8. ValType type;
  9. union pointer {
  10. void* ptr;
  11. bool* boolptr;
  12. ui8* ui8ptr;
  13. ui16* ui16ptr;
  14. ui32* ui32ptr;
  15. ui64* ui64ptr;
  16. i8* i8ptr;
  17. i16* i16ptr;
  18. i32* i32ptr;
  19. i64* i64ptr;
  20. f32* f32ptr;
  21. f64* f64ptr;
  22. pointer(void* ptr) : ptr(ptr) {}
  23. } ptr;
  24. friend class ValueIterator;
  25. public:
  26. ValueRef(Primitive& var) : type(toValueType(**reinterpret_cast<VarType**>(&var))), ptr(*reinterpret_cast<byte**>(&var) + 1) {}
  27. ValueRef(ValType type, void* pointer) : type(type), ptr(pointer) {}
  28. ValueRef(VarType type, void* pointer) : type(toValueType(type)), ptr(pointer) {}
  29. ValueRef(const ValueRef& other) : type(other.type), ptr(other.ptr.ptr) {}
  30. ValueRef(const ValueRef&& other) : type(other.type), ptr(other.ptr.ptr) {}
  31. ValueRef(const ValueIterator& it);
  32. ValueRef(const ValueIterator&& it);
  33. ValType getType() const {return type;}
  34. inline ui8 getSize() const {return toSize(type);}
  35. bool asBool() const;
  36. ui64 asUnsigned() const;
  37. i64 asSigned() const;
  38. f64 asFloat() const;
  39. bool setBool(const bool value);
  40. ui64 setUnsigned(const ui64 value);
  41. i64 setSigned(const i64 value);
  42. f64 setFloat(const f64 value);
  43. inline ui8 asUi8() const {return asUnsigned();}
  44. inline ui16 asUi16() const {return asUnsigned();}
  45. inline ui32 asUi32() const {return asUnsigned();}
  46. inline ui64 asUi64() const {return asUnsigned();}
  47. inline i8 asI8() const {return asSigned();}
  48. inline i16 asI16() const {return asSigned();}
  49. inline i32 asI32() const {return asSigned();}
  50. inline i64 asI64() const {return asSigned();}
  51. inline f32 asF32() const {return asFloat();}
  52. inline f64 asF64() const {return asFloat();}
  53. inline operator ui8 () {return asUi8();}
  54. inline operator ui16 () {return asUi16();}
  55. inline operator ui32 () {return asUi32();}
  56. inline operator ui64 () {return asUi64();}
  57. inline operator i8 () {return asI8();}
  58. inline operator i16 () {return asI16();}
  59. inline operator i32 () {return asI32();}
  60. inline operator i64 () {return asI64();}
  61. inline operator f32 () {return asF32();}
  62. inline operator f64 () {return asF64();}
  63. inline ValueIterator& toIterator();
  64. inline bool operator=(const bool value) {return setBool(value);}
  65. inline ui64 operator=(const ui64 value) {return setUnsigned(value);}
  66. inline i64 operator=(const i64 value) {return setSigned(value);}
  67. inline f64 operator=(const f64 value) {return setFloat(value);}
  68. ValueRef& operator=(const ValueRef& other);
  69. ValueRef& operator<<(const ValueRef& other); //!< Set value from other to this
  70. inline bool operator==(ValueRef other) const {return asUi64() == other.asUi64();}
  71. inline bool operator!=(ValueRef other) const {return asUi64() != other.asUi64();}
  72. inline bool operator>(ValueRef other) const {return asUi64() > other.asUi64();}
  73. inline bool operator>=(ValueRef other) const {return asUi64() >= other.asUi64();}
  74. inline bool operator<(ValueRef other) const {return asUi64() < other.asUi64();}
  75. inline bool operator<=(ValueRef other) const {return asUi64() <= other.asUi64();}
  76. };
  77. class ValueIterator {
  78. ValueRef pointer;
  79. inline ui8 getShift() const {
  80. switch (pointer.type) {
  81. case ValType::byte: return 1;
  82. case ValType::word: return 2;
  83. case ValType::dword: return 4;
  84. case ValType::qword: return 8;
  85. default: throw Exception(ErrCode::binom_invalid_type);
  86. }
  87. }
  88. public:
  89. ValueIterator(ValType type, void* pointer) : pointer(type, pointer) {}
  90. ValueIterator(VarType type, void* pointer) : pointer(type, pointer) {}
  91. ValueIterator(ValueRef& val_ptr) : pointer(val_ptr) {}
  92. ValueIterator(ValueRef&& val_ptr) : pointer(val_ptr) {}
  93. ValueIterator(ValueIterator& other) : pointer(other.pointer) {}
  94. ValueIterator(ValueIterator&& other) : pointer(other.pointer) {}
  95. inline ValueRef& operator*() const {return *const_cast<ValueRef*>(&pointer);}
  96. inline ValueRef* operator->() const {return const_cast<ValueRef*>(&pointer);}
  97. ValueRef operator[](i64 index) {
  98. return ValueRef(pointer.type, pointer.ptr.ui8ptr + getShift() * index);
  99. }
  100. ValueIterator operator+(i64 shift) {return ValueIterator(*this) += shift;}
  101. ValueIterator operator-(i64 shift) {return ValueIterator(*this) -= shift;}
  102. ValueIterator& operator++() {
  103. pointer.ptr.ui8ptr += getShift();
  104. return *this;
  105. }
  106. ValueIterator& operator+=(i64 shift) {
  107. pointer.ptr.ui8ptr += getShift() * shift;
  108. return *this;
  109. }
  110. ValueIterator& operator--() {
  111. pointer.ptr.ui8ptr -= getShift();
  112. return *this;
  113. }
  114. ValueIterator& operator-=(i64 shift) {
  115. pointer.ptr.ui8ptr -= getShift() * shift;
  116. return *this;
  117. }
  118. ValueIterator& operator=(const ValueIterator& other) {
  119. pointer = other.pointer;
  120. return *this;
  121. }
  122. ValueIterator& operator=(const ValueRef& other) {
  123. pointer = other;
  124. return *this;
  125. }
  126. inline bool operator==(const ValueIterator& other) const {return pointer.ptr.ptr == other.pointer.ptr.ptr;}
  127. inline bool operator!=(const ValueIterator& other) const {return pointer.ptr.ptr != other.pointer.ptr.ptr;}
  128. };
  129. }
  130. #endif // VALUEPTR_H