blob: 7ee252b9320d13af980460e0033c357235bc5e33 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001#ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
2#define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
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
20#ifndef BOOST_NO_AUTO_PTR
21# include <memory> // for std::auto_ptr
22#endif
23
24namespace ndnboost
25{
26
27// Debug hooks
28
29#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
30
31void sp_scalar_constructor_hook(void * p);
32void 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
41template<class T> class scoped_ptr // noncopyable
42{
43private:
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
55public:
56
57 typedef T element_type;
58
59 explicit scoped_ptr( T * p = 0 ): px( p ) // never throws
60 {
61#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
62 ndnboost::sp_scalar_constructor_hook( px );
63#endif
64 }
65
66#ifndef BOOST_NO_AUTO_PTR
67
68 explicit scoped_ptr( std::auto_ptr<T> p ) BOOST_NOEXCEPT : px( p.release() )
69 {
70#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
71 ndnboost::sp_scalar_constructor_hook( px );
72#endif
73 }
74
75#endif
76
77 ~scoped_ptr() // never throws
78 {
79#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
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 {
87 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
88 this_type(p).swap(*this);
89 }
90
91 T & operator*() const // never throws
92 {
93 BOOST_ASSERT( px != 0 );
94 return *px;
95 }
96
97 T * operator->() const // never throws
98 {
99 BOOST_ASSERT( px != 0 );
100 return px;
101 }
102
103 T * get() const BOOST_NOEXCEPT
104 {
105 return px;
106 }
107
108// implicit conversion to "bool"
109#include <ndnboost/smart_ptr/detail/operator_bool.hpp>
110
111 void swap(scoped_ptr & b) BOOST_NOEXCEPT
112 {
113 T * tmp = b.px;
114 b.px = px;
115 px = tmp;
116 }
117};
118
119#if !defined( BOOST_NO_CXX11_NULLPTR )
120
121template<class T> inline bool operator==( scoped_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
122{
123 return p.get() == 0;
124}
125
126template<class T> inline bool operator==( ndnboost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT
127{
128 return p.get() == 0;
129}
130
131template<class T> inline bool operator!=( scoped_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
132{
133 return p.get() != 0;
134}
135
136template<class T> inline bool operator!=( ndnboost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT
137{
138 return p.get() != 0;
139}
140
141#endif
142
143template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) BOOST_NOEXCEPT
144{
145 a.swap(b);
146}
147
148// get_pointer(p) is a generic way to say p.get()
149
150template<class T> inline T * get_pointer(scoped_ptr<T> const & p) BOOST_NOEXCEPT
151{
152 return p.get();
153}
154
155} // namespace ndnboost
156
157#endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED