1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_O3TL_HEAP_PTR_HXX
21 #define INCLUDED_O3TL_HEAP_PTR_HXX
24 #include <boost/assert.hpp>
25 #include <boost/checked_delete.hpp>
30 /** heap_ptr<> owns an object on the heap, which will be automatically
31 deleted, when ~heap_ptr<>() is called.
35 heap_ptr<> can be used for class members on the heap.
36 - One cannot forget to delete them in the destructor.
37 - Constness will be transfered from the owning instance.
39 heap_ptr<> can also be used as smart pointer in function bodies to
40 ensure exception safety.
42 Special Characteristics
43 -----------------------
44 - heap_ptr<> transfers constness from the owning object to
45 the pointed to object. Such it behaves like if *get() would be
46 a normal member of the owning object, not a pointer member.
47 This is preferable to the normal pointer behaviour, because
48 if an object is owned by another one, it is normally part of
51 - heap_ptr<> provides a ->release() function
53 - For reasons of simplicity and portability ->is()
54 is preferred over the safe-bool idiom.
58 heap_ptr is non copyable.
59 - It forbids the copyconstructor and operator=(self).
61 - Owning classes will automatically be non copyable, if they do not
62 redefine those two functions themselves.
66 heap_ptr<> also works with incomplete types. You only need to write
68 but need not include <T>.hxx.
70 If you use heap_ptr<> with an incomplete type, the owning class
71 needs to define a non-inline destructor. Else the compiler will
78 typedef T element_type
; /// Provided for generic programming.
79 typedef heap_ptr
<T
> self
;
82 typedef T
* (self::* safe_bool
)();
85 /// Now, pass_heapObject is owned by this.
87 T
* pass_heapObject
= 0 );
91 /** Identical to reset(), except of the return value.
92 @see heap_ptr<>::reset()
95 T
* pass_heapObject
);
96 const T
& operator*() const;
98 const T
* operator->() const;
101 /// True, if pHeapObject != 0.
103 operator safe_bool() const;
104 #else // workaround opt bug of Sun C++ compiler, when compiling with -xO3
105 operator bool() const;
109 /** This deletes any prevoiusly existing ->pHeapObject.
110 Now, pass_heapObject, if != 0, is owned by this.
113 Ignores self-assignment.
114 Such, multiple assignment of the same pointer to the same
115 instance of heap_ptr<> is possible (though not recommended).
118 T
* pass_heapObject
);
119 /** @return An object on the heap that must be deleted by the caller,
122 @postcond get() == 0;
128 /// True, if pHeapObject != 0.
130 const T
* get() const;
134 // Forbidden functions:
135 heap_ptr( const self
& ); /// Prevent copies.
136 self
& operator=( const self
& ); /// Prevent copies.
138 /// @attention Does not set ->pHeapObject = 0.
139 void internal_delete();
142 /// Will be deleted, when *this is destroyed.
147 /** Supports the semantic of std::swap(). Provided as an aid to
152 swap( heap_ptr
<T
> & io_a
,
164 heap_ptr
<T
>::internal_delete()
166 ::boost::checked_delete(pHeapObject
);
168 // Do not set pHeapObject to 0, because
169 // that is reset to a value in all code
170 // where internal_delete() is used.
175 heap_ptr
<T
>::heap_ptr( T
* pass_heapObject
)
176 : pHeapObject(pass_heapObject
)
182 heap_ptr
<T
>::~heap_ptr()
189 heap_ptr
<T
>::operator=(T
* pass_heapObject
)
191 reset(pass_heapObject
);
197 heap_ptr
<T
>::operator*() const
199 BOOST_ASSERT( pHeapObject
!= 0
200 && "Accessing a heap_ptr<>(0)." );
206 heap_ptr
<T
>::operator*()
208 BOOST_ASSERT( pHeapObject
!= 0
209 && "Accessing a heap_ptr<>(0)." );
215 heap_ptr
<T
>::operator->() const
222 heap_ptr
<T
>::operator->()
231 heap_ptr
<T
>::operator typename heap_ptr
<T
>::safe_bool() const
234 ? safe_bool(&self::get
)
241 inline heap_ptr
<T
>::operator bool() const
246 #endif // !defined(__SUNPRO_CC)
252 heap_ptr
<T
>::reset(T
* pass_heapObject
)
254 if ( pHeapObject
!= 0
255 && pHeapObject
== pass_heapObject
)
259 pHeapObject
= pass_heapObject
;
264 heap_ptr
<T
>::release()
266 T
* ret
= pHeapObject
;
273 heap_ptr
<T
>::swap(self
& io_other
)
275 T
* temp
= io_other
.pHeapObject
;
276 io_other
.pHeapObject
= pHeapObject
;
282 heap_ptr
<T
>::is() const
284 return pHeapObject
!= 0;
289 heap_ptr
<T
>::get() const
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */