Implement stdext::shared_ptr

It's an alternative to stdext::shared_object_ptr
with weak pointers support and some other functionallity, however
it's heavier, uses more memory, more allocation and slower,
this class should be used when really needed, prefer
stdext::shared_object_ptr
This commit is contained in:
Eduardo Bart 2013-03-04 13:14:35 -03:00
parent bdbe065c23
commit c452e74e0c
4 changed files with 242 additions and 9 deletions

View File

@ -50,6 +50,7 @@ set(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/stdext/packed_storage.h ${CMAKE_CURRENT_LIST_DIR}/stdext/packed_storage.h
${CMAKE_CURRENT_LIST_DIR}/stdext/packed_vector.h ${CMAKE_CURRENT_LIST_DIR}/stdext/packed_vector.h
${CMAKE_CURRENT_LIST_DIR}/stdext/shared_object.h ${CMAKE_CURRENT_LIST_DIR}/stdext/shared_object.h
${CMAKE_CURRENT_LIST_DIR}/stdext/shared_ptr.h
${CMAKE_CURRENT_LIST_DIR}/stdext/stdext.h ${CMAKE_CURRENT_LIST_DIR}/stdext/stdext.h
${CMAKE_CURRENT_LIST_DIR}/stdext/string.cpp ${CMAKE_CURRENT_LIST_DIR}/stdext/string.cpp
${CMAKE_CURRENT_LIST_DIR}/stdext/string.h ${CMAKE_CURRENT_LIST_DIR}/stdext/string.h

View File

@ -23,6 +23,7 @@
#ifndef STDEXT_SHARED_OBJECT_H #ifndef STDEXT_SHARED_OBJECT_H
#define STDEXT_SHARED_OBJECT_H #define STDEXT_SHARED_OBJECT_H
#include "types.h"
#include <type_traits> #include <type_traits>
#include <functional> #include <functional>
#include <cassert> #include <cassert>
@ -37,16 +38,14 @@ namespace stdext {
template<class T> template<class T>
class shared_object_ptr; class shared_object_ptr;
typedef unsigned long refcount_t;
class shared_object class shared_object
{ {
public: public:
shared_object() : m_refs(0) { } shared_object() : refs(0) { }
virtual ~shared_object() { } virtual ~shared_object() { }
void add_ref() { ++m_refs; assert(m_refs != 0xffffffff); } void add_ref() { ++refs; }
void dec_ref() { if(--m_refs == 0) delete this; } void dec_ref() { if(--refs == 0) delete this; }
refcount_t ref_count() { return m_refs; } refcount_t ref_count() { return refs; }
template<typename T> stdext::shared_object_ptr<T> static_self_cast() { return stdext::shared_object_ptr<T>(static_cast<T*>(this)); } template<typename T> stdext::shared_object_ptr<T> static_self_cast() { return stdext::shared_object_ptr<T>(static_cast<T*>(this)); }
template<typename T> stdext::shared_object_ptr<T> dynamic_self_cast() { return stdext::shared_object_ptr<T>(dynamic_cast<T*>(this)); } template<typename T> stdext::shared_object_ptr<T> dynamic_self_cast() { return stdext::shared_object_ptr<T>(dynamic_cast<T*>(this)); }
@ -54,9 +53,9 @@ public:
private: private:
#ifdef THREAD_SAFE #ifdef THREAD_SAFE
std::atomic<refcount_t> m_refs; std::atomic<refcount_t> refs;
#else #else
refcount_t m_refs; refcount_t refs;
#endif #endif
}; };
@ -78,7 +77,7 @@ public:
~shared_object_ptr() { if(px != nullptr) dec_ref(); } ~shared_object_ptr() { if(px != nullptr) dec_ref(); }
void reset() { shared_object_ptr().swap(*this); } void reset() { shared_object_ptr().swap(*this); }
void reset(T* rhs) { shared_object_ptr( rhs ).swap(*this); } void reset(T* rhs) { shared_object_ptr(rhs).swap(*this); }
void swap(shared_object_ptr& rhs) { std::swap(px, rhs.px); } void swap(shared_object_ptr& rhs) { std::swap(px, rhs.px); }
T* get() const { return px; } T* get() const { return px; }

View File

@ -0,0 +1,232 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef STDEXT_SHARED_PTR_H
#define STDEXT_SHARED_PTR_H
#include "types.h"
#include "dumper.h"
#include <type_traits>
#include <functional>
#include <cassert>
#include <ostream>
#ifdef THREAD_SAFE
#include <atomic>
#endif
namespace stdext {
template<class T>
class shared_ptr;
template<class T>
class weak_ptr;
template<class T>
class shared_base
{
public:
shared_base(T* p) : refs(1), weaks(0), px(p) { }
~shared_base() { }
void add_ref() { ++refs; }
void dec_ref() {
if(--refs == 0) {
delete px;
px = nullptr;
if(weaks == 0)
delete this;
}
}
void add_weak_ref() { ++weaks; }
void dec_weak_ref() {
if(--weaks == 0 && refs == 0)
delete this;
}
void set(T *p) {
T* tmp = px;
px = p;
if(tmp)
delete tmp;
}
T *get() { return px; }
refcount_t ref_count() { return refs; }
refcount_t weak_count() { return weaks; }
bool expired() { return refs == 0; }
private:
#ifdef THREAD_SAFE
std::atomic<refcount_t> refs;
std::atomic<refcount_t> weaks;
std::atomic<T*> px;
#else
refcount_t refs;
refcount_t weaks;
T* px;
#endif
};
template<class T>
class shared_ptr
{
public:
typedef T element_type;
shared_ptr() : base(nullptr) { }
shared_ptr(T* p) { if(p != nullptr) base = new shared_base<T>(p); else base = nullptr; }
shared_ptr(shared_ptr const& rhs): base(rhs.base) { if(base != nullptr) base->add_ref(); }
template<class U>
shared_ptr(shared_ptr<U> const& rhs, typename std::is_convertible<U,T>::type* = nullptr) : base(rhs.base) { if(base != nullptr) base->add_ref(); }
~shared_ptr() { if(base != nullptr) base->dec_ref(); }
void reset() { shared_ptr().swap(*this); }
void reset(T* rhs) { shared_ptr(rhs).swap(*this); }
void swap(shared_ptr& rhs) { std::swap(base, rhs.base); }
void set(T* p) { assert(p != nullptr); base->set(p); }
T* get() const { return base ? base->get() : nullptr; }
refcount_t use_count() const { return base ? base->ref_count() : 0; }
refcount_t weak_count() const { return base ? base->weak_count() : 0; }
bool is_unique() const { return use_count() == 1; }
T& operator*() const { assert(base != nullptr); return *base->get(); }
T* operator->() const { assert(base != nullptr); return base->get(); }
template<class U>
shared_ptr& operator=(shared_ptr<U> const& rhs) { shared_ptr(rhs).swap(*this); return *this; }
shared_ptr& operator=(shared_ptr const& rhs) { shared_ptr(rhs).swap(*this); return *this; }
shared_ptr& operator=(T* rhs) { shared_ptr(rhs).swap(*this); return *this; }
// implicit conversion to bool
typedef shared_base<T>* shared_ptr::*unspecified_bool_type;
operator unspecified_bool_type() const { return base == nullptr ? nullptr : &shared_ptr::base; }
bool operator!() const { return base == nullptr; }
// std::move support
shared_ptr(shared_ptr&& rhs): base(rhs.base) { rhs.base = nullptr; }
shared_ptr& operator=(shared_ptr&& rhs) { shared_ptr(static_cast<shared_ptr&&>(rhs)).swap(*this); return *this; }
private:
shared_ptr(shared_base<T> *base) {
if(base && !base->expired()) {
base->add_ref();
this->base = base;
} else
base = nullptr;
}
shared_base<T> *base;
friend class weak_ptr<T>;
};
template<class T>
class weak_ptr {
public:
typedef T element_type;
weak_ptr() : base(nullptr) { }
weak_ptr(shared_ptr<T> const& rhs): base(rhs.base) { if(base != nullptr) base->add_weak_ref(); }
template<class U>
weak_ptr(shared_ptr<U> const& rhs, typename std::is_convertible<U,T>::type* = nullptr) : base(rhs.base) { if(base != nullptr) base->add_weak_ref(); }
weak_ptr(weak_ptr<T> const& rhs): base(rhs.base) { if(base != nullptr) base->add_weak_ref(); }
template<class U>
weak_ptr(weak_ptr<U> const& rhs, typename std::is_convertible<U,T>::type* = nullptr) : base(rhs.base) { if(base != nullptr) base->add_weak_ref(); }
~weak_ptr() { if(base != nullptr) base->dec_weak_ref(); }
void reset() { weak_ptr().swap(*this); }
void swap(weak_ptr& rhs) { std::swap(base, rhs.base); }
T* get() const { return base ? base->get() : nullptr; }
refcount_t use_count() const { return base ? base->ref_count() : 0; }
refcount_t weak_count() const { return base ? base->weak_count() : 0; }
bool expired() const { return base ? base->expired() : false; }
shared_ptr<T> lock() { return shared_ptr<T>(base); }
weak_ptr& operator=(weak_ptr const& rhs) { weak_ptr(rhs).swap(*this); return *this; }
template<class U>
weak_ptr& operator=(shared_ptr<U> const& rhs) { weak_ptr(rhs).swap(*this); return *this; }
template<class U>
weak_ptr& operator=(weak_ptr<T> const& rhs) { weak_ptr(rhs).swap(*this); return *this; }
// implicit conversion to bool
typedef shared_base<T>* weak_ptr::*unspecified_bool_type;
operator unspecified_bool_type() const { return base == nullptr ? nullptr : &weak_ptr::base; }
bool operator!() const { return !expired(); }
// std::move support
weak_ptr(weak_ptr&& rhs): base(rhs.base) { rhs.base = nullptr; }
weak_ptr& operator=(weak_ptr&& rhs) { weak_ptr(static_cast<weak_ptr&&>(rhs)).swap(*this); return *this; }
private:
shared_base<T> *base;
};
template<class T, class U> bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) { return a.get() == b.get(); }
template<class T, class U> bool operator==(weak_ptr<T> const& a, weak_ptr<U> const& b) { return a.get() == b.get(); }
template<class T, class U> bool operator==(shared_ptr<T> const& a, weak_ptr<U> const& b) { return a.get() == b.get(); }
template<class T, class U> bool operator==(weak_ptr<T> const& a, shared_ptr<U> const& b) { return a.get() == b.get(); }
template<class T, class U> bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) { return a.get() != b.get(); }
template<class T, class U> bool operator!=(weak_ptr<T> const& a, weak_ptr<U> const& b) { return a.get() != b.get(); }
template<class T, class U> bool operator!=(shared_ptr<T> const& a, weak_ptr<U> const& b) { return a.get() != b.get(); }
template<class T, class U> bool operator!=(weak_ptr<T> const& a, shared_ptr<U> const& b) { return a.get() != b.get(); }
template<class T, class U> bool operator==(shared_ptr<T> const& a, U* b) { return a.get() == b; }
template<class T, class U> bool operator==(weak_ptr<T> const& a, U* b) { return a.get() == b; }
template<class T, class U> bool operator!=(shared_ptr<T> const& a, U* b) { return a.get() != b; }
template<class T, class U> bool operator!=(weak_ptr<T> const& a, U* b) { return a.get() != b; }
template<class T, class U> bool operator==(T * a, shared_ptr<U> const& b) { return a == b.get(); }
template<class T, class U> bool operator==(T * a, weak_ptr<U> const& b) { return a == b.get(); }
template<class T, class U> bool operator!=(T * a, shared_ptr<U> const& b) { return a != b.get(); }
template<class T, class U> bool operator!=(T * a, weak_ptr<U> const& b) { return a != b.get(); }
template<class T> bool operator<(shared_ptr<T> const& a, shared_ptr<T> const& b) { return std::less<T*>()(a.get(), b.get()); }
template<class T> bool operator<(weak_ptr<T> const& a, weak_ptr<T> const& b) { return std::less<T*>()(a.get(), b.get()); }
template<class T> T* get_pointer(shared_ptr<T> const& p) { return p.get(); }
template<class T> T* get_pointer(weak_ptr<T> const& p) { return p.get(); }
template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const& p) { return static_cast<T*>(p.get()); }
template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const& p) { return const_cast<T*>(p.get()); }
template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& p) { return dynamic_cast<T*>(p.get()); }
template<class T, typename... Args> stdext::shared_ptr<T> make_shared(Args... args) { return stdext::shared_ptr<T>(new T(args...)); }
// operator<< support
template<class E, class T, class Y> std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& os, shared_ptr<Y> const& p) { os << p.get(); return os; }
template<class E, class T, class Y> std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& os, weak_ptr<Y> const& p) { os << p.get(); return os; }
}
namespace std {
// hash, for unordered_map support
template<typename T> struct hash<stdext::shared_ptr<T>> { size_t operator()(const stdext::shared_ptr<T>& p) const { return std::hash<T*>()(p.get()); } };
template<typename T> struct hash<stdext::weak_ptr<T>> { size_t operator()(const stdext::weak_ptr<T>& p) const { return std::hash<T*>()(p.get()); } };
// swap support
template<class T> void swap(stdext::shared_ptr<T>& lhs, stdext::shared_ptr<T>& rhs) { lhs.swap(rhs); }
template<class T> void swap(stdext::weak_ptr<T>& lhs, stdext::weak_ptr<T>& rhs) { lhs.swap(rhs); }
}
#endif

View File

@ -41,6 +41,7 @@ typedef int16_t int16;
typedef int8_t int8; typedef int8_t int8;
typedef int64 ticks_t; typedef int64 ticks_t;
typedef uint_fast32_t refcount_t;
using std::size_t; using std::size_t;
using std::ptrdiff_t; using std::ptrdiff_t;