My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//---------------------------------------------------------------------
// Provide an non-intersive reference counting smart pointer template
// Copyright (C) 2007 Charles S. Wilson
// This file is part of the OptionParse library.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//---------------------------------------------------------------------
#ifndef OptionParse__ExternalCountedPtr_HEADER
#define OptionParse__ExternalCountedPtr_HEADER

#include <OptionParse/OptionParseVisibility.h>
#include <stdexcept>

namespace OptionParse {

// forward declaration
class OPTIONPARSE_IMPEXP ExternalCountedPtrBase;

class OPTIONPARSE_IMPEXP ExternalCounter
{
private:
unsigned count;
public:
ExternalCounter(unsigned c = 1) : count(c) {}
friend class ExternalCountedPtrBase;
};

class OPTIONPARSE_IMPEXP ExternalCountedPtrBase
{
protected:
ExternalCounter* theCounter; // managed by derived class

virtual void deleteTarget(void ) throw() = 0;
virtual void assignTarget(void*) throw() = 0;

const unsigned countVal(void) const { return (theCounter ? theCounter->count : 0); }

// derived class should only call this method immediately
// after calling release(), or in constructor
void acquire(ExternalCounter* c, void* p) throw()
{ // increment the count
theCounter = c;
if (c)
{
++c->count;
assignTarget(p);
}
}

void release()
{ // decrement the count, delete if it is 0
if (theCounter) {
if (--theCounter->count == 0) {
deleteTarget();
delete theCounter;
}
theCounter = 0;
}
}

// no default constructor
ExternalCountedPtrBase(ExternalCounter* c)
: theCounter(c)
{}
};

template <typename X>
class ExternalCountedPtr : public ExternalCountedPtrBase
{
private:
X* thePtr;
protected:
// this should only be called by the release() method
// in the base class.
virtual void deleteTarget(void) throw()
{
if (thePtr)
delete thePtr;
thePtr = NULL;
}
virtual void assignTarget(void* p) throw()
{
thePtr = static_cast<X*>(p);
}

public:
typedef X element_type;

explicit ExternalCountedPtr(X* p = 0) // allocate a new counter
:
ExternalCountedPtrBase(0),
thePtr(p)
{
if (p)
theCounter = new ExternalCounter();
}

virtual ~ExternalCountedPtr()
{
release();
}

ExternalCountedPtr(const ExternalCountedPtr& r)
:
ExternalCountedPtrBase(0),
thePtr(NULL)
{
acquire(r.theCounter, r.thePtr);
}

ExternalCountedPtr& operator=(const ExternalCountedPtr& r)
{
if (this != &r)
{
release();
acquire(r.theCounter, r.thePtr);
}
return *this;
}
bool operator==(const ExternalCountedPtr& rhs) const { return (thePtr == rhs.thePtr); }
bool operator!=(const ExternalCountedPtr& rhs) const { return (thePtr != rhs.thePtr); }
bool operator <(const ExternalCountedPtr& rhs) const { return (thePtr < rhs.thePtr); }

template <typename Y> friend class ExternalCountedPtr;
template <typename Y> ExternalCountedPtr(const ExternalCountedPtr<Y>& r)
:
ExternalCountedPtrBase(0),
thePtr(NULL) // do NOT want to assign here, before count incremented
{
X* ptr = dynamic_cast<X*>(r.thePtr);
if (ptr)
{
acquire(r.theCounter, ptr);
}
}
template <class Y> ExternalCountedPtr& operator=(const ExternalCountedPtr<Y>& r)
{
if (this != &r)
{
release();
X* ptr = dynamic_cast<X*>(r.thePtr);
if (ptr)
{
acquire(r.theCounter, ptr);
}
}
return *this;
}

X& operator*() const
{
if (isNotNull())
return *thePtr;
else
throw std::range_error("Operator* access to a null pointer is not allowed.");
}
X* operator->() const
{
if (isNotNull())
return thePtr;
else
throw std::range_error("Operator-> access to a null pointer is not allowed.");
}
X* get() const throw() {return thePtr; }
bool unique() const throw() {return (countVal() == 1 || countVal() == 0); }
bool isNull() const throw() {return (!theCounter || !thePtr); }
bool isNotNull() const throw() {return !isNull(); }
void nullify() throw() { release(); assignTarget(NULL); }
};

} // namespace OptionParse

#endif // OptionParse__ExternalCountedPtr_HEADER

Change log

r5 by cwilso11 on Mar 18, 2007   Diff
Fix compile warnings and virtual
destructor issue
Go to: 
Project members, sign in to write a code review

Older revisions

r2 by cwilso11 on Mar 9, 2007   Diff
Original import
All revisions of this file

File info

Size: 5382 bytes, 186 lines
Powered by Google Project Hosting