diff --git a/alib2common/src/object/AnyObject.h b/alib2common/src/object/AnyObject.h index 52ee7a2d6a6035cd1aa2c3cf781e9f20e211464d..00a70ed6bdb9b31fdec751f15e388f56a65bb50e 100644 --- a/alib2common/src/object/AnyObject.h +++ b/alib2common/src/object/AnyObject.h @@ -85,7 +85,7 @@ void AnyObject < T >::operator>>(std::ostream& out) const { template < class T > AnyObject < T > ::operator std::string () const { - return "V"; + return ext::to_string ( m_data ); } template < class T > diff --git a/alib2common/src/object/ObjectFactory.cpp b/alib2common/src/object/ObjectFactory.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3722c01f926e159cc661ff875711a45155852006 --- /dev/null +++ b/alib2common/src/object/ObjectFactory.cpp @@ -0,0 +1,46 @@ +/* + * ObjectFactory.cpp + * + * Created on: Mar 16, 2018 + * Author: Jan Travnicek + */ + +#include "ObjectFactory.h" +#include <primitive/Bool.h> +#include <primitive/Character.h> +#include <primitive/Double.h> +#include <primitive/Integer.h> +#include <primitive/String.h> +#include <primitive/Unsigned.h> + +namespace object { + +Object ObjectFactory::make ( bool boolean ) { + return Object ( primitive::Bool ( boolean ) ); +} + +Object ObjectFactory::make ( char character ) { + return Object ( primitive::Character ( character ) ); +} + +Object ObjectFactory::make ( double number ) { + return Object ( primitive::Double ( number ) ); +} + +Object ObjectFactory::make ( int number ) { + return Object ( primitive::Integer ( number ) ); +} + +Object ObjectFactory::make ( std::string string ) { + return Object ( primitive::String ( std::move ( string ) ) ); +} + +Object ObjectFactory::make ( const char * string ) { + return make ( ( std::string ) string ); +} + +Object ObjectFactory::make ( unsigned number ) { + return Object ( primitive::Unsigned ( number ) ); +} + +} /* namespace object */ diff --git a/alib2common/src/object/ObjectFactory.h b/alib2common/src/object/ObjectFactory.h new file mode 100644 index 0000000000000000000000000000000000000000..749a83dc8574df16fe29091ad4ebab7f6dfd0b9c --- /dev/null +++ b/alib2common/src/object/ObjectFactory.h @@ -0,0 +1,62 @@ +/* + * ObjectFactory.h + * + * Created on: Mar 16, 2018 + * Author: Jan Travnicek + */ + +#ifndef _OBJECT_FACTORY_H__ +#define _OBJECT_FACTORY_H__ + +#include "Object.h" +#include <alib/variant> +#include <object/AnyObject.h> + +namespace object { + +class ObjectFactory { + template < class Variant, class Type, class ... Types > + static Object processVariant ( Variant && data ) { + if ( data.template is < Type > ( ) ) { + return make ( std::move ( data.template get < Type > ( ) ) ); + } else { + return processVariant < Variant, Types ... > ( std::move ( data ) ); + } + } + + template < class Variant > + static Object processVariant ( Variant && ) { + throw std::logic_error ( "Variant state error" ); + } + +public: + static Object make ( bool boolean ); + static Object make ( char character ); + static Object make ( double number ); + static Object make ( int number ); + static Object make ( std::string string ); + static Object make ( const char * string ); + static Object make ( unsigned number ); + + static Object make ( Object other ) { + return other; + } + + template < class Type, typename std::enable_if < ! std::is_convertible < Type *, object::ObjectBase * >::value, Type >::type * = nullptr > + static Object make ( Type && data ) { + return Object ( object::AnyObject < Type > ( std::move ( data ) ) ); + } + + static Object make ( object::ObjectBase && data ) { + return Object ( std::move ( data ) ); + } + + template < class ... Types > + static Object make ( ext::variant < Types ... > && data ) { + return processVariant < ext::variant < Types ... >, Types ... > ( std::move ( data ) ); + } +}; + +} /* namespace object */ + +#endif /* _OBJECT_FACTORY_H__ */ diff --git a/alib2common/test-src/object/ObjectTest.cpp b/alib2common/test-src/object/ObjectTest.cpp index 557fe6ae838ab4ff3b6bea4a4ab46d57906c85bd..d9436dff0851a284547d52246c64aac3b783c163 100644 --- a/alib2common/test-src/object/ObjectTest.cpp +++ b/alib2common/test-src/object/ObjectTest.cpp @@ -1,11 +1,13 @@ #include <alib/list> +#include <alib/type_traits> +#include <alib/set> + #include "ObjectTest.h" #include "object/Object.h" +#include "object/ObjectFactory.h" #include <primitive/Unsigned.h> -#include <alib/type_traits> - CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ObjectTest, "object" ); CPPUNIT_TEST_SUITE_REGISTRATION ( ObjectTest ); @@ -28,3 +30,21 @@ void ObjectTest::testProperties ( ) { CPPUNIT_ASSERT ( tmp2 == object::Object ( primitive::Unsigned ( 1 ) ) ); } + +void ObjectTest::testConstruction ( ) { + object::Object tmp1 = object::ObjectFactory::make ( 1 ); + object::Object tmp2 = object::ObjectFactory::make ( ext::variant < int, std::string > ( 1 ) ); + object::Object tmp3 = object::ObjectFactory::make ( ext::variant < int, ext::variant < std::string, int > > ( ext::variant < std::string, int > ( 1 ) ) ); + + CPPUNIT_ASSERT ( tmp1 == tmp2 ); + CPPUNIT_ASSERT ( tmp1 == tmp3 ); + + object::Object tmp4 = object::Object ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ); + object::Object tmp5 = object::ObjectFactory::make ( ext::set < int > { } ); + object::Object tmp6 = object::ObjectFactory::make ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ); + + std::cout << tmp6 << std::endl; + + CPPUNIT_ASSERT ( tmp4 == tmp5 ); + CPPUNIT_ASSERT ( tmp4 == tmp6 ); +} diff --git a/alib2common/test-src/object/ObjectTest.h b/alib2common/test-src/object/ObjectTest.h index 33a46860207994de1fd5dbdd3690e298120d56a1..31cb9efefdf79a23423ab5ae12372b7b357f28d5 100644 --- a/alib2common/test-src/object/ObjectTest.h +++ b/alib2common/test-src/object/ObjectTest.h @@ -6,6 +6,7 @@ class ObjectTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE ( ObjectTest ); CPPUNIT_TEST ( testProperties ); + CPPUNIT_TEST ( testConstruction ); CPPUNIT_TEST_SUITE_END ( ); public: @@ -13,6 +14,7 @@ public: void tearDown ( ); void testProperties ( ); + void testConstruction ( ); }; #endif // OBJECT_TEST_H_ diff --git a/alib2data/src/PrimitiveRegistrator.cpp b/alib2data/src/PrimitiveRegistrator.cpp index f75e9fbcf03cf8451d37b6a937153f5321996e8f..d335bdd894962cb9e9ada14ec06873a36448ded1 100644 --- a/alib2data/src/PrimitiveRegistrator.cpp +++ b/alib2data/src/PrimitiveRegistrator.cpp @@ -67,7 +67,7 @@ public: abstraction::XmlComposerRegistry::registerXmlComposer < ext::set < string::LinearString < > > > ( ); - core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < object::Object, primitive::Unsigned > > > > > ( ); + core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::set < common::ranked_symbol < object::Object, primitive::Unsigned > > > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::set < ext::pair < object::Object, object::Object > > > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::pair < ext::set < ext::pair < object::Object, object::Object > >, object::Object > > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::pair < ext::set < ext::pair < object::Object, object::Object > >, ext::variant < string::Epsilon < object::Object >, object::Object > > > > ( ); diff --git a/alib2data/src/alphabet/common/SymbolNormalize.h b/alib2data/src/alphabet/common/SymbolNormalize.h index 5f9b2f1e1b9c00eaea1bc42f8f8370e0634de1e8..671dc3642f5078612f713f25e5d317a9b2e04154 100644 --- a/alib2data/src/alphabet/common/SymbolNormalize.h +++ b/alib2data/src/alphabet/common/SymbolNormalize.h @@ -13,7 +13,7 @@ #include <common/ranked_symbol.hpp> -#include <object/AnyObject.h> +#include <object/ObjectFactory.h> namespace alphabet { @@ -21,12 +21,6 @@ namespace alphabet { * This class contains methods to print XML representation of automata to the output stream. */ class SymbolNormalize { - template < class SymbolType > - static DefaultSymbolType normalizeSymbolInternal ( typename std::enable_if < std::is_constructible < DefaultSymbolType, SymbolType >::value, SymbolType && >::type symbol ); - - template < class SymbolType > - static DefaultSymbolType normalizeSymbolInternal ( typename std::enable_if < ! std::is_constructible < DefaultSymbolType, SymbolType >::value, SymbolType && >::type symbol ); - public: template < class SymbolType > static ext::set < DefaultSymbolType > normalizeAlphabet ( ext::set < SymbolType > && symbols ); @@ -65,19 +59,9 @@ ext::set < common::ranked_symbol < DefaultSymbolType, DefaultRankType > > Symbol return res; } -template < class SymbolType > -DefaultSymbolType SymbolNormalize::normalizeSymbolInternal ( typename std::enable_if < std::is_constructible < DefaultSymbolType, SymbolType >::value, SymbolType && >::type symbol ) { - return DefaultSymbolType ( std::move ( symbol ) ); -} - -template < class SymbolType > -DefaultSymbolType SymbolNormalize::normalizeSymbolInternal ( typename std::enable_if < ! std::is_constructible < DefaultSymbolType, SymbolType >::value, SymbolType && >::type symbol ) { - return DefaultSymbolType ( object::AnyObject < SymbolType > ( std::move ( symbol ) ) ); -} - template < class SymbolType > DefaultSymbolType SymbolNormalize::normalizeSymbol ( SymbolType && symbol ) { - return SymbolNormalize::normalizeSymbolInternal < SymbolType > ( std::move ( symbol ) ); + return object::ObjectFactory::make ( std::move ( symbol ) ); } template < class SymbolType, class RankType > diff --git a/alib2data/src/automaton/common/AutomatonNormalize.h b/alib2data/src/automaton/common/AutomatonNormalize.h index 76f39bf10f2808fc6aacc200942b2cd8f8e87810..b6a3f8f84449b222d81e536b9774aaa94b069281 100644 --- a/alib2data/src/automaton/common/AutomatonNormalize.h +++ b/alib2data/src/automaton/common/AutomatonNormalize.h @@ -17,6 +17,7 @@ #include <regexp/unbounded/UnboundedRegExpStructure.h> #include <alphabet/RankedSymbol.h> #include <alphabet/common/SymbolNormalize.h> +#include <object/ObjectFactory.h> namespace automaton { @@ -24,12 +25,6 @@ namespace automaton { * This class contains methods to print XML representation of automata to the output stream. */ class AutomatonNormalize { - template < class StateType > - static DefaultStateType normalizeStateInternal ( typename std::enable_if < std::is_constructible < DefaultStateType, StateType >::value, StateType && >::type symbol ); - - template < class StateType > - static DefaultStateType normalizeStateInternal ( typename std::enable_if < ! std::is_constructible < DefaultStateType, StateType >::value, StateType && >::type symbol ); - public: template < class StateType > static ext::set < DefaultStateType > normalizeStates ( ext::set < StateType > && states ); @@ -66,19 +61,9 @@ ext::vector < DefaultStateType > AutomatonNormalize::normalizeStates ( ext::vect return res; } -template < class StateType > -DefaultStateType AutomatonNormalize::normalizeStateInternal ( typename std::enable_if < std::is_constructible < DefaultStateType, StateType >::value, StateType && >::type state ) { - return DefaultStateType ( std::move ( state ) ); -} - -template < class StateType > -DefaultStateType AutomatonNormalize::normalizeStateInternal ( typename std::enable_if < ! std::is_constructible < DefaultStateType, StateType >::value, StateType && >::type state ) { - return DefaultStateType ( object::AnyObject < StateType > ( std::move ( state ) ) ); -} - template < class StateType > DefaultStateType AutomatonNormalize::normalizeState ( StateType && state) { - return AutomatonNormalize::normalizeStateInternal < StateType > ( std::move ( state ) ); + return object::ObjectFactory::make ( std::move ( state ) ); } template < class EpsilonType, class SymbolType > diff --git a/alib2data/src/regexp/RegExp.cpp b/alib2data/src/regexp/RegExp.cpp index 177c33fce1656d3460ce7dffd67255237a89f559..db4c7300f087750520ae9c1913f08ba2909fa67e 100644 --- a/alib2data/src/regexp/RegExp.cpp +++ b/alib2data/src/regexp/RegExp.cpp @@ -6,45 +6,15 @@ */ #include "RegExp.h" -#include <common/DefaultSymbolType.h> -#include "../string/LinearString.h" -#include "unbounded/UnboundedRegExpSymbol.h" -#include "unbounded/UnboundedRegExpEmpty.h" -#include "unbounded/UnboundedRegExpConcatenation.h" - -#include "unbounded/UnboundedRegExp.h" -#include "unbounded/UnboundedRegExpStructure.h" namespace regexp { regexp::RegExp regexpFrom ( const std::string & string ) { - return regexpFrom ( string::LinearString < > ( string ) ); + return regexpFrom ( string::LinearString < char > ( string ) ); } regexp::RegExp regexpFrom ( const char * string ) { - return regexpFrom ( string::LinearString < > ( string ) ); -} - -regexp::RegExp regexpFrom ( ext::vector < DefaultSymbolType > string ) { - regexp::UnboundedRegExpConcatenation < DefaultSymbolType > con; - - for ( auto & symbol : string ) - con.appendElement ( regexp::UnboundedRegExpSymbol < DefaultSymbolType > ( symbol ) ); - - return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < DefaultSymbolType > ( std::move ( con ) ) ) }; -} - -regexp::RegExp regexpFrom ( string::LinearString < > string ) { - regexp::UnboundedRegExpConcatenation < DefaultSymbolType > con; - - for ( auto & symbol : string.getContent ( ) ) - con.appendElement ( regexp::UnboundedRegExpSymbol < DefaultSymbolType > ( symbol ) ); - - return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < DefaultSymbolType > ( std::move ( con ) ) ) }; -} - -regexp::RegExp regexpFrom ( DefaultSymbolType symbol ) { - return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < DefaultSymbolType > ( regexp::UnboundedRegExpSymbol < DefaultSymbolType > ( std::move ( symbol ) ) ) ) }; + return regexpFrom ( std::string ( string ) ); } regexp::RegExp regexpFrom ( ) { diff --git a/alib2data/src/regexp/RegExp.h b/alib2data/src/regexp/RegExp.h index 16b97c50ba6781d07eec9e8d8944d59cbb208dc7..06d6194a58d6ac413fd8357d7ca338532e18c3de 100644 --- a/alib2data/src/regexp/RegExp.h +++ b/alib2data/src/regexp/RegExp.h @@ -12,9 +12,18 @@ #include "RegExpBase.h" #include <common/DefaultSymbolType.h> -#include <string/StringFeatures.h> #include <alib/string> #include <alib/vector> +#include <object/ObjectFactory.h> + +#include "unbounded/UnboundedRegExpSymbol.h" +#include "unbounded/UnboundedRegExpEmpty.h" +#include "unbounded/UnboundedRegExpConcatenation.h" + +#include "unbounded/UnboundedRegExp.h" +#include "unbounded/UnboundedRegExpStructure.h" + +#include <string/LinearString.h> namespace regexp { @@ -30,11 +39,26 @@ regexp::RegExp regexpFrom ( const std::string & string ); regexp::RegExp regexpFrom ( const char * string ); -regexp::RegExp regexpFrom ( string::LinearString < > string ); +template < class SymbolType > +regexp::RegExp regexpFrom ( ext::vector < SymbolType > string ) { + regexp::UnboundedRegExpConcatenation < DefaultSymbolType > con; + + for ( auto & symbol : string ) + con.appendElement ( regexp::UnboundedRegExpSymbol < DefaultSymbolType > ( object::ObjectFactory::make ( symbol ) ) ); + + return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < DefaultSymbolType > ( std::move ( con ) ) ) }; +} + +template < class SymbolType > +regexp::RegExp regexpFrom ( string::LinearString < SymbolType > string ) { + return regexpFrom ( string.getContent ( ) ); +} -regexp::RegExp regexpFrom ( ext::vector < DefaultSymbolType > string ); +template < class SymbolType > +regexp::RegExp regexpFrom ( const SymbolType & symbol ) { + return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < DefaultSymbolType > ( regexp::UnboundedRegExpSymbol < DefaultSymbolType > ( object::ObjectFactory::make ( symbol ) ) ) ) }; +} -regexp::RegExp regexpFrom ( DefaultSymbolType symbol ); regexp::RegExp regexpFrom ( ); diff --git a/alib2data/src/string/String.cpp b/alib2data/src/string/String.cpp index 6467767d719c753abeebd63ea446cd6e80bcb42a..43db66f6640e4019308b351dc41ecfb0a5f63170 100644 --- a/alib2data/src/string/String.cpp +++ b/alib2data/src/string/String.cpp @@ -6,26 +6,15 @@ */ #include "String.h" -#include "LinearString.h" -#include "CyclicString.h" -#include "Epsilon.h" namespace string { -string::String stringFrom ( const DefaultSymbolType & symbol ) { - return string::String { string::LinearString < > { ext::vector < DefaultSymbolType > { symbol } } }; -} - string::String stringFrom ( const std::string & string ) { - return string::String { string::LinearString < > { string } }; + return string::String { core::normalize < string::LinearString < char > >::eval ( string::LinearString < char > { string } ) }; } string::String stringFrom ( const char * string ) { return stringFrom ( ( std::string ) string ); } -string::String stringFrom ( const ext::vector < DefaultSymbolType > & str ) { - return string::String { string::LinearString < > { str } }; -} - } /* namespace string */ diff --git a/alib2data/src/string/String.h b/alib2data/src/string/String.h index 30c65d761768922279aec94098d39866bacdae8e..262f84047d562ffa541b901bbb3e0c90652ee61e 100644 --- a/alib2data/src/string/String.h +++ b/alib2data/src/string/String.h @@ -10,9 +10,13 @@ #include <base/WrapperBase.hpp> #include "StringBase.h" + #include <alib/set> #include <alib/vector> #include <common/DefaultSymbolType.h> +#include <object/ObjectFactory.h> + +#include "LinearString.h" namespace string { @@ -24,10 +28,20 @@ class String : public base::WrapperBase < StringBase > { }; -string::String stringFrom ( const DefaultSymbolType & symbol ); +template < class SymbolType > +string::String stringFrom ( const SymbolType & symbol ) { + return string::String { string::LinearString < DefaultSymbolType > { ext::vector < DefaultSymbolType > { object::ObjectFactory::make ( symbol ) } } }; +} + string::String stringFrom ( const std::string & str ); + string::String stringFrom ( const char * str ); -string::String stringFrom ( const ext::vector < DefaultSymbolType > & str ); + +template < class SymbolType > +string::String stringFrom ( const ext::vector < SymbolType > & str ) { + return string::String { core::normalize < string::LinearString < SymbolType > >::eval ( string::LinearString < SymbolType > { str } ) }; +} + } /* namespace string */ diff --git a/alib2xml/test-src/object/AnyObjectTest.cpp b/alib2xml/test-src/object/AnyObjectTest.cpp index 1913b43097fa5ed0372da11416840d1f6757fdb7..4fc523e22b44dca7e1fce5ca21bb938f5b3b3c01 100644 --- a/alib2xml/test-src/object/AnyObjectTest.cpp +++ b/alib2xml/test-src/object/AnyObjectTest.cpp @@ -1,12 +1,13 @@ #include "AnyObjectTest.h" -#include <object/xml/AnyObject.h> #include <container/xml/ObjectsPair.h> #include <primitive/xml/Character.h> #include <primitive/xml/Integer.h> #include <factory/XmlDataFactory.hpp> +#include <object/xml/AnyObject.h> + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( AnyObjectTest, "object" ); CPPUNIT_TEST_SUITE_REGISTRATION ( AnyObjectTest );