NAP
group.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 // Local Includes
8 #include "resource.h"
9 #include "resourceptr.h"
10 #include "rtti/objectptr.h"
11 #include "resource.h"
12 
13 namespace nap
14 {
15  namespace group
16  {
17  constexpr const char* members = "Members";
18  constexpr const char* children = "Children";
19  }
20 
21 
23  // Group Interface
25 
29  class NAPAPI IGroup : public Resource
30  {
31  RTTI_ENABLE(Resource)
32  public:
38  IGroup(rtti::TypeInfo memberType, std::string&& membersName, std::string&& childrenName) :
39  mMemberType(memberType),
40  mMembersPropertyName(std::move(membersName)),
41  mChildrenPropertyName(std::move(childrenName)) { }
42 
46  rtti::TypeInfo getMemberType() const { return mMemberType; }
47 
51  rttr::property getMembersProperty() const;
52 
56  rttr::property getChildrenProperty() const;
57 
61  const std::string& membersPropertyName() const { return mMembersPropertyName; }
62 
66  const std::string& childrenPropertyName() const { return mChildrenPropertyName; }
67 
68  private:
69  rtti::TypeInfo mMemberType;
70  std::string mMembersPropertyName;
71  std::string mChildrenPropertyName;
72  };
73 
74 
76  // Group
78 
82  template<typename T>
83  class Group : public IGroup
84  {
85  RTTI_ENABLE(IGroup)
86  public:
90  Group() : IGroup(RTTI_OF(T), group::members, group::children) { }
91 
97  virtual bool init(utility::ErrorState& errorState) override;
98 
104  rtti::ObjectPtr<T> findObject(const std::string& id) const;
105 
111  rtti::ObjectPtr<T> findObjectRecursive(const std::string& id) const;
112 
123  template<typename M>
124  rtti::ObjectPtr<M> findObject(const std::string& id) const;
125 
136  template<typename M>
137  rtti::ObjectPtr<M> findObjectRecursive(const std::string& id) const;
138 
139  std::vector<rtti::ObjectPtr<T>> mMembers;
140  std::vector<rtti::ObjectPtr<Group<T>>> mChildren;
141 
142  private:
143  std::unordered_map<std::string, T*> mMap;
144  };
145 
146 
148  // Group Definitions
150 
151  // Default NAP (resource) group.
153 
154 
156  // Template Definitions
158 
159  template<typename T>
161  {
162  mMap.reserve(mMembers.size());
163  for (const auto& member : mMembers)
164  {
165  mMap.emplace(std::make_pair(member->mID, member.get()));
166  }
167  return true;
168  }
169 
170  template<typename T>
171  rtti::ObjectPtr<T> nap::Group<T>::findObject(const std::string& id) const
172  {
173  // Find in this group
174  auto it = mMap.find(id);
175  if (it != mMap.end())
176  return rtti::ObjectPtr<T>(it->second);
177  return nullptr;
178  }
179 
180  template<typename T>
182  {
183  // Find in this group
184  rtti::ObjectPtr<T> object = findObject(id);
185  if (object != nullptr)
186  return object;
187 
188  // Find in sub-groups
189  for (const auto& child : mChildren)
190  {
191  rtti::ObjectPtr<T> object = child->findObjectRecursive(id);
192  if (object != nullptr)
193  return object;
194  }
195  return nullptr;
196  }
197 
198  template<typename T>
199  template<typename M>
200  rtti::ObjectPtr<M> nap::Group<T>::findObject(const std::string& id) const
201  {
202  return rtti::ObjectPtr<M>(findObject(id));
203  }
204 
205  template<typename T>
206  template<typename M>
207  rtti::ObjectPtr<M> nap::Group<T>::findObjectRecursive(const std::string& id) const
208  {
209  return rtti::ObjectPtr<M>(findObjectRecursive(id));
210  }
211 }
212 
213 
215 // Group registration
217 
218 // Backwards compatible -> registration without description
219 #define DEFINE_GROUP_1(Type) \
220  RTTI_BEGIN_CLASS(Type) \
221  RTTI_PROPERTY(nap::group::members, &Type::mMembers, nap::rtti::EPropertyMetaData::Embedded | nap::rtti::EPropertyMetaData::ReadOnly) \
222  RTTI_PROPERTY(nap::group::children, &Type::mChildren, nap::rtti::EPropertyMetaData::Embedded | nap::rtti::EPropertyMetaData::ReadOnly) \
223  RTTI_END_CLASS
224 
225 // Group registration with description
226 #define DEFINE_GROUP_2(Type, ObjectType) \
227  RTTI_BEGIN_CLASS(Type, "Groups together a set of '"#ObjectType"' objects") \
228  RTTI_PROPERTY(nap::group::members, &Type::mMembers, nap::rtti::EPropertyMetaData::Embedded | nap::rtti::EPropertyMetaData::ReadOnly) \
229  RTTI_PROPERTY(nap::group::children, &Type::mChildren, nap::rtti::EPropertyMetaData::Embedded | nap::rtti::EPropertyMetaData::ReadOnly) \
230  RTTI_END_CLASS
231 
232 #define GET_DEFINE_GROUP_MACRO(_1,_2,NAME,...) NAME
233 
240 #define DEFINE_GROUP(...) GET_DEFINE_GROUP_MACRO(__VA_ARGS__, DEFINE_GROUP_2, DEFINE_GROUP_1)(__VA_ARGS__)
nap::IGroup
Definition: group.h:29
nap::group::members
constexpr const char * members
Default group members property name.
Definition: group.h:17
nap::Group::init
virtual bool init(utility::ErrorState &errorState) override
Definition: group.h:160
nap::group::children
constexpr const char * children
Default group children property name.
Definition: group.h:18
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::utility::ErrorState
Definition: errorstate.h:19
nap::IGroup::getMemberType
rtti::TypeInfo getMemberType() const
Definition: group.h:46
nap::Group::findObjectRecursive
rtti::ObjectPtr< T > findObjectRecursive(const std::string &id) const
Definition: group.h:181
nap::Group::Group
Group()
Definition: group.h:90
nap::Group::findObject
rtti::ObjectPtr< T > findObject(const std::string &id) const
Definition: group.h:171
nap::Group::mChildren
std::vector< rtti::ObjectPtr< Group< T > > > mChildren
Property: 'Children' The sub groups.
Definition: group.h:140
nap::IGroup::IGroup
IGroup(rtti::TypeInfo memberType, std::string &&membersName, std::string &&childrenName)
Definition: group.h:38
nap::IGroup::childrenPropertyName
const std::string & childrenPropertyName() const
Definition: group.h:66
nap
Definition: templateapp.h:17
nap::Group::mMembers
std::vector< rtti::ObjectPtr< T > > mMembers
Property: 'Members' The members that belong to this group.
Definition: group.h:139
nap::Resource
Definition: resource.h:19
nap::IGroup::membersPropertyName
const std::string & membersPropertyName() const
Definition: group.h:61
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::Group
Definition: group.h:83