1 #ifndef SCORE_INTERFACELIST_2018_10_22
2 #define SCORE_INTERFACELIST_2018_10_22
4 #include <score/plugins/Interface.hpp>
5 #include <score/tools/std/IndirectContainer.hpp>
6 #include <score/tools/std/Pointer.hpp>
8 #include <ossia/detail/hash_map.hpp>
10 #include <nano_signal_slot.hpp>
11 #include <score_lib_base_export.h>
31 static constexpr
bool factory_list_tag =
true;
54 virtual
void optimize() noexcept = 0;
57 SCORE_LIB_BASE_EXPORT
void
76 template <typename FactoryType>
82 using factory_type = FactoryType;
83 using key_type =
typename FactoryType::ConcreteKey;
90 return FactoryType::static_interfaceKey();
95 return FactoryType::static_interfaceKey();
98 void insert(std::unique_ptr<score::InterfaceBase> e)
final override
100 if(
auto result =
dynamic_cast<factory_type*
>(e.get()))
103 std::unique_ptr<factory_type> pf{result};
104 vector_type::push_back(pf.get());
106 auto k = pf->concreteKey();
107 auto it = this->map.find(k);
108 if(it == this->map.end())
110 this->map.emplace(std::make_pair(k, std::move(pf)));
114 score::debug_types(it->second.get(), result);
115 it->second = std::move(pf);
118 added(vector_type::back());
123 FactoryType*
get(
const key_type& k)
const noexcept
125 auto it = this->map.find(k);
126 return (it != this->map.end()) ? it->second.get() :
nullptr;
129 mutable Nano::Signal<void(
const factory_type&)> added;
132 ossia::hash_map<typename FactoryType::ConcreteKey, std::unique_ptr<FactoryType>> map;
135 void optimize() noexcept final
override
138 this->map.max_load_factor(0.1f);
139 this->map.reserve(map.size());
142 InterfaceList(
const InterfaceList&) =
delete;
143 InterfaceList(InterfaceList&&) =
delete;
144 InterfaceList& operator=(
const InterfaceList&) =
delete;
145 InterfaceList& operator=(InterfaceList&&) =
delete;
151 template <
typename T>
166 template <
typename Fun,
typename... Args>
167 auto make(Fun f, Args&&... args)
const noexcept
169 using val_t = decltype(*this->begin());
170 for(
const val_t& elt : *
this)
172 if(elt.matches(std::forward<Args>(args)...))
174 return (elt.*f)(std::forward<Args>(args)...);
177 return decltype((std::declval<val_t>().*f)(std::forward<Args>(args)...)){};
Definition: UuidKey.hpp:343
Definition: IndirectContainer.hpp:129
Base class for plug-in interfaces.
Definition: Interface.hpp:52
InterfaceListBase Interface to access factories.
Definition: InterfaceList.hpp:29
virtual score::InterfaceKey interfaceKey() const noexcept=0
A key that uniquely identifies this family of factories.
InterfaceList Default implementation of InterfaceListBase.
Definition: InterfaceList.hpp:80
FactoryType * get(const key_type &k) const noexcept
Get a particular factory from its ConcreteKey.
Definition: InterfaceList.hpp:123
void insert(std::unique_ptr< score::InterfaceBase > e) final override
insert Register a new factory.
Definition: InterfaceList.hpp:98
constexpr score::InterfaceKey interfaceKey() const noexcept final override
A key that uniquely identifies this family of factories.
Definition: InterfaceList.hpp:93
Utility class for making a factory interface list.
Definition: InterfaceList.hpp:153
auto make(Fun f, Args &&... args) const noexcept
Apply a function on the correct factory according to a set of parameter.
Definition: InterfaceList.hpp:167
Base toolkit upon which the software is built.
Definition: Application.cpp:90