/usr/include/libmints/ref.h is in libpsi3-dev 3.4.0-6+b1.
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 | #ifndef _psi_src_lib_libmints_ref_h_
#define _psi_src_lib_libmints_ref_h_
/*!
\file libmints/ref.h
\ingroup MINTS
*/
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <assert.h>
namespace psi {
// For most object counted by Refs we can use the following simple object policy:
class StandardObjectPolicy {
public:
template<typename T> void dispose(T* object) {
delete object;
}
};
// Clearly, the above policy will not work for arrays allocated with operator new[].
// A replacement policy for this case is trival, fortunately:
class StandardArrayPolicy {
public:
template<typename T> void dispose(T* array) {
delete[] array;
}
};
class SimpleReferenceCount {
private:
size_t* _counter;
public:
SimpleReferenceCount() {
_counter = NULL;
}
public:
template<typename T> void init(T*) {
_counter = ::new size_t;
*_counter = 1;
}
template<typename T> void dispose(T*) {
::delete _counter;
}
template<typename T> void increment(T*) {
++*_counter;
}
template<typename T> void decrement(T*) {
--*_counter;
}
template<typename T> bool is_zero(T*) {
return *_counter == 0;
}
};
// Reference counting pointer:
template<typename T, typename CounterPolicy = SimpleReferenceCount, typename ObjectPolicy = StandardObjectPolicy>
class Ref : private CounterPolicy, private ObjectPolicy {
protected:
// shortcuts
typedef CounterPolicy CP;
typedef ObjectPolicy OP;
T* _object_pointed_to; // Object referred to (or NULL if none)
public:
// default constructor
Ref() {
_object_pointed_to = NULL;
}
// Test of copy constructor
Ref(T* p) {
_object_pointed_to = NULL;
if (p) {
init(p);
}
}
// explicit Ref(T* p) {
// init(p);
// }
Ref(Ref<T, CP, OP> const& cp) : CP((CP const&) cp), OP((OP const&)cp) {
_object_pointed_to = NULL;
attach(cp);
}
~Ref() {
detach();
}
Ref<T,CP,OP>& operator= (T* p) {
assert(p != _object_pointed_to);
detach();
init(p);
return *this;
}
Ref<T,CP,OP>& operator= (Ref<T,CP,OP> const& cp) {
if (_object_pointed_to != cp._object_pointed_to) {
detach();
CP::operator=((CP const&)cp);
OP::operator=((OP const&)cp);
attach(cp);
}
return *this;
}
T* operator-> () const {
return _object_pointed_to;
}
T& operator* () const {
return *_object_pointed_to;
}
operator T*() const { return _object_pointed_to; }
T* pointer() const {
#ifdef DEBUG
assert(_object_pointed_to != NULL);
#endif
return _object_pointed_to;
}
T& operator[] (int i) const {
return _object_pointed_to[i];
}
private:
void init(T* p) {
if (p != NULL) {
CounterPolicy::init(p);
}
_object_pointed_to = p;
}
void attach(Ref<T,CP,OP> const& cp) {
_object_pointed_to = cp._object_pointed_to;
if (cp._object_pointed_to != NULL) {
CounterPolicy::increment(cp._object_pointed_to);
}
}
void detach() {
if (_object_pointed_to != NULL) {
CounterPolicy::decrement(_object_pointed_to);
if (CounterPolicy::is_zero(_object_pointed_to)) {
CounterPolicy::dispose(_object_pointed_to);
ObjectPolicy::dispose(_object_pointed_to);
}
}
}
};
}
#endif /*Ref_H_*/
|