JaffarPlus
High-performance best-first search optimizer for tool-assisted speedruns
Loading...
Searching...
No Matches
property.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <cstring>
10#include <jaffarCommon/exceptions.hpp>
11#include <jaffarCommon/hash.hpp>
12#include <string>
13
14namespace jaffarPlus
15{
16
25{
26public:
42
45 {
47 big
48 };
49
51 Property() = delete;
52
60 Property(const std::string& name, void* const pointer, const datatype_t datatype, endianness_t endianness)
61 : _name(name), _pointer(pointer), _datatype(datatype), _endianness(endianness), _nameHash(jaffarCommon::hash::hashString(name))
62 {
63 }
64
69 __INLINE__ size_t getSize() const { return getDatatypeSize(_datatype); }
70
77 static __INLINE__ endianness_t parseEndiannessName(const std::string& endiannessName)
78 {
79 if (endiannessName == "Little") return endianness_t::little;
80 if (endiannessName == "Big") return endianness_t::big;
81
82 JAFFAR_THROW_LOGIC("Endianness '%s' not recognized.", endiannessName.c_str());
83 }
84
91 static __INLINE__ datatype_t parseDatatypeName(const std::string& datatypeName)
92 {
93 if (datatypeName == "UINT8") return datatype_t::dt_uint8;
94 if (datatypeName == "UINT16") return datatype_t::dt_uint16;
95 if (datatypeName == "UINT32") return datatype_t::dt_uint32;
96 if (datatypeName == "UINT64") return datatype_t::dt_uint64;
97 if (datatypeName == "INT8") return datatype_t::dt_int8;
98 if (datatypeName == "INT16") return datatype_t::dt_int16;
99 if (datatypeName == "INT32") return datatype_t::dt_int32;
100 if (datatypeName == "INT64") return datatype_t::dt_int64;
101 if (datatypeName == "BOOL") return datatype_t::dt_bool;
102 if (datatypeName == "FLOAT32") return datatype_t::dt_float32;
103 if (datatypeName == "FLOAT64") return datatype_t::dt_float64;
104
105 JAFFAR_THROW_LOGIC("Data type '%s' not recognized.", datatypeName.c_str());
106 }
107
114 static __INLINE__ size_t getDatatypeSize(const datatype_t datatype)
115 {
116 switch (datatype)
117 {
118 case dt_uint8: return 1;
119 case dt_uint16: return 2;
120 case dt_uint32: return 4;
121 case dt_uint64: return 8;
122 case dt_int8: return 1;
123 case dt_int16: return 2;
124 case dt_int32: return 4;
125 case dt_int64: return 8;
126 case dt_bool: return 1;
127 case dt_float32: return 4;
128 case dt_float64: return 8;
129 }
130
131 JAFFAR_THROW_LOGIC("Unidentified datatype %d\n", datatype);
132 }
133
143 template <typename T>
144 __INLINE__ T getValue() const
145 {
146 // Otherwise convert to big endian
147 const auto size = getSize();
148 const auto bufferSize = sizeof(T);
149 if (size != bufferSize) JAFFAR_THROW_LOGIC("Incompatible datatypes while getting value of property '%s'", getName().c_str());
150
151 // If its little endian, return value as is
152 if (_endianness == endianness_t::little) return *(T*)_pointer;
153
154 // Converting to big endian value byte by byte
155 T value;
156 const auto sBuf = (uint8_t*)_pointer;
157 auto dBuf = (uint8_t*)&value;
158 if (size == 1) { dBuf[0] = sBuf[0]; }
159 if (size == 2)
160 {
161 dBuf[0] = sBuf[1];
162 dBuf[1] = sBuf[0];
163 }
164 if (size == 4)
165 {
166 dBuf[0] = sBuf[3];
167 dBuf[1] = sBuf[2];
168 dBuf[2] = sBuf[1];
169 dBuf[3] = sBuf[0];
170 }
171 if (size == 8)
172 {
173 dBuf[0] = sBuf[7];
174 dBuf[1] = sBuf[6];
175 dBuf[2] = sBuf[5];
176 dBuf[3] = sBuf[4];
177 dBuf[4] = sBuf[3];
178 dBuf[5] = sBuf[2];
179 dBuf[6] = sBuf[1];
180 dBuf[7] = sBuf[0];
181 }
182 return value;
183 }
184
186 datatype_t getDatatype() const { return _datatype; }
188 std::string getName() const { return _name; }
190 jaffarCommon::hash::hash_t getNameHash() const { return _nameHash; }
192 void* getPointer() const { return _pointer; }
193
194private:
195 const std::string _name;
196 void* const _pointer;
199 const jaffarCommon::hash::hash_t _nameHash;
200};
201
202} // namespace jaffarPlus
A named, typed reference to a value stored at a memory address.
Definition property.hpp:25
const endianness_t _endianness
Byte order of the value at _pointer.
Definition property.hpp:198
datatype_t getDatatype() const
Returns the property's datatype.
Definition property.hpp:186
static endianness_t parseEndiannessName(const std::string &endiannessName)
Maps a configuration endianness string to its endianness_t value.
Definition property.hpp:77
std::string getName() const
Returns the property's name.
Definition property.hpp:188
const std::string _name
Identifying name of the property.
Definition property.hpp:195
T getValue() const
Reads the property's value at its memory address as type T, applying endianness conversion.
Definition property.hpp:144
const datatype_t _datatype
How the bytes at _pointer are interpreted.
Definition property.hpp:197
endianness_t
The byte order of the value stored at the property's memory address.
Definition property.hpp:45
@ little
Little endian byte order (config endianness "Little").
Definition property.hpp:46
@ big
Big endian byte order (config endianness "Big").
Definition property.hpp:47
void * getPointer() const
Returns the raw pointer to the property's value in memory.
Definition property.hpp:192
const jaffarCommon::hash::hash_t _nameHash
Precomputed hash of _name for fast lookup.
Definition property.hpp:199
size_t getSize() const
Returns the size in bytes of the property's value.
Definition property.hpp:69
Property(const std::string &name, void *const pointer, const datatype_t datatype, endianness_t endianness)
Constructs a property describing a value in memory.
Definition property.hpp:60
datatype_t
The interpretation of the bytes at the property's memory address.
Definition property.hpp:29
@ dt_int32
Signed 32-bit integer (config datatype "INT32").
Definition property.hpp:36
@ dt_int8
Signed 8-bit integer (config datatype "INT8").
Definition property.hpp:34
@ dt_float32
Single precision float, 32-bit (config datatype "FLOAT32").
Definition property.hpp:39
@ dt_uint64
Unsigned 64-bit integer (config datatype "UINT64").
Definition property.hpp:33
@ dt_uint16
Unsigned 16-bit integer (config datatype "UINT16").
Definition property.hpp:31
@ dt_uint8
Unsigned 8-bit integer (config datatype "UINT8").
Definition property.hpp:30
@ dt_int16
Signed 16-bit integer (config datatype "INT16").
Definition property.hpp:35
@ dt_uint32
Unsigned 32-bit integer (config datatype "UINT32").
Definition property.hpp:32
@ dt_bool
Boolean stored in a single byte (config datatype "BOOL").
Definition property.hpp:38
@ dt_float64
Double precision float, 64-bit (config datatype "FLOAT64").
Definition property.hpp:40
@ dt_int64
Signed 64-bit integer (config datatype "INT64").
Definition property.hpp:37
static size_t getDatatypeSize(const datatype_t datatype)
Returns the size in bytes of a given datatype.
Definition property.hpp:114
jaffarCommon::hash::hash_t getNameHash() const
Returns the hash of the property's name.
Definition property.hpp:190
static datatype_t parseDatatypeName(const std::string &datatypeName)
Maps a configuration datatype string to its datatype_t value.
Definition property.hpp:91
Property()=delete
Default construction is disabled; a property requires a name, pointer, datatype and endianness.
void *const _pointer
Pointer to the property's value in game memory.
Definition property.hpp:196