Meta.cppm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. export module Core.Meta;
  2. namespace Core {
  3. template<typename T>
  4. struct BaseRemovePointer final {
  5. using Type = T;
  6. };
  7. template<typename T>
  8. struct BaseRemovePointer<T*> final {
  9. using Type = T;
  10. };
  11. template<typename T>
  12. struct BaseRemoveReference final {
  13. using Type = T;
  14. };
  15. template<typename T>
  16. struct BaseRemoveReference<T&> final {
  17. using Type = T;
  18. };
  19. template<typename T>
  20. struct BaseRemoveReference<T&&> final {
  21. using Type = T;
  22. };
  23. template<typename A, typename B>
  24. struct BaseIsSame final {
  25. static constexpr bool value = false;
  26. };
  27. template<typename T>
  28. struct BaseIsSame<T, T> final {
  29. static constexpr bool value = true;
  30. };
  31. template<bool C, typename A, typename B>
  32. struct BaseIf final {
  33. using Type = A;
  34. };
  35. template<typename A, typename B>
  36. struct BaseIf<false, A, B> final {
  37. using Type = B;
  38. };
  39. }
  40. export namespace Core {
  41. template<typename T>
  42. concept Iterable = requires(T& t) {
  43. t.begin();
  44. t.end();
  45. };
  46. template<typename T>
  47. using RemovePointer = BaseRemovePointer<T>::Type;
  48. template<typename T>
  49. using RemoveReference = BaseRemoveReference<T>::Type;
  50. template<typename T, typename U>
  51. constexpr bool IsSame = BaseIsSame<T, U>::value;
  52. template<bool C, typename A, typename B>
  53. using If = BaseIf<C, A, B>::Type;
  54. template<typename T>
  55. constexpr RemoveReference<T>&& move(T&& t) noexcept {
  56. return static_cast<RemoveReference<T>&&>(t);
  57. }
  58. template<typename T>
  59. concept Moveable = requires(T t) {
  60. requires noexcept(T(Core::move(t)));
  61. requires noexcept(t = T(Core::move(t)));
  62. };
  63. template<typename T>
  64. constexpr T&& forward(RemoveReference<T>& t) noexcept {
  65. return static_cast<T&&>(t);
  66. }
  67. template<typename T>
  68. constexpr T&& forward(RemoveReference<T>&& t) noexcept {
  69. return static_cast<T&&>(t);
  70. }
  71. template<Moveable T>
  72. void swap(T& a, T& b) noexcept {
  73. T tmp = Core::move(a);
  74. a = Core::move(b);
  75. b = Core::move(tmp);
  76. }
  77. }