From 2e157e5b843f0824aca975a7f9ed62a6554d67dc Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 28 Aug 2017 17:23:17 +0200 Subject: [PATCH] add abstraction to compose to string only --- alib2common/src/abstraction/Registry.cpp | 5 ++ alib2common/src/abstraction/Registry.h | 1 + .../abstraction/XmlComposerAbstraction.hpp | 30 ++++++++ .../src/abstraction/XmlComposerRegistry.cpp | 39 ++++++++++ .../src/abstraction/XmlComposerRegistry.hpp | 74 +++++++++++++++++++ .../src/registration/TypeRegistration.hpp | 2 + alib2common/src/sax/SaxParseInterface.cpp | 2 +- 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 alib2common/src/abstraction/XmlComposerAbstraction.hpp create mode 100644 alib2common/src/abstraction/XmlComposerRegistry.cpp create mode 100644 alib2common/src/abstraction/XmlComposerRegistry.hpp diff --git a/alib2common/src/abstraction/Registry.cpp b/alib2common/src/abstraction/Registry.cpp index e45d6f808e..dde8c227ea 100644 --- a/alib2common/src/abstraction/Registry.cpp +++ b/alib2common/src/abstraction/Registry.cpp @@ -11,6 +11,7 @@ #include <abstraction/ImmediateRegistry.hpp> #include <abstraction/XmlParserRegistry.hpp> #include <abstraction/XmlFileWriterRegistry.hpp> +#include <abstraction/XmlComposerRegistry.hpp> #include <abstraction/ValuePrinterRegistry.hpp> #include <abstraction/CastRegistry.hpp> #include <abstraction/NormalizeRegistry.hpp> @@ -92,6 +93,10 @@ std::shared_ptr < abstraction::OperationAbstraction > Registry::getXmlFileWriter return XmlFileWriterRegistry::getAbstraction ( param, std::move ( filename ) ); } +std::shared_ptr < abstraction::OperationAbstraction > Registry::getXmlComposerAbstraction ( const std::string & param ) { + return XmlComposerRegistry::getAbstraction ( param ); +} + std::shared_ptr < abstraction::OperationAbstraction > Registry::getXmlParserAbstraction ( const std::string & typeName, ext::deque < sax::Token > tokens ) { return XmlParserRegistry::getAbstraction ( typeName, std::move ( tokens ) ); } diff --git a/alib2common/src/abstraction/Registry.h b/alib2common/src/abstraction/Registry.h index 32873ae54c..027c0fd5e6 100644 --- a/alib2common/src/abstraction/Registry.h +++ b/alib2common/src/abstraction/Registry.h @@ -38,6 +38,7 @@ public: static std::shared_ptr < abstraction::OperationAbstraction > getDowncastAbstraction ( const std::string & concrete, const std::string & base ); static std::shared_ptr < abstraction::OperationAbstraction > getValuePrinterAbstraction ( const std::string & param ); static std::shared_ptr < abstraction::OperationAbstraction > getXmlFileWriterAbstraction ( const std::string & param, std::string filename ); + static std::shared_ptr < abstraction::OperationAbstraction > getXmlComposerAbstraction ( const std::string & param ); static std::shared_ptr < abstraction::OperationAbstraction > getXmlParserAbstraction ( const std::string & typeName, ext::deque < sax::Token > tokens ); }; diff --git a/alib2common/src/abstraction/XmlComposerAbstraction.hpp b/alib2common/src/abstraction/XmlComposerAbstraction.hpp new file mode 100644 index 0000000000..da4003dbcc --- /dev/null +++ b/alib2common/src/abstraction/XmlComposerAbstraction.hpp @@ -0,0 +1,30 @@ +/* + * XmlComposerAbstraction.hpp + * + * Created on: 11. 7. 2017 + * Author: Jan Travnicek + */ + +#ifndef _XML_g_ABSTRACTION_HPP_ +#define _XML_g_ABSTRACTION_HPP_ + +#include <abstraction/UnaryOperationAbstraction.hpp> +#include <tuple> + +namespace abstraction { + +template < class ParamType > +class XmlComposerAbstraction : public UnaryOperationAbstraction < std::string, ParamType > { +public: + virtual bool run ( ) override { + if ( ! this->inputsReady ( ) ) + return false; + + this->m_data = alib::XmlDataFactory::toString ( std::get < 0 > ( this->m_params )->getConstValueReference ( ) ); + return true; + } +}; + +} /* namespace abstraction */ + +#endif /* _XML_g_ABSTRACTION_HPP_ */ diff --git a/alib2common/src/abstraction/XmlComposerRegistry.cpp b/alib2common/src/abstraction/XmlComposerRegistry.cpp new file mode 100644 index 0000000000..a45cbfd536 --- /dev/null +++ b/alib2common/src/abstraction/XmlComposerRegistry.cpp @@ -0,0 +1,39 @@ +/* + * XmlComposerRegistry.cpp + * + * Created on: 28. 8. 2017 + * Author: Jan Travnicek + */ + +#include <abstraction/XmlComposerRegistry.hpp> + +namespace abstraction { + +std::shared_ptr < abstraction::OperationAbstraction > XmlComposerRegistry::getAbstraction ( const std::string & param ) { + auto res = getEntries ( ).find ( param ); + if ( res == getEntries ( ).end ( ) ) + throw exception::CommonException ( "Entry " + param + " not available." ); + + return res->second->getAbstraction ( ); +} + +ext::set < std::string > XmlComposerRegistry::listGroup ( const std::string & group ) { + ext::set < std::string > res; + + for ( const std::pair < const std::string, std::unique_ptr < Entry > > & entry : getEntries ( ) ) + if ( entry.first.find ( group ) == 0 ) //found at the begining + res.insert ( entry.first ); + + return res; +} + +ext::set < std::string > XmlComposerRegistry::list ( ) { + ext::set < std::string > res; + + for ( const std::pair < const std::string, std::unique_ptr < Entry > > & entry : getEntries ( ) ) + res.insert ( entry.first ); + + return res; +} + +} /* namespace abstraction */ diff --git a/alib2common/src/abstraction/XmlComposerRegistry.hpp b/alib2common/src/abstraction/XmlComposerRegistry.hpp new file mode 100644 index 0000000000..7b85af772d --- /dev/null +++ b/alib2common/src/abstraction/XmlComposerRegistry.hpp @@ -0,0 +1,74 @@ +/* + * XmlComposerRegistry.hpp + * + * Created on: 28. 8. 2017 + * Author: Jan Travnicek + */ + +#ifndef _XML_COMPOSER_REGISTRY_HPP_ +#define _XML_COMPOSER_REGISTRY_HPP_ + +#include <memory> +#include <string> +#include <set> + +#include <exception/CommonException.h> +#include <abstraction/OperationAbstraction.hpp> + +namespace abstraction { + +class XmlComposerRegistry { + class Entry { + public: + virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( ) const = 0; + + }; + + template < class Param > + class EntryImpl : public Entry { + public: + EntryImpl ( ) { + } + + virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( ) const override; + }; + + static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ) { + static ext::map < std::string, std::unique_ptr < Entry > > fileWriters; + return fileWriters; + } + +public: + template < class ParamType > + static void registerXmlComposer ( std::string param ) { + getEntries ( ).insert ( std::make_pair ( param, std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); + } + + template < class ParamType > + static void registerXmlComposer ( ) { + std::string param = ext::to_string < ParamType > ( ); + registerXmlComposer < ParamType > ( std::move ( param ) ); + } + + static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & param ); + + static ext::set < std::string > listGroup ( const std::string & group ); + + static ext::set < std::string > list ( ); +}; + +} /* namespace abstraction */ + +#include <abstraction/XmlComposerAbstraction.hpp> + +namespace abstraction { + +template < class Param > +std::shared_ptr < abstraction::OperationAbstraction > XmlComposerRegistry::EntryImpl < Param >::getAbstraction ( ) const { + return std::make_shared < abstraction::XmlComposerAbstraction < const Param & > > ( ); +} + + +} /* namespace abstraction */ + +#endif /* _XML_COMPOSER_REGISTRY_HPP_ */ diff --git a/alib2common/src/registration/TypeRegistration.hpp b/alib2common/src/registration/TypeRegistration.hpp index db6742f52a..6f3eb09254 100644 --- a/alib2common/src/registration/TypeRegistration.hpp +++ b/alib2common/src/registration/TypeRegistration.hpp @@ -5,6 +5,7 @@ #include <introspection/DataTypes.hpp> #include <abstraction/XmlFileWriterRegistry.hpp> +#include <abstraction/XmlComposerRegistry.hpp> #include <abstraction/XmlParserRegistry.hpp> #include <abstraction/ValuePrinterRegistry.hpp> #include <abstraction/DowncastRegistry.hpp> @@ -20,6 +21,7 @@ public: introspection::DataTypes::registerGroup < Group > ( ); abstraction::XmlFileWriterRegistry::registerXmlFileWriter < Type > ( ); + abstraction::XmlComposerRegistry::registerXmlComposer < Type > ( ); abstraction::XmlParserRegistry::registerXmlParser < Type > ( ); abstraction::ValuePrinterRegistry::registerValuePrinter < Type > ( ); diff --git a/alib2common/src/sax/SaxParseInterface.cpp b/alib2common/src/sax/SaxParseInterface.cpp index 947f8dd7c5..0eca3287b9 100644 --- a/alib2common/src/sax/SaxParseInterface.cpp +++ b/alib2common/src/sax/SaxParseInterface.cpp @@ -29,7 +29,7 @@ void SaxParseInterface::parseMemory(const std::string& xmlIn, ext::deque<Token>& xmlCleanupParser(); if (result != 0) { - throw exception::CommonException("Cannot parse the XML file " + xmlIn); + throw exception::CommonException("Cannot parse the XML " + xmlIn); } } -- GitLab