/usr/include/cwidget/generic/util/ref_ptr.h is in libcwidget-dev 0.5.17-7.
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 | // ref_ptr.h -*-c++-*-
//
// A reference-counting pointer. The object behind the pointer should
// implement incref() and decref(); the pointer arranges for these
// methods to be called at the appropriate times.
#ifndef REF_PTR_H
#define REF_PTR_H
#include <sigc++/reference_wrapper.h>
#include <cwidget/generic/util/eassert.h>
namespace cwidget
{
namespace util
{
template<class T>
class ref_ptr
{
T *ref;
public:
ref_ptr(T *_ref)
:ref(_ref)
{
if(ref != 0)
ref->incref();
}
ref_ptr(const ref_ptr &other)
:ref(other.ref)
{
if(ref != 0)
ref->incref();
}
template<class S>
ref_ptr(const ref_ptr<S> &other)
:ref(other.unsafe_get_ref())
{
if(ref != 0)
ref->incref();
}
ref_ptr()
:ref(0)
{
}
~ref_ptr()
{
if(ref != 0)
ref->decref();
}
ref_ptr &operator=(const ref_ptr &other)
{
if(other.ref != 0)
other.ref->incref();
if(ref != 0)
ref->decref();
ref = other.ref;
return *this;
}
const sigc::reference_wrapper<T> weak_ref() const
{
eassert(ref != 0);
return sigc::ref(*ref);
}
// If S is assignment-compatible with T and both have
// reference-counting methods, perform this assignment.
//
// Read: upcasting pointers.
template<class S>
ref_ptr<T> &operator=(const ref_ptr<S> &other)
{
S * const other_ref = other.unsafe_get_ref();
if(other_ref != 0)
other_ref->incref();
if(ref != 0)
ref->decref();
ref = other_ref;
return *this;
}
template<class S>
bool operator==(const ref_ptr<S> &other) const
{
return ref == other.unsafe_get_ref();
}
template<class S>
bool operator!=(const ref_ptr<S> &other) const
{
return ref != other.unsafe_get_ref();
}
template<class S>
bool operator<(const ref_ptr<S> &other) const
{
return ref < other.unsafe_get_ref();
}
template<class S>
bool operator>(const ref_ptr<S> &other) const
{
return ref > other.unsafe_get_ref();
}
template<class S>
bool operator<=(const ref_ptr<S> &other) const
{
return ref <= other.unsafe_get_ref();
}
template<class S>
bool operator>=(const ref_ptr<S> &other) const
{
return ref >= other.unsafe_get_ref();
}
// Safe downcasting.
template<class S>
ref_ptr<S> dyn_downcast() const
{
return ref_ptr<S>(dynamic_cast<S*>(ref));
}
void clear()
{
(*this) = 0;
}
bool valid() const
{
return ref != 0;
}
T *operator->() const
{
return ref;
}
/** Extract the pointer. Should generally be used with care (but is
* used in the implementation to cast/compare between differently
* templated instances).
*/
T *unsafe_get_ref() const
{
return ref;
}
};
}
}
#endif
|