refcounted.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 2007-2011 by Educational Technology Resources, Inc. and
(c) 2007 by Jonathan Wakely. Others may also hold copyrights on
code in this file. See the CREDITS.txt file in the top directory
of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#if !defined(MYSQLPP_REFCOUNTED_H)
#define MYSQLPP_REFCOUNTED_H
#include <memory>
#include <stddef.h>
namespace mysqlpp {
template <class T>
struct RefCountedPointerDestroyer
{
void operator()(T* doomed) const { delete doomed; }
};
template <class T, class Destroyer = RefCountedPointerDestroyer<T> >
class RefCountedPointer
{
public:
typedef RefCountedPointer<T> ThisType;
RefCountedPointer() :
counted_(0),
refs_(0)
{
}
explicit RefCountedPointer(T* c) :
counted_(c),
refs_(0)
{
std::auto_ptr<T> exception_guard(counted_);
if (counted_) {
refs_ = new size_t(1);
}
exception_guard.release(); // previous new didn't throw
}
RefCountedPointer(const ThisType& other) :
counted_(other.counted_),
refs_(other.counted_ ? other.refs_ : 0)
{
if (counted_) {
++(*refs_);
}
}
~RefCountedPointer()
{
if (refs_ && (--(*refs_) == 0)) {
Destroyer()(counted_);
delete refs_;
}
}
ThisType& assign(T* c)
{
// The create-temporary-and-swap idiom lets us keep memory
// allocation in the ctor and deallocation in the dtor so
// we don't leak in the face of an exception.
ThisType(c).swap(*this);
return *this;
}
ThisType& assign(const ThisType& other)
{
// The create-temporary-and-swap idiom lets us keep memory
// allocation in the ctor and deallocation in the dtor so
// we don't leak in the face of an exception.
ThisType(other).swap(*this);
return *this;
}
ThisType& operator =(T* c)
{
return assign(c);
}
ThisType& operator =(const ThisType& rhs)
{
return assign(rhs);
}
T* operator ->() const
{
return counted_;
}
T& operator *() const
{
return *counted_;
}
operator void*()
{
return counted_;
}
operator const void*() const
{
return counted_;
}
T* raw()
{
return counted_;
}
const T* raw() const
{
return counted_;
}
void swap(ThisType& other)
{
std::swap(counted_, other.counted_);
std::swap(refs_, other.refs_);
}
private:
T* counted_;
size_t* refs_;
};
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_REFCOUNTED_H)
mysqlpp::RefCountedPointer::raw
T * raw()
Return the raw pointer in T* context.
Definition: refcounted.h:230
mysqlpp::RefCountedPointer::~RefCountedPointer
~RefCountedPointer()
Destructor.
Definition: refcounted.h:125
mysqlpp::RefCountedPointer::RefCountedPointer
RefCountedPointer(const ThisType &other)
Copy constructor.
Definition: refcounted.h:112
mysqlpp::RefCountedPointer::swap
void swap(ThisType &other)
Exchange our managed memory with another pointer.
Definition: refcounted.h:245
mysqlpp::RefCountedPointerDestroyer
Functor to call delete on the pointer you pass to it.
Definition: refcounted.h:45
mysqlpp::RefCountedPointer::RefCountedPointer
RefCountedPointer(T *c)
Standard constructor.
Definition: refcounted.h:100
mysqlpp::RefCountedPointerDestroyer::operator()
void operator()(T *doomed) const
Functor implementation.
Definition: refcounted.h:48
mysqlpp::RefCountedPointer::raw
const T * raw() const
Return the raw pointer when used in const T* context.
Definition: refcounted.h:236
mysqlpp::RefCountedPointer::assign
ThisType & assign(const ThisType &other)
Copy an existing refcounted pointer.
Definition: refcounted.h:157
mysqlpp::RefCountedPointer::ThisType
RefCountedPointer< T > ThisType
alias for this object&#39;s type
Definition: refcounted.h:83
mysqlpp::RefCountedPointer::operator*
T & operator*() const
Dereference the smart pointer.
Definition: refcounted.h:192
mysqlpp::RefCountedPointer
Creates an object that acts as a reference-counted pointer to another object.
Definition: refcounted.h:80
mysqlpp::RefCountedPointer::assign
ThisType & assign(T *c)
Sets (or resets) the pointer to the counted object.
Definition: refcounted.h:140
mysqlpp::RefCountedPointer::operator->
T * operator->() const
Access the object through the smart pointer.
Definition: refcounted.h:186
mysqlpp::RefCountedPointer::RefCountedPointer
RefCountedPointer()
Default constructor.
Definition: refcounted.h:89
mysqlpp::RefCountedPointer::operator=
ThisType & operator=(T *c)
Set (or reset) the pointer to the counted object.
Definition: refcounted.h:170