Utility.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef CORE_UTILITY_HPP
  2. #define CORE_UTILITY_HPP
  3. #include "utils/Check.hpp"
  4. #include "utils/Error.hpp"
  5. #define CORE_SIZE(t) static_cast<int>(sizeof(t))
  6. namespace Core {
  7. template<typename T>
  8. int popCount(const T& t) {
  9. static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
  10. 1, 2, 2, 3, 2, 3, 3, 4};
  11. int sum = 0;
  12. for(int i = 0; i < CORE_SIZE(T) * 8; i += 4) {
  13. sum += map[(t >> i) & 0xF];
  14. }
  15. return sum;
  16. }
  17. using ExitHandler = void (*)(int, void*);
  18. void exitWithHandler(const char* file, int line, int value);
  19. void setExitHandler(ExitHandler eh, void* data);
  20. #define CORE_EXIT(exitValue) \
  21. Core::exitWithHandler(__FILE__, __LINE__, exitValue)
  22. check_return Error toString(signed short s, char* buffer, int size);
  23. check_return Error toString(unsigned short s, char* buffer, int size);
  24. check_return Error toString(signed int i, char* buffer, int size);
  25. check_return Error toString(unsigned int i, char* buffer, int size);
  26. check_return Error toString(signed long l, char* buffer, int size);
  27. check_return Error toString(unsigned long l, char* buffer, int size);
  28. check_return Error toString(signed long long ll, char* buffer, int size);
  29. check_return Error toString(unsigned long long ll, char* buffer, int size);
  30. check_return Error toString(float f, char* buffer, int size);
  31. check_return Error toString(double d, char* buffer, int size);
  32. check_return Error toString(long double ld, char* buffer, int size);
  33. check_return Error putChar(int c);
  34. void memorySet(void* p, int c, int n);
  35. void memoryCopy(void* dest, const void* src, int n);
  36. bool memoryCompare(const void* a, const void* b, int n);
  37. check_return Error reallocate(char*& p, int n);
  38. void free(void* p);
  39. }
  40. #endif