]> git.lyx.org Git - lyx.git/blob - boost/boost/scoped_ptr.hpp
major boost update
[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 namespace boost
19 {
20
21 //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
22 //  of the object pointed to, either on destruction of the scoped_ptr or via
23 //  an explicit reset(). scoped_ptr is a simple solution for simple needs;
24 //  use shared_ptr or std::auto_ptr if your needs are more complex.
25
26 template<typename T> class scoped_ptr // noncopyable
27 {
28 private:
29
30     T* ptr;
31
32     scoped_ptr(scoped_ptr const &);
33     scoped_ptr & operator=(scoped_ptr const &);
34
35 public:
36
37     typedef T element_type;
38
39     explicit scoped_ptr(T * p = 0): ptr(p) // never throws
40     {
41     }
42
43     ~scoped_ptr() // never throws
44     {
45         checked_delete(ptr);
46     }
47
48     void reset(T * p = 0) // never throws
49     {
50         if (ptr != p)
51         {
52             checked_delete(ptr);
53             ptr = p;
54         }
55     }
56
57     T & operator*() const // never throws
58     {
59         BOOST_ASSERT(ptr != 0);
60         return *ptr;
61     }
62
63     T * operator->() const // never throws
64     {
65         BOOST_ASSERT(ptr != 0);
66         return ptr;
67     }
68
69     T * get() const // never throws
70     {
71         return ptr;
72     }
73
74     void swap(scoped_ptr & b) // never throws
75     {
76         T * tmp = b.ptr;
77         b.ptr = ptr;
78         ptr = tmp;
79     }
80 };
81
82 template<typename T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
83 {
84     a.swap(b);
85 }
86
87 } // namespace boost
88
89 #endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED