]> git.lyx.org Git - lyx.git/blob - boost/boost/scoped_ptr.hpp
update boost
[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 //  See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
13 //
14
15 #include <boost/assert.hpp>
16 #include <boost/checked_delete.hpp>
17
18 #ifndef BOOST_NO_AUTO_PTR
19 # include <memory>          // for std::auto_ptr
20 #endif
21
22 namespace boost
23 {
24
25 //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
26 //  of the object pointed to, either on destruction of the scoped_ptr or via
27 //  an explicit reset(). scoped_ptr is a simple solution for simple needs;
28 //  use shared_ptr or std::auto_ptr if your needs are more complex.
29
30 template<typename T> class scoped_ptr // noncopyable
31 {
32 private:
33
34     T * ptr;
35
36     scoped_ptr(scoped_ptr const &);
37     scoped_ptr & operator=(scoped_ptr const &);
38
39     typedef scoped_ptr<T> this_type;
40
41 public:
42
43     typedef T element_type;
44
45     explicit scoped_ptr(T * p = 0): ptr(p) // never throws
46     {
47     }
48
49 #ifndef BOOST_NO_AUTO_PTR
50
51     explicit scoped_ptr(std::auto_ptr<T> p): ptr(p.release()) // never throws
52     {
53     }
54
55 #endif
56
57     ~scoped_ptr() // never throws
58     {
59         checked_delete(ptr);
60     }
61
62     void reset(T * p = 0) // never throws
63     {
64         if (ptr != p)
65         {
66             checked_delete(ptr);
67             ptr = p;
68         }
69     }
70
71     T & operator*() const // never throws
72     {
73         BOOST_ASSERT(ptr != 0);
74         return *ptr;
75     }
76
77     T * operator->() const // never throws
78     {
79         BOOST_ASSERT(ptr != 0);
80         return ptr;
81     }
82
83     T * get() const // never throws
84     {
85         return ptr;
86     }
87
88     // implicit conversion to "bool"
89
90     typedef T * (this_type::*unspecified_bool_type)() const;
91
92     operator unspecified_bool_type() const // never throws
93     {
94         return ptr == 0? 0: &this_type::get;
95     }
96
97     bool operator! () const // never throws
98     {
99         return ptr == 0;
100     }
101
102     void swap(scoped_ptr & b) // never throws
103     {
104         T * tmp = b.ptr;
105         b.ptr = ptr;
106         ptr = tmp;
107     }
108 };
109
110 template<typename T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
111 {
112     a.swap(b);
113 }
114
115 // get_pointer(p) is a generic way to say p.get()
116
117 template<typename T> inline T * get_pointer(scoped_ptr<T> const & p)
118 {
119     return p.get();
120 }
121
122 } // namespace boost
123
124 #endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED