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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
All rights reserved.
This file is part of the Palm OS Emulator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
\* ===================================================================== */
#ifndef EmRefCounted_h
#define EmRefCounted_h
/*
EmRefCounted is a base class for any other class that you wish to be
reference counted. Simply descend from this guy; you don't have to
override any members.
EmRefCounter is a class for managing RefCounted objects. It takes care
of the tedious details of adding and removing refcounts, cloning the
object if necessary (if the object is marked unshareable).
See EmRegion's implementation for an example of this class's use. In
general, it's pretty simple:
{
EmRefCounter owner1(CreateRefCountedObject()); // One owner
EmRefCounter owner2 = owner1; // Two owners
{
EmRefCounter owner3 = owner1; // Three owners
}
// Back down to two owners here
}
// No more owners, the internal object is destroyed.
This class is based on the discussion by Scott Meyers in one of his
"More Effective C++" books.
*/
class EmRefCounted
{
public:
EmRefCounted (void);
EmRefCounted (const EmRefCounted& copy);
virtual ~EmRefCounted (void);
EmRefCounted& operator= (const EmRefCounted& other);
// Override of the assignment operator to prevent the refcount
// from being transferred from the rhs object.
void addReference (void);
void removeReference (void);
// Add or remove a reference to the object. Attempting to
// add a reference to an unshareable object is an error; an
// assertion will be thrown. Removing the last reference to
// an object will cause that object to delete itself.
void markUnshareable (void);
Bool isShareable (void) const;
Bool isShared (void) const;
// Marking an object unshareable is a signal to the client/owner
// of the RefCounted class that this object should not have
// addReference() called on it. Instead, the object should be
// cloned (EmRefCounter respects this convention). Call isShareable
// to see if the object can be shared. Call IsShared to see if
// more than one client is pointing to this object.
private:
long fRefCount;
};
template <class T>
class EmRefCounter
{
public:
EmRefCounter (T* p);
EmRefCounter (const EmRefCounter& other);
~EmRefCounter (void);
EmRefCounter<T>& operator= (const EmRefCounter& other);
EmRefCounter<T>& operator= (T* p);
// Methods for assuming (shared) ownership of a refcounted
// object. If the object does not descend from EmRefCounted,
// you'll get a compiler error.
int operator== (const EmRefCounter& other) const;
// See if the internal objects are equal to each other.
T* operator-> (void) const;
T& operator* (void) const;
T* get (void) const;
// Ways of getting to the internal object.
private:
EmRefCounter (void);
void Init (void);
void Replace (T*);
T* fObject;
};
// ----------------------------------------------------------------------
inline
EmRefCounted::EmRefCounted (void) :
fRefCount (0)
{
}
inline
EmRefCounted::EmRefCounted (const EmRefCounted&) :
fRefCount (0)
{
}
inline
EmRefCounted::~EmRefCounted (void)
{
}
inline
EmRefCounted&
EmRefCounted::operator= (const EmRefCounted&)
{
return *this;
}
inline
void
EmRefCounted::addReference (void)
{
EmAssert(fRefCount >= 0); // Can't add a reference to an unshareable item
fRefCount++;
}
inline
void
EmRefCounted::removeReference (void)
{
if (--fRefCount <= 0)
delete this;
}
inline
void
EmRefCounted::markUnshareable (void)
{
fRefCount = -1;
}
inline
Bool
EmRefCounted::isShareable (void) const
{
return fRefCount >= 0;
}
inline
Bool
EmRefCounted::isShared (void) const
{
return fRefCount > 0;
}
// ----------------------------------------------------------------------
template <class T>
EmRefCounter<T>::EmRefCounter (void) :
fObject (NULL)
{
this->Init ();
}
template <class T>
EmRefCounter<T>::EmRefCounter (T* p) :
fObject (p)
{
this->Init ();
}
template <class T>
EmRefCounter<T>::EmRefCounter (const EmRefCounter& other) :
fObject (other.fObject)
{
this->Init ();
}
template <class T>
EmRefCounter<T>::~EmRefCounter ()
{
if (fObject)
fObject->removeReference ();
}
template <class T>
EmRefCounter<T>&
EmRefCounter<T>::operator= (const EmRefCounter& other)
{
this->Replace (other.fObject);
return *this;
}
template <class T>
EmRefCounter<T>&
EmRefCounter<T>::operator= (T* p)
{
this->Replace (p);
return *this;
}
template <class T>
int
EmRefCounter<T>::operator== (const EmRefCounter& other) const
{
if (this == &other)
return true;
if (this->fObject == other.fObject)
return true;
return *(this->fObject) == *(other.fObject);
}
template <class T>
T*
EmRefCounter<T>::operator-> (void) const
{
return fObject;
}
template <class T>
T&
EmRefCounter<T>::operator* (void) const
{
return *fObject;
}
template <class T>
T*
EmRefCounter<T>::get (void) const
{
return fObject;
}
template <class T>
void
EmRefCounter<T>::Init ()
{
if (fObject)
{
if (!fObject->isShareable ())
{
fObject = new T (*fObject);
}
fObject->addReference ();
}
}
template <class T>
void
EmRefCounter<T>::Replace (T* p)
{
if (fObject != p)
{
if (fObject)
fObject->removeReference ();
fObject = p;
Init ();
}
}
#endif // EmRefCounted_h
|