Utility.h 6.8 KB

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