Utility.h 6.9 KB

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