]> git.lyx.org Git - lyx.git/blob - boost/boost/scoped_ptr.hpp
update to boost 1.30.1
[lyx.git] / boost / boost / scoped_ptr.hpp
1 #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
2 #define BOOST_SCOPED_PTR_HPP_INCLUDED
3
4 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
5 //  Copyright (c) 2001, 2002 Peter Dimov
6 //
7 //  Permission to copy, use, modify, sell and distribute this software
8 //  is granted provided this copyright notice appears in all copies.
9 //  This software is provided "as is" without express or implied
10 //  warranty, and with no claim as to its suitability for any purpose.
11 //
12 //  http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
13 //
14
15 #include <boost/assert.hpp>
16 #include <boost/checked_delete.hpp>
17 #include <boost/detail/workaround.hpp>
18
19 #ifndef BOOST_NO_AUTO_PTR
20 # include <memory>          // for std::auto_ptr
21 #endif
22
23 namespace boost
24 {
25
26 // Debug hooks
27
28 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
29
30 void sp_scalar_constructor_hook(void * p);
31 void sp_scalar_destructor_hook(void * p);
32
33 #endif
34
35 //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
36 //  of the object pointed to, either on destruction of the scoped_ptr or via
37 //  an explicit reset(). scoped_ptr is a simple solution for simple needs;
38 //  use shared_ptr or std::auto_ptr if your needs are more complex.
39
40 template<class T> class scoped_ptr // noncopyable
41 {
42 private:
43
44     T * ptr;
45
46     scoped_ptr(scoped_ptr const &);
47     scoped_ptr & operator=(scoped_ptr const &);
48
49     typedef scoped_ptr<T> this_type;
50
51 public:
52
53     typedef T element_type;
54
55     explicit scoped_ptr(T * p = 0): ptr(p) // never throws
56     {
57 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
58         boost::sp_scalar_constructor_hook(ptr);
59 #endif
60     }
61
62 #ifndef BOOST_NO_AUTO_PTR
63
64     explicit scoped_ptr(std::auto_ptr<T> p): ptr(p.release()) // never throws
65     {
66 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
67         boost::sp_scalar_constructor_hook(ptr);
68 #endif
69     }
70
71 #endif
72
73     ~scoped_ptr() // never throws
74     {
75 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
76         boost::sp_scalar_destructor_hook(ptr);
77 #endif
78         boost::checked_delete(ptr);
79     }
80
81     void reset(T * p = 0) // never throws
82     {
83         BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors
84         this_type(p).swap(*this);
85     }
86
87     T & operator*() const // never throws
88     {
89         BOOST_ASSERT(ptr != 0);
90         return *ptr;
91     }
92
93     T * operator->() const // never throws
94     {
95         BOOST_ASSERT(ptr != 0);
96         return ptr;
97     }
98
99     T * get() const // never throws
100     {
101         return ptr;
102     }
103
104     // implicit conversion to "bool"
105
106 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
107
108     operator bool () const
109     {
110         return ptr != 0;
111     }
112
113 #else
114
115     typedef T * (this_type::*unspecified_bool_type)() const;
116
117     operator unspecified_bool_type() const // never throws
118     {
119         return ptr == 0? 0: &this_type::get;
120     }
121
122 #endif
123
124     bool operator! () const // never throws
125     {
126         return ptr == 0;
127     }
128
129     void swap(scoped_ptr & b) // never throws
130     {
131         T * tmp = b.ptr;
132         b.ptr = ptr;
133         ptr = tmp;
134     }
135 };
136
137 template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
138 {
139     a.swap(b);
140 }
141
142 // get_pointer(p) is a generic way to say p.get()
143
144 template<class T> inline T * get_pointer(scoped_ptr<T> const & p)
145 {
146     return p.get();
147 }
148
149 } // namespace boost
150
151 #endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED