/usr/include/cppdb/ref_ptr.h is in libcppdb-dev 0.3.1+dfsg-5.
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 | ///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// Distributed under:
//
// the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// or (at your opinion) under:
//
// The MIT License
// (See accompanying file MIT.txt or a copy at
// http://www.opensource.org/licenses/mit-license.php)
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPDB_REF_PTR_H
#define CPPDB_REF_PTR_H
#include <cppdb/errors.h>
#include <cppdb/atomic_counter.h>
namespace cppdb {
///
/// \brief This is a smart intrusive reference counting pointer that throws a error on empty
/// access.
///
/// The T should follow these concepts:
///
/// \code
/// T::add_ref() // increases reference count
/// IntegerType T::del_ref() // decreases reference count and returns its current value
/// static T::dispose(T *) // destroys the object
/// \endcode
///
template<typename T>
class ref_ptr {
public:
///
/// Default create a new object, if v is not null increases its reference count and stores it
///
ref_ptr(T *v=0) : p(0)
{
reset(v);
}
///
/// Dereference the object, if reference count goes to 0 destroys it calling T::dispose
///
~ref_ptr()
{
reset();
}
///
/// Copy a pointer
///
ref_ptr(ref_ptr const &other) : p(0)
{
reset(other.p);
}
///
/// Assign a pointer
///
ref_ptr const &operator=(ref_ptr const &other)
{
reset(other.p);
return *this;
}
// Borland warns on assignments using operator=(ref_ptr...) with new sometype(...).
#ifdef __BORLANDC__
ref_ptr const &operator=(T *other)
{
reset(other);
return *this;
}
#endif
///
/// Get he pointer value, it may return NULL in case of empty pointer
///
T *get() const
{
return p;
}
///
/// Cast to boolean type: check if the pointer is not empty in similar way as you check ordinary pointers.
///
operator bool() const
{
return p!=0;
}
///
/// Returns pointer to object, throws cppdb_error if it is NULL
///
T *operator->() const
{
if(!p)
throw cppdb_error("cppdb::ref_ptr: attempt to access an empty object");
return p;
}
///
/// Returns reference to object, throws cppdb_error if it is NULL
///
T &operator*() const
{
if(!p)
throw cppdb_error("cppdb::ref_ptr: attempt to access an empty object");
return *p;
}
///
/// Reset the pointer with new value - old object is dereferenced new is added.
///
void reset(T *v=0)
{
if(v==p)
return;
if(p) {
if(p->del_ref() == 0) {
T::dispose(p);
}
p=0;
}
if(v) {
v->add_ref();
}
p=v;
}
private:
T *p;
};
///
/// \brief This is a class that implements reference counting and designed to be used with ref_ptr
///
class ref_counted {
public:
///
/// Create an object with 0 reference count
///
ref_counted() : count_(0)
{
}
///
/// Virtual destructor - for convenience
///
virtual ~ref_counted()
{
}
///
/// Increase reference count
///
long add_ref()
{
return ++count_;
}
///
/// Get reference count
///
long use_count() const
{
long val = count_;
return val;
}
///
/// Decrease reference count
///
long del_ref()
{
return --count_;
}
///
/// Delete the object
///
static void dispose(ref_counted *p)
{
delete p;
}
private:
atomic_counter count_;
};
} // cppdb
#endif
|