Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
components.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/typeinfo>
9
12
13namespace component {
14
15class Value;
16
17} /* namespace component */
18
19namespace core {
20
24template < class Derived, class ComponentType, class ComponentName >
26public:
36 static bool available ( const Derived & object, const ComponentType & element );
37
46 static void valid ( const Derived & object, const ComponentType & element );
47};
48
49template < class Derived, class ComponentType, class ComponentCategory, class ComponentName >
51
58template < class Derived, class ComponentType, class ComponentName >
59class Component < Derived, ComponentType, component::Value, ComponentName > {
63 ComponentType m_data;
64
69 void checkSet ( const ComponentType & element ) {
70 ElementConstraint < Derived, ComponentType, ComponentName >::valid ( static_cast < const Derived & > ( * this ), element );
71
72 if ( ! ElementConstraint < Derived, ComponentType, ComponentName >::available ( static_cast < const Derived & > ( * this ), element ) ) {
73 std::string elementTypeName ( ext::to_string < ComponentName * > ( ) );
74 elementTypeName.back ( ) = ' ';
75 throw exception::CommonException ( elementTypeName + ext::to_string ( element ) + " is not available." );
76 }
77 }
78
79protected:
83 void checkState ( ) {
84 checkSet ( m_data );
85 }
86
87public:
92 Component ( ComponentType element ) : m_data ( std::move ( element ) ) {
93 }
94
102 bool set ( ComponentType element ) {
103 checkSet ( element );
104
105 if ( m_data == element ) return false;
106
107 m_data = std::move ( element );
108 return true;
109 }
110
115 ComponentType & get ( ) {
116 return m_data;
117 }
118
123 const ComponentType & get ( ) const {
124 return m_data;
125 }
126
132 template < class AccessedComponentName >
133 requires std::is_same_v < AccessedComponentName, ComponentName >
135 return * this;
136 }
137
143 template < class AccessedComponentName >
144 requires std::is_same_v < AccessedComponentName, ComponentName >
146 return * this;
147 }
148
152 static void registerComponent ( ) {
153 std::array < std::string, 0 > emptyNames;
154 std::array < std::string, 1 > elementNames = { { "element" } };
155
156 ComponentType & ( Derived::* getMethod ) ( ) = & Component < Derived, ComponentType, component::Value, ComponentName >::get;
157 abstraction::AlgorithmRegistry::registerMethod < ComponentName > ( getMethod, "get", emptyNames );
158
159 const ComponentType & ( Derived::* constGetMethod ) ( ) const = & Component < Derived, ComponentType, component::Value, ComponentName >::get;
160 abstraction::AlgorithmRegistry::registerMethod < ComponentName > ( constGetMethod, "get", emptyNames );
161
162 bool ( Derived::* setMethod ) ( ComponentType ) = & Component < Derived, ComponentType, component::Value, ComponentName >::set;
163 abstraction::AlgorithmRegistry::registerMethod < ComponentName > ( setMethod, "set", elementNames );
164 }
165
169 static void unregisterComponent ( ) {
170 abstraction::AlgorithmRegistry::unregisterMethod < ComponentName, Derived & > ( "get" );
171 abstraction::AlgorithmRegistry::unregisterMethod < ComponentName, const Derived & > ( "get" );
172 abstraction::AlgorithmRegistry::unregisterMethod < ComponentName, Derived &, ComponentType > ( "set" );
173 }
174
175};
176
180template < class ... Data >
182public:
186 template < class T >
187 requires ( ! std::is_same_v < T, T > )
189
193 static void registerComponent ( ) {
194 }
195
199 static void unregisterComponent ( ) {
200 }
201};
202
206template < class Derived, class ComponentType, class ComponentCategory, class ComponentName, class ... Next >
207class Components < Derived, ComponentType, ComponentCategory, ComponentName, Next ... > : public Component < Derived, ComponentType, ComponentCategory, ComponentName >, public Components < Derived, Next ... > {
208public:
212 Components ( ) = default;
213
217 template < class ComponentValue, class ... NextValues >
218 Components ( ComponentValue param, NextValues ... nextParams ) : Component < Derived, ComponentType, ComponentCategory, ComponentName > ( std::move ( param ) ), Components < Derived, Next ... > ( std::move ( nextParams ) ... ) {
220 }
221
226
230 using Components < Derived, Next ... >::accessComponent;
231
235 static void registerComponent ( ) {
237 Components < Derived, Next ... >::registerComponent ( );
238 }
239
243 static void unregisterComponent ( ) {
245 Components < Derived, Next ... >::unregisterComponent ( );
246 }
247};
248
252template < class Derived, class ComponentType, class ComponentCategory, class ComponentName, class ... ComponentNames, class ... Next >
253class Components < Derived, ComponentType, ComponentCategory, std::tuple < ComponentName, ComponentNames ... >, Next ... > : public Component < Derived, ComponentType, ComponentCategory, ComponentName >, public Components < Derived, ComponentType, ComponentCategory, std::tuple < ComponentNames ... >, Next ... > {
254public:
258 Components ( ) = default;
259
263 template < class ComponentValue, class ... NextValues >
264 Components ( ComponentValue param, NextValues ... nextParams ) : Component < Derived, ComponentType, ComponentCategory, ComponentName > ( std::move ( param ) ), Components < Derived, ComponentType, ComponentCategory, std::tuple < ComponentNames ... >, Next ... > ( std::move ( nextParams ) ... ) {
266 }
267
272
276 using Components < Derived, ComponentType, ComponentCategory, std::tuple < ComponentNames ... >, Next ... >::accessComponent;
277
281 static void registerComponent ( ) {
283 Components < Derived, ComponentType, ComponentCategory, std::tuple < ComponentNames ... >, Next ... >::registerComponent ( );
284 }
285
289 static void unregisterComponent ( ) {
291 Components < Derived, ComponentType, ComponentCategory, std::tuple < ComponentNames ... >, Next ... >::unregisterComponent ( );
292 }
293};
294
298template < class Derived, class ComponentType, class ComponentCategory, class ... Next >
299class Components < Derived, ComponentType, ComponentCategory, std::tuple < >, Next ... > : public Components < Derived, Next ... > {
300public:
304 Components ( ) = default;
305
309 template < class ... NextValues >
310 Components ( NextValues ... nextParams ) : Components < Derived, Next ... > ( std::move ( nextParams ) ... ) {
311 }
312
316 using Components < Derived, Next ... >::accessComponent;
317
321 static void registerComponent ( ) {
322 Components < Derived, Next ... >::registerComponent ( );
323 }
324
328 static void unregisterComponent ( ) {
329 Components < Derived, Next ... >::unregisterComponent ( );
330 }
331};
332
333} /* namespace core */
334
336
Component(ComponentType element)
Definition: components.hpp:92
const ComponentType & get() const
Definition: components.hpp:123
const Component< Derived, ComponentType, component::Value, ComponentName > & accessComponent() const
Definition: components.hpp:134
bool set(ComponentType element)
Definition: components.hpp:102
Definition: components.hpp:50
Components(ComponentValue param, NextValues ... nextParams)
Definition: components.hpp:218
Components(NextValues ... nextParams)
Definition: components.hpp:310
Components(ComponentValue param, NextValues ... nextParams)
Definition: components.hpp:264
Definition: components.hpp:181
static void unregisterComponent()
Definition: components.hpp:199
static void registerComponent()
Definition: components.hpp:193
void accessComponent()
Definition: components.hpp:25
static void valid(const Derived &object, const ComponentType &element)
static bool available(const Derived &object, const ComponentType &element)
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Definition: components.hpp:13
Definition: normalize.hpp:10
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
Definition: FordFulkerson.hpp:16