From 625bd42fc0c780eefe37fad835bf33d2a1a55421 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Fri, 27 Apr 2018 17:36:09 +0200
Subject: [PATCH] cli - add xml tokens to/from string algorithms

---
 alib2cli/src/environment/Environment.h   | 39 ++++++++++++++++++------
 alib2cli/test-src/cli/CliTest.cpp        | 17 +++++++++++
 alib2cli/test-src/cli/CliTest.h          |  2 ++
 alib2common/src/PrimitiveRegistrator.cpp |  1 +
 alib2xml/src/sax/SaxComposeInterface.cpp |  7 +++++
 alib2xml/src/sax/SaxParseInterface.cpp   |  8 +++++
 6 files changed, 65 insertions(+), 9 deletions(-)

diff --git a/alib2cli/src/environment/Environment.h b/alib2cli/src/environment/Environment.h
index 7eea7201cd..993e2bc0f9 100644
--- a/alib2cli/src/environment/Environment.h
+++ b/alib2cli/src/environment/Environment.h
@@ -8,6 +8,8 @@
 #include <abstraction/OperationAbstraction.hpp>
 #include <exception/CommonException.h>
 
+#include <abstraction/ImmediateValueAbstraction.hpp>
+
 namespace cli {
 
 class Environment {
@@ -15,6 +17,23 @@ class Environment {
 	ext::map < std::string, std::shared_ptr < abstraction::OperationAbstraction > > m_variables;
 
 	std::unique_ptr < Environment > m_upper;
+
+	std::shared_ptr < abstraction::OperationAbstraction > getVariableInt ( const std::string & name ) {
+		auto res = m_variables.find ( name );
+
+		if ( res != m_variables.end ( ) )
+			return res->second;
+
+		if ( ( bool ) m_upper )
+			return m_upper->getVariable ( name );
+		else
+			throw exception::CommonException ( "Variable of name " + name + " not found." );
+	}
+
+	void setVariableInt ( std::string name, std::shared_ptr < abstraction::OperationAbstraction > value ) {
+		m_variables [ std::move ( name ) ] = std::move ( value );
+	}
+
 public:
 	Environment ( ) {
 	}
@@ -43,19 +62,21 @@ public:
 	}
 
 	std::shared_ptr < abstraction::OperationAbstraction > getVariable ( const std::string & name ) {
-		auto res = m_variables.find ( name );
-
-		if ( res != m_variables.end ( ) )
-			return res->second;
+		return getVariableInt ( name );
+	}
 
-		if ( ( bool ) m_upper )
-			return m_upper->getVariable ( name );
-		else
-			throw exception::CommonException ( "Variable of name " + name + " not found." );
+	template < class T >
+	const T & getVariable ( const std::string & name ) {
+		return std::dynamic_pointer_cast < abstraction::ValueProvider < const T & > > ( getVariableInt ( name ) )->getConstValueReference ( );
 	}
 
 	void setVariable ( std::string name, std::shared_ptr < abstraction::OperationAbstraction > value ) {
-		m_variables [ std::move ( name ) ] = std::move ( value );
+		setVariableInt ( std::move ( name ), std::move ( value ) );
+	}
+
+	template < class T >
+	void setVariable ( std::string name, const T & value ) {
+		setVariableInt ( name, std::make_shared < abstraction::ImmediateValueAbstraction < T > > ( value ) );
 	}
 
 	bool clearVariable ( const std::string & name ) {
diff --git a/alib2cli/test-src/cli/CliTest.cpp b/alib2cli/test-src/cli/CliTest.cpp
index 9840822ae6..d3c5484f8e 100644
--- a/alib2cli/test-src/cli/CliTest.cpp
+++ b/alib2cli/test-src/cli/CliTest.cpp
@@ -304,3 +304,20 @@ void CliTest::testMember ( ) {
 	parser = cli::Parser ( cli::Lexer ( "execute Foo 3 | Foo::bar - 2" ) );
 	parser.parse ( )->run ( environment );
 }
+
+void CliTest::testXmlIO ( ) {
+	std::string in = "<Integer>1</Integer>";
+
+	cli::Environment environment;
+	environment.setVariable ( "in", in );
+	cli::Parser parser ( cli::Lexer ( "execute sax::SaxParseInterface $in | xml::Parse ^ - | (Integer) Add <(One) - | xml::Compose - | sax::SaxComposeInterface - > $out" ) );
+	parser.parse ( )->run ( environment );
+	std::string out = environment.getVariable < std::string > ( "out" );
+
+	std::string ref = "<?xml version=\"1.0\"?>\n<Integer>2</Integer>\n";
+
+	std::cout << out << std::endl;
+	std::cout << ref << std::endl;
+
+	CPPUNIT_ASSERT ( out == ref );
+}
diff --git a/alib2cli/test-src/cli/CliTest.h b/alib2cli/test-src/cli/CliTest.h
index 21a3fdd0f5..8e28c9bad0 100644
--- a/alib2cli/test-src/cli/CliTest.h
+++ b/alib2cli/test-src/cli/CliTest.h
@@ -13,6 +13,7 @@ class CliTest : public CppUnit::TestFixture {
 	CPPUNIT_TEST ( testConstRvalueReferencePassing );
 	CPPUNIT_TEST ( testSetConstruction );
 	CPPUNIT_TEST ( testMember );
+	CPPUNIT_TEST ( testXmlIO );
 	CPPUNIT_TEST_SUITE_END ( );
 
 public:
@@ -27,6 +28,7 @@ public:
 	void testConstRvalueReferencePassing ( );
 	void testSetConstruction ( );
 	void testMember ( );
+	void testXmlIO ( );
 };
 
 #endif // CLI_TEST_H_
diff --git a/alib2common/src/PrimitiveRegistrator.cpp b/alib2common/src/PrimitiveRegistrator.cpp
index 6e8df93761..831d9d0df6 100644
--- a/alib2common/src/PrimitiveRegistrator.cpp
+++ b/alib2common/src/PrimitiveRegistrator.cpp
@@ -42,6 +42,7 @@ public:
 		abstraction::CastRegistry::registerCast < unsigned, int > ( );
 		
 		abstraction::CastRegistry::registerCast < int, primitive::Integer > ( );
+		abstraction::CastRegistry::registerCast < primitive::Integer, int > ( );
 
 		abstraction::ContainerRegistry::registerSet < int > ( );
 
diff --git a/alib2xml/src/sax/SaxComposeInterface.cpp b/alib2xml/src/sax/SaxComposeInterface.cpp
index bce6fb8c5a..42078c1795 100644
--- a/alib2xml/src/sax/SaxComposeInterface.cpp
+++ b/alib2xml/src/sax/SaxComposeInterface.cpp
@@ -15,6 +15,8 @@
 #include "ComposerException.h"
 #include <measure>
 
+#include <registration/AlgoRegistration.hpp>
+
 namespace sax {
 
 void SaxComposeInterface::composeMemory(std::string& xmlOut, const ext::deque<Token>& in) {
@@ -106,3 +108,8 @@ void SaxComposeInterface::xmlSAXUserCompose(xmlTextWriterPtr writer, const ext::
 
 } /* namespace sax */
 
+namespace {
+
+auto SaxComposeInterfaceString = registration::AbstractRegister < sax::SaxComposeInterface, std::string, const ext::deque < sax::Token > & > ( sax::SaxComposeInterface::composeMemory );
+
+} /* namespace */
diff --git a/alib2xml/src/sax/SaxParseInterface.cpp b/alib2xml/src/sax/SaxParseInterface.cpp
index cccb9c45a3..92a53e20cc 100644
--- a/alib2xml/src/sax/SaxParseInterface.cpp
+++ b/alib2xml/src/sax/SaxParseInterface.cpp
@@ -15,6 +15,8 @@
 #include <exception/CommonException.h>
 #include <measure>
 
+#include <registration/AlgoRegistration.hpp>
+
 namespace sax {
 
 void SaxParseInterface::parseMemory(const std::string& xmlIn, ext::deque<Token>& out) {
@@ -122,3 +124,9 @@ int SaxParseInterface::xmlSAXUserParse(xmlTextReaderPtr reader, ext::deque<Token
 }
 
 } /* namespace sax */
+
+namespace {
+
+auto SaxParseInterfaceToken = registration::AbstractRegister < sax::SaxParseInterface, ext::deque < sax::Token >, const std::string & > ( sax::SaxParseInterface::parseMemory );
+
+} /* namespace */
-- 
GitLab