Loading...
Searching...
No Matches
InterfaceList.hpp
1#ifndef SCORE_INTERFACELIST_2018_10_22
2#define SCORE_INTERFACELIST_2018_10_22
3#pragma once
4#include <score/plugins/Interface.hpp>
5#include <score/tools/Debug.hpp>
6#include <score/tools/std/IndirectContainer.hpp>
7#include <score/tools/std/Pointer.hpp>
8
9#include <ossia/detail/hash_map.hpp>
10
11#include <nano_signal_slot.hpp>
12#include <score_lib_base_export.h>
13
14namespace score
15{
29class SCORE_LIB_BASE_EXPORT InterfaceListBase
30{
31public:
32 static constexpr bool factory_list_tag = true;
33 InterfaceListBase() noexcept = default;
34 InterfaceListBase(const InterfaceListBase&) = delete;
36 InterfaceListBase& operator=(const InterfaceListBase&) = delete;
37 InterfaceListBase& operator=(InterfaceListBase&&) = delete;
38 virtual ~InterfaceListBase();
39
41 virtual score::InterfaceKey interfaceKey() const noexcept = 0;
42
48 virtual void insert(std::unique_ptr<score::InterfaceBase>) = 0;
49
55 virtual void optimize() noexcept = 0;
56};
57
58SCORE_LIB_BASE_EXPORT void
59debug_types(const score::InterfaceBase* orig, const score::InterfaceBase* repl) noexcept;
60
61class SCORE_LIB_BASE_EXPORT InterfaceListMain : public score::InterfaceListBase
62{
63public:
66 void insert_base(std::unique_ptr<score::InterfaceBase> e, score::uuid_t k);
67
68 mutable Nano::Signal<void(const score::InterfaceBase&)> added;
69
70 auto reserve(std::size_t v) { return map.reserve(v); }
71 auto size() const noexcept { return map.size(); }
72 auto empty() const noexcept { return map.empty(); }
73
74 void optimize() noexcept final override;
75 ossia::hash_map<score::uuid_t, std::unique_ptr<score::InterfaceBase>> map;
76};
77
95template <typename FactoryType>
97{
98public:
99 using factory_type = FactoryType;
100 using key_type = typename FactoryType::ConcreteKey;
102 InterfaceList() = default;
103 ~InterfaceList() = default;
104
105 static const constexpr score::InterfaceKey static_interfaceKey() noexcept
106 {
107 return FactoryType::static_interfaceKey();
108 }
109
110 constexpr score::InterfaceKey interfaceKey() const noexcept final override
111 {
112 return FactoryType::static_interfaceKey();
113 }
114
115 void insert(std::unique_ptr<score::InterfaceBase> e) final override
116 {
117 if(auto result = dynamic_cast<factory_type*>(e.get()))
118 {
119 return insert_base(std::move(e), result->concreteKey().impl());
120 }
121 else
122 {
123 SCORE_SOFT_ASSERT("Invalid interface detected");
124 }
125 }
126
128 FactoryType* get(const key_type& k) const noexcept
129 {
130 auto it = this->map.find(k.impl());
131 return (it != this->map.end()) ? static_cast<FactoryType*>(it->second.get())
132 : nullptr;
133 }
134
135 auto begin() noexcept
136 {
137 return make_indirect_cast_map_iterator<factory_type>(map.begin());
138 }
139 auto end() noexcept
140 {
141 return make_indirect_cast_map_iterator<factory_type>(map.end());
142 }
143 auto begin() const noexcept
144 {
145 return make_indirect_cast_map_iterator<factory_type>(map.begin());
146 }
147 auto end() const noexcept
148 {
149 return make_indirect_cast_map_iterator<factory_type>(map.end());
150 }
151
152 auto cbegin() const noexcept
153 {
154 return make_indirect_cast_map_iterator<factory_type>(map.cbegin());
155 }
156 auto cend() const noexcept
157 {
158 return make_indirect_cast_map_iterator<factory_type>(map.cend());
159 }
160
161private:
162 InterfaceList(const InterfaceList&) = delete;
163 InterfaceList(InterfaceList&&) = delete;
164 InterfaceList& operator=(const InterfaceList&) = delete;
165 InterfaceList& operator=(InterfaceList&&) = delete;
166};
167
171template <typename T>
173{
174public:
186 template <typename Fun, typename... Args>
187 requires std::is_invocable_v<
188 Fun, typename score::InterfaceList<T>::factory_type&, Args...>
189 auto make(Fun f, Args&&... args) const noexcept
190 {
191 using val_t = decltype(*this->begin());
192 for(const val_t& elt : *this)
193 {
194 if(elt.matches(std::forward<Args>(args)...))
195 {
196 return (elt.*f)(std::forward<Args>(args)...);
197 }
198 }
199 return decltype((std::declval<val_t>().*f)(std::forward<Args>(args)...)){};
200 }
201};
202}
203#endif
Definition UuidKey.hpp:345
Definition IndirectContainer.hpp:169
Base class for plug-in interfaces.
Definition Interface.hpp:52
InterfaceListBase Interface to access factories.
Definition InterfaceList.hpp:30
virtual score::InterfaceKey interfaceKey() const noexcept=0
A key that uniquely identifies this family of factories.
InterfaceList Default implementation of InterfaceListBase.
Definition InterfaceList.hpp:97
void insert(std::unique_ptr< score::InterfaceBase > e) final override
insert Register a new factory.
Definition InterfaceList.hpp:115
constexpr score::InterfaceKey interfaceKey() const noexcept final override
A key that uniquely identifies this family of factories.
Definition InterfaceList.hpp:110
FactoryType * get(const key_type &k) const noexcept
Get a particular factory from its ConcreteKey.
Definition InterfaceList.hpp:128
Definition InterfaceList.hpp:62
Utility class for making a factory interface list.
Definition InterfaceList.hpp:173
auto make(Fun f, Args &&... args) const noexcept
Apply a function on the correct factory according to a set of parameter.
Definition InterfaceList.hpp:189
Base toolkit upon which the software is built.
Definition Application.cpp:97
STL namespace.
Definition UuidKey.hpp:24