Goddess of Justice DB, the database used for storage on IzaroDFS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.4 KiB

пре 5 година
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <array>
  5. #include <algorithm>
  6. #if __cplusplus <= 201703L
  7. namespace _hidden{
  8. enum class endian
  9. {
  10. #ifdef _WIN32
  11. little = 0,
  12. big = 1,
  13. native = little
  14. #else
  15. little = __ORDER_LITTLE_ENDIAN__,
  16. big = __ORDER_BIG_ENDIAN__,
  17. native = __BYTE_ORDER__
  18. #endif
  19. };
  20. }
  21. namespace bitops{
  22. using endian = _hidden::endian;
  23. }
  24. #else
  25. #include <type_traits>
  26. namespace bitops{
  27. using endian = std::endian;
  28. }
  29. #endif
  30. namespace bitops{
  31. template<typename T, size_t sz = sizeof(T)>
  32. constexpr std::array<uint8_t,sizeof(T)> swap_if_little_raw(T xsz)
  33. {
  34. std::array<uint8_t,sizeof(T)> ret = {0};
  35. auto abstract = (std::array<uint8_t,sizeof(T)>*)&xsz;
  36. if constexpr (endian::native == endian::little)
  37. {
  38. std::copy(abstract->rbegin(), abstract->rend(), ret.begin());
  39. }
  40. else
  41. {
  42. std::copy(abstract->begin(), abstract->end(), ret.begin());
  43. }
  44. return ret;
  45. }
  46. template<typename T, size_t sz = sizeof(T)>
  47. constexpr T swap_if_little(T xsz)
  48. {
  49. auto tmp = swap_if_little_raw(xsz);
  50. return *(T*)&tmp;
  51. }
  52. template<typename T>
  53. struct [[gnu::packed]] regulated{
  54. T internal = 0;
  55. constexpr regulated(T value)
  56. : internal{swap_if_little(value)}
  57. { }
  58. constexpr void operator=(T value)
  59. {
  60. internal = swap_if_little(value);
  61. }
  62. constexpr void operator+=(const int& v)
  63. {
  64. internal = swap_if_little(T(*this)+v);
  65. }
  66. constexpr operator T() {
  67. return swap_if_little(internal);
  68. }
  69. };
  70. }