Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1 | #ifndef NDNBOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 3 | |
| 4 | // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. |
| 5 | // Copyright (c) 2001, 2002 Peter Dimov |
| 6 | // |
| 7 | // Distributed under the Boost Software License, Version 1.0. (See |
| 8 | // accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | // |
| 11 | // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm |
| 12 | // |
| 13 | |
| 14 | #include <ndnboost/config.hpp> |
| 15 | #include <ndnboost/assert.hpp> |
| 16 | #include <ndnboost/checked_delete.hpp> |
| 17 | #include <ndnboost/smart_ptr/detail/sp_nullptr_t.hpp> |
| 18 | #include <ndnboost/detail/workaround.hpp> |
| 19 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 20 | #ifndef NDNBOOST_NO_AUTO_PTR |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 21 | # include <memory> // for std::auto_ptr |
| 22 | #endif |
| 23 | |
| 24 | namespace ndnboost |
| 25 | { |
| 26 | |
| 27 | // Debug hooks |
| 28 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 29 | #if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 30 | |
| 31 | void sp_scalar_constructor_hook(void * p); |
| 32 | void sp_scalar_destructor_hook(void * p); |
| 33 | |
| 34 | #endif |
| 35 | |
| 36 | // scoped_ptr mimics a built-in pointer except that it guarantees deletion |
| 37 | // of the object pointed to, either on destruction of the scoped_ptr or via |
| 38 | // an explicit reset(). scoped_ptr is a simple solution for simple needs; |
| 39 | // use shared_ptr or std::auto_ptr if your needs are more complex. |
| 40 | |
| 41 | template<class T> class scoped_ptr // noncopyable |
| 42 | { |
| 43 | private: |
| 44 | |
| 45 | T * px; |
| 46 | |
| 47 | scoped_ptr(scoped_ptr const &); |
| 48 | scoped_ptr & operator=(scoped_ptr const &); |
| 49 | |
| 50 | typedef scoped_ptr<T> this_type; |
| 51 | |
| 52 | void operator==( scoped_ptr const& ) const; |
| 53 | void operator!=( scoped_ptr const& ) const; |
| 54 | |
| 55 | public: |
| 56 | |
| 57 | typedef T element_type; |
| 58 | |
| 59 | explicit scoped_ptr( T * p = 0 ): px( p ) // never throws |
| 60 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 61 | #if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 62 | ndnboost::sp_scalar_constructor_hook( px ); |
| 63 | #endif |
| 64 | } |
| 65 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 66 | #ifndef NDNBOOST_NO_AUTO_PTR |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 67 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 68 | explicit scoped_ptr( std::auto_ptr<T> p ) NDNBOOST_NOEXCEPT : px( p.release() ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 69 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 70 | #if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 71 | ndnboost::sp_scalar_constructor_hook( px ); |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | #endif |
| 76 | |
| 77 | ~scoped_ptr() // never throws |
| 78 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 79 | #if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 80 | ndnboost::sp_scalar_destructor_hook( px ); |
| 81 | #endif |
| 82 | ndnboost::checked_delete( px ); |
| 83 | } |
| 84 | |
| 85 | void reset(T * p = 0) // never throws |
| 86 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 87 | NDNBOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 88 | this_type(p).swap(*this); |
| 89 | } |
| 90 | |
| 91 | T & operator*() const // never throws |
| 92 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 93 | NDNBOOST_ASSERT( px != 0 ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 94 | return *px; |
| 95 | } |
| 96 | |
| 97 | T * operator->() const // never throws |
| 98 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 99 | NDNBOOST_ASSERT( px != 0 ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 100 | return px; |
| 101 | } |
| 102 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 103 | T * get() const NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 104 | { |
| 105 | return px; |
| 106 | } |
| 107 | |
| 108 | // implicit conversion to "bool" |
| 109 | #include <ndnboost/smart_ptr/detail/operator_bool.hpp> |
| 110 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 111 | void swap(scoped_ptr & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 112 | { |
| 113 | T * tmp = b.px; |
| 114 | b.px = px; |
| 115 | px = tmp; |
| 116 | } |
| 117 | }; |
| 118 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 119 | #if !defined( NDNBOOST_NO_CXX11_NULLPTR ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 120 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 121 | template<class T> inline bool operator==( scoped_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 122 | { |
| 123 | return p.get() == 0; |
| 124 | } |
| 125 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 126 | template<class T> inline bool operator==( ndnboost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 127 | { |
| 128 | return p.get() == 0; |
| 129 | } |
| 130 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 131 | template<class T> inline bool operator!=( scoped_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 132 | { |
| 133 | return p.get() != 0; |
| 134 | } |
| 135 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 136 | template<class T> inline bool operator!=( ndnboost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 137 | { |
| 138 | return p.get() != 0; |
| 139 | } |
| 140 | |
| 141 | #endif |
| 142 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 143 | template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 144 | { |
| 145 | a.swap(b); |
| 146 | } |
| 147 | |
| 148 | // get_pointer(p) is a generic way to say p.get() |
| 149 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 150 | template<class T> inline T * get_pointer(scoped_ptr<T> const & p) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 151 | { |
| 152 | return p.get(); |
| 153 | } |
| 154 | |
| 155 | } // namespace ndnboost |
| 156 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 157 | #endif // #ifndef NDNBOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED |