Nebula
Loading...
Searching...
No Matches
objectref.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
18#include "core/refcounted.h"
19
20//------------------------------------------------------------------------------
21namespace Threading
22{
24{
26public:
28 ObjectRef();
30 virtual ~ObjectRef();
31
33 bool IsValid() const;
35 template<class TYPE> void Validate(TYPE* obj);
37 void Invalidate();
39 template<class TYPE> TYPE* Ref() const;
40
41private:
43};
44
45//------------------------------------------------------------------------------
48inline bool
50{
51 return (0 != this->ref);
52}
53
54//------------------------------------------------------------------------------
57template<class TYPE> inline void
59{
60 n_assert(0 != obj);
61 n_assert(obj->IsA(TYPE::RTTI));
62 n_assert(0 == this->ref);
63 obj->AddRef();
64 Interlocked::ExchangePointer((void* volatile*)&this->ref, obj);
65}
66
67//------------------------------------------------------------------------------
70inline void
72{
73 n_assert(0 != this->ref);
74 this->ref->Release();
75 Interlocked::ExchangePointer((void* volatile*)&this->ref, 0);
76}
77
78//------------------------------------------------------------------------------
81template<class TYPE> inline TYPE*
83{
84 n_assert(0 != this->ref);
85 n_assert(this->ref->IsA(TYPE::RTTI));
86 return (TYPE*) this->ref;
87}
88
89} // namespace Threading
90//------------------------------------------------------------------------------
91
The common base class of Nebula.
Definition refcounted.h:38
bool IsA(const Rtti &rtti) const
return true if this object is instance of given class, or a derived class
Definition refcounted.h:180
void Release()
decrement refcount and destroy object if refcount is zero
Definition refcounted.h:131
A thread-safe reference to a shared object.
Definition objectref.h:24
__DeclareClass(ObjectRef)
bool IsValid() const
return true if the ObjectRef points to a valid object
Definition objectref.h:49
void Invalidate()
invalidate the ref
Definition objectref.h:71
ObjectRef()
constructor
Definition objectref.cc:15
TYPE * Ref() const
get pointer to object
Definition objectref.h:82
Core::RefCounted *volatile ref
Definition objectref.h:42
void Validate(TYPE *obj)
validate the ref with a pointer to a target object (must be RefCounted)
Definition objectref.h:58
virtual ~ObjectRef()
destructor
Definition objectref.cc:24
#define n_assert(exp)
Definition debug.h:50
void * ExchangePointer(void *volatile *dest, void *value)
interlocked exchange
Definition gccinterlocked.cc:130
The Jobs2 system provides a set of threads and a pool of jobs from which threads can pickup work.
Definition jobs2.h:16