From 12264829fb37d906a69c04a5a10fb2a6963f19db Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Mon, 14 Aug 2017 22:59:07 +0200
Subject: [PATCH] use registry to get all followup abstractions

---
 alib2cli/src/ast/params/BindedValueParam.h    |  4 +--
 alib2cli/src/ast/params/CastParam.h           |  4 +--
 alib2cli/src/ast/statements/CastStatement.h   |  6 ++--
 .../statements/ResultBindedFileStatement.h    |  2 +-
 .../statements/ResultImmediateFileStatement.h |  2 +-
 .../src/ast/statements/ResultPrintStatement.h |  2 +-
 .../src/ast/statements/SingleStatement.cpp    | 10 +++----
 .../src/abstraction/AlgorithmAbstraction.hpp  | 14 ++-------
 .../NullaryOperationAbstraction.hpp           |  5 +---
 .../src/abstraction/OperationAbstraction.hpp  | 23 ++++++++------
 .../abstraction/UnaryOperationAbstraction.hpp | 12 ++------
 .../abstraction/ValueOperationAbstraction.hpp | 30 ++-----------------
 .../abstraction/VoidOperationAbstraction.hpp  | 24 ++-------------
 13 files changed, 39 insertions(+), 99 deletions(-)

diff --git a/alib2cli/src/ast/params/BindedValueParam.h b/alib2cli/src/ast/params/BindedValueParam.h
index 1b7b39aca8..0fd4a46313 100644
--- a/alib2cli/src/ast/params/BindedValueParam.h
+++ b/alib2cli/src/ast/params/BindedValueParam.h
@@ -2,7 +2,7 @@
 #define _CLI_BINDED_VALUE_PARAM_H_
 
 #include <ast/Param.h>
-#include <abstraction/ImmediateValueAbstraction.hpp>
+#include <abstraction/Registry.h>
 
 namespace cli {
 
@@ -14,7 +14,7 @@ public:
 	}
 
 	virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > &, Environment & environment ) const override {
-		return std::make_shared < abstraction::ImmediateValueAbstraction < std::string > > ( environment.getBinding ( m_bind ) );
+		return abstraction::Registry::getImmediateAbstraction ( ext::to_string < std::string > ( ), environment.getBinding ( m_bind ) );
 	}
 };
 
diff --git a/alib2cli/src/ast/params/CastParam.h b/alib2cli/src/ast/params/CastParam.h
index 0f7c0876dd..c073a5bed6 100644
--- a/alib2cli/src/ast/params/CastParam.h
+++ b/alib2cli/src/ast/params/CastParam.h
@@ -18,12 +18,12 @@ public:
 
 		bool normalize;
 
