Goddess of Justice DB, the database used for storage on IzaroDFS
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

81 rader
1.5 KiB

5 år sedan
5 år sedan
5 år sedan
5 år sedan
  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. template<typename Ta>
  63. constexpr void operator+=(const Ta& v)
  64. {
  65. internal = swap_if_little(T(*this)+(T)v);
  66. }
  67. constexpr operator T() const {
  68. return swap_if_little(internal);
  69. }
  70. };
  71. }