/usr/include/trilinos/fei_SharedPtr.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | /*--------------------------------------------------------------------*/
/* Copyright 2005 Sandia Corporation. */
/* Under the terms of Contract DE-AC04-94AL85000, there is a */
/* non-exclusive license for use of this work by or on behalf */
/* of the U.S. Government. Export of this program may require */
/* a license from the United States Government. */
/*--------------------------------------------------------------------*/
#ifndef _fei_SharedPtr_hpp_
#define _fei_SharedPtr_hpp_
#include <fei_macros.hpp>
//
//fei::SharedPtr is a copy of the Sierra system's SharedPtr class, which
//was added by Kevin Copps. This class is a close copy of the boost shared_ptr.
//
//NOTE: In this copy, I've removed the member function 'swap', and the
//std::less specialization.
//
//boost::shared_ptr now allows a second template parameter which specifies
//a deleter object. Instead of adopting that, I'm taking a lazy approach and
//simply adding a default bool argument to a constructor which specifies
//whether the SharedPtr should delete the pointer or not.
//
// #ifdef SIERRA_NO_MEMBER_TEMPLATES
// #define FEI_NO_MEMBER_TEMPLATES
// #endif
namespace fei {
/**
* A smart pointer with reference counted copy semantics. This class is
* a close copy of boost::shared_ptr. See www.boost.org, and
* www.boost.org/libs/smart_ptr/smart_ptr.htm.
* For logistical reasons it was deemed easier to make a copy of the
* boost::shared_ptr rather than having FEI be dependent on boost.
* According to TR1 (C++ Technical Report 1), referenced on the boost
* web site, shared_ptr will be migrating into the C++ standard.
*
* fei::SharedPtr usage notes:
*
* Construction:
* - Construct fei::SharedPtr with a raw pointer obtained from new:
* fei::SharedPtr<MyObject> myptr(new MyObject);
* - fei::SharedPtr also has a copy-constructor.
*
* Assignment:
* - fei::SharedPtr objects can be assigned to each other. You can not
* assign a raw pointer to a fei::SharedPtr. Instead, use the reset
* member:
* myptr.reset(new MyObject);
*
* Comparison:
* - fei::SharedPtr objects can be compared to each other, but can
* not be compared to raw pointers. Instead, use the get member:
* if (myptr.get() == NULL) {...
*
* The object pointed to is deleted when the last SharedPtr pointing
* to it is destroyed or reset.
*
* @author Kevin Copps
* @date 12/4/2001
*/
template<typename T> class SharedPtr {
public:
/**
* The type of the stored pointer.
*/
typedef T element_type;
/**
* Constructs a SharedPtr, storing a copy of p, which must have
* been allocated via a C++ new expression or be 0. On exit,
* use_count() is 1 (even if p==0; see the destructor).
*
* The only exception which may be thrown by this constructor is
* std::bad_alloc. If an exception is thrown, delete p is called.
*
* @param p the pointer value recently allocated
*/
explicit SharedPtr(T* p)
: pointer(p)
{
try { // prevent leak if new throws
count = new long(1);
} catch (...) {
delete p;
throw;
}
}
SharedPtr(void)
: pointer(0) {
count = new long(1);
}
/**
* Destructor. If use_count() == 1, deletes the object pointed
* to by the stored pointer. Otherwise, use_count() for any
* remaining copies is decremented by 1. Note that in C++
* delete on a pointer with a value of 0 is harmless.
*
* Never throws an exception.
*/
~SharedPtr() { dispose(); }
#if !defined( FEI_NO_MEMBER_TEMPLATES )
/**
* Constructs a SharedPtr, as if by storing a copy of the pointer
* stored in x. Afterwards, use_count() for all copies is 1 more
* than the initial x.use_count().
*
* Never throws an exception.
*
* @param x a shared pointer to another type
*/
template<typename Y>
SharedPtr(const SharedPtr<Y>& x)
: pointer(x.pointer)
{
++*(count = x.count);
}
/**
* Assignment to a shared pointer of another type.
*
* First, if use_count() == 1, deletes the object pointed to by the
* stored pointer. Otherwise, use_count() for any remaining copies
* is decremented by 1. Note that in C++ delete on a pointer with a
* value of 0 is harmless.
*
* Then replaces the contents of this, as if by storing a copy of
* the pointer stored in x. Afterwards, use_count() for all copies
* is 1 more than the initial x.use_count().
*
* Never throws an exception.
*
* @param x a shared pointer to another type
*/
template<typename Y>
SharedPtr& operator=(const SharedPtr<Y>& x) {
share(x.pointer,x.count);
return *this;
}
#endif // FEI_NO_MEMBER_TEMPLATES
/**
* Constructs a SharedPtr, as if by storing a copy of the pointer
* stored in x. Afterwards, use_count() for all copies is 1 more
* than the initial x.use_count().
*
* Never throws an exception.
*
* @param x the shared pointer to copy
*/
SharedPtr(const SharedPtr& x)
: pointer(x.pointer)
{
++*(count = x.count);
}
/**
* Assignment to another shared pointer.
* First, if use_count() == 1, deletes the object pointed to by the
* stored pointer. Otherwise, use_count() for any remaining copies
* is decremented by 1. Note that in C++ delete on a pointer with a
* value of 0 is harmless.
*
* Then replaces the contents of this, as if by storing a copy of
* the pointer stored in x. Afterwards, use_count() for all copies
* is 1 more than the initial x.use_count().
*
* Does not throw any exception.
*
* @param x the shared pointer to copy
*/
SharedPtr& operator=(const SharedPtr& x) {
share(x.pointer, x.count);
return *this;
}
/**
* Reset the pointer value of this shared pointer.
* First, if use_count() == 1, deletes the object pointed to by the
* stored pointer. Otherwise, use_count() for any remaining copies
* is decremented by 1. Then replaces the contents of this, as if
* by storing a copy of p, which must have been allocated via a C++
* new expression or be 0. Afterwards, use_count() is 1
* (even if p==0; see ~SharedPtr).
*
* Note that in C++ delete on a pointer with a value of 0 is
* harmless.
*
* The only exception which may be thrown is std::bad_alloc.
* If an exception is thrown, delete p is called.
*
* @param p a pointer value, or 0 if not present
*/
void reset(T* p=0) {
if ( pointer == p ) return;
if (--*count == 0) {
delete pointer;
}
else { // allocate new reference counter
try {
count = new long;
}
catch (...) {
++*count;
delete p;
throw;
}
}
*count = 1;
pointer = p;
}
/**
* Returns a reference to the object pointed to by the stored
* pointer.
*
* Never throws an exception.
*/
T& operator*() const { return *pointer; }
/**
* Return the stored pointer.
*
* Never throws an exception.
*/
T* operator->() const { return pointer; }
/**
* Return the stored pointer. T is not required to be a complete type.
*
* Never throws an exception.
*/
T* get() const { return pointer; }
/**
* Returns the number of SharedPtr's sharing ownership
* of the stored pointer. T is not required to be a complete type.
*
* Never throws an exception.
*/
long use_count() const { return *count; }
/**
* Returns use_count() == 1.
* T is not required to be a complete type.
*
* Never throws an exception.
*/
bool unique() const { return *count == 1; }
/** power users only */
void share(T* xpointer, long* xcount) {
if (count != xcount) {
++*xcount;
dispose();
pointer = xpointer;
count = xcount;
}
}
/** power users only */
void dispose() {
if (--*count == 0) {
delete pointer;
delete count;
}
}
// Making all members public allows member templates
// to work in the absence of member template friends.
#if defined( FEI_NO_MEMBER_TEMPLATES ) || !defined( FEI_NO_MEMBER_TEMPLATES )
private:
#endif
T* pointer; // contained pointer
long* count; // ptr to reference counter
#if !defined( FEI_NO_MEMBER_TEMPLATES ) && !defined( FEI_NO_MEMBER_TEMPLATES )
template<typename Y> friend class SharedPtr;
#endif
}; // end class SharedPtr
/**
* Equals operator for shared pointers.
*/
template<typename T, typename U>
inline bool operator==(const SharedPtr<T>& a, const SharedPtr<U>& b)
{ return a.get() == b.get(); }
/**
* Not equals operator for shared pointers.
*/
template<typename T, typename U>
inline bool operator!=(const SharedPtr<T>& a, const SharedPtr<U>& b)
{ return a.get() != b.get(); }
} // namespace fei
#endif // _fei_SharedPtr_hpp_
|