-		std::shared_ptr < abstraction::OperationAbstraction > res = translatedParam->getCastFromResult ( m_type, normalize );
+		std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getCastAbstraction ( m_type, translatedParam->getReturnType ( ), normalize );
 		res->attachInput ( translatedParam, 0 );
 		res->eval ( );
 
 		if ( normalize ) {
-			std::shared_ptr < abstraction::OperationAbstraction > normalized = res->getNormalizeResult ( );
+			std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( res->getReturnType ( ) );
 			normalized->attachInput ( res, 0 );
 			normalized->eval ( );
 
diff --git a/alib2cli/src/ast/statements/CastStatement.h b/alib2cli/src/ast/statements/CastStatement.h
index 864a901c1d..14454beb46 100644
--- a/alib2cli/src/ast/statements/CastStatement.h
+++ b/alib2cli/src/ast/statements/CastStatement.h
@@ -16,14 +16,14 @@ public:
 	virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > & prev, Environment & environment ) const override {
 		std::shared_ptr < abstraction::OperationAbstraction > translatedStatement = m_statement->translateAndEval ( prev, environment );
 
-		bool normalize = false;
+		bool normalize;
 
-		std::shared_ptr < abstraction::OperationAbstraction > res = translatedStatement->getCastFromResult ( m_type, normalize );
+		std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getCastAbstraction ( m_type, translatedStatement->getReturnType ( ), normalize );
 		res->attachInput ( translatedStatement, 0 );
 		res->eval ( );
 
 		if ( normalize ) {
-			std::shared_ptr < abstraction::OperationAbstraction > normalized = res->getNormalizeResult ( );
+			std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( res->getReturnType ( ) );
 			normalized->attachInput ( res, 0 );
 			normalized->eval ( );
 
diff --git a/alib2cli/src/ast/statements/ResultBindedFileStatement.h b/alib2cli/src/ast/statements/ResultBindedFileStatement.h
index f40c471db2..28af7abdb3 100644
--- a/alib2cli/src/ast/statements/ResultBindedFileStatement.h
+++ b/alib2cli/src/ast/statements/ResultBindedFileStatement.h
@@ -13,7 +13,7 @@ public:
 	}
 
 	virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > & prev, Environment & environment ) const override {
-		std::shared_ptr < abstraction::OperationAbstraction > res = prev->getXmlFileWriterFromResult ( environment.getBinding ( m_bind ) );
+		std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getXmlFileWriterAbstraction ( prev->getReturnType ( ), environment.getBinding ( m_bind ) );
 		res->attachInput ( prev, 0 );
 		res->eval ( );
 		return res;
diff --git a/alib2cli/src/ast/statements/ResultImmediateFileStatement.h b/alib2cli/src/ast/statements/ResultImmediateFileStatement.h
index 6a816a1306..950cf9817f 100644
--- a/alib2cli/src/ast/statements/ResultImmediateFileStatement.h
+++ b/alib2cli/src/ast/statements/ResultImmediateFileStatement.h
@@ -13,7 +13,7 @@ public:
 	}
 
 	virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > & prev, Environment & ) const override {
-		std::shared_ptr < abstraction::OperationAbstraction > res = prev->getXmlFileWriterFromResult ( m_name );
+		std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getXmlFileWriterAbstraction ( prev->getReturnType ( ), m_name );
 		res->attachInput ( prev, 0 );
 		res->eval ( );
 		return res;
diff --git a/alib2cli/src/ast/statements/ResultPrintStatement.h b/alib2cli/src/ast/statements/ResultPrintStatement.h
index 18d515331b..c4a45f9154 100644
--- a/alib2cli/src/ast/statements/ResultPrintStatement.h
+++ b/alib2cli/src/ast/statements/ResultPrintStatement.h
@@ -11,7 +11,7 @@ public:
 	}
 
 	virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > & prev, Environment & ) const override {
-		std::shared_ptr < abstraction::OperationAbstraction > res = prev->getValuePrinterFromResult ( );
+		std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getValuePrinterAbstraction ( prev->getReturnType ( ) );
 		res->attachInput ( prev, 0 );
 		res->eval ( );
 		return res;
diff --git a/alib2cli/src/ast/statements/SingleStatement.cpp b/alib2cli/src/ast/statements/SingleStatement.cpp
index 4804e8791e..d7ce1f3cb0 100644
--- a/alib2cli/src/ast/statements/SingleStatement.cpp
+++ b/alib2cli/src/ast/statements/SingleStatement.cpp
@@ -17,8 +17,8 @@ std::shared_ptr < abstraction::OperationAbstraction > SingleStatement::translate
 	}
 
 	ext::vector < ext::type_index > paramTypes;
-	for ( const  std::shared_ptr < abstraction::OperationAbstraction > & param : params ) {
-		paramTypes.push_back ( param->type ( ) );
+	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : params ) {
+		paramTypes.push_back ( param->getReturnTypeIndex ( ) );
 	}
 
 	bool downcast = false;
