Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/boost/scoped_ptr.hpp @ 18

Last change on this file since 18 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.4 KB
RevLine 
[12]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//  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 <boost/assert.hpp>
15#include <boost/checked_delete.hpp>
16#include <boost/detail/workaround.hpp>
17
18#ifndef BOOST_NO_AUTO_PTR
19# include <memory>          // for std::auto_ptr
20#endif
21
22namespace boost
23{
24
25// Debug hooks
26
27#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
28
29void sp_scalar_constructor_hook(void * p);
30void sp_scalar_destructor_hook(void * p);
31
32#endif
33
34//  scoped_ptr mimics a built-in pointer except that it guarantees deletion
35//  of the object pointed to, either on destruction of the scoped_ptr or via
36//  an explicit reset(). scoped_ptr is a simple solution for simple needs;
37//  use shared_ptr or std::auto_ptr if your needs are more complex.
38
39template<class T> class scoped_ptr // noncopyable
40{
41private:
42
43    T * ptr;
44
45    scoped_ptr(scoped_ptr const &);
46    scoped_ptr & operator=(scoped_ptr const &);
47
48    typedef scoped_ptr<T> this_type;
49
50public:
51
52    typedef T element_type;
53
54    explicit scoped_ptr(T * p = 0): ptr(p) // never throws
55    {
56#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
57        boost::sp_scalar_constructor_hook(ptr);
58#endif
59    }
60
61#ifndef BOOST_NO_AUTO_PTR
62
63    explicit scoped_ptr(std::auto_ptr<T> p): ptr(p.release()) // never throws
64    {
65#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
66        boost::sp_scalar_constructor_hook(ptr);
67#endif
68    }
69
70#endif
71
72    ~scoped_ptr() // never throws
73    {
74#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
75        boost::sp_scalar_destructor_hook(ptr);
76#endif
77        boost::checked_delete(ptr);
78    }
79
80    void reset(T * p = 0) // never throws
81    {
82        BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors
83        this_type(p).swap(*this);
84    }
85
86    T & operator*() const // never throws
87    {
88        BOOST_ASSERT(ptr != 0);
89        return *ptr;
90    }
91
92    T * operator->() const // never throws
93    {
94        BOOST_ASSERT(ptr != 0);
95        return ptr;
96    }
97
98    T * get() const // never throws
99    {
100        return ptr;
101    }
102
103    // implicit conversion to "bool"
104
105#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
106
107    operator bool () const
108    {
109        return ptr != 0;
110    }
111
112#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
113    typedef T * (this_type::*unspecified_bool_type)() const;
114   
115    operator unspecified_bool_type() const // never throws
116    {
117        return ptr == 0? 0: &this_type::get;
118    }
119
120#else
121    typedef T * this_type::*unspecified_bool_type;
122
123    operator unspecified_bool_type() const // never throws
124    {
125        return ptr == 0? 0: &this_type::ptr;
126    }
127
128#endif
129
130    bool operator! () const // never throws
131    {
132        return ptr == 0;
133    }
134
135    void swap(scoped_ptr & b) // never throws
136    {
137        T * tmp = b.ptr;
138        b.ptr = ptr;
139        ptr = tmp;
140    }
141};
142
143template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
144{
145    a.swap(b);
146}
147
148// get_pointer(p) is a generic way to say p.get()
149
150template<class T> inline T * get_pointer(scoped_ptr<T> const & p)
151{
152    return p.get();
153}
154
155} // namespace boost
156
157#endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
Note: See TracBrowser for help on using the repository browser.