ptr_compare.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // ptr_compare.hpp
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 1/5/17.
  6. //
  7. #pragma once
  8. #define POINTER_TEMPLATE_COMPARE(ptr_t) \
  9. POINTER_TEMPLATE_COMPARE_FULL(ptr_t, (typename T), (T), (typename U), (U))
  10. #define EXPAND(...) __VA_ARGS__
  11. #define POINTER_TEMPLATE_COMPARE_FULL(ptr_t, T_tname, T, U_tname, U) \
  12. template <EXPAND T_tname, EXPAND U_tname> \
  13. bool operator==(ptr_t<EXPAND T> const & lhs, \
  14. ptr_t<EXPAND U> const & rhs) noexcept { \
  15. return lhs.get() == rhs.get(); \
  16. } \
  17. \
  18. template <EXPAND T_tname, EXPAND U_tname> \
  19. bool operator!=(ptr_t<EXPAND T> const & lhs, \
  20. ptr_t<EXPAND U> const & rhs) noexcept { \
  21. return !(lhs == rhs); \
  22. } \
  23. \
  24. template <EXPAND T_tname, EXPAND U_tname> \
  25. bool operator<(ptr_t<EXPAND T> const & lhs, \
  26. ptr_t<EXPAND U> const & rhs) noexcept { \
  27. typedef \
  28. typename std::common_type<typename ptr_t<EXPAND T>::pointer, \
  29. typename ptr_t<EXPAND U>::pointer>::type V; \
  30. return std::less<V>(lhs.get(), rhs.get()); \
  31. } \
  32. \
  33. template <EXPAND T_tname, EXPAND U_tname> \
  34. bool operator>(ptr_t<EXPAND T> const & lhs, \
  35. ptr_t<EXPAND U> const & rhs) noexcept { \
  36. return rhs < lhs; \
  37. } \
  38. \
  39. template <EXPAND T_tname, EXPAND U_tname> \
  40. bool operator<=(ptr_t<EXPAND T> const & lhs, \
  41. ptr_t<EXPAND U> const & rhs) noexcept { \
  42. return !(rhs < lhs); \
  43. } \
  44. \
  45. template <EXPAND T_tname, EXPAND U_tname> \
  46. bool operator>=(ptr_t<EXPAND T> const & lhs, \
  47. ptr_t<EXPAND U> const & rhs) noexcept { \
  48. return !(lhs < rhs); \
  49. } \
  50. \
  51. template <EXPAND T_tname> \
  52. bool operator==(ptr_t<EXPAND T> const & lhs, std::nullptr_t) noexcept { \
  53. return !lhs; \
  54. } \
  55. \
  56. template <EXPAND T_tname> \
  57. bool operator==(std::nullptr_t, ptr_t<EXPAND T> const & rhs) noexcept { \
  58. return !rhs; \
  59. } \
  60. \
  61. template <EXPAND T_tname> \
  62. bool operator!=(ptr_t<EXPAND T> const & lhs, std::nullptr_t) noexcept { \
  63. return static_cast<bool>(lhs); \
  64. } \
  65. \
  66. template <EXPAND T_tname> \
  67. bool operator!=(std::nullptr_t, ptr_t<EXPAND T> const & rhs) noexcept { \
  68. return static_cast<bool>(rhs); \
  69. } \
  70. \
  71. template <EXPAND T_tname> \
  72. bool operator<(ptr_t<EXPAND T> const & lhs, std::nullptr_t) noexcept { \
  73. typedef typename ptr_t<EXPAND T>::pointer V; \
  74. return std::less<V>(lhs.get(), nullptr); \
  75. } \
  76. \
  77. template <EXPAND T_tname> \
  78. bool operator<(std::nullptr_t, ptr_t<EXPAND T> const & rhs) noexcept { \
  79. typedef typename ptr_t<EXPAND T>::pointer V; \
  80. return std::less<V>(nullptr, rhs.get()); \
  81. } \
  82. \
  83. template <EXPAND T_tname> \
  84. bool operator>(ptr_t<EXPAND T> const & lhs, std::nullptr_t) noexcept { \
  85. return nullptr < lhs; \
  86. } \
  87. \
  88. template <EXPAND T_tname> \
  89. bool operator>(std::nullptr_t, ptr_t<EXPAND T> const & rhs) noexcept { \
  90. return rhs < nullptr; \
  91. } \
  92. \
  93. template <EXPAND T_tname> \
  94. bool operator<=(ptr_t<EXPAND T> const & lhs, std::nullptr_t) noexcept { \
  95. return !(nullptr < lhs); \
  96. } \
  97. \
  98. template <EXPAND T_tname> \
  99. bool operator<=(std::nullptr_t, ptr_t<EXPAND T> const & rhs) noexcept { \
  100. return !(rhs < nullptr); \
  101. } \
  102. \
  103. template <EXPAND T_tname> \
  104. bool operator>=(ptr_t<EXPAND T> const & lhs, std::nullptr_t) noexcept { \
  105. return !(lhs < nullptr); \
  106. } \
  107. \
  108. template <EXPAND T_tname> \
  109. bool operator>=(std::nullptr_t, ptr_t<EXPAND T> const & rhs) noexcept { \
  110. return !(nullptr < rhs); \
  111. }