NAP
componentptr.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include "component.h"
8 #include <rtti/typeinfo.h>
9 #include <rtti/objectptr.h>
10 
11 namespace nap
12 {
20  {
21  RTTI_ENABLE();
22  public:
23  virtual ~ComponentPtrBase() = default;
24 
30  static std::string translateTargetID(const std::string& targetID);
31 
36  virtual std::string toString() const = 0;
37 
42  virtual Component* toObject() const = 0;
43 
51  virtual void assign(const std::string& targetPath, rtti::Object* targetObject) = 0;
52  };
53 
90  template<class ComponentType>
91  class ComponentPtr : public ComponentPtrBase
92  {
93  RTTI_ENABLE(ComponentPtrBase)
94 
95  public:
96  // ctr
97  ComponentPtr() = default;
98 
99  // ctr override
100  ComponentPtr(ComponentType* component) : mResource(component) { }
101 
106  const std::string& getInstancePath() const { return mPath; }
107 
112  virtual std::string toString() const override { return mPath; }
113 
118  virtual Component* toObject() const override { return rtti_cast<nap::Component>(mResource.get()); }
119 
127  virtual void assign(const std::string& targetID, rtti::Object* targetObject) override;
128 
132  ComponentType* get() const { return mResource.get(); }
133 
137  ComponentType* get() { return mResource.get(); }
138 
139  const ComponentType& operator*() const { assert(mResource != nullptr); return *mResource; }
140 
141  ComponentType& operator*() { assert(mResource != nullptr); return *mResource; }
142 
143  const ComponentType* operator->() const { assert(mResource != nullptr); return mResource.get(); }
144 
145  ComponentType* operator->() { assert(mResource != nullptr); return mResource.get(); }
146 
147  bool operator==(const ComponentPtr<ComponentType>& other) const { return mResource == other.mResource; }
148 
149  template<typename OTHER>
150  bool operator==(const ComponentPtr<OTHER>& other) const { return mResource == other.mResource; }
151 
152  template<typename OTHER>
153  bool operator==(const OTHER* ptr) const { return mResource == ptr; }
154 
155  bool operator==(std::nullptr_t) const { return mResource == nullptr; }
156 
157  bool operator!=(const ComponentPtr<ComponentType>& other) const { return mResource != other.mResource; }
158 
159  template<typename OTHER>
160  bool operator!=(const ComponentPtr<OTHER>& other) const { return mResource != other.mResource; }
161 
162  template<typename OTHER>
163  bool operator!=(const OTHER* ptr) const { return mResource != ptr; }
164 
165  bool operator!=(std::nullptr_t) const { return mResource != nullptr; }
166 
167  bool operator<(const ComponentPtr<ComponentType>& other) const { return mResource < other.mResource; }
168 
169  bool operator>(const ComponentPtr<ComponentType>& other) const { return mResource > other.mResource; }
170 
171  bool operator<=(const ComponentPtr<ComponentType>& other) const { return mResource <= other.mResource; }
172 
173  bool operator>=(const ComponentPtr<ComponentType>& other) const { return mResource >= other.mResource; }
174 
175  private:
177  std::string mPath;
178  };
179 
180  template<class ComponentType>
181  void nap::ComponentPtr<ComponentType>::assign(const std::string& targetID, rtti::Object* targetObject)
182  {
183  mPath = targetID;
184  mResource = rtti_cast<ComponentType>(targetObject);
185  }
186 
187 
192  template<typename TargetComponentType, typename SourceComponentType>
194  {
197  };
198 
239  template<class TargetComponentType>
241  {
242  public:
243  using TargetComponentInstanceType = typename TargetComponentType::InstanceType;
244 
245  ComponentInstancePtr() = default;
246 
251  template<class SourceComponentType>
252  ComponentInstancePtr(ComponentInstance* sourceComponentInstance, ComponentPtr<TargetComponentType>(SourceComponentType::* componentMemberPointer))
253  {
254  SourceComponentType* resource = sourceComponentInstance->getComponent<SourceComponentType>();
255  ComponentPtr<TargetComponentType>& target_component_resource = resource->*componentMemberPointer;
256  sourceComponentInstance->addToComponentLinkMap(target_component_resource.get(), target_component_resource.getInstancePath(), (ComponentInstance**)&mInstance);
257  }
258 
262  template<class SourceComponentType>
264  ComponentInstancePtr(proxy.mSourceComponentInstance, proxy.mComponentMemberPointer)
265  { }
266 
267  const TargetComponentInstanceType& operator*() const { assert(mInstance != nullptr); return *mInstance; }
268 
269  TargetComponentInstanceType& operator*() { assert(mInstance != nullptr); return *mInstance; }
270 
271  TargetComponentInstanceType* operator->() const { assert(mInstance != nullptr); return mInstance; }
272 
273  TargetComponentInstanceType* operator->() { assert(mInstance != nullptr); return mInstance; }
274 
275  bool operator==(const ComponentInstancePtr<TargetComponentType>& other) const { return mInstance == other.mPtr; }
276 
277  template<typename OTHER>
278  bool operator==(const ComponentInstancePtr<OTHER>& other) const { return mInstance == other.mPtr; }
279 
280  template<typename OTHER>
281  bool operator==(const OTHER* ptr) const { return mInstance == ptr; }
282 
283  bool operator==(std::nullptr_t) const { return mInstance == nullptr; }
284 
285  bool operator!=(const ComponentInstancePtr<TargetComponentType>& other) const { return mInstance != other.mPtr; }
286 
287  template<typename OTHER>
288  bool operator!=(const ComponentInstancePtr<OTHER>& other) const { return mInstance != other.mPtr; }
289 
290  template<typename OTHER>
291  bool operator!=(const OTHER* ptr) const { return mInstance != ptr; }
292 
293  bool operator!=(std::nullptr_t) const { return mInstance != nullptr; }
294 
295  bool operator<(const ComponentInstancePtr<TargetComponentType>& other) const { return mInstance < other.mInstance; }
296 
297  bool operator>(const ComponentInstancePtr<TargetComponentType>& other) const { return mInstance > other.mInstance; }
298 
299  bool operator<=(const ComponentInstancePtr<TargetComponentType>& other) const { return mInstance <= other.mInstance; }
300 
301  bool operator>=(const ComponentInstancePtr<TargetComponentType>& other) const { return mInstance >= other.mInstance; }
302 
303  TargetComponentInstanceType* get() const { return mInstance; }
304 
305  TargetComponentInstanceType* get() { return mInstance; }
306 
307  private:
308  template<typename TargetComponentType_, typename SourceComponentType_>
309  friend std::vector<ComponentInstancePtr<TargetComponentType_>> initComponentInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<ComponentPtr<TargetComponentType_>>(SourceComponentType_::*componentMemberPointer));
310 
311  private:
312  TargetComponentInstanceType* mInstance = nullptr;
313  };
314 
324  template<typename TargetComponentType, typename SourceComponentType>
325  ComponentInstancePtrInitProxy<TargetComponentType, SourceComponentType> initComponentInstancePtr(ComponentInstance* sourceComponentInstance, ComponentPtr<TargetComponentType>(SourceComponentType::*componentMemberPointer));
326 
335  template<typename TargetComponentType, typename SourceComponentType>
336  std::vector<ComponentInstancePtr<TargetComponentType>> initComponentInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<ComponentPtr<TargetComponentType>>(SourceComponentType::*componentMemberPointer));
337 }
338 
339 
341 // The following construct is required to support ComponentPtr in RTTR as a regular pointer.
343 namespace rttr
344 {
345  template<typename T>
346  struct wrapper_mapper<nap::ComponentPtr<T>>
347  {
348  using wrapped_type = T*;
349  using type = nap::ComponentPtr<T>;
350  inline static wrapped_type get(const type& obj) { return obj.get(); }
351  inline static type create(const wrapped_type& value) { return nap::ComponentPtr<T>(value); }
352  };
353 }
354 
355 
356 namespace nap
357 {
359  // Template definitions
361 
362  template<typename TargetComponentType, typename SourceComponentType>
364  initComponentInstancePtr(ComponentInstance* sourceComponentInstance, ComponentPtr<TargetComponentType>(SourceComponentType::*componentMemberPointer))
365  {
366  return{ sourceComponentInstance, componentMemberPointer };
367  }
368 
369 
370  template<typename TargetComponentType, typename SourceComponentType>
371  std::vector<nap::ComponentInstancePtr<TargetComponentType>>
372  initComponentInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<ComponentPtr<TargetComponentType>>(SourceComponentType::*componentMemberPointer))
373  {
374  SourceComponentType* resource = sourceComponentInstance->getComponent<SourceComponentType>();
375  std::vector<ComponentPtr<TargetComponentType>>& target_component_resource = resource->*componentMemberPointer;
376 
377  std::vector<ComponentInstancePtr<TargetComponentType>> result;
378  result.resize(target_component_resource.size());
379 
380  for (int i = 0; i != result.size(); ++i)
381  sourceComponentInstance->addToComponentLinkMap(target_component_resource[i].get(), target_component_resource[i].getInstancePath(), (ComponentInstance**)&result[i].mInstance);
382 
383  return result;
384  }
385 }
nap::ComponentInstancePtr::initComponentInstancePtr
friend std::vector< ComponentInstancePtr< TargetComponentType_ > > initComponentInstancePtr(ComponentInstance *sourceComponentInstance, std::vector< ComponentPtr< TargetComponentType_ >>(SourceComponentType_::*componentMemberPointer))
nap::ComponentPtr::assign
virtual void assign(const std::string &targetID, rtti::Object *targetObject) override
Definition: componentptr.h:181
nap::ComponentInstancePtr::operator*
const TargetComponentInstanceType & operator*() const
Definition: componentptr.h:267
nap::ComponentPtr::operator>
bool operator>(const ComponentPtr< ComponentType > &other) const
Definition: componentptr.h:169
nap::ComponentPtr::operator>=
bool operator>=(const ComponentPtr< ComponentType > &other) const
Definition: componentptr.h:173
nap::ComponentInstancePtr::ComponentInstancePtr
ComponentInstancePtr(const ComponentInstancePtrInitProxy< TargetComponentType, SourceComponentType > &proxy)
Definition: componentptr.h:263
nap::ComponentPtr::ComponentPtr
ComponentPtr()=default
nap::ComponentInstancePtr::operator!=
bool operator!=(const ComponentInstancePtr< OTHER > &other) const
Definition: componentptr.h:288
nap::ComponentInstancePtr::get
TargetComponentInstanceType * get() const
Definition: componentptr.h:303
nap::ComponentPtrBase::assign
virtual void assign(const std::string &targetPath, rtti::Object *targetObject)=0
nap::ComponentInstancePtr::operator!=
bool operator!=(const OTHER *ptr) const
Definition: componentptr.h:291
nap::ComponentInstancePtrInitProxy::mComponentMemberPointer
ComponentPtr< TargetComponentType > SourceComponentType::* mComponentMemberPointer
Member pointer to the ComponentPtr located in the Component.
Definition: componentptr.h:196
nap::ComponentPtr::operator<
bool operator<(const ComponentPtr< ComponentType > &other) const
Definition: componentptr.h:167
nap::rtti::ObjectPtr< ComponentType >
nap::ComponentInstancePtr::operator!=
bool operator!=(const ComponentInstancePtr< TargetComponentType > &other) const
Definition: componentptr.h:285
nap::initComponentInstancePtr
ComponentInstancePtrInitProxy< TargetComponentType, SourceComponentType > initComponentInstancePtr(ComponentInstance *sourceComponentInstance, ComponentPtr< TargetComponentType >(SourceComponentType::*componentMemberPointer))
Definition: componentptr.h:364
nap::rtti::Object
Definition: object.h:30
nap::ComponentInstancePtr::operator>
bool operator>(const ComponentInstancePtr< TargetComponentType > &other) const
Definition: componentptr.h:297
nap::ComponentPtr::operator==
bool operator==(const OTHER *ptr) const
Definition: componentptr.h:153
nap::ComponentPtr::get
ComponentType * get() const
Definition: componentptr.h:132
nap::ComponentInstancePtr::TargetComponentInstanceType
typename TargetComponentType::InstanceType TargetComponentInstanceType
Definition: componentptr.h:243
nap::ComponentInstancePtr::operator==
bool operator==(const OTHER *ptr) const
Definition: componentptr.h:281
nap::ComponentPtr::toObject
virtual Component * toObject() const override
Definition: componentptr.h:118
nap::ComponentPtrBase::toString
virtual std::string toString() const =0
nap::ComponentInstancePtr::operator->
TargetComponentInstanceType * operator->()
Definition: componentptr.h:273
nap::ComponentPtr::getInstancePath
const std::string & getInstancePath() const
Definition: componentptr.h:106
nap::ComponentPtr::operator!=
bool operator!=(const ComponentPtr< ComponentType > &other) const
Definition: componentptr.h:157
nap::ComponentPtr::operator==
bool operator==(const ComponentPtr< ComponentType > &other) const
Definition: componentptr.h:147
nap::ComponentPtrBase::~ComponentPtrBase
virtual ~ComponentPtrBase()=default
nap::ComponentInstancePtr::ComponentInstancePtr
ComponentInstancePtr()=default
nap::ComponentInstance::getComponent
nap::Component * getComponent() const
Definition: component.h:74
nap::ComponentInstancePtr::operator==
bool operator==(const ComponentInstancePtr< TargetComponentType > &other) const
Definition: componentptr.h:275
nap::ComponentPtr::operator*
ComponentType & operator*()
Definition: componentptr.h:141
nap::ComponentInstancePtr::get
TargetComponentInstanceType * get()
Definition: componentptr.h:305
nap::ComponentPtr::operator*
const ComponentType & operator*() const
Definition: componentptr.h:139
nap::ComponentInstancePtr::operator==
bool operator==(const ComponentInstancePtr< OTHER > &other) const
Definition: componentptr.h:278
nap::ComponentPtr::operator!=
bool operator!=(const OTHER *ptr) const
Definition: componentptr.h:163
nap::ComponentPtr::operator!=
bool operator!=(std::nullptr_t) const
Definition: componentptr.h:165
nap::ComponentInstancePtr::ComponentInstancePtr
ComponentInstancePtr(ComponentInstance *sourceComponentInstance, ComponentPtr< TargetComponentType >(SourceComponentType::*componentMemberPointer))
Definition: componentptr.h:252
nap::ComponentPtr::ComponentPtr
ComponentPtr(ComponentType *component)
Definition: componentptr.h:100
nap::ComponentPtr::toString
virtual std::string toString() const override
Definition: componentptr.h:112
nap::ComponentInstancePtr
Definition: component.h:28
nap::ComponentPtr::operator->
ComponentType * operator->()
Definition: componentptr.h:145
nap::ComponentInstancePtr::operator>=
bool operator>=(const ComponentInstancePtr< TargetComponentType > &other) const
Definition: componentptr.h:301
nap::ComponentInstance
Definition: component.h:43
nap::ComponentInstancePtrInitProxy
Definition: componentptr.h:193
nap::rtti::ObjectPtr::get
T * get() const
Definition: objectptr.h:181
nap::Component
Definition: component.h:151
nap::ComponentPtrBase
Definition: componentptr.h:19
nap::ComponentInstancePtrInitProxy::mSourceComponentInstance
ComponentInstance * mSourceComponentInstance
The ComponentInstance that the ComponentInstancePtr is located in.
Definition: componentptr.h:195
nap::ComponentPtr::operator!=
bool operator!=(const ComponentPtr< OTHER > &other) const
Definition: componentptr.h:160
nap::ComponentInstancePtr::operator!=
bool operator!=(std::nullptr_t) const
Definition: componentptr.h:293
nap::ComponentPtr::operator==
bool operator==(const ComponentPtr< OTHER > &other) const
Definition: componentptr.h:150
nap::ComponentInstancePtr::operator<
bool operator<(const ComponentInstancePtr< TargetComponentType > &other) const
Definition: componentptr.h:295
nap
Definition: templateapp.h:17
nap::ComponentInstancePtr::operator==
bool operator==(std::nullptr_t) const
Definition: componentptr.h:283
nap::ComponentPtr::operator==
bool operator==(std::nullptr_t) const
Definition: componentptr.h:155
nap::ComponentInstancePtr::operator*
TargetComponentInstanceType & operator*()
Definition: componentptr.h:269
nap::ComponentPtr::operator->
const ComponentType * operator->() const
Definition: componentptr.h:143
nap::ComponentPtrBase::toObject
virtual Component * toObject() const =0
nap::ComponentInstancePtr::operator<=
bool operator<=(const ComponentInstancePtr< TargetComponentType > &other) const
Definition: componentptr.h:299
nap::ComponentPtrBase::translateTargetID
static std::string translateTargetID(const std::string &targetID)
nap::ComponentPtr
Definition: component.h:31
nap::ComponentInstancePtr::operator->
TargetComponentInstanceType * operator->() const
Definition: componentptr.h:271
nap::ComponentPtr::get
ComponentType * get()
Definition: componentptr.h:137
nap::ComponentPtr::operator<=
bool operator<=(const ComponentPtr< ComponentType > &other) const
Definition: componentptr.h:171