/usr/include/bakery-2.6/bakery/Utilities/sharedptr.h is in libbakery-2.6-dev 2.6.3-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* Copyright 2004 Murray Cumming
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef BAKERY_UTILITIES_SHAREDPTR_H
#define BAKERY_UTILITIES_SHAREDPTR_H
#include <iostream> //Just for debugging.
namespace Bakery
{
/**A shared reference-counting smart-pointer.
*
*/
template< typename T_obj >
class sharedptr
{
public:
typedef size_t size_type;
///Allocate a new instance.
sharedptr();
///Take ownership
explicit sharedptr(T_obj* pobj);
///Share ownership
sharedptr(const sharedptr& src);
///Share ownership
sharedptr& operator=(const sharedptr& src);
virtual ~sharedptr();
/** Dereferencing.
*/
inline T_obj& operator*();
/** Dereferencing.
*/
inline const T_obj& operator*() const;
/** Dereferencing.
*
* Use the methods of the underlying instance like so:
* @code
* refptr->memberfun()
* @endcode
*/
inline T_obj* operator->() const;
/** Test whether the RefPtr<> points to any underlying instance.
*
* Mimics usage of ordinary pointers:
* @code
* if (ptr)
* do_something();
* @endcode
*/
inline operator bool() const;
///Get the underlying instance:
inline T_obj* obj();
///Get the underlying instance:
inline const T_obj* obj() const;
protected:
inline void ref();
inline void unref();
size_type* m_pRefCount; //Shared between instances, by copying.
T_obj* m_pobj; //The underlying instance.
};
template< typename T_obj>
sharedptr<T_obj>::sharedptr()
: m_pRefCount(0), m_pobj(0)
{
}
template< typename T_obj>
sharedptr<T_obj>::sharedptr(T_obj* pobj)
: m_pRefCount(0), m_pobj(pobj)
{
//Start refcounting:
ref();
}
template< typename T_obj>
sharedptr<T_obj>::sharedptr(const sharedptr<T_obj>& src)
: m_pRefCount(src.m_pRefCount), m_pobj(src.m_pobj)
{
ref();
}
template< typename T_obj>
sharedptr<T_obj>& sharedptr<T_obj>::operator=(const sharedptr<T_obj>& src)
{
//std::cout << "sharedptr& operator=(const sharedptr& src)" << std::endl;
if(&src != this)
{
//Unref any existing stuff.
//operator= can never run before a constructor, so these values will be initialized already.
if(m_pobj) //The if() might not be required.
{
unref(); //Could cause a deallocation.
}
//Copy:
m_pobj = src.m_pobj;
m_pRefCount = src.m_pRefCount;
ref();
}
return *this;
}
template< typename T_obj>
sharedptr<T_obj>::~sharedptr()
{
unref();
}
/*
template< typename T_obj>
void sharedptr<T_obj>::clear_without_deallocating()
{
m_pobj = 0;
}
*/
template< typename T_obj>
inline
T_obj* sharedptr<T_obj>::obj()
{
return m_pobj;
}
template< typename T_obj>
inline
const T_obj* sharedptr<T_obj>::obj() const
{
return m_pobj;
}
template< typename T_obj>
inline
T_obj& sharedptr<T_obj>::operator*()
{
return *m_pobj;
}
template< typename T_obj>
inline
const T_obj& sharedptr<T_obj>::operator*() const
{
return *m_pobj;
}
template< typename T_obj>
inline
T_obj* sharedptr<T_obj>::operator->() const
{
return m_pobj;
}
template <class T_obj>
inline
sharedptr<T_obj>::operator bool() const
{
return (m_pobj != 0);
}
template <class T_obj>
inline
void sharedptr<T_obj>::ref()
{
if(m_pobj) //Don't waste time on invalid instances. These would be very rare anyway, and intentionally created with (0,0) construction.
{
if(m_pRefCount == 0)
{
//std::cout << "sharedptr::ref(): first ref" << std::endl;
//First ref, so allocate the shared count:
m_pRefCount = new size_type();
*m_pRefCount = 1;
}
else
{
//std::cout << "sharedptr::ref(): starting at" << *m_pRefCount << std::endl;
(*m_pRefCount)++;
}
}
}
template <class T_obj>
inline
void sharedptr<T_obj>::unref()
{
if(m_pRefCount)
{
//std::cout << "sharedptr::unref(): starting at " << *m_pRefCount << std::endl;
if( (*m_pRefCount) > 0 )
(*m_pRefCount)--;
//Unalloc if this is the last user of the obj:
if(*m_pRefCount == 0)
{
if(m_pobj)
{
//try
//{
//std::cout << "sharedptr::unref(): deallocating " << *m_pRefCount << std::endl;
delete m_pobj;
m_pobj = 0;
//}
/*
catch(ex_base&)
{
//std::cout << "sharedptr::unref(): exception thrown during deallocation." << std::endl;
//Ignore it. Can't throw an expection up to the destructor.
}
*/
m_pobj = 0;
}
//Clear ref count:
delete m_pRefCount;
m_pRefCount = 0;
}
}
else
{
//std::cout << "sharedptr::unref(): ref not setup." << std::endl;
}
}
} //namespace
#endif //BAKERY_UTILITIES_SHAREDPTR_H
|