Utility.h 6.6 KB

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