| #pragma once |
| |
| #ifndef TCG_UNIQUE_PTR_H |
| #define TCG_UNIQUE_PTR_H |
| |
| |
| #include "traits.h" |
| #include "base.h" |
| #include "deleter_types.h" |
| |
| |
| #include "assert.h" |
| #include <utility> |
| |
| namespace tcg |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T, typename D = typename tcg::deleter<T>> |
| class unique_ptr : private D |
| { |
| public: |
| typedef typename tcg::traits<T>::element_type element_type; |
| typedef typename tcg::traits<element_type>::pointer_type ptr_type; |
| typedef typename tcg::traits<element_type>::reference_type ref_type; |
| |
| public: |
| explicit unique_ptr(ptr_type ptr = ptr_type()) |
| : m_ptr(ptr) |
| { |
| } |
| explicit unique_ptr(D d) : m_ptr(), D(d) {} |
| unique_ptr(ptr_type ptr, D d) : m_ptr(ptr), D(d) {} |
| |
| ~unique_ptr() { D::operator()(m_ptr); } |
| |
| friend void swap(unique_ptr &a, unique_ptr &b) |
| { |
| using std::swap; |
| |
| swap(static_cast<D &>(a), static_cast<D &>(b)); |
| swap(a.m_ptr, b.m_ptr); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ptr_type operator->() const { return m_ptr; } |
| ref_type operator*() const { return *m_ptr; } |
| ref_type operator[](size_t idx) const |
| { |
| return m_ptr[idx]; |
| } |
| |
| void reset(ptr_type ptr = ptr_type()) |
| { |
| D::operator()(m_ptr); |
| m_ptr = ptr; |
| } |
| |
| void reset(ptr_type ptr, D d) |
| { |
| reset(ptr); |
| D::operator=(d); |
| } |
| |
| ptr_type release() |
| { |
| ptr_type ptr = m_ptr; |
| m_ptr = ptr_type(); |
| return ptr; |
| } |
| |
| const ptr_type get() const { return m_ptr; } |
| ptr_type get() { return m_ptr; } |
| |
| private: |
| ptr_type m_ptr; |
| }; |
| |
| } |
| |
| #endif // TCG_UNIQUE_PTR_H |