123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #ifndef CORE_UTILITY_H
- #define CORE_UTILITY_H
- #include "utils/Check.h"
- #define CORE_SIZE(t) static_cast<int>(sizeof(t))
- namespace Core {
- namespace Internal {
- template<typename T>
- struct BaseRemoveReference {
- using Type = T;
- };
- template<typename T>
- struct BaseRemoveReference<T&> {
- using Type = T;
- };
- template<typename T>
- struct BaseRemoveReference<T&&> {
- using Type = T;
- };
- template<typename A, typename B>
- struct BaseIsSame {
- static constexpr bool value = false;
- };
- template<typename T>
- struct BaseIsSame<T, T> {
- static constexpr bool value = true;
- };
- template<bool C, typename A, typename B>
- struct BaseIf {
- using Type = A;
- };
- template<typename A, typename B>
- struct BaseIf<false, A, B> {
- using Type = B;
- };
- }
- template<typename T>
- using RemoveReference = Internal::BaseRemoveReference<T>::Type;
- template<typename T, typename U>
- constexpr bool IsSame = Internal::BaseIsSame<T, U>::value;
- template<bool C, typename A, typename B>
- using If = Internal::BaseIf<C, A, B>::Type;
- namespace Internal {
- template<int N, typename T1, typename T2, typename T3, typename T4,
- typename T5>
- using SelectType =
- If<sizeof(T1) == N, T1,
- If<sizeof(T2) == N, T2,
- If<sizeof(T3) == N, T3, If<sizeof(T4) == N, T4, T5>>>>;
- template<int N>
- using SelectSigned =
- SelectType<N, signed char, signed short, signed int, signed long,
- signed long long>;
- template<int N>
- using SelectUnsigned =
- SelectType<N, unsigned char, unsigned short, unsigned int,
- unsigned long, unsigned long long>;
- }
- template<typename T>
- constexpr RemoveReference<T>&& move(T&& t) {
- return static_cast<RemoveReference<T>&&>(t);
- }
- template<typename T>
- constexpr T&& forward(RemoveReference<T>& t) {
- return static_cast<T&&>(t);
- }
- template<typename T>
- constexpr T&& forward(RemoveReference<T>&& t) {
- return static_cast<T&&>(t);
- }
- template<typename T>
- void swap(T& a, T& b) {
- T tmp = Core::move(a);
- a = Core::move(b);
- b = Core::move(tmp);
- }
- template<typename T>
- int popCount(const T& t) {
- static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
- 1, 2, 2, 3, 2, 3, 3, 4};
- int sum = 0;
- for(int i = 0; i < CORE_SIZE(T) * 8; i += 4) {
- sum += map[(t >> i) & 0xF];
- }
- return sum;
- }
- using ExitHandler = void (*)(int, void*);
- void exitWithHandler(const char* file, int line, int value);
- void setExitHandler(ExitHandler eh, void*);
- #define CORE_EXIT(exitValue) \
- Core::exitWithHandler(__FILE__, __LINE__, exitValue)
- // returns true on error
- check_return bool toString(signed short s, char* buffer, int size);
- // returns true on error
- check_return bool toString(unsigned short s, char* buffer, int size);
- // returns true on error
- check_return bool toString(signed int i, char* buffer, int size);
- // returns true on error
- check_return bool toString(unsigned int i, char* buffer, int size);
- // returns true on error
- check_return bool toString(signed long l, char* buffer, int size);
- // returns true on error
- check_return bool toString(unsigned long l, char* buffer, int size);
- // returns true on error
- check_return bool toString(signed long long ll, char* buffer, int size);
- // returns true on error
- check_return bool toString(unsigned long long ll, char* buffer, int size);
- // returns true on error
- check_return bool toString(float f, char* buffer, int size);
- // returns true on error
- check_return bool toString(double d, char* buffer, int size);
- // returns true on error
- check_return bool toString(long double ld, char* buffer, int size);
- // returns true on error
- check_return bool putChar(int c);
- void memorySet(void* p, int c, int n);
- void memoryCopy(void* dest, const void* src, int n);
- }
- using i64 = Core::Internal::SelectSigned<8>;
- using i32 = Core::Internal::SelectSigned<4>;
- using i16 = Core::Internal::SelectSigned<2>;
- using i8 = Core::Internal::SelectSigned<1>;
- using u64 = Core::Internal::SelectUnsigned<8>;
- using u32 = Core::Internal::SelectUnsigned<4>;
- using u16 = Core::Internal::SelectUnsigned<2>;
- using u8 = Core::Internal::SelectUnsigned<1>;
- static_assert(sizeof(i64) == 8, "invalid size");
- static_assert(sizeof(i32) == 4, "invalid size");
- static_assert(sizeof(i16) == 2, "invalid size");
- static_assert(sizeof(i8) == 1, "invalid size");
- static_assert(sizeof(u64) == 8, "invalid size");
- static_assert(sizeof(u32) == 4, "invalid size");
- static_assert(sizeof(u16) == 2, "invalid size");
- static_assert(sizeof(u8) == 1, "invalid size");
- using size_t = u64;
- void* operator new(size_t bytes) noexcept;
- void* operator new[](size_t bytes) noexcept;
- void operator delete(void* p) noexcept;
- void operator delete[](void* p) noexcept;
- void operator delete(void* p, size_t bytes) noexcept;
- void operator delete[](void* p, size_t bytes) noexcept;
- void* operator new(size_t bytes, void* p) noexcept;
- // void* operator new[](size_t bytes, void* p) noexcept;
- #endif
|