real.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. namespace nall {
  3. template<uint Precision> struct Real {
  4. static_assert(Precision == 32 || Precision == 64);
  5. static constexpr auto bits() -> uint { return Precision; }
  6. using ftype =
  7. conditional_t<bits() == 32, float32_t,
  8. conditional_t<bits() == 64, float64_t,
  9. void>>;
  10. Real() : data(0.0) {}
  11. template<int Bits> Real(Real<Bits> value) : data((ftype)value) {}
  12. template<typename T> Real(const T& value) : data((ftype)value) {}
  13. explicit Real(const char* value) : data((ftype)toReal(value)) {}
  14. operator ftype() const { return data; }
  15. auto operator++(int) { auto value = *this; ++data; return value; }
  16. auto operator--(int) { auto value = *this; --data; return value; }
  17. auto& operator++() { data++; return *this; }
  18. auto& operator--() { data--; return *this; }
  19. template<typename T> auto& operator =(const T& value) { data = value; return *this; }
  20. template<typename T> auto& operator*=(const T& value) { data = data * value; return *this; }
  21. template<typename T> auto& operator/=(const T& value) { data = data / value; return *this; }
  22. template<typename T> auto& operator%=(const T& value) { data = data % value; return *this; }
  23. template<typename T> auto& operator+=(const T& value) { data = data + value; return *this; }
  24. template<typename T> auto& operator-=(const T& value) { data = data - value; return *this; }
  25. auto serialize(serializer& s) { s(data); }
  26. private:
  27. ftype data;
  28. };
  29. }