2 #include <score/application/ApplicationComponents.hpp>
3 #include <score/model/ComponentSerialization.hpp>
4 #include <score/tools/IdentifierGeneration.hpp>
6 #include <ossia/detail/algorithms.hpp>
8 #include <nano_observer.hpp>
25 template <
typename ParentComponent_T,
typename ChildModel_T,
typename ChildComponent_T>
27 :
public ParentComponent_T
28 ,
public Nano::Observer
35 ChildPair(ChildModel_T* m, ChildComponent_T* c)
40 ChildModel_T* model{};
41 ChildComponent_T* component{};
44 template <
typename... Args>
46 : ParentComponent_T{std::forward<Args>(args)...}
51 template <
typename... Args>
53 : ParentComponent_T{std::forward<Args>(args)...}
59 auto& child_models = ParentComponent_T::template models<ChildModel_T>();
60 for(
auto& child_model : child_models)
65 child_models.mutable_added.template connect<&hierarchy_t::add>(
this);
67 child_models.removing.template connect<&hierarchy_t::remove>(
this);
70 const auto& children()
const {
return m_children; }
72 #if defined(SCORE_SERIALIZABLE_COMPONENTS)
73 void add(ChildModel_T& element)
75 add(element,
typename score::is_component_serializable<ChildComponent_T>::type{});
78 void add(ChildModel_T& element, score::serializable_tag)
82 auto comp = score::deserialize_component<ChildComponent_T>(
83 element.components(), [&](
auto&& deserializer) {
84 ParentComponent_T::template load<ChildComponent_T>(deserializer, element);
90 comp = ParentComponent_T::template make<ChildComponent_T>(
91 getStrongId(element.components()), element);
97 element.components().add(comp);
98 m_children.emplace_back(ChildPair{&element, comp});
101 void add(ChildModel_T& model, score::not_serializable_tag)
103 void add(ChildModel_T& model)
108 auto proc_comp = ParentComponent_T::make(getStrongId(model.components()), model);
111 model.components().add(proc_comp);
112 m_children.emplace_back(ChildPair{&model, proc_comp});
116 void remove(
const ChildModel_T& model)
119 = ossia::find_if(m_children, [&](
auto pair) {
return pair.model == &model; });
121 if(it != m_children.end())
124 m_children.erase(it);
128 void cleanup(
const ChildPair& pair)
130 ParentComponent_T::removing(*pair.model, *pair.component);
131 pair.model->components().remove(*pair.component);
136 for(
const auto& element : m_children)
143 ~ComponentHierarchyManager() { clear(); }
146 std::vector<ChildPair> m_children;
160 typename ParentComponent_T,
typename ChildModel_T,
typename ChildComponent_T,
161 typename ChildComponentFactoryList_T,
bool HasOwnership =
true>
163 :
public ParentComponent_T
164 ,
public Nano::Observer
171 ChildPair(ChildModel_T* m, ChildComponent_T* c)
176 ChildModel_T* model{};
177 ChildComponent_T* component{};
180 template <
typename... Args>
182 : ParentComponent_T{std::forward<Args>(args)...}
183 , m_componentFactory{
184 score::AppComponents().template interfaces<ChildComponentFactoryList_T>()}
189 template <
typename... Args>
191 : ParentComponent_T{std::forward<Args>(args)...}
192 , m_componentFactory{
193 score::AppComponents().template interfaces<ChildComponentFactoryList_T>()}
197 void init_hierarchy()
199 auto& child_models = ParentComponent_T::template models<ChildModel_T>();
200 for(
auto& child_model : child_models)
205 child_models.mutable_added.template connect<&hierarchy_t::add>(
this);
207 child_models.removing.template connect<&hierarchy_t::remove>(
this);
209 const auto& children()
const {
return m_children; }
211 void add(ChildModel_T& element)
213 #if defined(SCORE_SERIALIZABLE_COMPONENTS)
215 element,
typename score::is_component_serializable<ChildComponent_T>::type{});
220 void remove(
const ChildModel_T& model)
223 = ossia::find_if(m_children, [&](
auto pair) {
return pair.model == &model; });
225 if(it != m_children.end())
228 m_children.erase(it);
234 for(
const auto& element : m_children)
241 ~PolymorphicComponentHierarchyManager() { clear(); }
244 #if defined(SCORE_SERIALIZABLE_COMPONENTS)
246 template <
typename TheChild>
247 void add_impl(TheChild& model, score::serializable_tag)
250 if(
auto factory = m_componentFactory.factory(model))
254 ChildComponent_T* comp = score::deserialize_component<ChildComponent_T>(
255 model.components(), [&](
auto&& deserializer) {
256 ParentComponent_T::template load<ChildComponent_T>(
257 deserializer, *factory, model);
263 comp = ParentComponent_T::template make<ChildComponent_T>(
264 getStrongId(model.components()), *factory, model);
270 model.components().add(comp);
271 m_children.emplace_back(ChildPair{&model, comp});
272 ParentComponent_T::added(*comp);
277 template <
typename TheChild>
278 void add_impl(TheChild& model, score::not_serializable_tag)
280 template <
typename TheChild>
281 void add_impl(TheChild& model)
285 if(
auto factory = m_componentFactory.factory(model))
289 auto comp = ParentComponent_T::make(*factory, model);
292 model.components().push_back(comp);
293 m_children.emplace_back(ChildPair{&model, comp});
294 ParentComponent_T::added(*comp);
299 auto comp = ParentComponent_T::make(model);
302 model.components().push_back(comp);
303 m_children.emplace_back(ChildPair{&model, comp});
304 ParentComponent_T::added(*comp);
309 void do_cleanup(
const ChildPair& pair)
311 if constexpr(HasOwnership)
313 ParentComponent_T::removing(*pair.model, *pair.component);
314 pair.model->components().remove(*pair.component);
318 auto t = ParentComponent_T::removing(*pair.model, *pair.component);
319 pair.model->components().erase(*pair.component);
320 ParentComponent_T::removed(*pair.model, *pair.component, std::move(t));
324 const ChildComponentFactoryList_T& m_componentFactory;
326 std::vector<ChildPair> m_children;
330 template <
typename Component>
331 using ComponentHierarchy = ComponentHierarchyManager<
332 Component,
typename Component::model_t,
typename Component::component_t>;
334 template <
typename Component,
bool HasOwnership = true>
335 using PolymorphicComponentHierarchy = PolymorphicComponentHierarchyManager<
336 Component,
typename Component::model_t,
typename Component::component_t,
337 typename Component::component_factory_list_t, HasOwnership>;
Manages simple hierarchies of components.
Definition: ComponentHierarchy.hpp:29
Manages polymorphic hierarchies of components.
Definition: ComponentHierarchy.hpp:165
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: ComponentHierarchy.hpp:34
Definition: ComponentHierarchy.hpp:170
Definition: lib/score/model/Component.hpp:16