2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _RSTL_RC_PTR
|
|
|
|
#define _RSTL_RC_PTR
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2022-10-01 06:19:09 +00:00
|
|
|
namespace rstl {
|
2022-04-10 00:17:06 +00:00
|
|
|
class CRefData {
|
|
|
|
public:
|
2022-10-01 06:19:09 +00:00
|
|
|
CRefData(const void* ptr) : x0_ptr(ptr), x4_refCount(1) {}
|
|
|
|
CRefData(const void* ptr, int refCount) : x0_ptr(ptr), x4_refCount(refCount) {}
|
|
|
|
|
|
|
|
void* GetPtr() const { return const_cast< void* >(x0_ptr); }
|
|
|
|
int GetRefCount() const { return x4_refCount; }
|
|
|
|
int AddRef() { return ++x4_refCount; }
|
|
|
|
int DelRef() { return --x4_refCount; }
|
2022-09-13 04:26:54 +00:00
|
|
|
|
2022-10-01 06:19:09 +00:00
|
|
|
const void* x0_ptr;
|
|
|
|
int x4_refCount;
|
|
|
|
|
|
|
|
static CRefData sNull;
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
class rc_ptr {
|
|
|
|
public:
|
2022-10-01 06:19:09 +00:00
|
|
|
rc_ptr(const T* ptr) : x0_refData(new CRefData(ptr)) {}
|
|
|
|
rc_ptr(const rc_ptr& other) : x0_refData(other.x0_refData) { x0_refData->AddRef(); }
|
|
|
|
~rc_ptr() { ReleaseData(); }
|
|
|
|
T* GetPtr() const { return static_cast< T* >(x0_refData->GetPtr()); }
|
2022-10-17 00:46:11 +00:00
|
|
|
void ReleaseData();
|
2022-08-16 02:14:28 +00:00
|
|
|
T* operator->() const { return GetPtr(); }
|
|
|
|
T& operator*() const { return *GetPtr(); }
|
2022-10-16 20:20:36 +00:00
|
|
|
operator bool() const { return GetPtr() != nullptr; }
|
2022-09-13 04:26:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
CRefData* x0_refData;
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
2022-08-14 18:38:41 +00:00
|
|
|
|
2022-10-17 00:46:11 +00:00
|
|
|
template < typename T >
|
|
|
|
void rc_ptr< T >::ReleaseData() {
|
|
|
|
if (x0_refData->DelRef() <= 0) {
|
|
|
|
delete GetPtr();
|
|
|
|
delete x0_refData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 18:38:41 +00:00
|
|
|
template < typename T >
|
|
|
|
class ncrc_ptr : public rc_ptr< T > {
|
2022-09-13 04:26:54 +00:00
|
|
|
public:
|
2022-10-21 01:32:04 +00:00
|
|
|
ncrc_ptr(T* ptr) : rc_ptr< T >(ptr) {}
|
2022-08-14 18:38:41 +00:00
|
|
|
};
|
2022-04-10 00:17:06 +00:00
|
|
|
} // namespace rstl
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _RSTL_RC_PTR
|