NAP
entityptr.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 "entity.h"
8 #include <rtti/objectptr.h>
9 
10 namespace nap
11 {
15  class EntityPtr
16  {
17  public:
18  EntityPtr() = default;
19 
20  EntityPtr(Entity* entity) : mResource(entity)
21  { }
22 
26  const std::string& getInstancePath() const { return mPath; }
27 
32  std::string toString() const { return mPath; }
33 
38  Entity* toObject() const { return mResource.get(); }
39 
47  void assign(const std::string& targetPath, rtti::Object* targetObject) { mPath = targetPath; mResource = rtti_cast<Entity>(targetObject); }
48 
52  Entity* get() const { return mResource.get(); }
53 
57  Entity* get() { return mResource.get(); }
58 
64  static std::string translateTargetID(const std::string& targetID);
65 
66  const Entity& operator*() const { assert(mResource != nullptr); return *mResource; }
67 
68  Entity& operator*() { assert(mResource != nullptr); return *mResource; }
69 
70  const Entity* operator->() const { assert(mResource != nullptr); return mResource.get(); }
71 
72  Entity* operator->() { assert(mResource != nullptr); return mResource.get(); }
73 
74  bool operator==(const EntityPtr& other) const { return mResource == other.mResource; }
75 
76  bool operator==(std::nullptr_t) const { return mResource == nullptr; }
77 
78  bool operator!=(const EntityPtr& other) const { return mResource != other.mResource; }
79 
80  bool operator!=(std::nullptr_t) const { return mResource != nullptr; }
81 
82  bool operator<(const EntityPtr& other) const { return mResource < other.mResource; }
83 
84  bool operator>(const EntityPtr& other) const { return mResource > other.mResource; }
85 
86  bool operator<=(const EntityPtr& other) const { return mResource <= other.mResource; }
87 
88  bool operator>=(const EntityPtr& other) const { return mResource >= other.mResource; }
89 
90  private:
91  rtti::ObjectPtr<Entity> mResource;
92  std::string mPath;
93  };
94 
95 
100  template<typename SourceComponentType>
102  {
104  EntityPtr SourceComponentType::*mEntityMemberPointer;
105  };
106 
108  {
109  public:
110  EntityInstancePtr() = default;
111 
112  template<class SourceComponentType>
113  EntityInstancePtr(ComponentInstance* sourceComponentInstance, EntityPtr(SourceComponentType::*entityMemberPointer))
114  {
115  SourceComponentType* resource = sourceComponentInstance->getComponent<SourceComponentType>();
116  EntityPtr& target_entity_resource = resource->*entityMemberPointer;
117  sourceComponentInstance->addToEntityLinkMap(target_entity_resource.get(), target_entity_resource.getInstancePath(), &mInstance);
118  }
119 
123  template<class SourceComponentType>
125  EntityInstancePtr(proxy.mSourceComponentInstance, proxy.mEntityMemberPointer)
126  { }
127 
128  const EntityInstance& operator*() const { assert(mInstance != nullptr); return *mInstance; }
129 
130  EntityInstance& operator*() { assert(mInstance != nullptr); return *mInstance; }
131 
132  EntityInstance* operator->() const { assert(mInstance != nullptr); return mInstance; }
133 
134  EntityInstance* operator->() { assert(mInstance != nullptr); return mInstance; }
135 
136  bool operator==(const EntityInstancePtr& other) const { return mInstance == other.mInstance; }
137 
138  bool operator==(std::nullptr_t) const { return mInstance == nullptr; }
139 
140  bool operator!=(const EntityInstancePtr& other) const { return mInstance != other.mInstance; }
141 
142  bool operator!=(std::nullptr_t) const { return mInstance != nullptr; }
143 
144  bool operator<(const EntityInstancePtr& other) const { return mInstance < other.mInstance; }
145 
146  bool operator>(const EntityInstancePtr& other) const { return mInstance > other.mInstance; }
147 
148  bool operator<=(const EntityInstancePtr& other) const { return mInstance <= other.mInstance; }
149 
150  bool operator>=(const EntityInstancePtr& other) const { return mInstance >= other.mInstance; }
151 
152  EntityInstance* get() const { return mInstance; }
153 
154  EntityInstance* get() { return mInstance; }
155 
156  private:
157  template<typename SourceComponentType_>
158  friend std::vector<EntityInstancePtr> initEntityInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<EntityPtr>(SourceComponentType_::*entityMemberPointer));
159 
160  EntityInstance* mInstance = nullptr;
161  };
162 
172  template<typename SourceComponentType>
173  EntityInstancePtrInitProxy<SourceComponentType> initEntityInstancePtr(ComponentInstance* sourceComponentInstance, EntityPtr(SourceComponentType::*entityMemberPointer));
174 
183  template<typename SourceComponentType>
184  std::vector<EntityInstancePtr> initEntityInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<EntityPtr>(SourceComponentType::*entityMemberPointer));
185 }
186 
187 
189 // The following construct is required to support EntityPtr in RTTR as a regular pointer.
191 namespace rttr
192 {
193  template<>
194  struct wrapper_mapper<nap::EntityPtr>
195  {
196  using wrapped_type = nap::Entity*;
197  using type = nap::EntityPtr;
198  inline static wrapped_type get(const type& obj) { return obj.get(); }
199  inline static type create(const wrapped_type& value) { return nap::EntityPtr(value); }
200  };
201 }
202 
203 
205 // Template Definitions
207 namespace nap
208 {
209  template<typename SourceComponentType>
211  initEntityInstancePtr(ComponentInstance* sourceComponentInstance, EntityPtr(SourceComponentType::*entityMemberPointer))
212  {
213  return{ sourceComponentInstance, entityMemberPointer };
214  }
215 
216 
217  template<typename SourceComponentType>
218  std::vector<nap::EntityInstancePtr>
219  initEntityInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<EntityPtr>(SourceComponentType::*entityMemberPointer))
220  {
221  SourceComponentType* resource = sourceComponentInstance->getComponent<SourceComponentType>();
222  std::vector<EntityPtr>& target_entity_resource = resource->*entityMemberPointer;
223 
224  std::vector<EntityInstancePtr> result;
225  result.resize(target_entity_resource.size());
226 
227  for (int i = 0; i != result.size(); ++i)
228  sourceComponentInstance->addToEntityLinkMap(target_entity_resource[i].get(), target_entity_resource[i].getInstancePath(), &result[i].mInstance);
229 
230  return result;
231  }
232 
233 }
nap::EntityInstancePtr
Definition: entityptr.h:107
nap::EntityInstancePtr::operator==
bool operator==(const EntityInstancePtr &other) const
Definition: entityptr.h:136
nap::EntityInstancePtr::EntityInstancePtr
EntityInstancePtr(const EntityInstancePtrInitProxy< SourceComponentType > &proxy)
Definition: entityptr.h:124
nap::EntityInstancePtr::operator*
EntityInstance & operator*()
Definition: entityptr.h:130
nap::EntityPtr
Definition: entityptr.h:15
nap::EntityInstancePtr::EntityInstancePtr
EntityInstancePtr()=default
nap::EntityPtr::getInstancePath
const std::string & getInstancePath() const
Definition: entityptr.h:26
nap::EntityPtr::operator==
bool operator==(const EntityPtr &other) const
Definition: entityptr.h:74
nap::EntityInstancePtr::get
EntityInstance * get()
Definition: entityptr.h:154
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::EntityPtr::get
Entity * get() const
Definition: entityptr.h:52
nap::rtti::Object
Definition: object.h:30
nap::EntityInstancePtr::operator>=
bool operator>=(const EntityInstancePtr &other) const
Definition: entityptr.h:150
nap::EntityPtr::operator->
Entity * operator->()
Definition: entityptr.h:72
nap::EntityPtr::operator*
Entity & operator*()
Definition: entityptr.h:68
nap::EntityPtr::assign
void assign(const std::string &targetPath, rtti::Object *targetObject)
Definition: entityptr.h:47
nap::EntityInstancePtrInitProxy
Definition: entityptr.h:101
nap::EntityInstancePtr::EntityInstancePtr
EntityInstancePtr(ComponentInstance *sourceComponentInstance, EntityPtr(SourceComponentType::*entityMemberPointer))
Definition: entityptr.h:113
nap::ComponentInstance::getComponent
nap::Component * getComponent() const
Definition: component.h:74
nap::EntityInstancePtr::operator!=
bool operator!=(const EntityInstancePtr &other) const
Definition: entityptr.h:140
nap::EntityPtr::EntityPtr
EntityPtr(Entity *entity)
Definition: entityptr.h:20
nap::EntityPtr::operator<
bool operator<(const EntityPtr &other) const
Definition: entityptr.h:82
nap::EntityInstancePtr::operator*
const EntityInstance & operator*() const
Definition: entityptr.h:128
nap::EntityInstancePtr::operator->
EntityInstance * operator->()
Definition: entityptr.h:134
nap::EntityInstancePtr::operator<=
bool operator<=(const EntityInstancePtr &other) const
Definition: entityptr.h:148
nap::EntityPtr::operator->
const Entity * operator->() const
Definition: entityptr.h:70
nap::EntityPtr::EntityPtr
EntityPtr()=default
nap::EntityInstance
Definition: entity.h:34
nap::EntityPtr::operator!=
bool operator!=(const EntityPtr &other) const
Definition: entityptr.h:78
nap::EntityInstancePtr::operator->
EntityInstance * operator->() const
Definition: entityptr.h:132
nap::EntityInstancePtrInitProxy::mEntityMemberPointer
EntityPtr SourceComponentType::* mEntityMemberPointer
Member pointer to the EntityPtr located in the Component.
Definition: entityptr.h:104
nap::EntityPtr::operator>=
bool operator>=(const EntityPtr &other) const
Definition: entityptr.h:88
nap::EntityPtr::operator*
const Entity & operator*() const
Definition: entityptr.h:66
nap::ComponentInstance
Definition: component.h:43
nap::EntityInstancePtr::operator==
bool operator==(std::nullptr_t) const
Definition: entityptr.h:138
nap::EntityInstancePtr::operator>
bool operator>(const EntityInstancePtr &other) const
Definition: entityptr.h:146
nap::EntityInstancePtr::get
EntityInstance * get() const
Definition: entityptr.h:152
nap
Definition: templateapp.h:17
nap::EntityPtr::operator==
bool operator==(std::nullptr_t) const
Definition: entityptr.h:76
nap::EntityInstancePtr::operator!=
bool operator!=(std::nullptr_t) const
Definition: entityptr.h:142
nap::EntityPtr::operator<=
bool operator<=(const EntityPtr &other) const
Definition: entityptr.h:86
nap::EntityPtr::toObject
Entity * toObject() const
Definition: entityptr.h:38
nap::EntityPtr::operator>
bool operator>(const EntityPtr &other) const
Definition: entityptr.h:84
nap::EntityPtr::translateTargetID
static std::string translateTargetID(const std::string &targetID)
nap::EntityPtr::get
Entity * get()
Definition: entityptr.h:57
nap::EntityInstancePtr::operator<
bool operator<(const EntityInstancePtr &other) const
Definition: entityptr.h:144
nap::EntityPtr::operator!=
bool operator!=(std::nullptr_t) const
Definition: entityptr.h:80
nap::Entity
Definition: entity.h:339
nap::initEntityInstancePtr
EntityInstancePtrInitProxy< SourceComponentType > initEntityInstancePtr(ComponentInstance *sourceComponentInstance, EntityPtr(SourceComponentType::*entityMemberPointer))
Definition: entityptr.h:211
nap::EntityInstancePtrInitProxy::mSourceComponentInstance
ComponentInstance * mSourceComponentInstance
The ComponentInstance that the EntityInstancePtr is located in.
Definition: entityptr.h:103
nap::EntityPtr::toString
std::string toString() const
Definition: entityptr.h:32
nap::EntityInstancePtr::initEntityInstancePtr
friend std::vector< EntityInstancePtr > initEntityInstancePtr(ComponentInstance *sourceComponentInstance, std::vector< EntityPtr >(SourceComponentType_::*entityMemberPointer))