@@ -29,7 +29,7 @@ std::shared_ptr < abstraction::OperationAbstraction > SingleStatement::translate
 	unsigned i = 0;
 	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : params ) {
 		if ( ! algo->attachInput ( param, i ) )
-			throw exception::CommonException ( "Can't connect param at " + ext::to_string ( i ) + " of algorithm " + m_name + " with result of type " + ext::to_string ( param->type ( ) ) + "." );
+			throw exception::CommonException ( "Can't connect param at " + ext::to_string ( i ) + " of algorithm " + m_name + " with result of type " + param->getReturnType ( ) + "." );
 		i++;
 	}
 
@@ -37,7 +37,7 @@ std::shared_ptr < abstraction::OperationAbstraction > SingleStatement::translate
 		throw exception::CommonException ( "Eval of algorithm " + m_name + " failed." );
 
 	if ( downcast ) {
-		std::shared_ptr < abstraction::OperationAbstraction > downcaster = algo->getDowncastResult ( );
+		std::shared_ptr < abstraction::OperationAbstraction > downcaster = abstraction::Registry::getDowncastAbstraction ( algo->getRuntimeReturnType ( ), algo->getReturnType ( ) );
 		downcaster->attachInput ( algo, 0 );
 		downcaster->eval ( );
 
@@ -45,7 +45,7 @@ std::shared_ptr < abstraction::OperationAbstraction > SingleStatement::translate
 	}
 
 	if ( normalize ) {
-		std::shared_ptr < abstraction::OperationAbstraction > normalized = algo->getNormalizeResult ( );
+		std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( algo->getReturnType ( ) );
 		normalized->attachInput ( algo, 0 );
 		normalized->eval ( );
 
diff --git a/alib2common/src/abstraction/AlgorithmAbstraction.hpp b/alib2common/src/abstraction/AlgorithmAbstraction.hpp
index 9feeb21306..a73c0b9a8b 100644
--- a/alib2common/src/abstraction/AlgorithmAbstraction.hpp
+++ b/alib2common/src/abstraction/AlgorithmAbstraction.hpp
@@ -111,21 +111,13 @@ public:
 		return sizeof ... ( ParamTypes );
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getImmediateValueFromParam ( unsigned index, const std::string & value ) const override {
+	virtual ext::type_index getParamTypeIndex ( unsigned index ) const override {
 		auto callback = [ & ] ( auto & param ) {
-			std::string paramType = ext::to_string < typename std::decay < decltype ( param ) >::type::element_type::return_type > ( );
-			return Registry::getImmediateAbstraction ( std::move ( paramType ), value );
+			return ext::type_index ( typeid ( typename std::decay < decltype ( param ) >::type::element_type::return_type ) );
 		};
-		return ext::call_on_nth < std::shared_ptr < OperationAbstraction > > ( inputs, index, callback );
+		return ext::call_on_nth < ext::type_index > ( inputs, index, callback );
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getXmlParserFromParam ( unsigned index, ext::deque < sax::Token > tokens ) const override {
-		auto callback = [ & ] ( auto & param ) {
-			std::string paramType = ext::to_string < typename std::decay < decltype ( param ) >::type::element_type::return_type > ( );
-			return Registry::getXmlParserAbstraction ( std::move ( paramType ), std::move ( tokens ) );
-		};
-		return ext::call_on_nth < std::shared_ptr < OperationAbstraction > > ( inputs, index, callback );
-	}
 };
 
 template < class ReturnType, class ... ParamTypes >
diff --git a/alib2common/src/abstraction/NullaryOperationAbstraction.hpp b/alib2common/src/abstraction/NullaryOperationAbstraction.hpp
index 6bd5726002..a7eebcf460 100644
--- a/alib2common/src/abstraction/NullaryOperationAbstraction.hpp
+++ b/alib2common/src/abstraction/NullaryOperationAbstraction.hpp
@@ -46,13 +46,10 @@ public:
 		return 0;
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getImmediateValueFromParam ( unsigned, const std::string & ) const override {
+	virtual ext::type_index getParamTypeIndex ( unsigned ) const override {
 		throw exception::CommonException ( "Nullary algorithm does not have any parameter." );
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getXmlParserFromParam ( unsigned, ext::deque < sax::Token > ) const override {
-		throw exception::CommonException ( "Nullary algorithm does not have any parameter." );
-	}
 };
 
 } /* namespace abstraction */
diff --git a/alib2common/src/abstraction/OperationAbstraction.hpp b/alib2common/src/abstraction/OperationAbstraction.hpp
index 3f9ad2e667..c3065761a8 100644
--- a/alib2common/src/abstraction/OperationAbstraction.hpp
+++ b/alib2common/src/abstraction/OperationAbstraction.hpp
@@ -30,19 +30,24 @@ public:
 	virtual bool inputsAttached ( ) const = 0;
 	virtual bool run ( ) = 0;
 	virtual bool eval ( ) = 0;
-	virtual ext::type_index type ( ) const = 0;
-	virtual ext::type_index runtime_type ( ) const = 0;
 	virtual unsigned numberOfParams ( ) const = 0;
 	virtual bool cached ( ) const = 0;
 
-	virtual std::shared_ptr < OperationAbstraction > getXmlParserFromParam ( unsigned index, ext::deque < sax::Token > tokens ) const = 0;
-	virtual std::shared_ptr < OperationAbstraction > getImmediateValueFromParam ( unsigned index, const std::string & data ) const = 0;
+	virtual ext::type_index getParamTypeIndex ( unsigned index ) const = 0;
+	virtual ext::type_index getReturnTypeIndex ( ) const = 0;
+	virtual ext::type_index getRuntimeReturnTypeIndex ( ) const = 0;
 
-	virtual std::shared_ptr < OperationAbstraction > getXmlFileWriterFromResult ( const std::string & filename ) const = 0;
-	virtual std::shared_ptr < OperationAbstraction > getValuePrinterFromResult ( ) const = 0;
-	virtual std::shared_ptr < OperationAbstraction > getCastFromResult ( const std::string & type, bool & normalize ) const = 0;
-	virtual std::shared_ptr < OperationAbstraction > getNormalizeResult ( ) const = 0;
-	virtual std::shared_ptr < OperationAbstraction > getDowncastResult ( ) const = 0;
+	std::string getParamType ( unsigned index ) const {
+		return ext::to_string ( getParamTypeIndex ( index ) );
+	}
+
+	std::string getReturnType ( ) const {
+		return ext::to_string ( getReturnTypeIndex ( ) );
+	}
+
+	std::string getRuntimeReturnType ( ) const {
+		return ext::to_string ( getRuntimeReturnTypeIndex ( ) );
+	}
 
 };
 
diff --git a/alib2common/src/abstraction/UnaryOperationAbstraction.hpp b/alib2common/src/abstraction/UnaryOperationAbstraction.hpp
index 34f072a353..074e7b79eb 100644
--- a/alib2common/src/abstraction/UnaryOperationAbstraction.hpp
+++ b/alib2common/src/abstraction/UnaryOperationAbstraction.hpp
@@ -69,21 +69,13 @@ public:
 		return 1;
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getImmediateValueFromParam ( unsigned index, const std::string & value ) const override {
+	virtual ext::type_index getParamTypeIndex ( unsigned index ) const override {
 		if ( index != 0 )
 			throw std::out_of_range ( "Invalid input index" );
 
-		std::string paramType = ext::to_string < ParamType > ( );
-		return Registry::getImmediateAbstraction ( std::move ( paramType ), value );
+		return ext::type_index ( typeid ( ParamType ) );
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getXmlParserFromParam ( unsigned index, ext::deque < sax::Token > tokens ) const override {
-		if ( index != 0 )
-			throw exception::CommonException ( "Out of range index: " + ext::to_string ( index ) + " max: " + ext::to_string ( numberOfParams ( ) ) + "." );
-
-		std::string paramType = ext::to_string < ParamType > ( );
-		return Registry::getXmlParserAbstraction ( std::move ( paramType ), std::move ( tokens ) );
-	}
 };
 
 } /* namespace abstraction */
diff --git a/alib2common/src/abstraction/ValueOperationAbstraction.hpp b/alib2common/src/abstraction/ValueOperationAbstraction.hpp
index 5f6c50bf5f..d29837b162 100644
--- a/alib2common/src/abstraction/ValueOperationAbstraction.hpp
+++ b/alib2common/src/abstraction/ValueOperationAbstraction.hpp
@@ -44,37 +44,11 @@ public:
 		return * m_data;
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getXmlFileWriterFromResult ( const std::string & filename ) const override {
-		std::string param = ext::to_string < ReturnType > ( );
-		return Registry::getXmlFileWriterAbstraction ( param, filename );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getValuePrinterFromResult ( ) const override {
-		std::string param = ext::to_string < ReturnType > ( );
-		return Registry::getValuePrinterAbstraction ( param );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getCastFromResult ( const std::string & target, bool & normalize ) const override {
-		std::string param = ext::to_string < ReturnType > ( );
-		return Registry::getCastAbstraction ( target, param, normalize );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getNormalizeResult ( ) const override {
-		std::string param = ext::to_string < ReturnType > ( );
-		return Registry::getNormalizeAbstraction ( param );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getDowncastResult ( ) const override {
-		std::string base = ext::to_string < ReturnType > ( );
-		std::string concrete = ext::to_string ( runtime_type ( ) );
-		return Registry::getDowncastAbstraction ( concrete, base );
-	}
-
-	virtual ext::type_index type ( ) const override {
+	virtual ext::type_index getReturnTypeIndex ( ) const override {
 		return ext::type_index ( typeid ( ReturnType ) );
 	}
 
-	virtual ext::type_index runtime_type ( ) const override {
+	virtual ext::type_index getRuntimeReturnTypeIndex ( ) const override {
 		if ( isReady ( ) )
 			return ext::type_index ( typeid ( getData ( ) ) );
 		else
diff --git a/alib2common/src/abstraction/VoidOperationAbstraction.hpp b/alib2common/src/abstraction/VoidOperationAbstraction.hpp
index 2df106a3a7..28c2ff4736 100644
--- a/alib2common/src/abstraction/VoidOperationAbstraction.hpp
+++ b/alib2common/src/abstraction/VoidOperationAbstraction.hpp
@@ -25,31 +25,11 @@ public:
 		callback ( input->getData ( ) );
 	}
 
-	virtual std::shared_ptr < OperationAbstraction > getXmlFileWriterFromResult ( const std::string & ) const override {
-		throw exception::CommonException ( "Void algorithm does not have a result." );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getValuePrinterFromResult ( ) const override {
-		throw exception::CommonException ( "Void algorithm does not have a result." );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getCastFromResult ( const std::string &, bool & ) const override {
-		throw exception::CommonException ( "Void algorithm does not have a result." );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getNormalizeResult ( ) const override {
-		throw exception::CommonException ( "Void algorithm does not have a result." );
-	}
-
-	virtual std::shared_ptr < OperationAbstraction > getDowncastResult ( ) const override {
-		throw exception::CommonException ( "Void algorithm does not have a result." );
-	}
-
-	virtual ext::type_index type ( ) const override {
+	virtual ext::type_index getReturnTypeIndex ( ) const override {
 		return ext::type_index ( typeid ( void ) );
 	}
 
-	virtual ext::type_index runtime_type ( ) const override {
+	virtual ext::type_index getRuntimeReturnTypeIndex ( ) const override {
 		return ext::type_index ( typeid ( void ) );
 	}
 
-- 
GitLab