17 #ifndef INC_KY_Kernel_RefCount_H
18 #define INC_KY_Kernel_RefCount_H
36 template<
class C,
int StatType>
38 template<
class C,
int StatType>
39 class RefCountBaseNTS;
40 template<
class C,
int StatType>
41 class RefCountBaseWeakSupport;
44 class RefCountNTSImpl;
45 class RefCountWeakSupportImpl;
58 #define KY_REFCOUNT_NORMAL 0x00000000
59 #define KY_REFCOUNT_THREADSAFE 0x00000001
67 class RefCountImplCore
70 std::atomic<int> RefCount;
74 KY_INLINE RefCountImplCore() : RefCount(1) { }
79 virtual ~RefCountImplCore();
82 int GetRefCount()
const {
return RefCount; }
87 #ifdef KY_CONFIG_DEBUG
88 static void reportInvalidDelete(
void *pmem);
89 inline static void checkInvalidDelete(RefCountImplCore *pmem)
91 if (pmem->RefCount != 0)
92 reportInvalidDelete(pmem);
95 inline static void checkInvalidDelete(RefCountImplCore *) { }
99 RefCountImplCore(
const RefCountImplCore &) : RefCount(1) { }
100 void operator = (
const RefCountImplCore &) { }
103 class RefCountNTSImplCore
106 mutable int RefCount;
110 KY_INLINE RefCountNTSImplCore() : RefCount(1) { }
115 virtual ~RefCountNTSImplCore();
118 int GetRefCount()
const {
return RefCount; }
123 #ifdef KY_CONFIG_DEBUG
124 static void reportInvalidDelete(
void *pmem);
125 KY_INLINE
static void checkInvalidDelete(RefCountNTSImplCore *pmem)
127 if (pmem->RefCount != 0)
128 reportInvalidDelete(pmem);
131 KY_INLINE
static void checkInvalidDelete(RefCountNTSImplCore *) { }
135 RefCountNTSImplCore(
const RefCountNTSImplCore &) : RefCount(1) { }
136 void operator = (
const RefCountNTSImplCore &) { }
144 class RefCountImpl :
public RefCountImplCore
155 class RefCountVImpl :
public RefCountImplCore
159 virtual void AddRef();
160 virtual void Release();
167 class RefCountNTSImpl :
public RefCountNTSImplCore
170 KY_INLINE
void AddRef()
const { RefCount++; }
171 void Release()
const;
179 class RefCountWeakSupportImpl :
public RefCountNTSImpl
182 mutable WeakPtrProxy* pWeakProxy;
184 RefCountWeakSupportImpl() { pWeakProxy = 0; }
185 virtual ~RefCountWeakSupportImpl();
188 WeakPtrProxy* CreateWeakProxy()
const;
195 template<
class Base,
int StatType>
196 class RefCountBaseStatImpl :
public Base
199 RefCountBaseStatImpl() { }
205 #ifdef KY_BUILD_DEFINE_NEW
211 #ifdef KY_CONFIG_DEBUG
213 #define KY_REFCOUNTALLOC_CHECK_DELETE(class_name, p) \
214 do {if (p) Base::checkInvalidDelete((class_name*)p); } while(0)
216 #define KY_REFCOUNTALLOC_CHECK_DELETE(class_name, p)
220 KY_MEMORY_REDEFINE_NEW_IMPL(Base, KY_REFCOUNTALLOC_CHECK_DELETE, StatType)
223 #define new KY_DEFINE_NEW
242 template<
class C,
int Stat>
243 class RefCountBase :
public RefCountBaseStatImpl<RefCountImpl, Stat>
246 enum { StatType = Stat };
248 KY_INLINE RefCountBase() : RefCountBaseStatImpl<RefCountImpl, Stat>() { }
253 template<
class C,
int Stat>
254 class RefCountBaseV :
public RefCountBaseStatImpl<RefCountVImpl, Stat>
257 enum { StatType = Stat };
259 KY_INLINE RefCountBaseV() : RefCountBaseStatImpl<RefCountVImpl, Stat>() { }
272 template<
class C,
int Stat>
273 class RefCountBaseNTS :
public RefCountBaseStatImpl<RefCountNTSImpl, Stat>
276 enum { StatType = Stat };
278 KY_INLINE RefCountBaseNTS() : RefCountBaseStatImpl<RefCountNTSImpl, Stat>() { }
290 template<
class C,
int Stat>
291 class RefCountBaseWeakSupport :
public RefCountBaseStatImpl<RefCountWeakSupportImpl, Stat>
294 enum { StatType = Stat };
296 KY_INLINE RefCountBaseWeakSupport() : RefCountBaseStatImpl<RefCountWeakSupportImpl, Stat>() { }
302 enum PickType { PickValue };
304 template <
typename T>
308 Pickable() : pV(NULL) {}
309 explicit Pickable(T* p) : pV(p) {}
310 Pickable(T* p, PickType) : pV(p)
316 template <
typename OT>
317 Pickable(
const Pickable<OT>& other) : pV(other.GetPtr()) {}
320 Pickable& operator =(
const Pickable& other)
322 KY_ASSERT(pV == NULL);
330 T* GetPtr()
const {
return pV; }
331 T* operator->()
const
345 template <
typename T>
347 Pickable<T> MakePickable(T* p)
349 return Pickable<T>(p);
356 void* ReturnArg0(
void* p);
368 KY_INLINE Ptr() : pObject(0)
370 KY_INLINE Ptr(C &robj) : pObject(&robj)
372 KY_INLINE Ptr(Pickable<C> v) : pObject(v.GetPtr())
376 KY_INLINE Ptr(Ptr<C>& other, PickType) : pObject(other.pObject)
378 other.pObject = NULL;
381 KY_INLINE Ptr(C *pobj)
383 if (pobj) pobj->AddRef();
386 KY_INLINE Ptr(
const Ptr<C> &src)
388 if (src.pObject) src.pObject->AddRef();
389 pObject = src.pObject;
393 KY_INLINE Ptr(Ptr<R> &src)
395 if (src) src->AddRef();
399 KY_INLINE Ptr(Pickable<R> v) : pObject(v.GetPtr())
407 if (pObject) pObject->Release();
411 KY_INLINE
bool operator == (
const Ptr &other)
const {
return pObject == other.pObject; }
412 KY_INLINE
bool operator != (
const Ptr &other)
const {
return pObject != other.pObject; }
414 KY_INLINE
bool operator == (C *pother)
const {
return pObject == pother; }
415 KY_INLINE
bool operator != (C *pother)
const {
return pObject != pother; }
418 KY_INLINE
bool operator < (
const Ptr &other)
const {
return pObject < other.pObject; }
422 KY_INLINE
const Ptr<C>& operator = (
const Ptr<R> &src)
424 if (src) src->AddRef();
425 if (pObject) pObject->Release();
430 KY_INLINE
const Ptr<C>& operator = (
const Ptr<C> &src)
432 if (src) src->AddRef();
433 if (pObject) pObject->Release();
438 KY_INLINE
const Ptr<C>& operator = (C *psrc)
440 if (psrc) psrc->AddRef();
441 if (pObject) pObject->Release();
445 KY_INLINE
const Ptr<C>& operator = (C &src)
447 if (pObject) pObject->Release();
451 KY_INLINE Ptr<C>& operator = (Pickable<C> src)
456 KY_INLINE Ptr<C>& operator = (Pickable<R> src)
463 KY_INLINE Ptr<C>& SetPtr(
const Ptr<R> &src)
465 if (src) src->AddRef();
466 if (pObject) pObject->Release();
471 KY_INLINE Ptr<C>& SetPtr(
const Ptr<C> &src)
473 if (src) src->AddRef();
474 if (pObject) pObject->Release();
479 KY_INLINE Ptr<C>& SetPtr(C *psrc)
481 if (psrc) psrc->AddRef();
482 if (pObject) pObject->Release();
486 KY_INLINE Ptr<C>& SetPtr(C &src)
488 if (pObject) pObject->Release();
492 KY_INLINE Ptr<C>& SetPtr(Pickable<C> src)
498 KY_INLINE
void NullWithoutRelease()
504 KY_INLINE
void Clear()
506 if (pObject) pObject->Release();
511 KY_INLINE C*& GetRawRef() {
return pObject; }
514 KY_INLINE C* GetPtr()
const {
return pObject; }
515 KY_INLINE C&
operator * ()
const {
return *pObject; }
516 KY_INLINE C* operator -> ()
const {
return pObject; }
518 KY_INLINE
operator C* ()
const {
return pObject; }
523 KY_INLINE Ptr<C>& Pick(Ptr<C>& other)
527 if (pObject) pObject->Release();
528 pObject = other.pObject;
535 KY_INLINE Ptr<C>& Pick(Pickable<C> v)
537 if (v.GetPtr() != pObject)
539 if (pObject) pObject->Release();
540 pObject = v.GetPtr();
547 KY_INLINE Ptr<C>& Pick(Pickable<R> v)
549 if (v.GetPtr() != pObject)
551 if (pObject) pObject->Release();
552 pObject = v.GetPtr();
558 KY_INLINE Ptr<C>& Pick(C* p)
562 if (pObject) pObject->Release();
577 class WeakPtrProxy :
public NewOverrideBase<Stat_Default_Mem>
580 WeakPtrProxy(RefCountWeakSupportImpl* pobject)
581 : RefCount(1), pObject(pobject)
585 KY_INLINE
bool IsAlive()
const {
return (pObject != 0); }
588 KY_INLINE
void NotifyObjectDied() { pObject = 0; }
590 RefCountWeakSupportImpl* GetObject()
const {
return pObject; }
592 KY_INLINE
void AddRef()
599 KY_INLINE
void Release()
611 WeakPtrProxy(
const WeakPtrProxy& w) { KY_UNUSED(w); }
612 void operator=(
const WeakPtrProxy& w) { KY_UNUSED(w); }
615 RefCountWeakSupportImpl* pObject;
638 KY_INLINE WeakPtr(C* ptr)
639 : pProxy(*(ptr ? ptr->CreateWeakProxy() : (WeakPtrProxy*)0))
641 KY_INLINE WeakPtr(
const Ptr<C>& ptr)
642 : pProxy(*(ptr.GetPtr() ? ptr->CreateWeakProxy() : (WeakPtrProxy*)0))
646 KY_INLINE
void operator = (C* ptr)
650 pProxy = *ptr->CreateWeakProxy();
658 KY_INLINE
void operator = (
const Ptr<C>& ptr)
659 { operator=(ptr.GetPtr()); }
662 inline operator Ptr<C>()
const
664 return Ptr<C>(GetObjectThroughProxy());
667 KY_INLINE
bool operator == (C* ptr)
668 {
return GetObjectThroughProxy() == ptr; }
669 KY_INLINE
bool operator == (
const Ptr<C>& ptr)
670 {
return GetObjectThroughProxy() == ptr.GetPtr(); }
675 KY_INLINE C* GetObjectThroughProxy()
const
681 if (pProxy->IsAlive())
683 pobject = (C*)pProxy->GetObject();
694 mutable Ptr<WeakPtrProxy> pProxy;
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
Vec2f operator*(KyFloat32 s, const Vec2f &v)
scalar * vec operator
Definition: vec2f.h:120