Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1 | #ifndef NDNBOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_SCOPED_ARRAY_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_array.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 | |
| 19 | #include <ndnboost/detail/workaround.hpp> |
| 20 | |
| 21 | #include <cstddef> // for std::ptrdiff_t |
| 22 | |
| 23 | namespace ndnboost |
| 24 | { |
| 25 | |
| 26 | // Debug hooks |
| 27 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 28 | #if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 29 | |
| 30 | void sp_array_constructor_hook(void * p); |
| 31 | void sp_array_destructor_hook(void * p); |
| 32 | |
| 33 | #endif |
| 34 | |
| 35 | // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to |
| 36 | // is guaranteed, either on destruction of the scoped_array or via an explicit |
| 37 | // reset(). Use shared_array or std::vector if your needs are more complex. |
| 38 | |
| 39 | template<class T> class scoped_array // noncopyable |
| 40 | { |
| 41 | private: |
| 42 | |
| 43 | T * px; |
| 44 | |
| 45 | scoped_array(scoped_array const &); |
| 46 | scoped_array & operator=(scoped_array const &); |
| 47 | |
| 48 | typedef scoped_array<T> this_type; |
| 49 | |
| 50 | void operator==( scoped_array const& ) const; |
| 51 | void operator!=( scoped_array const& ) const; |
| 52 | |
| 53 | public: |
| 54 | |
| 55 | typedef T element_type; |
| 56 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 57 | explicit scoped_array( T * p = 0 ) NDNBOOST_NOEXCEPT : px( p ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 58 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 59 | #if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 60 | ndnboost::sp_array_constructor_hook( px ); |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | ~scoped_array() // never throws |
| 65 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 66 | #if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 67 | ndnboost::sp_array_destructor_hook( px ); |
| 68 | #endif |
| 69 | ndnboost::checked_array_delete( px ); |
| 70 | } |
| 71 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 72 | void reset(T * p = 0) // never throws (but has a NDNBOOST_ASSERT in it, so not marked with NDNBOOST_NOEXCEPT) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 73 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 74 | NDNBOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 75 | this_type(p).swap(*this); |
| 76 | } |
| 77 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 78 | T & operator[](std::ptrdiff_t i) const // never throws (but has a NDNBOOST_ASSERT in it, so not marked with NDNBOOST_NOEXCEPT) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 79 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 80 | NDNBOOST_ASSERT( px != 0 ); |
| 81 | NDNBOOST_ASSERT( i >= 0 ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 82 | return px[i]; |
| 83 | } |
| 84 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 85 | T * get() const NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 86 | { |
| 87 | return px; |
| 88 | } |
| 89 | |
| 90 | // implicit conversion to "bool" |
| 91 | #include <ndnboost/smart_ptr/detail/operator_bool.hpp> |
| 92 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 93 | void swap(scoped_array & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 94 | { |
| 95 | T * tmp = b.px; |
| 96 | b.px = px; |
| 97 | px = tmp; |
| 98 | } |
| 99 | }; |
| 100 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 101 | #if !defined( NDNBOOST_NO_CXX11_NULLPTR ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 102 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 103 | template<class T> inline bool operator==( scoped_array<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 104 | { |
| 105 | return p.get() == 0; |
| 106 | } |
| 107 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 108 | template<class T> inline bool operator==( ndnboost::detail::sp_nullptr_t, scoped_array<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 109 | { |
| 110 | return p.get() == 0; |
| 111 | } |
| 112 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 113 | template<class T> inline bool operator!=( scoped_array<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 114 | { |
| 115 | return p.get() != 0; |
| 116 | } |
| 117 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 118 | template<class T> inline bool operator!=( ndnboost::detail::sp_nullptr_t, scoped_array<T> const & p ) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 119 | { |
| 120 | return p.get() != 0; |
| 121 | } |
| 122 | |
| 123 | #endif |
| 124 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 125 | template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 126 | { |
| 127 | a.swap(b); |
| 128 | } |
| 129 | |
| 130 | } // namespace ndnboost |
| 131 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 132 | #endif // #ifndef NDNBOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED |