2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #ifndef SMARTPTR_HPP_INCLUDED
25 #define SMARTPTR_HPP_INCLUDED
29 /// @brief A generic, non-intrusive shared pointer class.
33 /// The object pointed to. If this is NULL, counter must also be NULL.
35 /// Pointer to the reference counter of the object pointed to.
36 /// If this is NULL, rawptr must also be NULL.
39 /// For handling inherited pointed to objects.
40 template <typename U
> friend class SmartPtr
;
42 /// Like the copy constructor. Copy the pointer of the pointed to
43 /// object to this SmartPtr.
44 /// @param the_other The other smart pointer to copy.
46 void copy(SmartPtr
<U
> const &the_other
) {
47 rawptr
= the_other
.rawptr
;
48 counter
= the_other
.counter
;
54 /// Default ctor, which constructs a NULL smartpointer.
55 SmartPtr() : rawptr(NULL
), counter(NULL
) {}
57 /// Create a smart pointer, optionally pointing to a newly allocated
58 /// object. If the pointer given as the first parameter is not NULL,
59 /// it must have been allocated with new, and no other SmartPtr objects
60 /// are allowed to point to it.
61 /// @param rawptr The object to point to.
64 : rawptr(rawptr
), counter(NULL
) {
70 /// @param the_other The other smart pointer to share the managed object with.
71 SmartPtr(SmartPtr
const &the_other
) {
75 /// Templated copy ctor to allow smart pointers to handle inheritance
76 /// relationships between objects pointed to.
77 /// @param the_other The other smart pointer to share the managed object with.
79 SmartPtr(SmartPtr
<U
> const &the_other
) {
83 /// Simple assignment operator. May delete the object that was pointed to.
84 /// @param the_other The other smart pointer to share the managed object with.
85 SmartPtr
&operator=(SmartPtr
const &the_other
) {
86 if (this != &the_other
) {
93 /// Templated assignment operator to allow smart pointers to handle inheritance
94 /// relationships between objects pointed to.
95 /// @param the_other The other smart pointer to share the managed object with.
97 SmartPtr
&operator=(SmartPtr
<U
> const &the_other
) {
98 if (this != &the_other
) {
105 /// Destructor. If the destructed smart pointer was the last one pointing to
106 /// the managed object, it will be deleted.
112 T
&operator*() const {
114 throw std::logic_error("Null SmartPtr");
119 T
*operator->() const {
121 throw std::logic_error("Null SmartPtr");
125 /// Compare for equality: two smart pointers are equal if they point to the same object.
126 bool operator==(void *ptr
) const {
127 return rawptr
== ptr
;
130 /// Compare for inequality: two smart pointers are inequal if they point to different objects.
131 bool operator!=(void *ptr
) const {
132 return rawptr
!= ptr
;
135 /// Release the managed object, and become a NULL pointer.
136 /// If this smart pointer was the last one pointing to
137 /// the managed object, it will be deleted.
139 if (counter
!= NULL
) {