2010-04-01 20:25:07 +00:00
|
|
|
|
|
|
|
#ifndef INCLUDED_AI_BOOST_SHARED_PTR
|
|
|
|
#define INCLUDED_AI_BOOST_SHARED_PTR
|
|
|
|
|
2014-05-16 12:57:33 +00:00
|
|
|
#ifndef BOOST_SHARED_PTR_HPP_INCLUDED
|
2010-04-01 20:25:07 +00:00
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Internal stub
|
2015-04-15 19:22:26 +00:00
|
|
|
|
|
|
|
#include <stddef.h> //NULL
|
|
|
|
#include <algorithm> //std::swap
|
2010-04-01 20:25:07 +00:00
|
|
|
namespace boost {
|
|
|
|
namespace detail {
|
|
|
|
class controller {
|
|
|
|
public:
|
|
|
|
|
|
|
|
controller()
|
2010-05-10 10:18:54 +00:00
|
|
|
: cnt(1)
|
2010-04-01 20:25:07 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
controller* decref(T* pt) {
|
|
|
|
if (--cnt <= 0) {
|
|
|
|
delete this;
|
|
|
|
delete pt;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller* incref() {
|
|
|
|
++cnt;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-04-11 02:46:52 +00:00
|
|
|
long get() const {
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:25:07 +00:00
|
|
|
private:
|
|
|
|
long cnt;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct empty {};
|
|
|
|
|
|
|
|
template <typename DEST, typename SRC>
|
|
|
|
struct is_convertible_stub {
|
|
|
|
|
|
|
|
struct yes {char s[1];};
|
|
|
|
struct no {char s[2];};
|
|
|
|
|
|
|
|
static yes foo(DEST*);
|
|
|
|
static no foo(...);
|
|
|
|
|
2010-04-02 00:17:58 +00:00
|
|
|
enum {result = (sizeof(foo((SRC*)0)) == sizeof(yes) ? 1 : 0)};
|
2010-04-01 20:25:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <bool> struct enable_if {};
|
|
|
|
template <> struct enable_if<true> {
|
|
|
|
typedef empty result;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename DEST, typename SRC>
|
|
|
|
struct is_convertible : public enable_if<is_convertible_stub<DEST,SRC>::result > {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Small replacement for boost::shared_ptr, not threadsafe because no
|
|
|
|
// atomic reference counter is in use.
|
|
|
|
// ------------------------------
|
|
|
|
template <class T>
|
|
|
|
class shared_ptr
|
|
|
|
{
|
2010-04-02 00:17:58 +00:00
|
|
|
template <typename TT> friend class shared_ptr;
|
2010-04-11 02:46:52 +00:00
|
|
|
|
2010-05-10 10:18:54 +00:00
|
|
|
template<class TT, class U> friend shared_ptr<TT> static_pointer_cast (shared_ptr<U> ptr);
|
|
|
|
template<class TT, class U> friend shared_ptr<TT> dynamic_pointer_cast (shared_ptr<U> ptr);
|
|
|
|
template<class TT, class U> friend shared_ptr<TT> const_pointer_cast (shared_ptr<U> ptr);
|
|
|
|
|
2010-04-11 02:46:52 +00:00
|
|
|
template<class TT> friend bool operator== (const shared_ptr<TT>& a, const shared_ptr<TT>& b);
|
|
|
|
template<class TT> friend bool operator!= (const shared_ptr<TT>& a, const shared_ptr<TT>& b);
|
|
|
|
template<class TT> friend bool operator< (const shared_ptr<TT>& a, const shared_ptr<TT>& b);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef T element_type;
|
|
|
|
|
2010-04-01 20:25:07 +00:00
|
|
|
public:
|
|
|
|
|
2010-04-11 02:46:52 +00:00
|
|
|
// provide a default constructor
|
2010-04-01 20:25:07 +00:00
|
|
|
shared_ptr()
|
|
|
|
: ptr()
|
2010-05-10 10:18:54 +00:00
|
|
|
, ctr(NULL)
|
2010-04-01 20:25:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// construction from an existing object of type T
|
2010-05-10 10:18:54 +00:00
|
|
|
explicit shared_ptr(T* ptr)
|
|
|
|
: ptr(ptr)
|
|
|
|
, ctr(ptr ? new detail::controller() : NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr(const shared_ptr& r)
|
|
|
|
: ptr(r.ptr)
|
|
|
|
, ctr(r.ctr ? r.ctr->incref() : NULL)
|
2010-04-01 20:25:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Y>
|
2010-05-10 10:18:54 +00:00
|
|
|
shared_ptr(const shared_ptr<Y>& r,typename detail::is_convertible<T,Y>::result = detail::empty())
|
|
|
|
: ptr(r.ptr)
|
|
|
|
, ctr(r.ctr ? r.ctr->incref() : NULL)
|
2010-04-01 20:25:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-05-10 10:18:54 +00:00
|
|
|
// automatic destruction of the wrapped object when all
|
|
|
|
// references are freed.
|
|
|
|
~shared_ptr() {
|
|
|
|
if (ctr) {
|
|
|
|
ctr = ctr->decref(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr& operator=(const shared_ptr& r) {
|
|
|
|
if (this == &r) {
|
2010-04-11 02:52:27 +00:00
|
|
|
return *this;
|
2010-04-11 02:46:52 +00:00
|
|
|
}
|
2010-05-10 10:18:54 +00:00
|
|
|
if (ctr) {
|
|
|
|
ctr->decref(ptr);
|
|
|
|
}
|
2010-04-11 02:46:52 +00:00
|
|
|
ptr = r.ptr;
|
2010-05-10 10:18:54 +00:00
|
|
|
ctr = ptr?r.ctr->incref():NULL;
|
2010-04-11 02:52:27 +00:00
|
|
|
return *this;
|
2010-04-11 02:46:52 +00:00
|
|
|
}
|
|
|
|
|
2010-05-10 10:18:54 +00:00
|
|
|
template <typename Y>
|
|
|
|
shared_ptr& operator=(const shared_ptr<Y>& r) {
|
|
|
|
if (this == &r) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
if (ctr) {
|
|
|
|
ctr->decref(ptr);
|
|
|
|
}
|
|
|
|
ptr = r.ptr;
|
|
|
|
ctr = ptr?r.ctr->incref():NULL;
|
|
|
|
return *this;
|
2010-04-01 20:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pointer access
|
2011-05-31 17:35:17 +00:00
|
|
|
inline operator T*() const {
|
2010-04-01 20:25:07 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2010-05-10 10:18:54 +00:00
|
|
|
inline T* operator-> () const {
|
2010-04-01 20:25:07 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// standard semantics
|
2010-05-10 10:18:54 +00:00
|
|
|
inline T* get() {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const T* get() const {
|
2010-04-01 20:25:07 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2010-04-06 11:46:49 +00:00
|
|
|
inline operator bool () const {
|
|
|
|
return ptr != NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-11 02:46:52 +00:00
|
|
|
inline bool unique() const {
|
|
|
|
return use_count() == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline long use_count() const {
|
|
|
|
return ctr->get();
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:25:07 +00:00
|
|
|
inline void reset (T* t = 0) {
|
2010-05-10 10:18:54 +00:00
|
|
|
if (ctr) {
|
|
|
|
ctr->decref(ptr);
|
2010-04-01 20:25:07 +00:00
|
|
|
}
|
2010-05-10 10:18:54 +00:00
|
|
|
ptr = t;
|
|
|
|
ctr = ptr?new detail::controller():NULL;
|
2010-04-01 20:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void swap(shared_ptr & b) {
|
|
|
|
std::swap(ptr, b.ptr);
|
|
|
|
std::swap(ctr, b.ctr);
|
|
|
|
}
|
|
|
|
|
2010-05-10 10:18:54 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
|
|
// for use by the various xxx_pointer_cast helper templates
|
|
|
|
explicit shared_ptr(T* ptr, detail::controller* ctr)
|
|
|
|
: ptr(ptr)
|
|
|
|
, ctr(ctr->incref())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:25:07 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
// encapsulated object pointer
|
|
|
|
T* ptr;
|
|
|
|
|
|
|
|
// control block
|
|
|
|
detail::controller* ctr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2010-04-11 02:46:52 +00:00
|
|
|
template<class T>
|
|
|
|
bool operator== (const shared_ptr<T>& a, const shared_ptr<T>& b) {
|
|
|
|
return a.ptr == b.ptr;
|
|
|
|
}
|
|
|
|
template<class T>
|
|
|
|
bool operator!= (const shared_ptr<T>& a, const shared_ptr<T>& b) {
|
|
|
|
return a.ptr != b.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
bool operator< (const shared_ptr<T>& a, const shared_ptr<T>& b) {
|
|
|
|
return a.ptr < b.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-10 10:18:54 +00:00
|
|
|
template<class T, class U>
|
|
|
|
inline shared_ptr<T> static_pointer_cast( shared_ptr<U> ptr)
|
|
|
|
{
|
|
|
|
return shared_ptr<T>(static_cast<T*>(ptr.ptr),ptr.ctr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> ptr)
|
|
|
|
{
|
|
|
|
return shared_ptr<T>(dynamic_cast<T*>(ptr.ptr),ptr.ctr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline shared_ptr<T> const_pointer_cast( shared_ptr<U> ptr)
|
|
|
|
{
|
|
|
|
return shared_ptr<T>(const_cast<T*>(ptr.ptr),ptr.ctr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-01 20:25:07 +00:00
|
|
|
} // end of namespace boost
|
|
|
|
|
|
|
|
#else
|
|
|
|
# error "shared_ptr.h was already included"
|
|
|
|
#endif
|
2014-05-16 12:57:33 +00:00
|
|
|
#endif // INCLUDED_AI_BOOST_SHARED_PTR
|