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