3 #include "g_cont_common.h"
4 #include "g_cont_HeapAbstract.h"
5 #include "g_cont_AllocationPolicyAbstract.h"
29 #define G_REF_HEADER(acls) G_STR(_g_ref/acls.h)
36 template <
class T >
class ref_const;
39 template <
class T >
class ref;
44 template <
class O_T >
friend class ref_base;
48 ref_base ( ) : mRefMarkerP(NULL),mAllocPolicyP(NULL),mObjOffset(-1){ G_VERBOSE_MSG_L5(
"ref_base<T>::ref_base()"); }
49 ref_base (
const ref_base& other ) { G_VERBOSE_MSG_L5(
"ref_base( const ref_base& other )"); }
52 virtual ~ref_base() { G_VERBOSE_MSG_L5(
"ref_base<T>::~ref_base()"); mRefLeave(); }
54 bool operator == (
const ref_base<T>& other )
const {
return other.mRefMarkerP == this->mRefMarkerP; }
56 size_t getNumRefs ( )
const {
return ( mRefMarkerP!=NULL ) ? mAllocPolicyP->getNumRefs(mRefMarkerP):0; }
57 HeapAbstract* getHeapP ( )
const {
return ( mRefMarkerP!=NULL ) ? mAllocPolicyP->getHeapP():NULL; }
59 bool isEmpty ( )
const {
return (mRefMarkerP==NULL); }
66 G_VERBOSE_MSG_L5(_g_method_context);
68 if ( aDataAllocPolicy != NULL )
70 mAllocPolicyP = aDataAllocPolicy;
71 mRefMarkerP = mAllocPolicyP->reserveRef(
sizeof(T));
74 G_VERBOSE_MSG_L3(
"Allocate new ref at mark " << std::hex << (
int)mRefMarkerP << std::dec );
76 return mAllocPolicyP->getRefDataLocation(mRefMarkerP);
80 G_EXC_RAISE_CONT (
"Not a valid allocation policy!");
85 void mIncRef ( ) { mAllocPolicyP->incRef(mRefMarkerP); }
95 G_VERBOSE_MSG_L5(
"ref_base<T>::mRefLeave()");
97 if ( mRefMarkerP != NULL )
99 G_VERBOSE_MSG_L3 (
"Leaving ref marked at " << std::hex << (
int)mRefMarkerP << std::dec <<
" new ref count " << getNumRefs() );
101 int num_ref = mAllocPolicyP->
decRef(mRefMarkerP);
105 G_VERBOSE_MSG_L3(
"Num refs == 0 deleting ref marked at " << std::hex << (
int)mRefMarkerP << std::dec );
109 mAllocPolicyP->freeRef ( mRefMarkerP );
111 else if ( num_ref < 0 )
113 G_VERBOSE_MSG_L3(
"Num refs < 0 probably constructor has thrown an exception deleting ref(without deleting object) marked at " << std::hex << (
int)mRefMarkerP << std::dec );
115 mAllocPolicyP->freeRef ( mRefMarkerP );
118 mAllocPolicyP = NULL;
124 G_VERBOSE_MSG_L3(
"Empty content." );
129 void mRefTake (
const ref_base& o )
131 G_VERBOSE_MSG_L5(
"ref_base<T>::mRefTake ( const ref_base& o )");
133 if ( o.mRefMarkerP != mRefMarkerP )
139 mRefMarkerP = o.mRefMarkerP;
140 mAllocPolicyP = o.mAllocPolicyP;
141 mAllocPolicyP->incRef(mRefMarkerP);
142 mObjOffset = o.mObjOffset;
144 G_VERBOSE_MSG_L3 (
"Taking ref from other ref marked at " << std::hex << (
int)mRefMarkerP << std::dec <<
" new ref count " << getNumRefs() );
151 G_VERBOSE_MSG_L5(
"ref_base<T>::mRefTake ( const ref_base<O_T>& o )");
153 if ( o.mRefMarkerP != mRefMarkerP )
159 mRefMarkerP = o.mRefMarkerP;
160 mAllocPolicyP = o.mAllocPolicyP;
161 mAllocPolicyP->incRef(mRefMarkerP);
166 T* fake =
static_cast<T*
>(
reinterpret_cast<O_T*
>(1));
168 mObjOffset = o.mObjOffset + (
long long)fake - 1;
170 G_VERBOSE_MSG_L3 (
"Taking ref from other ref of OTHER TYPE marked at " << std::hex << (
int)mRefMarkerP << std::dec <<
" new ref count " << getNumRefs() );
175 template <
class O_T>
bool mRefTakeTryDynamically (
ref_base<O_T>& o )
177 G_VERBOSE_MSG_L5(
"ref_base<T>::mRefTakeTryDynamically ( const ref_base<O_T>& o )");
179 if ( o.mRefMarkerP != NULL && dynamic_cast<T*>(o.mGetObjP()) )
190 T* mGetObjP ( )
const
194 G_VERBOSE_MSG_L5 (
"ref_base<T>::mGetObjP()const");
196 if ( mRefMarkerP != NULL )
198 G_VERBOSE_MSG_L3 (
"Getting pointer to ref marked at " << std::hex << (
int)mRefMarkerP << std::dec <<
" current count " << getNumRefs() );
199 return reinterpret_cast<T*
>(
reinterpret_cast<char*
>(mAllocPolicyP->getRefDataLocation(mRefMarkerP))+mObjOffset);
203 G_EXC_RAISE_CONT (
"Empty object can't be dereferenced!" );
Definition: g_cont_ref_base.h:37
virtual int decRef(MemMarker *)=0
implements decrement ref policy
Definition: g_cont_AllocationPolicyAbstract.h:16
Definition: g.mthread.ThreadSimpleEvent.h:5
Definition: g_cont_it_ref.h:11
Definition: g_cont_ref_base.h:41
Definition: g_cont_HeapAbstract.h:20
Definition: g_cont_it_ref.h:10
Definition: g_cont_AllocationPolicyAbstract.h:13
#define G_EXC_SET_CONTEXT(acontextstr)
Sets the method context.
Definition: g_exception_macros.h:52
Definition: g_cont_ref_base.h:38