OpenBarnyard
 
Loading...
Searching...
No Matches
TRefCounted.h
Go to the documentation of this file.
1#pragma once
2
4
6{
7public:
10
14 TRefCounted& operator=( TRefCounted const& ) { return *this; }
15
16protected:
18};
19
20// typename T should have Delete method in order to be successfully destroyed
21// NOTE: Each TObject has it by default
22template <class T>
23class TRef
24{
25public:
26 // constructors/destructor
27 constexpr TRef()
28 : m_pPtr( TNULL )
29 {
30 }
31
32 TRef( T* a_pPtr )
33 : m_pPtr( TNULL )
34 {
35 Create( a_pPtr );
36 }
37
38 TRef( const TRef& a_rcOther )
39 : m_pPtr( TNULL )
40 {
41 Create( a_rcOther.m_pPtr );
42 }
43
44 TRef( TRef&& a_rOther )
45 {
46 a_rOther.m_pPtr = m_pPtr;
47 m_pPtr = TNULL;
48 }
49
50 ~TRef() { Destroy(); }
51
52 TRef& operator=( T* a_pPtr )
53 {
54 if ( a_pPtr != m_pPtr )
55 {
56 Create( a_pPtr );
57 }
58
59 return *this;
60 }
61
62 TRef& operator=( const TRef& a_rOther )
63 {
64 if ( a_rOther.m_pPtr != m_pPtr )
65 {
66 Create( a_rOther.m_pPtr );
67 }
68
69 return *this;
70 }
71
72 T* Get() { return m_pPtr; }
73 const T* Get() const { return m_pPtr; }
74
75 T* operator->() { return m_pPtr; }
76 const T* operator->() const { return m_pPtr; }
77
78 operator T*() { return m_pPtr; }
79 operator const T*() const { return m_pPtr; }
80
81private:
82 TFORCEINLINE void Create( T* a_pPtr )
83 {
84 Destroy();
85 m_pPtr = a_pPtr;
86
87 if ( m_pPtr )
88 {
89 m_pPtr->IncRefCount();
90 }
91 }
92
93 TFORCEINLINE void Destroy()
94 {
95 if ( m_pPtr && m_pPtr->DecRefCount() == 1 )
96 {
97 m_pPtr->Delete();
98 }
99
100 m_pPtr = TNULL;
101 }
102
103protected:
105};
106
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TFORCEINLINE
Definition Defines.h:74
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
TINT m_iRefCount
Definition TRefCounted.h:17
TINT DecRefCount()
Definition TRefCounted.h:11
TINT IncRefCount()
Definition TRefCounted.h:13
TINT GetRefCount()
Definition TRefCounted.h:12
TRefCounted & operator=(TRefCounted const &)
Definition TRefCounted.h:14
TRef(T *a_pPtr)
Definition TRefCounted.h:32
~TRef()
Definition TRefCounted.h:50
TRef & operator=(const TRef &a_rOther)
Definition TRefCounted.h:62
TRef(const TRef &a_rcOther)
Definition TRefCounted.h:38
const T * operator->() const
Definition TRefCounted.h:76
T * m_pPtr
T * Get()
Definition TRefCounted.h:72
constexpr TRef()
Definition TRefCounted.h:27
TRef & operator=(T *a_pPtr)
Definition TRefCounted.h:52
TRef(TRef &&a_rOther)
Definition TRefCounted.h:44
const T * Get() const
Definition TRefCounted.h:73
T * operator->()
Definition TRefCounted.h:75