| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- //
- // copy_ptr.hpp
- // pointers
- //
- // Created by Sam Jaffe on 12/6/16.
- //
- #pragma once
- #include <memory>
- #include "detail/compare.hpp"
- #include "pointer_fwd.hpp"
- namespace pointers {
- namespace detail {
- template <typename T> using clone_func = T * (T::*)() const;
- template <typename T, typename = void> class value_ptr_copy;
- template <typename T>
- struct value_ptr_copy<T, std::enable_if_t<!std::is_polymorphic<T>::value>> {
- T * Copy(T * ptr) const { return ptr ? new T(*ptr) : nullptr; }
- };
- #define POLYMORPHIC_VALUE_PTR_GROUP_FROM_CLONE_FUNCTION(fclone) \
- template <typename T> \
- struct value_ptr_copy< \
- T, std::enable_if_t< \
- std::is_polymorphic<T>::value && \
- std::is_same<clone_func<T>, decltype(&T::fclone)>::value>> { \
- T * Copy(T * ptr) const { return ptr ? ptr->fclone() : nullptr; } \
- }
- POLYMORPHIC_VALUE_PTR_GROUP_FROM_CLONE_FUNCTION(Clone);
- POLYMORPHIC_VALUE_PTR_GROUP_FROM_CLONE_FUNCTION(clone);
- POLYMORPHIC_VALUE_PTR_GROUP_FROM_CLONE_FUNCTION(Copy);
- POLYMORPHIC_VALUE_PTR_GROUP_FROM_CLONE_FUNCTION(copy);
- }
- template <typename T>
- class value_ptr : private detail::value_ptr_copy<T>,
- public detail::pointer_compare<value_ptr<T>> {
- public:
- using element_type = T;
- using pointer = element_type *;
- using reference = element_type &;
- value_ptr() noexcept : _ptr(nullptr) {}
- value_ptr(T * const & p) = delete;
- value_ptr(T *&& p) noexcept(std::is_nothrow_move_constructible<T>::value)
- : _ptr(std::move(p)) {}
- value_ptr(value_ptr const & other)
- : _ptr(detail::value_ptr_copy<T>::Copy(other._ptr)) {}
- value_ptr(value_ptr && other) noexcept(noexcept(std::swap(_ptr,
- other._ptr)))
- : _ptr(nullptr) {
- std::swap(_ptr, other._ptr);
- }
- static value_ptr copy_of(T * const & p) {
- return detail::value_ptr_copy<T>().Copy(p);
- }
- template <typename Y> explicit operator value_ptr<Y>() const {
- return Copy(_ptr);
- }
- ~value_ptr() { delete _ptr; }
- value_ptr & operator=(value_ptr const & other) {
- swap(_ptr, value_ptr{other}._ptr);
- return *this;
- }
- value_ptr & operator=(value_ptr && other) noexcept {
- swap(_ptr, other._ptr);
- return *this;
- }
- operator bool() const noexcept { return static_cast<bool>(_ptr); }
- pointer get() const noexcept { return _ptr; }
- pointer operator->() const noexcept { return get(); }
- reference operator*() const noexcept { return *get(); }
- private:
- static void swap(T * a, T * b) {
- T * tmp = a;
- a = b;
- b = tmp;
- }
- T * _ptr;
- };
- template <typename T, typename... Args>
- value_ptr<T> make_value(Args &&... args) {
- return value_ptr<T>(new T(std::forward<Args>(args)...));
- }
- }
|