mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-11 13:17:00 -04:00
default smart pointer type switched to intrusive_ptr
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@337 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+63
-14
@@ -29,7 +29,7 @@ using namespace boost;
|
||||
// ----------------------------------------------------------------------------- : Declaring
|
||||
|
||||
/// Declares the type TypeP as a shared_ptr<Type>
|
||||
#define DECLARE_POINTER_TYPE(Type) \
|
||||
#define DECLARE_SHARED_POINTER_TYPE(Type) \
|
||||
class Type; \
|
||||
typedef shared_ptr<Type> Type##P;
|
||||
|
||||
@@ -91,8 +91,8 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
|
||||
#ifdef USE_INTRUSIVE_PTR
|
||||
|
||||
/// Declares the type TypeP as a intrusive_ptr<Type>
|
||||
#define DECLARE_INTRUSIVE_POINTER_TYPE(Type) \
|
||||
class Type; \
|
||||
#define DECLARE_POINTER_TYPE(Type) \
|
||||
class Type; \
|
||||
typedef intrusive_ptr<Type> Type##P;
|
||||
|
||||
|
||||
@@ -147,11 +147,52 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
|
||||
return intrusive_ptr<T>(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8));
|
||||
}
|
||||
|
||||
/// Base class for objects wishing to use intrusive_ptrs
|
||||
class IntrusivePtrBase {
|
||||
// ----------------------------------------------------------------------------- : Intrusive pointer base
|
||||
|
||||
/// Base class for objects wishing to use intrusive_ptrs.
|
||||
/** There is no implicit virtual destructor, objects are destructed as type T
|
||||
* Usage:
|
||||
* @code
|
||||
* DECLARE_POINTER_TYPE(MyClass);
|
||||
* class MyClass : public IntrusivePtrBase<MyClass> { ... }
|
||||
* @endcode
|
||||
*/
|
||||
template <typename T> class IntrusivePtrBase {
|
||||
public:
|
||||
inline IntrusivePtrBase() : ref_count(0) {}
|
||||
virtual ~IntrusivePtrBase() {}
|
||||
inline IntrusivePtrBase() : ref_count(0) {}
|
||||
inline IntrusivePtrBase(const IntrusivePtrBase&) : ref_count(0) {} // don't copy construct the reference count!
|
||||
private:
|
||||
AtomicInt ref_count;
|
||||
template <typename T> friend void intrusive_ptr_add_ref(IntrusivePtrBase*);
|
||||
template <typename T> friend void intrusive_ptr_release(IntrusivePtrBase*);
|
||||
};
|
||||
|
||||
template <typename T> inline void intrusive_ptr_add_ref(IntrusivePtrBase<T>* p) {
|
||||
++p->ref_count;
|
||||
}
|
||||
template <typename T> inline void intrusive_ptr_release(IntrusivePtrBase<T>* p) {
|
||||
if (--p->ref_count == 0) {
|
||||
delete static_cast<T*>(p);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Intrusive pointer base : virtual
|
||||
|
||||
/// IntrusivePtrBase with a virtual destructor
|
||||
class IntrusivePtrVirtualBase : public IntrusivePtrBase<IntrusivePtrVirtualBase> {
|
||||
public:
|
||||
virtual ~IntrusivePtrVirtualBase() {}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Intrusive pointer base : with delete
|
||||
|
||||
/// Base class for objects wishing to use intrusive_ptrs, using a manual delete function
|
||||
class IntrusivePtrBaseWithDelete {
|
||||
public:
|
||||
inline IntrusivePtrBaseWithDelete() : ref_count(0) {}
|
||||
inline IntrusivePtrBaseWithDelete(const IntrusivePtrBaseWithDelete&)
|
||||
: ref_count(0) {} // don't copy construct the reference count!
|
||||
virtual ~IntrusivePtrBaseWithDelete() {}
|
||||
protected:
|
||||
/// Delete this object
|
||||
virtual void destroy() {
|
||||
@@ -159,21 +200,21 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
|
||||
}
|
||||
private:
|
||||
AtomicInt ref_count;
|
||||
friend void intrusive_ptr_add_ref(IntrusivePtrBase*);
|
||||
friend void intrusive_ptr_release(IntrusivePtrBase*);
|
||||
friend void intrusive_ptr_add_ref(IntrusivePtrBaseWithDelete*);
|
||||
friend void intrusive_ptr_release(IntrusivePtrBaseWithDelete*);
|
||||
};
|
||||
|
||||
inline void intrusive_ptr_add_ref(IntrusivePtrBase* p) {
|
||||
inline void intrusive_ptr_add_ref(IntrusivePtrBaseWithDelete* p) {
|
||||
++p->ref_count;
|
||||
}
|
||||
inline void intrusive_ptr_release(IntrusivePtrBase* p) {
|
||||
inline void intrusive_ptr_release(IntrusivePtrBaseWithDelete* p) {
|
||||
if (--p->ref_count == 0) {
|
||||
p->destroy();
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
#define DECLARE_INTRUSIVE_POINTER_TYPE DECLARE_POINTER_TYPE
|
||||
#define DECLARE_POINTER_TYPE DECLARE_SHARED_POINTER_TYPE
|
||||
#define intrusive_ptr shared_ptr
|
||||
#define new_intrusive new_shared
|
||||
#define new_intrusive1 new_shared1
|
||||
@@ -186,9 +227,17 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
|
||||
#define new_intrusive8 new_shared8
|
||||
#define new_intrusive9 new_shared9
|
||||
|
||||
class IntrusivePtrBase {
|
||||
template <typename T> class IntrusivePtrBase {};
|
||||
|
||||
/// IntrusivePtrBase with a virtual destructor
|
||||
class IntrusivePtrVirtualBase : public IntrusivePtrBase<IntrusivePtrVirtualBase> {
|
||||
public:
|
||||
virtual ~IntrusivePtrBase() {};
|
||||
virtual ~IntrusivePtrVirtualBase() {}
|
||||
};
|
||||
|
||||
class IntrusivePtrBaseWithDelete {
|
||||
public:
|
||||
virtual ~IntrusivePtrBaseWithDelete() {}
|
||||
protected:
|
||||
/// Delete this object
|
||||
virtual void destroy() {
|
||||
|
||||
Reference in New Issue
Block a user