From 2024fafbc648a37ce0b4cfa4bdd3b548b21c217f Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 14 Nov 2019 08:25:57 +0100
Subject: [PATCH] take out result interpretation for cli from environment

---
 alib2cli/src/common/ResultInterpret.h         | 31 +++++++++++++++++++
 alib2cli/src/environment/Environment.h        | 18 ++---------
 .../test-src/testing/TimeoutAqlTest.cpp       |  4 ++-
 aql2/src/aql.cpp                              |  4 ++-
 4 files changed, 39 insertions(+), 18 deletions(-)
 create mode 100644 alib2cli/src/common/ResultInterpret.h

diff --git a/alib2cli/src/common/ResultInterpret.h b/alib2cli/src/common/ResultInterpret.h
new file mode 100644
index 0000000000..140edece21
--- /dev/null
+++ b/alib2cli/src/common/ResultInterpret.h
@@ -0,0 +1,31 @@
+#ifndef _CLI_RESULT_INTERPRET_H_
+#define _CLI_RESULT_INTERPRET_H_
+
+#include <memory>
+
+namespace cli {
+
+class ResultInterpret {
+public:
+	static int cli ( std::shared_ptr < abstraction::Value > result ) {
+		if ( result ) {
+			std::shared_ptr < abstraction::ValueHolderInterface < int > > ptr1 = std::dynamic_pointer_cast < abstraction::ValueHolderInterface < int > > ( result );
+			if ( ptr1 )
+				return ptr1->getValue ( );
+			std::shared_ptr < abstraction::ValueHolderInterface < unsigned > > ptr2 = std::dynamic_pointer_cast < abstraction::ValueHolderInterface < unsigned > > ( result );
+			if ( ptr2 )
+				return ptr2->getValue ( );
+			std::shared_ptr < abstraction::ValueHolderInterface < bool > > ptr3 = std::dynamic_pointer_cast < abstraction::ValueHolderInterface < bool > > ( result );
+			if ( ptr3 )
+				return static_cast < int > ( ! ptr3->getValue ( ) );
+
+			throw exception::CommonException ( "Invalid result type. Provided: " + result->getType ( ) );
+		} else {
+			return 0;
+		}
+	}
+};
+
+} /* namespace cli */
+
+#endif /* _CLI_RESULT_INTERPRET_H_ */
diff --git a/alib2cli/src/environment/Environment.h b/alib2cli/src/environment/Environment.h
index 2e29d6f757..b7d7fc72d0 100644
--- a/alib2cli/src/environment/Environment.h
+++ b/alib2cli/src/environment/Environment.h
@@ -111,22 +111,8 @@ public:
 		m_result = std::move ( value );
 	}
 
-	int getResult ( ) const {
-		if ( m_result ) {
-			std::shared_ptr < abstraction::ValueHolderInterface < int > > ptr1 = std::dynamic_pointer_cast < abstraction::ValueHolderInterface < int > > ( m_result );
-			if ( ptr1 )
-				return ptr1->getValue ( );
-			std::shared_ptr < abstraction::ValueHolderInterface < unsigned > > ptr2 = std::dynamic_pointer_cast < abstraction::ValueHolderInterface < unsigned > > ( m_result );
-			if ( ptr2 )
-				return ptr2->getValue ( );
-			std::shared_ptr < abstraction::ValueHolderInterface < bool > > ptr3 = std::dynamic_pointer_cast < abstraction::ValueHolderInterface < bool > > ( m_result );
-			if ( ptr3 )
-				return static_cast < int > ( ! ptr3->getValue ( ) );
-
-			throw exception::CommonException ( "Invalid result type. Provided: " + m_result->getType ( ) );
-		} else {
-			return 0;
-		}
+	std::shared_ptr < abstraction::Value > getResult ( ) const {
+		return m_result;
 	}
 
 	cli::CommandResult execute ( std::shared_ptr < cli::LineInterface > lineInterface );
diff --git a/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp b/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp
index ed0bc38907..311b9b1ca6 100644
--- a/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp
+++ b/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp
@@ -14,6 +14,8 @@
 
 #include <readline/StringLineInterface.h>
 
+#include <common/ResultInterpret.h>
+
 #define PIPE_RD 0
 #define PIPE_WR 1
 
@@ -69,7 +71,7 @@ int aqlTest ( int fd, const ext::vector < std::string > & queries, unsigned seed
 				throw std::runtime_error ( "TimeoutAqlTest: child output write() failure (child to parent communication)" );
 		}
 
-		return environment.getResult ( ); /* 0 = OK */
+		return cli::ResultInterpret::cli ( environment.getResult ( ) ); /* 0 = OK */
 	} catch ( const std::exception & ) {
 		std::ostringstream oss;
 		alib::ExceptionHandler::handle ( oss );
diff --git a/aql2/src/aql.cpp b/aql2/src/aql.cpp
index ab47823236..28afac30ae 100644
--- a/aql2/src/aql.cpp
+++ b/aql2/src/aql.cpp
@@ -45,6 +45,8 @@ std::istream& operator>> ( std::istream & in, std::pair < T, U > & value ) {
 
 #include <prompt/ReadlinePromptHistory.h>
 
+#include <common/ResultInterpret.h>
+
 namespace TCLAP {
 
 template < class T, class U >
@@ -161,7 +163,7 @@ int main ( int argc, char * argv[] ) {
 		/* --------------------------------------------------------------------------------------------------------- */
 
 		if ( res == cli::CommandResult::QUIT )
-			return Prompt::getPrompt ( ).getEnvironment ( ).getResult ( );
+			return cli::ResultInterpret::cli ( Prompt::getPrompt ( ).getEnvironment ( ).getResult ( ) );
 		else if ( res == cli::CommandResult::EOT )
 			return 0;
 		else
-- 
GitLab