SmartEngine  1.6.0
ObjectPtr.h
1 // Copyright (C) Entropy Software LLC - All Rights Reserved
2 
3 #pragma once
4 
5 namespace SmartEngine
6 {
7 
8 class IObject;
9 
10 #pragma pack(push, 1)
11 
15 template <typename T> class ObjectPtr
16 {
17 public:
18  ObjectPtr() {}
19 
20  ObjectPtr(const ObjectPtr<T>& ptr)
21  {
22  _ptr = ptr._ptr;
23  AddRef();
24  }
25 
26  ObjectPtr(T* ptr)
27  {
28  _ptr = ptr;
29  if (_ptr)
30  {
31  _ptr->AddRef();
32  }
33  }
34 
35  ~ObjectPtr() { Release(); }
36 
37  void Clear() { Release(); }
38 
40  {
41  Release();
42  _ptr = ptr._ptr;
43  ptr._ptr = nullptr;
44  return *this;
45  }
46 
48  {
49  T* tmp = _ptr;
50 
51  if (ptr)
52  {
53  _ptr = static_cast<T*>(ptr->QueryInterface(T::GetClassId()));
54  if (_ptr)
55  {
56  _ptr->AddRef();
57  }
58  }
59  else
60  {
61  _ptr = nullptr;
62  }
63 
64  if (tmp)
65  {
66  tmp->Release();
67  }
68 
69  return *this;
70  }
71 
73  {
74  T* tmp = _ptr;
75 
76  _ptr = ptr._ptr;
77  AddRef();
78 
79  if (tmp)
80  {
81  tmp->Release();
82  }
83 
84  return *this;
85  }
86 
87  T* operator->() const { return _ptr; }
88 
89  T* Get() const { return _ptr; }
90 
91  T*& Get() { return _ptr; }
92 
93  void Attach(IObject* ptr)
94  {
95  Release();
96  if (ptr)
97  {
98  _ptr = static_cast<T*>(ptr->QueryInterface(T::GetClassId()));
99  }
100  else
101  {
102  _ptr = nullptr;
103  }
104  }
105 
106  T* Detach()
107  {
108  T* tmp = _ptr;
109  _ptr = nullptr;
110  return tmp;
111  }
112 
113  operator T*() const { return _ptr; }
114 
115  template <typename D>
116  bool IsA()
117  {
118  if (!_ptr)
119  {
120  return false;
121  }
122  return (_ptr->QueryInterface(D::GetClassId()) != nullptr);
123  }
124 
125 private:
126  void Release()
127  {
128  if (_ptr)
129  {
130  _ptr->Release();
131  _ptr = nullptr;
132  }
133  }
134 
135  void AddRef()
136  {
137  if (_ptr)
138  {
139  _ptr->AddRef();
140  }
141  }
142 
143  T* _ptr = nullptr;
144 };
145 
146 #pragma pack(pop)
147 
149 template <typename T> bool operator==(const ObjectPtr<T>& ptr, const T* p) { return ptr.Get() == p; }
150 template <typename T> bool operator==(const ObjectPtr<T>& ptr, T* p) { return ptr.Get() == p; }
152 
156 template <typename D> ObjectPtr<D> DynamicCast(IObject* p)
157 {
158  ObjectPtr<D> ptr;
159  ptr = p;
160  return ptr;
161 }
162 
166 template <typename D> ObjectPtr<D> DynamicCast(const IObject* p)
167 {
168  ObjectPtr<D> ptr;
169  ptr = const_cast<IObject*>(p);
170  return ptr;
171 }
172 
177 template <typename D> ObjectPtr<D> DynamicCastAttach(IObject* p)
178 {
179  ObjectPtr<D> ptr;
180  ptr.Attach(p);
181  return ptr;
182 }
183 
184 template <typename D> ObjectPtr<D> IsA(IObject* p)
185 {
186  if (!p)
187  {
188  return false;
189  }
190 
191  return (p->QueryInterface(D::GetClassId()) != nullptr);
192 }
193 
194 } // namespace SmartEngine
SmartEngine::ObjectPtr::Get
T * Get() const
Definition: ObjectPtr.h:89
SmartEngine::ObjectPtr::ObjectPtr
ObjectPtr(const ObjectPtr< T > &ptr)
Definition: ObjectPtr.h:20
SmartEngine::ObjectPtr::~ObjectPtr
~ObjectPtr()
Definition: ObjectPtr.h:35
SmartEngine::ObjectPtr::Detach
T * Detach()
Definition: ObjectPtr.h:106
SmartEngine::DynamicCastAttach
ObjectPtr< D > DynamicCastAttach(IObject *p)
Safe cast an IObject to a pointer of a different type. Does not call AddRef() on the interface.
Definition: ObjectPtr.h:177
SmartEngine::IsA
ObjectPtr< D > IsA(IObject *p)
Definition: ObjectPtr.h:184
SmartEngine::ObjectPtr::ObjectPtr
ObjectPtr(T *ptr)
Definition: ObjectPtr.h:26
SmartEngine::ObjectPtr::operator=
ObjectPtr< T > & operator=(const ObjectPtr< T > &ptr)
Definition: ObjectPtr.h:72
SmartEngine::ObjectPtr::Get
T *& Get()
Definition: ObjectPtr.h:91
SmartEngine::ObjectPtr::Attach
void Attach(IObject *ptr)
Definition: ObjectPtr.h:93
SmartEngine::ObjectPtr::operator->
T * operator->() const
Definition: ObjectPtr.h:87
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
SmartEngine
Definition: A2CTrainer.h:10
SmartEngine::IObject
Base class for SmartEngine AI objects. It is not common to deal with this class directly.
Definition: Object.h:19
SmartEngine::ObjectPtr::Clear
void Clear()
Definition: ObjectPtr.h:37
SmartEngine::IObject::QueryInterface
virtual void * QueryInterface(ObjectClassId id)=0
Queries the object for an interface and returns a pointer to that interface if found.
SmartEngine::ObjectPtr::IsA
bool IsA()
Definition: ObjectPtr.h:116
SmartEngine::ObjectPtr::operator=
ObjectPtr< T > & operator=(IObject *ptr)
Definition: ObjectPtr.h:47
SmartEngine::ObjectPtr::ObjectPtr
ObjectPtr()
Definition: ObjectPtr.h:18
SmartEngine::ObjectPtr::operator=
ObjectPtr< T > & operator=(ObjectPtr< T > &&ptr)
Definition: ObjectPtr.h:39
SmartEngine::DynamicCast
ObjectPtr< D > DynamicCast(IObject *p)
Safe cast an IObject to a pointer of a different type.
Definition: ObjectPtr.h:156