20130420
[gdash.git] / src / misc / smartptr.hpp
blobd685edcfcdc0e56e4fa0ae6df9dba5b938d59e2e
1 /*
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
27 #include <stdexcept>
29 /// @brief A generic, non-intrusive shared pointer class.
30 template <typename T>
31 class SmartPtr {
32 private:
33 /// The object pointed to. If this is NULL, counter must also be NULL.
34 T *rawptr;
35 /// Pointer to the reference counter of the object pointed to.
36 /// If this is NULL, rawptr must also be NULL.
37 int *counter;
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.
45 template <class U>
46 void copy(SmartPtr<U> const &the_other) {
47 rawptr = the_other.rawptr;
48 counter = the_other.counter;
49 if (counter != NULL)
50 ++ *counter;
53 public:
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.
62 template <typename U>
63 SmartPtr(U *rawptr)
64 : rawptr(rawptr), counter(NULL) {
65 if (rawptr != NULL)
66 counter = new int(1);
69 /// Simple copy ctor.
70 /// @param the_other The other smart pointer to share the managed object with.
71 SmartPtr(SmartPtr const &the_other) {
72 copy(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.
78 template <typename U>
79 SmartPtr(SmartPtr<U> const &the_other) {
80 copy(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) {
87 release();
88 copy(the_other);
90 return *this;
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.
96 template <typename U>
97 SmartPtr &operator=(SmartPtr<U> const &the_other) {
98 if (this != &the_other) {
99 release();
100 copy(the_other);
102 return *this;
105 /// Destructor. If the destructed smart pointer was the last one pointing to
106 /// the managed object, it will be deleted.
107 ~SmartPtr() {
108 release();
111 /// Dereferencing.
112 T &operator*() const {
113 if (rawptr == NULL)
114 throw std::logic_error("Null SmartPtr");
115 return *rawptr;
118 /// Dereferencing.
119 T *operator->() const {
120 if (rawptr == NULL)
121 throw std::logic_error("Null SmartPtr");
122 return rawptr;
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.
138 void release() {
139 if (counter != NULL) {
140 -- *counter;
141 if (*counter == 0) {
142 delete rawptr;
143 delete counter;
146 rawptr = NULL;
147 counter = NULL;
152 #endif