/usr/include/simgear/structure/SGSharedPtr.hxx is in libsimgear-dev 3.4.0-3.
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 | ///@file
/// Pointer proxy doing reference counting.
//
// Copyright (C) 2005-2012 Mathias Froehlich
//
// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef SGSharedPtr_HXX
#define SGSharedPtr_HXX
#include "SGReferenced.hxx"
#include <algorithm>
template<typename T>
class SGWeakPtr;
/// This class is a pointer proxy doing reference counting on the object
/// it is pointing to.
/// The SGSharedPtr class handles reference counting and possible
/// destruction if no nore references are in use automatically.
/// Classes derived from SGReferenced can be handled with SGSharedPtr.
/// Once you have a SGSharedPtr available you can use it just like
/// a usual pointer with the exception that you don't need to delete it.
/// Such a reference is initialized by zero if not initialized with a
/// class pointer.
/// One thing you need to avoid are cyclic loops with such pointers.
/// As long as such a cyclic loop exists the reference count never drops
/// to zero and consequently the objects will never be destroyed.
/// Always try to use directed graphs where the references away from the
/// top node are made with SGSharedPtr's and the back references are done with
/// ordinary pointers or SGWeakPtr's.
/// There is a very good description of OpenSceneGraphs ref_ptr which is
/// pretty much the same than this one at
/// http://dburns.dhs.org/OSG/Articles/RefPointers/RefPointers.html
template<typename T>
class SGSharedPtr {
public:
typedef T element_type;
SGSharedPtr(void) : _ptr(0)
{}
SGSharedPtr(T* ptr) : _ptr(ptr)
{ get(_ptr); }
SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
{ get(_ptr); }
template<typename U>
SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
{ get(_ptr); }
template<typename U>
explicit SGSharedPtr(const SGWeakPtr<U>& p): _ptr(0)
{ reset(p.lock().get()); }
~SGSharedPtr(void)
{ reset(); }
SGSharedPtr& operator=(const SGSharedPtr& p)
{ reset(p.get()); return *this; }
template<typename U>
SGSharedPtr& operator=(const SGSharedPtr<U>& p)
{ reset(p.get()); return *this; }
template<typename U>
SGSharedPtr& operator=(U* p)
{ reset(p); return *this; }
T* operator->(void) const
{ return _ptr; }
T& operator*(void) const
{ return *_ptr; }
operator T*(void) const
{ return _ptr; }
T* ptr(void) const
{ return _ptr; }
T* get(void) const
{ return _ptr; }
T* release()
{ T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
void reset()
{ if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
void reset(T* p)
{ SGSharedPtr(p).swap(*this); }
bool isShared(void) const
{ return T::shared(_ptr); }
unsigned getNumRefs(void) const
{ return T::count(_ptr); }
bool valid(void) const
{ return _ptr != (T*)0; }
void clear()
{ reset(); }
void swap(SGSharedPtr& other)
{ std::swap(_ptr, other._ptr); }
private:
void assignNonRef(T* p)
{ reset(); _ptr = p; }
void get(const T* p) const
{ T::get(p); }
// The reference itself.
T* _ptr;
template<typename U>
friend class SGWeakPtr;
};
/**
* Support for boost::mem_fn
*/
template<typename T>
T* get_pointer(SGSharedPtr<T> const & p)
{
return p.ptr();
}
/**
* static_cast for SGSharedPtr
*/
template<class T, class U>
SGSharedPtr<T> static_pointer_cast(SGSharedPtr<U> const & r)
{
return SGSharedPtr<T>( static_cast<T*>(r.get()) );
}
/**
* Compare two SGSharedPtr<T> objects for equality.
*
* @note Only pointer values are compared, not the actual objects they are
* pointing at.
*/
template<class T, class U>
bool operator==(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
{
return lhs.get() == rhs.get();
}
/**
* Compare two SGSharedPtr<T> objects for equality.
*
* @note Only pointer values are compared, not the actual objects they are
* pointing at.
*/
template<class T, class U>
bool operator!=(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
{
return lhs.get() != rhs.get();
}
/**
* Compare two SGSharedPtr<T> objects for weak ordering.
*
* @note Only pointer values are compared, not the actual objects they are
* pointing at.
* @note This allows using SGSharedPtr as key in associative containers like for
* example std::map and std::set.
*/
template<class T, class U>
bool operator<(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
{
return lhs.get() < rhs.get();
}
#endif
|