blob: d79b393f8c5422bdf07ffb641cfa0eb52c7f8a90 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07003
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 Thompson3d613fd2013-10-15 15:39:04 -070020#ifndef NDNBOOST_NO_AUTO_PTR
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070021# include <memory> // for std::auto_ptr
22#endif
23
24namespace ndnboost
25{
26
27// Debug hooks
28
Jeff Thompson3d613fd2013-10-15 15:39:04 -070029#if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070030
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 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070061#if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070062 ndnboost::sp_scalar_constructor_hook( px );
63#endif
64 }
65
Jeff Thompson3d613fd2013-10-15 15:39:04 -070066#ifndef NDNBOOST_NO_AUTO_PTR
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070067
Jeff Thompson3d613fd2013-10-15 15:39:04 -070068 explicit scoped_ptr( std::auto_ptr<T> p ) NDNBOOST_NOEXCEPT : px( p.release() )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070069 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070070#if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070071 ndnboost::sp_scalar_constructor_hook( px );
72#endif
73 }
74
75#endif
76
77 ~scoped_ptr() // never throws
78 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070079#if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070080 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 Thompson3d613fd2013-10-15 15:39:04 -070087 NDNBOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070088 this_type(p).swap(*this);
89 }
90
91 T & operator*() const // never throws
92 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070093 NDNBOOST_ASSERT( px != 0 );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070094 return *px;
95 }
96
97 T * operator->() const // never throws
98 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070099 NDNBOOST_ASSERT( px != 0 );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700100 return px;
101 }
102
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700103 T * get() const NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700104 {
105 return px;
106 }
107
108// implicit conversion to "bool"
109#include <ndnboost/smart_ptr/detail/operator_bool.hpp>
110
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700111 void swap(scoped_ptr & b) NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700112 {
113 T * tmp = b.px;
114 b.px = px;
115 px = tmp;
116 }
117};
118
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700119#if !defined( NDNBOOST_NO_CXX11_NULLPTR )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700120
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700121template<class T> inline bool operator==( scoped_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700122{
123 return p.get() == 0;
124}
125
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700126template<class T> inline bool operator==( ndnboost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700127{
128 return p.get() == 0;
129}
130
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700131template<class T> inline bool operator!=( scoped_ptr<T> const & p, ndnboost::detail::sp_nullptr_t ) NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700132{
133 return p.get() != 0;
134}
135
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700136template<class T> inline bool operator!=( ndnboost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700137{
138 return p.get() != 0;
139}
140
141#endif
142
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700143template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700144{
145 a.swap(b);
146}
147
148// get_pointer(p) is a generic way to say p.get()
149
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700150template<class T> inline T * get_pointer(scoped_ptr<T> const & p) NDNBOOST_NOEXCEPT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700151{
152 return p.get();
153}
154
155} // namespace ndnboost
156
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700157#endif // #ifndef NDNBOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED