ComponentUtils.hpp
1 #pragma once
2 #include <score/model/Component.hpp>
3 
4 #include <ossia/detail/algorithms.hpp>
5 
6 namespace score
7 {
8 
19 template <typename T>
21 {
22  static_assert(T::is_unique, "Components must be unique to use getComponent");
23 
24  auto it
25  = ossia::find_if(c, [](auto& other) { return other.key_match(T::static_key()); });
26 
27  SCORE_ASSERT(it != c.end());
28  return static_cast<T&>(*it);
29 }
30 
38 template <typename T>
39 T* findComponent(const score::Components& c) noexcept
40 {
41  static_assert(T::is_unique, "Components must be unique to use getComponent");
42 
43  auto it
44  = ossia::find_if(c, [](auto& other) { return other->key_match(T::static_key()); });
45 
46  if(it != c.end())
47  {
48  return static_cast<T*>(*it);
49  }
50  else
51  {
52  return (T*)nullptr;
53  }
54 }
55 }
Base toolkit upon which the software is built.
Definition: Application.cpp:90
T * findComponent(const score::Components &c) noexcept
findComponent Tryies to fetch a Component from Components by type.
Definition: ComponentUtils.hpp:39
T & component(const score::Components &c)
component Fetch a Component from Components by type
Definition: ComponentUtils.hpp:20
Definition: lib/score/model/Component.hpp:41