semf
bigendianinteger.h
Go to the documentation of this file.
1
10#ifndef SEMF_UTILS_PROCESSING_ENDIAN_BIGENDIANINTEGER_H_
11#define SEMF_UTILS_PROCESSING_ENDIAN_BIGENDIANINTEGER_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#include <type_traits>
19
20#if __cplusplus >= 201703L
21#define IF_CONSTEXPR if constexpr
22#else
23#define IF_CONSTEXPR if
24#endif
25
26namespace semf
27{
32template <typename T>
34{
35public:
40 constexpr BigEndianInteger(T value);
41 virtual ~BigEndianInteger() = default;
42
47 static BigEndianInteger<T> max();
52 static BigEndianInteger<T> min();
57 T native() const;
63 bool operator!=(BigEndianInteger<T> other) const;
147 bool operator==(BigEndianInteger<T> other) const;
160
161private:
166 void setValue(T value);
168 T m_value;
169};
170
171template <typename T>
173: m_value{std::endian::little == std::endian::native ? reverse<T>(reinterpret_cast<uint8_t*>(&value), reinterpret_cast<uint8_t*>(&value) + sizeof(T)) : value}
174{
175}
176
177template <typename T>
179{
180 BigEndianInteger<T> value(std::numeric_limits<T>::max());
181 return value;
182}
183
184template <typename T>
186{
187 BigEndianInteger<T> value(std::numeric_limits<T>::min());
188 return value;
189}
190
191template <typename T>
193{
194 T native = m_value;
195 IF_CONSTEXPR(std::endian::little == std::endian::native)
196 {
197 std::reverse(reinterpret_cast<uint8_t*>(&native), reinterpret_cast<uint8_t*>(&native) + sizeof(T));
198 }
199 return native;
200}
201
202template <typename T>
204{
205 return m_value != other.m_value;
206}
207
208template <typename T>
210{
211 T nativeValue = native();
212 nativeValue %= i;
213 setValue(nativeValue);
214 return *this;
215}
216
217template <typename T>
219{
220 m_value = value;
221 IF_CONSTEXPR(std::endian::little == std::endian::native)
222 {
223 std::reverse(reinterpret_cast<uint8_t*>(&m_value), reinterpret_cast<uint8_t*>(&m_value) + sizeof(T));
224 }
225}
226
227template <typename T>
229{
230 T nativeValue = native();
231 nativeValue &= i;
232 setValue(nativeValue);
233 return *this;
234}
235
236template <typename T>
238{
239 T nativeValue = native();
240 nativeValue *= i;
241 setValue(nativeValue);
242 return *this;
243}
244
245template <typename T>
247{
248 T nativeValue = native();
249 nativeValue++;
250 setValue(nativeValue);
251 return *this;
252}
253
254template <typename T>
256{
257 (void)i;
258 return this->operator++();
259}
260
261template <typename T>
263{
264 T nativeValue = native();
265 nativeValue += i;
266 setValue(nativeValue);
267 return *this;
268}
269
270template <typename T>
272{
273 T nativeValue = native();
274 nativeValue--;
275 setValue(nativeValue);
276 return *this;
277}
278
279template <typename T>
281{
282 (void)i;
283 return this->operator--();
284}
285
286template <typename T>
288{
289 T nativeValue = native();
290 nativeValue -= i;
291 setValue(nativeValue);
292 return *this;
293}
294
295template <typename T>
297{
298 T nativeValue = native();
299 nativeValue /= i;
300 setValue(nativeValue);
301 return *this;
302}
303
304template <typename T>
306{
307 T nativeValue = native();
308 nativeValue <<= i;
309 setValue(nativeValue);
310 return *this;
311}
312
313template <typename T>
315{
316 setValue(i);
317 return *this;
318}
319
320template <typename T>
322{
323 return m_value == other.m_value;
324}
325
326template <typename T>
328{
329 T nativeValue = native();
330 nativeValue >>= i;
331 setValue(nativeValue);
332 return *this;
333}
334
335template <typename T>
337{
338 T nativeValue = native();
339 nativeValue ^= i;
340 setValue(nativeValue);
341 return *this;
342}
343
344template <typename T>
346{
347 T nativeValue = native();
348 nativeValue |= i;
349 setValue(nativeValue);
350 return *this;
351}
352} // namespace semf
353#endif // SEMF_UTILS_PROCESSING_ENDIAN_BIGENDIANINTEGER_H_
#define IF_CONSTEXPR
Class for representing big-endian numbers.
BigEndianInteger< T > & operator<<=(T i)
Shift i bits left.
bool operator!=(BigEndianInteger< T > other) const
Check if this and other are unequal.
BigEndianInteger< T > & operator^=(T i)
Performs a bitwise XOR.
BigEndianInteger< T > & operator%=(T i)
Calculates the modulo.
BigEndianInteger< T > & operator--()
Decrements this (prefix) and returns a reference to this.
T native() const
Returns a native representation of the stored number.
bool operator==(BigEndianInteger< T > other) const
Checks for equality between this and other.
BigEndianInteger< T > & operator>>=(T i)
Shift i bits right.
BigEndianInteger< T > & operator+=(T i)
Performs an addition.
BigEndianInteger< T > & operator|=(T i)
Performs a bitwise OR.
BigEndianInteger< T > & operator&=(T i)
Performs a bitwise AND.
virtual ~BigEndianInteger()=default
BigEndianInteger< T > & operator/=(T i)
Performs a division.
BigEndianInteger< T > & operator++()
Increments this (prefix) and returns a reference to this.
static BigEndianInteger< T > min()
Returns the minimum (finite) value representable by the numeric type T.
static BigEndianInteger< T > max()
Returns the maximum (finite) value representable by the numeric type T.
BigEndianInteger< T > & operator*=(T i)
Performs a multiplication.
constexpr BigEndianInteger(T value)
Constructor.
BigEndianInteger< T > & operator-=(T i)
Performs a subtraction.
BigEndianInteger< T > & operator=(T i)
Assigns this with the value i.