yat
0.11.3pre
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
yat
utility
SmartPtr.h
1
#ifndef _theplu_yat_utility_smart_ptr_
2
#define _theplu_yat_utility_smart_ptr_
3
4
// $Id: SmartPtr.h 2778 2012-07-19 00:53:17Z peter $
5
6
/*
7
Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8
Copyright (C) 2012 Peter Johansson
9
10
This file is part of the yat library, http://dev.thep.lu.se/yat
11
12
The yat library is free software; you can redistribute it and/or
13
modify it under the terms of the GNU General Public License as
14
published by the Free Software Foundation; either version 3 of the
15
License, or (at your option) any later version.
16
17
The yat library is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
General Public License for more details.
21
22
You should have received a copy of the GNU General Public License
23
along with yat. If not, see <http://www.gnu.org/licenses/>.
24
*/
25
26
#include "yat_assert.h"
27
28
namespace
theplu {
29
namespace
yat {
30
namespace
utility {
31
45
template
<
typename
T>
46
class
SmartPtr
47
{
48
public
:
71
explicit
SmartPtr
(T* p=NULL,
bool
owner=
true
)
72
: pointee_(p)
73
{
74
if
(owner)
75
ref_count_ =
new
unsigned
int(1);
76
else
77
ref_count_ = NULL;
78
}
79
83
SmartPtr
(
const
SmartPtr
& other)
84
: pointee_(other.pointee_), ref_count_(other.ref_count_)
85
{
86
if
(ref_count_)
87
++(*ref_count_);
88
}
89
95
virtual
~SmartPtr
()
96
{
97
detach();
98
}
99
105
SmartPtr
&
operator=
(
const
SmartPtr
& rhs)
106
{
107
if
(pointee_!=rhs.pointee_){
108
detach();
109
pointee_ = rhs.pointee_;
110
ref_count_= rhs.ref_count_;
111
if
(ref_count_)
112
++(*ref_count_);
113
}
114
return
*
this
;
115
}
116
120
T*
operator->
(
void
)
const
121
{
122
return
pointee_;
123
}
124
128
T&
operator*
(
void
)
const
129
{
130
return
*pointee_;
131
}
132
133
private
:
134
T* pointee_;
135
unsigned
int
* ref_count_;
136
137
void
detach(
void
)
138
{
139
if
(ref_count_)
140
if
(!--(*ref_count_))
141
delete
pointee_;
142
}
143
};
144
145
}}}
// of namespace utility, yat, and theplu
146
#endif
Generated on Sat May 24 2014 03:33:05 for yat by
1.8.2