123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- #ifndef CORE_UTILITY_H
- #define CORE_UTILITY_H
- #include "utils/Check.h"
- #define CORE_SIZE(t) static_cast<int>(sizeof(t))
- namespace Core {
- enum class Error {
- NONE = 0,
- NEGATIVE_ARGUMENT,
- CAPACITY_REACHED,
- BLOCKED_STDOUT,
- OUT_OF_MEMORY,
- INVALID_CHAR,
- NOT_FOUND,
- INVALID_STATE,
- INVALID_INDEX,
- TIME_NOT_AVAILABLE,
- SLEEP_INTERRUPTED,
- THREAD_ERROR,
- INVALID_ID,
- EXISTING_KEY
- };
- const char* getErrorName(Error e);
- inline bool checkError(Error& storage, Error e) {
- return (storage = e) != Error::NONE;
- }
- #define CORE_RETURN_ERROR(checked) \
- { \
- Core::Error error = Core::Error::NONE; \
- if(checkError(error, checked)) [[unlikely]] { \
- return error; \
- } \
- }
- namespace Internal {
- template<typename T>
- struct BaseRemovePointer final {
- using Type = T;
- };
- template<typename T>
- struct BaseRemovePointer<T*> final {
- using Type = T;
- };
- template<typename T>
- struct BaseRemoveReference final {
- using Type = T;
- };
- template<typename T>
- struct BaseRemoveReference<T&> final {
- using Type = T;
- };
- template<typename T>
- struct BaseRemoveReference<T&&> final {
- using Type = T;
- };
- template<typename A, typename B>
- struct BaseIsSame final {
- static constexpr bool value = false;
- };
- template<typename T>
- struct BaseIsSame<T, T> final {
- static constexpr bool value = true;
- };
- template<bool C, typename A, typename B>
- struct BaseIf final {
- using Type = A;
- };
- template<typename A, typename B>
- struct BaseIf<false, A, B> final {
- using Type = B;
- };
- }
- template<typename T>
- using RemovePointer = Internal::BaseRemovePointer<T>::Type;
- 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* data);
- #define CORE_EXIT(exitValue) \
- Core::exitWithHandler(__FILE__, __LINE__, exitValue)
- check_return Error toString(signed short s, char* buffer, int size);
- check_return Error toString(unsigned short s, char* buffer, int size);
- check_return Error toString(signed int i, char* buffer, int size);
- check_return Error toString(unsigned int i, char* buffer, int size);
- check_return Error toString(signed long l, char* buffer, int size);
- check_return Error toString(unsigned long l, char* buffer, int size);
- check_return Error toString(signed long long ll, char* buffer, int size);
- check_return Error toString(unsigned long long ll, char* buffer, int size);
- check_return Error toString(float f, char* buffer, int size);
- check_return Error toString(double d, char* buffer, int size);
- check_return Error toString(long double ld, char* buffer, int size);
- check_return Error putChar(int c);
- void memorySet(void* p, int c, int n);
- void memoryCopy(void* dest, const void* src, int n);
- bool memoryCompare(const void* a, const void* b, int n);
- check_return Error reallocate(char*& p, int n);
- void free(void* p);
- const char* getFileName(const char* path);
- template<typename T>
- constexpr int stringLength(const T* c) {
- int i = 0;
- while(*c != '\0') {
- c++;
- i++;
- }
- return i;
- }
- }
- 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>;
- using c32 = char32_t;
- 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
|