Utility.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef CORE_UTILITY_H
  2. #define CORE_UTILITY_H
  3. #include "utils/Check.h"
  4. #define CORE_SIZE(t) static_cast<int>(sizeof(t))
  5. namespace Core {
  6. enum class Error {
  7. NONE = 0,
  8. NEGATIVE_ARGUMENT,
  9. CAPACITY_REACHED,
  10. BLOCKED_STDOUT,
  11. OUT_OF_MEMORY,
  12. INVALID_CHAR,
  13. NOT_FOUND,
  14. INVALID_STATE,
  15. INVALID_INDEX,
  16. INVALID_ARGUMENT,
  17. TIME_NOT_AVAILABLE,
  18. SLEEP_INTERRUPTED,
  19. THREAD_ERROR,
  20. INVALID_ID,
  21. EXISTING_KEY
  22. };
  23. const char* getErrorName(Error e);
  24. inline bool checkError(Error& storage, Error e) {
  25. return (storage = e) != Error::NONE;
  26. }
  27. #define CORE_RETURN_ERROR(checked) \
  28. { \
  29. Core::Error error = Core::Error::NONE; \
  30. if(checkError(error, checked)) [[unlikely]] { \
  31. return error; \
  32. } \
  33. }
  34. namespace Internal {
  35. template<typename T>
  36. struct BaseRemovePointer final {
  37. using Type = T;
  38. };
  39. template<typename T>
  40. struct BaseRemovePointer<T*> final {
  41. using Type = T;
  42. };
  43. template<typename T>
  44. struct BaseRemoveReference final {
  45. using Type = T;
  46. };
  47. template<typename T>
  48. struct BaseRemoveReference<T&> final {
  49. using Type = T;
  50. };
  51. template<typename T>
  52. struct BaseRemoveReference<T&&> final {
  53. using Type = T;
  54. };
  55. template<typename A, typename B>
  56. struct BaseIsSame final {
  57. static constexpr bool value = false;
  58. };
  59. template<typename T>
  60. struct BaseIsSame<T, T> final {
  61. static constexpr bool value = true;
  62. };
  63. template<bool C, typename A, typename B>
  64. struct BaseIf final {
  65. using Type = A;
  66. };
  67. template<typename A, typename B>
  68. struct BaseIf<false, A, B> final {
  69. using Type = B;
  70. };
  71. }
  72. template<typename T>
  73. using RemovePointer = Internal::BaseRemovePointer<T>::Type;
  74. template<typename T>
  75. using RemoveReference = Internal::BaseRemoveReference<T>::Type;
  76. template<typename T, typename U>
  77. constexpr bool IsSame = Internal::BaseIsSame<T, U>::value;
  78. template<bool C, typename A, typename B>
  79. using If = Internal::BaseIf<C, A, B>::Type;
  80. namespace Internal {
  81. template<int N, typename T1, typename T2, typename T3, typename T4,
  82. typename T5>
  83. using SelectType =
  84. If<sizeof(T1) == N, T1,
  85. If<sizeof(T2) == N, T2,
  86. If<sizeof(T3) == N, T3, If<sizeof(T4) == N, T4, T5>>>>;
  87. template<int N>
  88. using SelectSigned =
  89. SelectType<N, signed char, signed short, signed int, signed long,
  90. signed long long>;
  91. template<int N>
  92. using SelectUnsigned =
  93. SelectType<N, unsigned char, unsigned short, unsigned int,
  94. unsigned long, unsigned long long>;
  95. }
  96. template<typename T>
  97. constexpr RemoveReference<T>&& move(T&& t) {
  98. return static_cast<RemoveReference<T>&&>(t);
  99. }
  100. template<typename T>
  101. constexpr T&& forward(RemoveReference<T>& t) {
  102. return static_cast<T&&>(t);
  103. }
  104. template<typename T>
  105. constexpr T&& forward(RemoveReference<T>&& t) {
  106. return static_cast<T&&>(t);
  107. }
  108. template<typename T>
  109. void swap(T& a, T& b) {
  110. T tmp = Core::move(a);
  111. a = Core::move(b);
  112. b = Core::move(tmp);
  113. }
  114. template<typename T>
  115. int popCount(const T& t) {
  116. static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
  117. 1, 2, 2, 3, 2, 3, 3, 4};
  118. int sum = 0;
  119. for(int i = 0; i < CORE_SIZE(T) * 8; i += 4) {
  120. sum += map[(t >> i) & 0xF];
  121. }
  122. return sum;
  123. }
  124. using ExitHandler = void (*)(int, void*);
  125. void exitWithHandler(const char* file, int line, int value);
  126. void setExitHandler(ExitHandler eh, void* data);
  127. #define CORE_EXIT(exitValue) \
  128. Core::exitWithHandler(__FILE__, __LINE__, exitValue)
  129. check_return Error toString(signed short s, char* buffer, int size);
  130. check_return Error toString(unsigned short s, char* buffer, int size);
  131. check_return Error toString(signed int i, char* buffer, int size);
  132. check_return Error toString(unsigned int i, char* buffer, int size);
  133. check_return Error toString(signed long l, char* buffer, int size);
  134. check_return Error toString(unsigned long l, char* buffer, int size);
  135. check_return Error toString(signed long long ll, char* buffer, int size);
  136. check_return Error toString(unsigned long long ll, char* buffer, int size);
  137. check_return Error toString(float f, char* buffer, int size);
  138. check_return Error toString(double d, char* buffer, int size);
  139. check_return Error toString(long double ld, char* buffer, int size);
  140. check_return Error putChar(int c);
  141. void memorySet(void* p, int c, int n);
  142. void memoryCopy(void* dest, const void* src, int n);
  143. bool memoryCompare(const void* a, const void* b, int n);
  144. check_return Error reallocate(char*& p, int n);
  145. void free(void* p);
  146. const char* getFileName(const char* path);
  147. template<typename T>
  148. constexpr int stringLength(const T* c) {
  149. int i = 0;
  150. while(*c != '\0') {
  151. c++;
  152. i++;
  153. }
  154. return i;
  155. }
  156. }
  157. using i64 = Core::Internal::SelectSigned<8>;
  158. using i32 = Core::Internal::SelectSigned<4>;
  159. using i16 = Core::Internal::SelectSigned<2>;
  160. using i8 = Core::Internal::SelectSigned<1>;
  161. using u64 = Core::Internal::SelectUnsigned<8>;
  162. using u32 = Core::Internal::SelectUnsigned<4>;
  163. using u16 = Core::Internal::SelectUnsigned<2>;
  164. using u8 = Core::Internal::SelectUnsigned<1>;
  165. using c32 = char32_t;
  166. static_assert(sizeof(i64) == 8, "invalid size");
  167. static_assert(sizeof(i32) == 4, "invalid size");
  168. static_assert(sizeof(i16) == 2, "invalid size");
  169. static_assert(sizeof(i8) == 1, "invalid size");
  170. static_assert(sizeof(u64) == 8, "invalid size");
  171. static_assert(sizeof(u32) == 4, "invalid size");
  172. static_assert(sizeof(u16) == 2, "invalid size");
  173. static_assert(sizeof(u8) == 1, "invalid size");
  174. using size_t = u64;
  175. void* operator new(size_t bytes) noexcept;
  176. void* operator new[](size_t bytes) noexcept;
  177. void operator delete(void* p) noexcept;
  178. void operator delete[](void* p) noexcept;
  179. void operator delete(void* p, size_t bytes) noexcept;
  180. void operator delete[](void* p, size_t bytes) noexcept;
  181. void* operator new(size_t bytes, void* p) noexcept;
  182. // void* operator new[](size_t bytes, void* p) noexcept;
  183. #endif