Utility.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef CORE_UTILITY_H
  2. #define CORE_UTILITY_H
  3. // #include <stdlib.h>
  4. namespace Core {
  5. namespace Internal {
  6. template<typename T>
  7. struct RemoveReferenceBase {
  8. typedef T t;
  9. };
  10. template<typename T>
  11. struct RemoveReferenceBase<T&> {
  12. typedef T t;
  13. };
  14. template<typename T>
  15. struct RemoveReferenceBase<T&&> {
  16. typedef T t;
  17. };
  18. template<typename A, typename B>
  19. struct IsSameBase {
  20. static constexpr bool value = false;
  21. };
  22. template<typename T>
  23. struct IsSameBase<T, T> {
  24. static constexpr bool value = true;
  25. };
  26. }
  27. template<typename T>
  28. using RemoveReference = typename Internal::RemoveReferenceBase<T>::t;
  29. template<typename T, typename U>
  30. constexpr bool IsSame = Internal::IsSameBase<T, U>::value;
  31. template<typename T>
  32. constexpr RemoveReference<T>&& move(T&& t) {
  33. return static_cast<RemoveReference<T>&&>(t);
  34. }
  35. template<typename T>
  36. constexpr T&& forward(RemoveReference<T>& t) {
  37. return static_cast<T&&>(t);
  38. }
  39. template<typename T>
  40. constexpr T&& forward(RemoveReference<T>&& t) {
  41. return static_cast<T&&>(t);
  42. }
  43. template<typename T>
  44. void swap(T& a, T& b) {
  45. T tmp = Core::move(a);
  46. a = Core::move(b);
  47. b = Core::move(tmp);
  48. }
  49. template<typename T>
  50. int popCount(const T& t) {
  51. static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
  52. 1, 2, 2, 3, 2, 3, 3, 4};
  53. int sum = 0;
  54. for(int i = 0; i < static_cast<int>(sizeof(T) * 8); i += 4) {
  55. sum += map[(t >> i) & 0xF];
  56. }
  57. return sum;
  58. }
  59. }
  60. // void* operator new(size_t bytes) noexcept;
  61. // void* operator new[](size_t bytes) noexcept;
  62. // void operator delete(void* p) noexcept;
  63. // void operator delete[](void* p) noexcept;
  64. // void operator delete(void* p, size_t bytes) noexcept;
  65. // void operator delete[](void* p, size_t bytes) noexcept;
  66. // void* operator new(size_t bytes, void* p) noexcept;
  67. // void* operator new[](size_t bytes, void* p) noexcept;
  68. #endif