semf
littleendianinteger.h
Go to the documentation of this file.
1
10#ifndef SEMF_UTILS_PROCESSING_ENDIAN_LITTLEENDIANINTEGER_H_
11#define SEMF_UTILS_PROCESSING_ENDIAN_LITTLEENDIANINTEGER_H_
12
13#include <semf/utils/processing/stdworkarounds/algorithm>
14#include <semf/utils/processing/stdworkarounds/bit>
15#include <algorithm>
16#include <cstdint>
17#include <limits>
18
19#if __cplusplus >= 201703L
20#define IF_CONSTEXPR if constexpr
21#else
22#define IF_CONSTEXPR if
23#endif
24
25namespace semf
26{
31template <typename T>
33{
34public:
39 constexpr LittleEndianInteger(T value);
40 virtual ~LittleEndianInteger() = default;
41
56 T native() const;
62 bool operator!=(LittleEndianInteger<T> other) const;
146 bool operator==(LittleEndianInteger<T> other) const;
159
160private:
165 void setValue(T value);
167 T m_value;
168};
169
170template <typename T>
172: m_value{std::endian::big == std::endian::native ? reverse<T>(reinterpret_cast<uint8_t*>(&value), reinterpret_cast<uint8_t*>(&value) + sizeof(T)) : value}
173{
174}
175
176template <typename T>
178{
179 return std::numeric_limits<T>::max();
180}
181
182template <typename T>
184{
185 return std::numeric_limits<T>::min();
186}
187
188template <typename T>
190{
191 T value = m_value;
192 IF_CONSTEXPR(std::endian::big == std::endian::native)
193 {
194 std::reverse(reinterpret_cast<uint8_t*>(&value), reinterpret_cast<uint8_t*>(&value) + sizeof(T));
195 }
196 return value;
197}
198
199template <typename T>
201{
202 return m_value != other.m_value;
203}
204
205template <typename T>
207{
208 T value = native();
209 value %= i;
210 setValue(value);
211 return *this;
212}
213
214template <typename T>
216{
217 T value = native();
218 value &= i;
219 setValue(value);
220 return *this;
221}
222
223template <typename T>
225{
226 T value = native();
227 value *= i;
228 setValue(value);
229 return *this;
230}
231
232template <typename T>
234{
235 T value = native();
236 value++;
237 setValue(value);
238 return *this;
239}
240
241template <typename T>
243{
244 (void)i;
245 return this->operator++();
246}
247
248template <typename T>
250{
251 T value = native();
252 value += i;
253 setValue(value);
254 return *this;
255}
256
257template <typename T>
259{
260 T value = native();
261 value--;
262 setValue(value);
263 return *this;
264}
265
266template <typename T>
268{
269 (void)i;
270 return this->operator--();
271}
272
273template <typename T>
275{
276 T value = native();
277 value -= i;
278 setValue(value);
279 return *this;
280}
281
282template <typename T>
284{
285 T value = native();
286 value %= i;
287 setValue(value);
288 return *this;
289}
290
291template <typename T>
293{
294 T value = native();
295 value <<= i;
296 setValue(value);
297 return *this;
298}
299
300template <typename T>
302{
303 T value = native();
304 value = i;
305 setValue(value);
306 return *this;
307}
308
309template <typename T>
311{
312 T value = native();
313 value ^= i;
314 setValue(value);
315 return *this;
316}
317
318template <typename T>
320{
321 return m_value == other.m_value;
322}
323
324template <typename T>
326{
327 T value = native();
328 value >>= i;
329 setValue(value);
330 return *this;
331}
332
333template <typename T>
335{
336 T value = native();
337 value |= i;
338 setValue(value);
339 return *this;
340}
341
342template <typename T>
344{
345 IF_CONSTEXPR(std::endian::big == std::endian::native)
346 {
347 std::reverse(reinterpret_cast<uint8_t*>(&value), reinterpret_cast<uint8_t*>(&value) + sizeof(T));
348 }
349 m_value = value;
350}
351} // namespace semf
352#endif // SEMF_UTILS_PROCESSING_ENDIAN_LITTLEENDIANINTEGER_H_
Class for representing little-endian numbers.
LittleEndianInteger< T > & operator>>=(T i)
Shift i bits right.
T native() const
Returns a native representation of the stored number.
LittleEndianInteger< T > & operator/=(T i)
Performs a division.
LittleEndianInteger< T > & operator=(T i)
Assigns this with the value i.
bool operator!=(LittleEndianInteger< T > other) const
Checks if this and other are unequal.
constexpr LittleEndianInteger(T value)
Constructor.
static LittleEndianInteger< T > max()
Returns the maximum (finite) value representable by the numeric type T.
LittleEndianInteger< T > & operator--()
Decrements this (prefix) and returns a reference to this.
LittleEndianInteger< T > & operator|=(T i)
Performs a bitwise OR.
bool operator==(LittleEndianInteger< T > other) const
Checks for equality between this and other.
LittleEndianInteger< T > & operator^=(T i)
Performs a bitwise XOR.
LittleEndianInteger< T > & operator+=(T i)
Performs an addition.
LittleEndianInteger< T > & operator++()
Increments this (prefix) and returns a reference to this.
virtual ~LittleEndianInteger()=default
LittleEndianInteger< T > & operator%=(T i)
Calculates the modulo.
LittleEndianInteger< T > & operator&=(T i)
Performs a bitwise AND.
static LittleEndianInteger< T > min()
Returns the minimum (finite) value representable by the numeric type T.
LittleEndianInteger< T > & operator-=(T i)
Performs a subtraction.
LittleEndianInteger< T > & operator<<=(T i)
Shift i bits left.
LittleEndianInteger< T > & operator*=(T i)
Performs a multiplication.
#define IF_CONSTEXPR