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
|
//
// Copyright (c) October 2004 Jean Gressmann (jsg@rz.uni-potsdam.de)
// Copyright (c) 2006-2007, Benjamin Kaufmann
//
// This file is part of aspcud.
//
// gringo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// gringo 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with gringo. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef PROGRAM_OPTIONS_DETAIL_SMARTPOINTER_H
#define PROGRAM_OPTIONS_DETAIL_SMARTPOINTER_H
#if defined(_MSC_VER)
# define for if(0);else for
# pragma warning(disable:4521)
# pragma warning(disable:4786)
# pragma warning(disable:4522)
# pragma warning(disable:4355)
# pragma warning(disable:4291)
#endif
#include <algorithm>
#include <cassert>
namespace ProgramOptions {
// Thanks to Benjamin Kaufmann (kaufmann@cs.uni-potsdam.de)
// who provided for compile time assertions for void pointers
template <bool> struct StaticAssert;
template <> struct StaticAssert<true> {};
template <class T> struct is_void {enum {result = 0};};
template <> struct is_void<void> {enum {result = 1};};
// These freeResource() functions are required to handle their
// specific Null indifferently, say delete 0 is
// to do nothing.
template<class T>
struct PointerTraits_
{
StaticAssert<!is_void<T>::result> Error_Smart_Pointer_Does_Not_Support_Void_Ptr;
typedef T ValueType;
typedef T& ReferenceType;
typedef const T& ConstReferenceType;
typedef T* PointerType;
typedef const T* ConstPointerType;
static const PointerType Null;
static void freeResource(PointerType p)
{
enum { no_incomplete_types_please = sizeof(*p) };
delete p;
}
};
template<class T>
const typename PointerTraits_<T>::PointerType PointerTraits_<T>::Null = 0;
// reference counting based on a shared counter
template<class Traits>
class ReferenceCountedOwner_
{
public:
typedef typename Traits::PointerType PT;
typedef typename Traits::ConstPointerType CPT;
static CPT null() { return Traits::Null; }
private:
class RefCount_
{
PT managed_;
size_t count_;
RefCount_();
RefCount_(const RefCount_&);
RefCount_& operator=(const RefCount_&);
public:
explicit
RefCount_(PT p)
: managed_(p)
, count_(1)
{}
inline
void inc()
{
++count_;
}
inline
size_t count() const { return count_; }
inline
bool unique() const { return count_ == 1; }
void dec()
{
--count_;
if(!count_)
{
PT deleteme = managed_;
delete this;
Traits::freeResource(deleteme);
}
}
void weakDec()
{
--count_;
if(!count_)
{
delete this;
}
}
inline
PT get() { return managed_; }
inline
CPT get() const { return managed_; }
};
RefCount_* refCount_;
void decIfValidRefCount()
{
if(refCount_)
{
RefCount_* temp = 0;
std::swap(temp, refCount_);
temp->dec();
}
}
inline
void incIfValidRefCount() // nothrow guarantee
{
if(refCount_)
refCount_->inc();
}
inline
static RefCount_* createRefCount(PT p)
{
return p == Traits::Null ? 0 : new RefCount_(p);
}
public:
~ReferenceCountedOwner_()
{
decIfValidRefCount();
}
explicit
ReferenceCountedOwner_()
: refCount_(0)
{}
ReferenceCountedOwner_(PT p)
: refCount_(createRefCount(p))
{}
ReferenceCountedOwner_(const ReferenceCountedOwner_& o)
: refCount_(o.refCount_)
{
incIfValidRefCount();
}
inline
ReferenceCountedOwner_& operator=(const ReferenceCountedOwner_& o)
{
ReferenceCountedOwner_(o).swap(*this);
return *this;
}
inline
ReferenceCountedOwner_& operator=(PT p)
{
ReferenceCountedOwner_(p).swap(*this);
return *this;
}
inline
bool unique() const { return refCount_ ? refCount_->unique() : true; } // nothrow guarantee
inline
size_t count() const { return refCount_ ? refCount_->count() : 1; } // nothrow guarantee
inline
PT get() { return refCount_ ? refCount_->get() : Traits::Null; }
inline
CPT get() const { return refCount_ ? refCount_->get() : Traits::Null; }
inline
void swap(ReferenceCountedOwner_& o) // nothrow guarantee
{
std::swap(refCount_, o.refCount_);
}
inline
void reset()
{
ReferenceCountedOwner_().swap(*this);
}
inline
void release()
{
if(refCount_)
refCount_->weakDec();
refCount_ = 0;
}
};
template
<
class T,
class Traits = PointerTraits_<T>,
class RefCountPolicy = ReferenceCountedOwner_<Traits>
>
class SmartPointer : public RefCountPolicy
{
public:
typedef typename Traits::PointerType PT;
typedef typename Traits::ConstPointerType CPT;
typedef typename Traits::ReferenceType RT;
typedef typename Traits::ConstReferenceType CRT;
explicit
SmartPointer()
{}
explicit
SmartPointer(PT p)
: RefCountPolicy(p)
{}
SmartPointer(const SmartPointer& o)
: RefCountPolicy(o)
{}
SmartPointer(SmartPointer& o)
: RefCountPolicy(o)
{}
inline
SmartPointer& operator=(SmartPointer& o)
{
RefCountPolicy::operator=(o);
return *this;
}
inline
SmartPointer& operator=(const SmartPointer& o)
{
RefCountPolicy::operator=(o);
return *this;
}
inline
SmartPointer& operator=(PT p)
{
RefCountPolicy::operator=(p);
return *this;
}
inline
bool operator==(const SmartPointer& o) const
{
return this->get() == o.get();
}
inline
bool operator!=(const SmartPointer& o) const
{
return this->get() != o.get();
}
inline
operator bool() const { return this->get() != Traits::Null; }
inline
PT operator->() { return this->get(); }
inline
CPT operator->() const { return this->get(); }
inline
RT operator*() { return *this->get(); }
inline
CRT operator*() const { return *this->get(); }
};
template<class T, class Traits = PointerTraits_<T> >
class SharedPtr : public SmartPointer<T, Traits, ReferenceCountedOwner_<Traits> >
{
typedef SmartPointer<T, Traits, ReferenceCountedOwner_<Traits> > Base;
typedef typename Base::PT PT;
public:
explicit
SharedPtr()
: Base()
{}
SharedPtr(PT p)
: Base(p)
{}
SharedPtr(const SharedPtr& o)
: Base(o)
{}
inline
SharedPtr& operator=(PT p)
{
Base::operator=(p);
return *this;
}
inline
SharedPtr& operator=(const SharedPtr& o)
{
Base::operator=(o);
return *this;
}
};
}
#endif
|