copy_ptr.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // copy_ptr.hpp
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 12/6/16.
  6. //
  7. #pragma once
  8. #include <memory>
  9. #include "pointer_fwd.hpp"
  10. #include "ptr_compare.hpp"
  11. namespace detail {
  12. template <typename T>
  13. T * copy_ptr_from_ptr(T const * ptr) {
  14. return ptr ? new T(*ptr) : nullptr;
  15. }
  16. template <typename T>
  17. using clone_func = T* (T::*)() const;
  18. template <typename T, clone_func<T> Clone>
  19. T * clone_ptr_from_ptr(T const * ptr) {
  20. return ptr ? (ptr->*Clone)() : nullptr;
  21. }
  22. }
  23. template <typename T, T*(*Copy)(T const *) = &detail::copy_ptr_from_ptr<T> >
  24. class copy_ptr {
  25. public:
  26. using element_type = T;
  27. using pointer = element_type *;
  28. using reference = element_type &;
  29. copy_ptr() noexcept : _ptr(nullptr) {}
  30. copy_ptr(T * const & p) = delete;
  31. copy_ptr(T * && p) noexcept(std::is_nothrow_move_constructible<T>::value) : _ptr(std::move(p)) {}
  32. copy_ptr(copy_ptr const & other) : _ptr(Copy(other._ptr)) {}
  33. copy_ptr(copy_ptr && other) noexcept(noexcept(swap(_ptr, other._ptr))) : _ptr(nullptr) { swap(_ptr, other._ptr); }
  34. static copy_ptr copy_of(T * const & p) {
  35. return Copy(p);
  36. }
  37. template <typename Y>
  38. explicit operator copy_ptr<Y>() const {
  39. return Copy(_ptr);
  40. }
  41. ~copy_ptr() { delete _ptr; }
  42. copy_ptr & operator=(copy_ptr const & other) noexcept(noexcept(swap(_ptr, other._ptr))) {
  43. swap(_ptr, copy_ptr{other}._ptr);
  44. return *this;
  45. }
  46. copy_ptr & operator=(copy_ptr && other) noexcept(noexcept(swap(_ptr, other._ptr))) {
  47. swap(_ptr, other._ptr);
  48. return *this;
  49. }
  50. operator bool() const noexcept {
  51. return static_cast<bool>(_ptr);
  52. }
  53. pointer get() const noexcept { return _ptr; }
  54. pointer operator->() const noexcept { return get(); }
  55. reference operator*() const noexcept { return *get(); }
  56. private:
  57. T * _ptr;
  58. };
  59. template <typename T, detail::clone_func<T> Clone>
  60. using clone_ptr = copy_ptr<T, &detail::clone_ptr_from_ptr<T, Clone>>;
  61. POINTER_TEMPLATE_COMPARE_FULL( copy_ptr,
  62. (typename T1, T1*(*C1)(T1*)), (T1, C1),
  63. (typename T2, T2*(*C2)(T2*)), (T2, C2))