copy_ptr.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // copy_ptr.hpp
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 12/6/16.
  6. //
  7. #pragma once
  8. #include <functional>
  9. #include <memory>
  10. #include "pointer_fwd.hpp"
  11. namespace detail {
  12. template <typename T>
  13. T * copy_ptr_from_ptr(T * ptr) {
  14. return ptr ? new T(*ptr) : nullptr;
  15. }
  16. }
  17. template <typename T, T*(*Copy)(T*) = &detail::copy_ptr_from_ptr<T> >
  18. class copy_ptr {
  19. public:
  20. using element_type = T;
  21. using pointer = element_type *;
  22. using reference = element_type &;
  23. copy_ptr() : _ptr(nullptr) {}
  24. copy_ptr(P const & p) = delete;
  25. copy_ptr(P && p) : _ptr(std::move(p)) {}
  26. copy_ptr(copy_ptr const & other) : _ptr(Copy(other._ptr)) {}
  27. copy_ptr(copy_ptr &&) = default;
  28. template <typename Y>
  29. explicit operator copy_ptr<Y>() const {
  30. return Copy(_ptr);
  31. }
  32. ~copy_ptr() { delete _ptr; }
  33. copy_ptr & operator=(copy_ptr const & other) {
  34. swap(copy_ptr{other});
  35. return *this;
  36. }
  37. copy_ptr & operator=(copy_ptr &&) = default;
  38. operator bool() const {
  39. return static_cast<bool>(_ptr);
  40. }
  41. pointer get() const { return _ptr; }
  42. pointer operator->() const { return get(); }
  43. reference operator*() const { return *get(); }
  44. private:
  45. P _ptr;
  46. };
  47. template <typename T, typename U>
  48. bool operator==(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
  49. return lhs.get() == rhs.get();
  50. }
  51. template <typename T, typename U>
  52. bool operator!=(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
  53. return !(lhs == rhs);
  54. }
  55. template <typename T, typename U>
  56. bool operator< (copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
  57. typedef typename std::common_type<
  58. typename copy_ptr<T>::pointer,
  59. typename copy_ptr<U>::pointer>::type V;
  60. return std::less<V>(lhs.get(), rhs.get());
  61. }
  62. template <typename T, typename U>
  63. bool operator> (copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
  64. return rhs < lhs;
  65. }
  66. template <typename T, typename U>
  67. bool operator<=(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
  68. return !(rhs < lhs);
  69. }
  70. template <typename T, typename U>
  71. bool operator>=(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
  72. return !(lhs < rhs);
  73. }
  74. template <typename T>
  75. bool operator==(copy_ptr<T> const&lhs, std::nullptr_t) {
  76. return !lhs;
  77. }
  78. template <typename T>
  79. bool operator==(std::nullptr_t, copy_ptr<T> const&rhs) {
  80. return !rhs;
  81. }
  82. template <typename T>
  83. bool operator!=(copy_ptr<T> const&lhs, std::nullptr_t) {
  84. return static_cast<bool>(lhs);
  85. }
  86. template <typename T>
  87. bool operator!=(std::nullptr_t, copy_ptr<T> const&rhs) {
  88. return static_cast<bool>(rhs);
  89. }
  90. template <typename T>
  91. bool operator< (copy_ptr<T> const&lhs, std::nullptr_t) {
  92. typedef typename copy_ptr<T>::pointer V;
  93. return std::less<V>(lhs.get(), nullptr);
  94. }
  95. template <typename T>
  96. bool operator< (std::nullptr_t, copy_ptr<T> const&rhs) {
  97. typedef typename copy_ptr<T>::pointer V;
  98. return std::less<V>(nullptr, rhs.get());
  99. }
  100. template <typename T>
  101. bool operator> (copy_ptr<T> const&lhs, std::nullptr_t) {
  102. return nullptr < lhs;
  103. }
  104. template <typename T>
  105. bool operator> (std::nullptr_t, copy_ptr<T> const&rhs) {
  106. return rhs < nullptr;
  107. }
  108. template <typename T>
  109. bool operator<=(copy_ptr<T> const&lhs, std::nullptr_t) {
  110. return !(nullptr < lhs);
  111. }
  112. template <typename T>
  113. bool operator<=(std::nullptr_t, copy_ptr<T> const&rhs) {
  114. return !(rhs < nullptr);
  115. }
  116. template <typename T>
  117. bool operator>=(copy_ptr<T> const&lhs, std::nullptr_t) {
  118. return !(lhs < nullptr);
  119. }
  120. template <typename T>
  121. bool operator>=(std::nullptr_t, copy_ptr<T> const&rhs) {
  122. return !(nullptr < rhs);
  123. }