ptr_compare.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, ptr_t< EXPAND U > const&rhs) { \
  14. return lhs.get() == rhs.get(); \
  15. } \
  16. \
  17. template <EXPAND T_tname, EXPAND U_tname> \
  18. bool operator!=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
  19. return !(lhs == rhs); \
  20. } \
  21. \
  22. template <EXPAND T_tname, EXPAND U_tname> \
  23. bool operator< (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
  24. typedef typename std::common_type< \
  25. typename ptr_t< EXPAND T >::pointer, \
  26. typename ptr_t< EXPAND U >::pointer>::type V; \
  27. return std::less<V>(lhs.get(), rhs.get()); \
  28. } \
  29. \
  30. template <EXPAND T_tname, EXPAND U_tname> \
  31. bool operator> (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
  32. return rhs < lhs; \
  33. } \
  34. \
  35. template <EXPAND T_tname, EXPAND U_tname> \
  36. bool operator<=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
  37. return !(rhs < lhs); \
  38. } \
  39. \
  40. template <EXPAND T_tname, EXPAND U_tname> \
  41. bool operator>=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
  42. return !(lhs < rhs); \
  43. } \
  44. \
  45. template <EXPAND T_tname> \
  46. bool operator==(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
  47. return !lhs; \
  48. } \
  49. \
  50. template <EXPAND T_tname> \
  51. bool operator==(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
  52. return !rhs; \
  53. } \
  54. \
  55. template <EXPAND T_tname> \
  56. bool operator!=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
  57. return static_cast<bool>(lhs); \
  58. } \
  59. \
  60. template <EXPAND T_tname> \
  61. bool operator!=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
  62. return static_cast<bool>(rhs); \
  63. } \
  64. \
  65. template <EXPAND T_tname> \
  66. bool operator< (ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
  67. typedef typename ptr_t< EXPAND T >::pointer V; \
  68. return std::less<V>(lhs.get(), nullptr); \
  69. } \
  70. \
  71. template <EXPAND T_tname> \
  72. bool operator< (std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
  73. typedef typename ptr_t< EXPAND T >::pointer V; \
  74. return std::less<V>(nullptr, rhs.get()); \
  75. } \
  76. \
  77. template <EXPAND T_tname> \
  78. bool operator> (ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
  79. return nullptr < lhs; \
  80. } \
  81. \
  82. template <EXPAND T_tname> \
  83. bool operator> (std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
  84. return rhs < nullptr; \
  85. } \
  86. \
  87. template <EXPAND T_tname> \
  88. bool operator<=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
  89. return !(nullptr < lhs); \
  90. } \
  91. \
  92. template <EXPAND T_tname> \
  93. bool operator<=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
  94. return !(rhs < nullptr); \
  95. } \
  96. \
  97. template <EXPAND T_tname> \
  98. bool operator>=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
  99. return !(lhs < nullptr); \
  100. } \
  101. \
  102. template <EXPAND T_tname> \
  103. bool operator>=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
  104. return !(nullptr < rhs); \
  105. }