1 | #ifndef _theplu_yat_utility_smart_ptr_ |
---|
2 | #define _theplu_yat_utility_smart_ptr_ |
---|
3 | |
---|
4 | // $Id: SmartPtr.h 1050 2008-02-07 18:47:34Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "yat_assert.h" |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace utility { |
---|
32 | |
---|
33 | /** |
---|
34 | */ |
---|
35 | template<typename T> |
---|
36 | class SmartPtr |
---|
37 | { |
---|
38 | public: |
---|
39 | /** |
---|
40 | \brief Constructor |
---|
41 | |
---|
42 | \param p underlying pointer |
---|
43 | \param owner if true SmartPtr will be owner of pointer and if |
---|
44 | there is no more owner delete it in destructor. |
---|
45 | |
---|
46 | Never use this constructor to create two SmartPtr to the same |
---|
47 | pointer such as |
---|
48 | \code |
---|
49 | MyClass* my_pointer = new MyClass; |
---|
50 | SmartPtr<MyClass> sp(my_pointer); |
---|
51 | SmartPtr<MyClass> sp2(my_pointer); // this is evil |
---|
52 | \endcode |
---|
53 | since this will cause multiple deletion. Instead use copy constructor |
---|
54 | \code |
---|
55 | MyClass* my_pointer = new MyClass; |
---|
56 | SmartPtr<MyClass> sp(my_pointer); |
---|
57 | SmartPtr<MyClass> sp2(sp); |
---|
58 | \endcode |
---|
59 | so the internal reference counter is updated. |
---|
60 | */ |
---|
61 | explicit SmartPtr(T* p=NULL, bool owner=true) |
---|
62 | : pointee_(p) |
---|
63 | { |
---|
64 | if (owner) |
---|
65 | ref_count_ = new u_int(1); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | \brief Copy constructor |
---|
70 | */ |
---|
71 | SmartPtr(const SmartPtr& other) |
---|
72 | : pointee_(other.pointee_), ref_count_(other.ref_count_) |
---|
73 | { |
---|
74 | if (ref_count_) |
---|
75 | ++(*ref_count_); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | \brief Destructor |
---|
80 | |
---|
81 | If SmartPtr is owner and the only owner, underlying pointer is deleted. |
---|
82 | */ |
---|
83 | virtual ~SmartPtr() |
---|
84 | { |
---|
85 | detach(); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | If SmartPtr is owner and the only owner, underlying pointer is deleted. |
---|
90 | |
---|
91 | If rhs is owner, lhs become also owner. |
---|
92 | */ |
---|
93 | SmartPtr& operator=(const SmartPtr& rhs) |
---|
94 | { |
---|
95 | if (pointee_!=rhs.pointee_){ |
---|
96 | detach(); |
---|
97 | pointee_ = rhs.pointee_; |
---|
98 | ref_count_= rhs.ref_count_; |
---|
99 | if (ref_count_) |
---|
100 | ++(*ref_count_); |
---|
101 | } |
---|
102 | return *this; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | \return underlying pointer |
---|
107 | */ |
---|
108 | T* operator->(void) const |
---|
109 | { |
---|
110 | return pointee_; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | \return reference to underlying object |
---|
115 | */ |
---|
116 | T& operator*(void) const |
---|
117 | { |
---|
118 | return *pointee_; |
---|
119 | } |
---|
120 | |
---|
121 | private: |
---|
122 | T* pointee_; |
---|
123 | u_int* ref_count_; |
---|
124 | |
---|
125 | void detach(void) |
---|
126 | { |
---|
127 | if (ref_count_) |
---|
128 | if (!--(*ref_count_)) |
---|
129 | delete pointee_; |
---|
130 | } |
---|
131 | }; |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | }}} // of namespace utility, yat, and theplu |
---|
139 | |
---|
140 | #endif |
---|