From 2f2f84513474a4f138dd3e5ba25ca4a642c60ea3 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 27 Aug 2015 11:16:46 +0200 Subject: [PATCH] reenable support for variant xmlApi --- .../container/ContainerFromXMLParser.hpp.tmp | 62 ---------- .../container/ContainerToXMLComposer.hpp.tmp | 33 ------ alib2data/src/container/ObjectsVariant.h | 109 ++++++++++++++++++ .../test-src/container/ContainerTest.cpp | 50 ++++---- alib2data/test-src/container/ContainerTest.h | 25 ++-- 5 files changed, 145 insertions(+), 134 deletions(-) delete mode 100644 alib2data/src/container/ContainerFromXMLParser.hpp.tmp delete mode 100644 alib2data/src/container/ContainerToXMLComposer.hpp.tmp create mode 100644 alib2data/src/container/ObjectsVariant.h diff --git a/alib2data/src/container/ContainerFromXMLParser.hpp.tmp b/alib2data/src/container/ContainerFromXMLParser.hpp.tmp deleted file mode 100644 index 6e00305186..0000000000 --- a/alib2data/src/container/ContainerFromXMLParser.hpp.tmp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * ContainerFromXMLParser.h - * - * Created on: Oct 12, 2013 - * Author: Jan Travnicek - */ - -#ifndef CONTAINER_FROM_XML_PARSER_H_ -#define CONTAINER_FROM_XML_PARSER_H_ - -/* TODO for gcc-4.9 -template<typename ... Ts> -std::variant<Ts...> parseVariant(std::deque<sax::Token>::iterator& input) const; - -template<typename T> -T parseVariantHelper(std::deque<sax::Token>::iterator&) { - throw exception::AlibException("Can't parse variant type"); -} - -template<typename T, typename R, typename ... Ts> -T parseVariantHelper(std::deque<sax::Token>::iterator& input) { - if(alib::xmlApi<R>::first(input)) return T(alib::xmlApi<R>::parse(input)); - return parseVariantHelper<T, Ts...>(input); -} - -template<typename ... Ts> -std::variant<Ts...> ContainerFromXMLParser::parseVariant(std::deque<sax::Token>::iterator& input) const { - return parseVariantHelper<std::variant<Ts...>, Ts...>(input); -} -*/ - -#endif /* CONTAINER_FROM_XML_PARSER_H_ */ - -/* TODO for gcc-4.9 -template<typename ... Ts> -struct xmlApi<std::variant<Ts...>> { - static std::variant<Ts...> parse(std::deque<sax::Token>::iterator& input); - static bool first(const std::deque<sax::Token>::const_iterator& input); - static void compose(std::deque<sax::Token>& output, const std::variant<Ts...>& data); -}; - -/* TODO For gcc-4.9 -template<typename ... Ts> -std::variant<Ts...> xmlApi<std::variant<Ts...>>::parse(std::deque<sax::Token>::iterator& input) { - return FromXMLParsers::containerParser.parseVariant<Ts...>(input); -} - -template<typename ... Ts> -bool xmlApi<std::variant<Ts...>>::first(const std::deque<sax::Token>::const_iterator& input) { - return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::CONTAINER_OBJECTS_VARIANT); -} - -template<typename ... Ts> -std::deque<sax::Token> xmlApi<std::variant<Ts...>>::compose(std::deque<sax::Token>& output, const std::variant<Ts...>& input) { - return ToXMLComposers::containerComposer.compose<Ts...>(input); -} -*/ - -} /* namespace alib */ - -*/ - diff --git a/alib2data/src/container/ContainerToXMLComposer.hpp.tmp b/alib2data/src/container/ContainerToXMLComposer.hpp.tmp deleted file mode 100644 index ad338b3b49..0000000000 --- a/alib2data/src/container/ContainerToXMLComposer.hpp.tmp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * ContainerToXMLComposer.h - * - * Created on: Nov 11, 2013 - * Author: Jan Travnicek - */ - -#ifndef CONTAINER_TO_XML_COMPOSER_H_ -#define CONTAINER_TO_XML_COMPOSER_H_ - -/* TODO for gcc-4.9 uncomment -template<typename ... Ts> -void compose(std::deque<sax::Token>& output, const std::variant<Ts...>& container) const; - -template<typename T> -void composeVariantHelper(const T&) { - throw exception::AlibException("Can't compose variant type"); -} - -template<typename T, typename R, typename ... Ts> -void composeVariantHelper(std::deque<sax::Token>& out, const T& container) { - if(container.template is<R>()) return alib::xmlApi<R>::compose(out, container.template get<R>()); - composeVariantHelper<T, Ts...>(out, container); -} - -template<typename ... Ts> -void ContainerToXMLComposer::compose(std::deque<sax::Token>& out, const std::variant<Ts...>& container) const { - composeVariantHelper<std::variant<Ts...>, Ts...>(out, container); -} -*/ - -#endif /* CONTAINER_TO_XML_COMPOSER_H_ */ - diff --git a/alib2data/src/container/ObjectsVariant.h b/alib2data/src/container/ObjectsVariant.h new file mode 100644 index 0000000000..a400e58951 --- /dev/null +++ b/alib2data/src/container/ObjectsVariant.h @@ -0,0 +1,109 @@ +/* + * Map.h + * + * Created on: Apr 1, 2013 + * Author: Jan Travnicek + */ + +#ifndef OBJECTS_VARIANT_H_ +#define OBJECTS_VARIANT_H_ + +#include <variant> +#include <string> + +#include "../XmlApi.hpp" +#include "../exception/AlibException.h" + +namespace container { + +/** + * Basic container from which are derived all other containers. + * Contains reason why the container occured. + */ +class ObjectsVariant { +private: + explicit ObjectsVariant ( ); + +public: + template < class ... Ts > + static std::variant < Ts ... > parseRaw ( std::deque < sax::Token >::iterator & input ); + + template < class ... Ts > + static void compose ( std::deque < sax::Token > & out, const std::variant < Ts ... > & input ); +}; + +template < typename T > +T parseVariantHelper ( std::deque < sax::Token >::iterator & ) { + throw exception::AlibException ( "Can't parse variant type" ); +} + +template < typename T, typename R, typename ... Ts > +T parseVariantHelper ( std::deque < sax::Token >::iterator & input ) { + if ( alib::xmlApi < R >::first ( input ) ) return T ( alib::xmlApi < R >::parse ( input ) ); + + return parseVariantHelper < T, Ts ... > ( input ); +} + +template < typename ... Ts > +std::variant < Ts ... > ObjectsVariant::parseRaw ( std::deque < sax::Token >::iterator & input ) { + return parseVariantHelper < std::variant < Ts ... >, Ts ... > ( input ); +} + +template < typename T > +bool firstVariantHelper ( std::deque < sax::Token >::iterator & ) { + return false; +} + +template < typename T, typename R, typename ... Ts > +T firstVariantHelper ( std::deque < sax::Token >::iterator & input ) { + if ( alib::xmlApi < R >::first ( input ) ) return true; + + return firstVariantHelper < T, Ts ... > ( input ); +} + +template < typename T > +void composeVariantHelper ( std::deque < sax::Token > &, const T & ) { + throw exception::AlibException ( "Can't compose variant type" ); +} + +template < typename T, typename R, typename ... Ts > +void composeVariantHelper ( std::deque < sax::Token > & out, const T & container ) { + if ( container.template is < R > ( ) ) return alib::xmlApi < R >::compose ( out, container.template get < R > ( ) ); + + composeVariantHelper < T, Ts ... > ( out, container ); +} + +template < typename ... Ts > +void ObjectsVariant::compose ( std::deque < sax::Token > & out, const std::variant < Ts ... > & container ) { + composeVariantHelper < std::variant < Ts ... >, Ts ... > ( out, container ); +} + +} /* namespace container */ + +namespace alib { + +template < typename ... Ts > +struct xmlApi < std::variant < Ts ... > > { + static std::variant < Ts ... > parse ( std::deque < sax::Token >::iterator & input ); + static bool first ( const std::deque < sax::Token >::const_iterator & input ); + static void compose ( std::deque < sax::Token > & output, const std::variant < Ts ... > & data ); +}; + +template < typename ... Ts > +std::variant < Ts ... > xmlApi < std::variant < Ts ... > >::parse ( std::deque < sax::Token >::iterator & input ) { + return container::ObjectsVariant::parseRaw < Ts ... > ( input ); +} + +template < typename ... Ts > +bool xmlApi < std::variant < Ts ... > >::first ( const std::deque < sax::Token >::const_iterator & input ) { + return container::firstVariantHelper < std::variant < Ts ... >, Ts ... > ( input ); +} + +template < typename ... Ts > +void xmlApi < std::variant < Ts ... > >::compose ( std::deque < sax::Token > & output, const std::variant < Ts ... > & input ) { + return container::ObjectsVariant::compose < Ts ... > ( output, input ); +} + +} /* namespace alib */ + +#endif /* OBJECTS_VARIANT_H_ */ diff --git a/alib2data/test-src/container/ContainerTest.cpp b/alib2data/test-src/container/ContainerTest.cpp index 54ba122a73..db9e40b35b 100644 --- a/alib2data/test-src/container/ContainerTest.cpp +++ b/alib2data/test-src/container/ContainerTest.cpp @@ -6,57 +6,59 @@ #include "object/Object.h" #include "container/ObjectsSet.h" +#include "container/ObjectsVariant.h" #include "alphabet/LabeledSymbol.h" +#include "primitive/Integer.h" +#include "primitive/String.h" #include "factory/XmlDataFactory.hpp" -#define CPPUNIT_IMPLY(x, y) CPPUNIT_ASSERT(!(x) || (y)) -#define CPPUNIT_EXCLUSIVE_OR(x, y) CPPUNIT_ASSERT((!(x) && (y)) || ((x) && !(y))) +#define CPPUNIT_IMPLY( x, y ) CPPUNIT_ASSERT ( !( x ) || ( y ) ) +#define CPPUNIT_EXCLUSIVE_OR( x, y ) CPPUNIT_ASSERT ( ( !( x ) && ( y ) ) || ( ( x ) && !( y ) ) ) -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ContainerTest, "container" ); -CPPUNIT_TEST_SUITE_REGISTRATION( ContainerTest ); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ContainerTest, "container" ); +CPPUNIT_TEST_SUITE_REGISTRATION ( ContainerTest ); -void ContainerTest::setUp() { +void ContainerTest::setUp ( ) { } -void ContainerTest::tearDown() { +void ContainerTest::tearDown ( ) { } -void ContainerTest::testXMLParser() { +void ContainerTest::testXMLParser ( ) { - alib::Object tmp(alphabet::LabeledSymbol("1")); + alib::Object tmp ( alphabet::LabeledSymbol ( "1" ) ); container::ObjectsSet set; - set.insert(tmp); - alib::Object object(set); + set.insert ( tmp ); + + alib::Object object ( set ); { - std::string tmp = alib::XmlDataFactory::toString(object); - alib::Object object2 = alib::XmlDataFactory::fromString<alib::Object>(tmp); + std::string tmp = alib::XmlDataFactory::toString ( object ); + alib::Object object2 = alib::XmlDataFactory::fromString < alib::Object > ( tmp ); - CPPUNIT_ASSERT( object == object2 ); + CPPUNIT_ASSERT ( object == object2 ); - std::set<alphabet::LabeledSymbol> concrete = alib::XmlDataFactory::fromString<std::set<alphabet::LabeledSymbol>>(tmp); - std::string tmp2 = alib::XmlDataFactory::toString(concrete); + std::set < alphabet::LabeledSymbol > concrete = alib::XmlDataFactory::fromString < std::set < alphabet::LabeledSymbol > > ( tmp ); + std::string tmp2 = alib::XmlDataFactory::toString ( concrete ); - CPPUNIT_ASSERT( tmp == tmp2 ); + CPPUNIT_ASSERT ( tmp == tmp2 ); } } -/* TODO for gcc-4.9 uncomment -void ContainerTest::testVariantParsing() { +void ContainerTest::testVariantParsing ( ) { { std::string string = "<String>aaa</String>"; - std::variant<int, std::string> object = alib::XmlDataFactory::fromString<std::variant<int, std::string>>(string); + std::variant < int, std::string > object = alib::XmlDataFactory::fromString < std::variant < int, std::string > > ( string ); - CPPUNIT_ASSERT( "aaa" == object.get<std::string>()); + CPPUNIT_ASSERT ( "aaa" == object.get < std::string > ( ) ); - std::string string2 = alib::XmlDataFactory::toString(object); + std::string string2 = alib::XmlDataFactory::toString ( object ); - std::variant<int, std::string> object2 = alib::XmlDataFactory::fromString<std::variant<int, std::string>>(string2); + std::variant < int, std::string > object2 = alib::XmlDataFactory::fromString < std::variant < int, std::string > > ( string2 ); - CPPUNIT_ASSERT( object == object2 ); + CPPUNIT_ASSERT ( object == object2 ); } } -*/ diff --git a/alib2data/test-src/container/ContainerTest.h b/alib2data/test-src/container/ContainerTest.h index 84e68b835a..7e2a07b4c6 100644 --- a/alib2data/test-src/container/ContainerTest.h +++ b/alib2data/test-src/container/ContainerTest.h @@ -3,23 +3,18 @@ #include <cppunit/extensions/HelperMacros.h> -class ContainerTest : public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE( ContainerTest ); - CPPUNIT_TEST( testXMLParser ); -/* TODO for gcc-4.9 uncomment - CPPUNIT_TEST( testVariantParsing ); -*/ - CPPUNIT_TEST_SUITE_END(); +class ContainerTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE ( ContainerTest ); + CPPUNIT_TEST ( testXMLParser ); + CPPUNIT_TEST ( testVariantParsing ); + CPPUNIT_TEST_SUITE_END ( ); public: - void setUp(); - void tearDown(); + void setUp ( ); + void tearDown ( ); - void testXMLParser(); -/* TODO for gcc-4.9 uncomment - void testVariantParsing(); -*/ + void testXMLParser ( ); + void testVariantParsing ( ); }; -#endif // CONTAINER_TEST_H_ +#endif // CONTAINER_TEST_H_ -- GitLab