diff --git a/alib2common/src/core/components.hpp b/alib2common/src/core/components.hpp index 13771894e78d1c791b5aa42733e37306216cf32d..bf3b3bdba72af42aad4fc26454a234ca1e89b5c4 100644 --- a/alib2common/src/core/components.hpp +++ b/alib2common/src/core/components.hpp @@ -23,7 +23,7 @@ namespace std { * @param SetType arbitrary type used to distinguish different sets. */ template < class Derived, class SymbolType, class SetType > -class Set { +class Component { /** * The set. */ @@ -37,7 +37,7 @@ class Set { valid ( symbol ); if ( !available ( symbol ) ) - throw ::exception::CommonException ( "Symbol " + ( std::string ) symbol + " is not available." ); + throw::exception::CommonException ( "Symbol " + ( std::string ) symbol + " is not available." ); } /** @@ -46,7 +46,7 @@ class Set { */ void checkRemove ( const SymbolType & symbol ) { if ( used ( symbol ) ) - throw ::exception::CommonException ( "Symbol " + ( std::string ) symbol + " is used." ); + throw::exception::CommonException ( "Symbol " + ( std::string ) symbol + " is used." ); } protected: @@ -61,14 +61,14 @@ public: /* * Constructs an empty set. */ - Set ( ) { + Component ( ) { } /** * Constructs a set containing given elements. * @throw CommonException if elements are not available in context of datatype where the set is used */ - Set ( std::set < SymbolType > symbols ) : data ( std::move ( symbols ) ) { + Component ( std::set < SymbolType > symbols ) : data ( std::move ( symbols ) ) { } /** @@ -147,7 +147,7 @@ public: } /** - * Set emptiness checker. + * Component emptiness checker. * @return true if set is an empty */ bool empty ( ) const { @@ -284,19 +284,20 @@ public: * Auxiliary base handling all sets of elements from components */ template < class Derived, class SymbolType, class ... SetTypes > -struct SetAux; +struct ComponentAux; /** * Specialisation for tuple. */ template < class Derived, class SymbolType, class ... SetTypes > -struct SetAux < Derived, SymbolType, tuple < SetTypes ... > > : public Set < Derived, SymbolType, SetTypes > ... { +struct ComponentAux < Derived, SymbolType, tuple < SetTypes ... > > : public Component < Derived, SymbolType, SetTypes > ... { /** * Constructor */ template < class ... RealSetTypes, size_t ... Indexes > - SetAux ( tuple < RealSetTypes ... > params, std::index_sequence < Indexes ... > ) : Set < Derived, SymbolType, SetTypes > ( std::move ( get < Indexes > ( params ) ) ) ... { + ComponentAux ( tuple < RealSetTypes ... > params, std::index_sequence < Indexes ... > ) : Component < Derived, SymbolType, SetTypes > ( std::move ( get < Indexes > ( params ) ) ) ... { + ( void ) params; // No-op } /** @@ -305,8 +306,8 @@ struct SetAux < Derived, SymbolType, tuple < SetTypes ... > > : public Set < Der * @return sub-component */ template < class SetType > - const Set < Derived, SymbolType, SetType > & accessSet ( ) const { - return static_cast < const Set < Derived, SymbolType, SetType > & > ( * this ); + const Component < Derived, SymbolType, SetType > & accessComponent ( ) const { + return static_cast < const Component < Derived, SymbolType, SetType > & > ( * this ); } /** @@ -315,8 +316,8 @@ struct SetAux < Derived, SymbolType, tuple < SetTypes ... > > : public Set < Der * @return sub-component */ template < class SetType > - Set < Derived, SymbolType, SetType > & accessSet ( ) { - return static_cast < Set < Derived, SymbolType, SetType > & > ( * this ); + Component < Derived, SymbolType, SetType > & accessComponent ( ) { + return static_cast < Component < Derived, SymbolType, SetType > & > ( * this ); } protected: @@ -324,7 +325,7 @@ protected: * postponed checker function */ void checkState ( ) { - std::initializer_list < int > { ( Set < Derived, SymbolType, SetTypes >::checkState ( ), 0 ) ... }; + std::initializer_list < int > { ( Component < Derived, SymbolType, SetTypes >::checkState ( ), 0 ) ... }; } }; @@ -346,6 +347,7 @@ struct ElementAux < Derived, SymbolType, tuple < ElementTypes ... > > : public E */ template < class ... RealElementTypes, size_t ... Indexes > ElementAux ( tuple < RealElementTypes ... > params, std::index_sequence < Indexes ... > ) : Element < Derived, SymbolType, ElementTypes > ( std::move ( get < Indexes > ( params ) ) ) ... { + ( void ) params; // No-op } /** @@ -378,18 +380,38 @@ protected: }; +template < class ... Types > +class Components; + /** * Auxiliary class allowing simple access to the alphabets. */ template < class Derived, class SymbolType, class SetTypesPack, class ElementTypesPack > -class Components : public SetAux < Derived, SymbolType, SetTypesPack >, public ElementAux < Derived, SymbolType, ElementTypesPack > { +class Components < Derived, SymbolType, SetTypesPack, ElementTypesPack > : public ComponentAux < Derived, SymbolType, SetTypesPack >, public ElementAux < Derived, SymbolType, ElementTypesPack > { +public: + /** + * Construct an alphabet pack from two alphabets. + */ + template < class RealSetTypes, class RealElementTypes > + Components ( RealSetTypes params1, RealElementTypes params2 ) : ComponentAux < Derived, SymbolType, SetTypesPack > ( std::move ( params1 ), std::make_index_sequence < std::tuple_size < typename std::decay < SetTypesPack >::type >::value > { } ), ElementAux < Derived, SymbolType, ElementTypesPack > ( std::move ( params2 ), std::make_index_sequence < std::tuple_size < typename std::decay < ElementTypesPack >::type >::value > { } ) { + ComponentAux < Derived, SymbolType, SetTypesPack >::checkState ( ); + + ElementAux < Derived, SymbolType, ElementTypesPack >::checkState ( ); + } +}; + +/** + * Auxiliary class allowing simple access to the alphabets. + */ +template < class Derived, class SymbolType, class SetTypesPack, class ElementTypesPack, class ... NextGroup > +class Components < Derived, SymbolType, SetTypesPack, ElementTypesPack, NextGroup ... > : public ComponentAux < Derived, SymbolType, SetTypesPack >, public ElementAux < Derived, SymbolType, ElementTypesPack >, public Components < Derived, NextGroup ... > { public: /** * Construct an alphabet pack from two alphabets. */ template < class RealSetTypes, class RealElementTypes > - Components ( RealSetTypes params1, RealElementTypes params2 ) : SetAux < Derived, SymbolType, SetTypesPack > ( std::move ( params1 ), std::make_index_sequence < std::tuple_size < typename std::decay < SetTypesPack >::type >::value > { } ), ElementAux < Derived, SymbolType, ElementTypesPack > ( std::move ( params2 ), std::make_index_sequence < std::tuple_size < typename std::decay < ElementTypesPack >::type >::value > { } ) { - SetAux < Derived, SymbolType, SetTypesPack >::checkState ( ); + Components ( RealSetTypes params1, RealElementTypes params2 ) : ComponentAux < Derived, SymbolType, SetTypesPack > ( std::move ( params1 ), std::make_index_sequence < std::tuple_size < typename std::decay < SetTypesPack >::type >::value > { } ), ElementAux < Derived, SymbolType, ElementTypesPack > ( std::move ( params2 ), std::make_index_sequence < std::tuple_size < typename std::decay < ElementTypesPack >::type >::value > { } ) { + ComponentAux < Derived, SymbolType, SetTypesPack >::checkState ( ); ElementAux < Derived, SymbolType, ElementTypesPack >::checkState ( ); } diff --git a/alib2common/test-src/core/ComponentsTest.cpp b/alib2common/test-src/core/ComponentsTest.cpp index 63e60e0a87ffccba58891fe78543926b1f2142ad..71f367a1d9d2b6dc40e6b3c1e8fcd7cf4efa6a10 100644 --- a/alib2common/test-src/core/ComponentsTest.cpp +++ b/alib2common/test-src/core/ComponentsTest.cpp @@ -22,43 +22,43 @@ public: namespace std { template < > -bool A::Set < A, std::string, GeneralAlphabet >::used ( const std::string & string ) const { - return static_cast < const A * > ( this )->accessSet < NonlinearAlphabet > ( ).get ( ).count ( string ) || static_cast < const A * > ( this )->accessElement < SubtreeWildcard > ( ).get ( ) == string; +bool A::Component < A, std::string, GeneralAlphabet >::used ( const std::string & string ) const { + return static_cast < const A * > ( this )->accessComponent < NonlinearAlphabet > ( ).get ( ).count ( string ) || static_cast < const A * > ( this )->accessElement < SubtreeWildcard > ( ).get ( ) == string; } template < > -bool A::Set < A, std::string, NonlinearAlphabet >::used ( const std::string & ) const { +bool A::Component < A, std::string, NonlinearAlphabet >::used ( const std::string & ) const { return false; } template < > -bool A::Set < A, std::string, GeneralAlphabet >::available ( const std::string & ) const { +bool A::Component < A, std::string, GeneralAlphabet >::available ( const std::string & ) const { return true; } template < > -bool A::Set < A, std::string, NonlinearAlphabet >::available ( const std::string & string ) const { - return static_cast < const A * > ( this )->accessSet < GeneralAlphabet > ( ).get ( ).count ( string ); +bool A::Component < A, std::string, NonlinearAlphabet >::available ( const std::string & string ) const { + return static_cast < const A * > ( this )->accessComponent < GeneralAlphabet > ( ).get ( ).count ( string ); } template < > bool A::Element < A, std::string, SubtreeWildcard >::available ( const std::string & string ) const { - return static_cast < const A * > ( this )->accessSet < GeneralAlphabet > ( ).get ( ).count ( string ); + return static_cast < const A * > ( this )->accessComponent < GeneralAlphabet > ( ).get ( ).count ( string ); } template < > -void A::Set < A, std::string, GeneralAlphabet >::valid ( const std::string & ) const { +void A::Component < A, std::string, GeneralAlphabet >::valid ( const std::string & ) const { } template < > -void A::Set < A, std::string, NonlinearAlphabet >::valid ( const std::string & string ) const { +void A::Component < A, std::string, NonlinearAlphabet >::valid ( const std::string & string ) const { if ( static_cast < const A * > ( this )->accessElement < SubtreeWildcard > ( ).get ( ) == string ) throw::exception::CommonException ( "Symbol " + ( std::string ) string + "cannot be set as nonlinear variable since it is already a subtree wildcard" ); } template < > void A::Element < A, std::string, SubtreeWildcard >::valid ( const std::string & string ) const { - if ( static_cast < const A * > ( this )->accessSet < NonlinearAlphabet > ( ).get ( ).count ( string ) ) + if ( static_cast < const A * > ( this )->accessComponent < NonlinearAlphabet > ( ).get ( ).count ( string ) ) throw::exception::CommonException ( "Symbol " + ( std::string ) string + "cannot be set as subtree wildcard since it is already a nonlinear variable" ); } @@ -70,31 +70,31 @@ class B : public std::Components < A, std::string, std::tuple < GeneralAlphabet, namespace std { template < > -bool B::Set < B, std::string, GeneralAlphabet >::used ( const std::string & ) const { +bool B::Component < B, std::string, GeneralAlphabet >::used ( const std::string & ) const { return false; } template < > -bool B::Set < B, std::string, NonlinearAlphabet >::used ( const std::string & ) const { +bool B::Component < B, std::string, NonlinearAlphabet >::used ( const std::string & ) const { return false; } template < > -bool B::Set < B, std::string, GeneralAlphabet >::available ( const std::string & ) const { +bool B::Component < B, std::string, GeneralAlphabet >::available ( const std::string & ) const { return true; } template < > -bool B::Set < B, std::string, NonlinearAlphabet >::available ( const std::string & ) const { +bool B::Component < B, std::string, NonlinearAlphabet >::available ( const std::string & ) const { return true; } template < > -void B::Set < B, std::string, GeneralAlphabet >::valid ( const std::string & ) const { +void B::Component < B, std::string, GeneralAlphabet >::valid ( const std::string & ) const { } template < > -void B::Set < B, std::string, NonlinearAlphabet >::valid ( const std::string & ) const { +void B::Component < B, std::string, NonlinearAlphabet >::valid ( const std::string & ) const { } } @@ -108,17 +108,17 @@ void ComponentsTest::tearDown ( ) { void ComponentsTest::testAdd ( ) { A tmp ( "2" ); - CPPUNIT_ASSERT_THROW ( tmp.accessSet < NonlinearAlphabet > ( ).add ( "1" ), exception::CommonException ); - tmp.accessSet < GeneralAlphabet > ( ).add ( "1" ); - tmp.accessSet < NonlinearAlphabet > ( ).add ( "1" ); + CPPUNIT_ASSERT_THROW ( tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ), exception::CommonException ); + tmp.accessComponent < GeneralAlphabet > ( ).add ( "1" ); + tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ); } void ComponentsTest::testRemove ( ) { A tmp ( "2" ); - tmp.accessSet < GeneralAlphabet > ( ).add ( "1" ); - tmp.accessSet < NonlinearAlphabet > ( ).add ( "1" ); - CPPUNIT_ASSERT_THROW ( tmp.accessSet < GeneralAlphabet > ( ).remove ( "1" ), exception::CommonException ); - tmp.accessSet < NonlinearAlphabet > ( ).remove ( "1" ); - tmp.accessSet < GeneralAlphabet > ( ).remove ( "1" ); + tmp.accessComponent < GeneralAlphabet > ( ).add ( "1" ); + tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ); + CPPUNIT_ASSERT_THROW ( tmp.accessComponent < GeneralAlphabet > ( ).remove ( "1" ), exception::CommonException ); + tmp.accessComponent < NonlinearAlphabet > ( ).remove ( "1" ); + tmp.accessComponent < GeneralAlphabet > ( ).remove ( "1" ); }