From b441900a8347aa88f93d0a61f891dbf597818de1 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Fri, 14 Dec 2018 22:34:14 +0100 Subject: [PATCH] Abstraction: make run function private --- .../src/abstraction/AlgorithmAbstraction.hpp | 6 ---- .../src/abstraction/CastAbstraction.hpp | 6 ---- .../src/abstraction/MemberAbstraction.hpp | 6 ---- .../src/abstraction/NormalizeAbstraction.hpp | 6 ---- .../src/abstraction/OperationAbstraction.hpp | 4 ++- .../src/abstraction/PackingAbstraction.hpp | 10 ++---- .../src/abstraction/ReferenceAbstraction.hpp | 6 ---- .../src/abstraction/SetAbstraction.hpp | 6 ---- .../abstraction/ValuePrinterAbstraction.hpp | 6 ---- .../src/abstraction/WrapperAbstraction.hpp | 36 ++++++++++--------- .../src/abstraction/RawReaderAbstraction.hpp | 3 -- .../src/abstraction/RawWriterAbstraction.hpp | 3 -- .../abstraction/StringReaderAbstraction.hpp | 3 -- .../abstraction/StringWriterAbstraction.hpp | 3 -- .../abstraction/XmlComposerAbstraction.hpp | 3 -- .../src/abstraction/XmlParserAbstraction.hpp | 3 -- .../XmlTokensComposerAbstraction.hpp | 3 -- .../XmlTokensParserAbstraction.hpp | 3 -- 18 files changed, 24 insertions(+), 92 deletions(-) diff --git a/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp b/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp index 5026b83459..a5ce5f239a 100644 --- a/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp +++ b/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp @@ -25,12 +25,6 @@ public: } virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - this->template run_helper < ParamTypes ... > ( m_callback, this->m_params ); return true; } diff --git a/alib2abstraction/src/abstraction/CastAbstraction.hpp b/alib2abstraction/src/abstraction/CastAbstraction.hpp index c1b1a7d2e1..d9ce574285 100644 --- a/alib2abstraction/src/abstraction/CastAbstraction.hpp +++ b/alib2abstraction/src/abstraction/CastAbstraction.hpp @@ -16,12 +16,6 @@ template < class ReturnType, class ParamType > class CastAbstraction : public UnaryOperationAbstraction < ReturnType, const ParamType & > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); this->m_data = ReturnType ( retrieveValue < const ParamType & > ( param.first, param.second ) ); diff --git a/alib2abstraction/src/abstraction/MemberAbstraction.hpp b/alib2abstraction/src/abstraction/MemberAbstraction.hpp index fe743cd8a3..05661f5ba5 100644 --- a/alib2abstraction/src/abstraction/MemberAbstraction.hpp +++ b/alib2abstraction/src/abstraction/MemberAbstraction.hpp @@ -26,12 +26,6 @@ public: } virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( ParamTypes ) + 1 > params = this->m_params; std::shared_ptr < OperationAbstraction > reference = std::make_shared < ReferenceAbstraction < typename std::remove_reference < ObjectType >::type > > ( ); diff --git a/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp b/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp index 63cc53532a..71f3649450 100644 --- a/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp +++ b/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp @@ -18,12 +18,6 @@ template < class ReturnType, class ParamType > class NormalizeAbstraction : public UnaryOperationAbstraction < ReturnType, ParamType && > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & rawParam = std::get < 0 > ( this->m_params ); ParamType && param = retrieveValue < ParamType && > ( rawParam.first, rawParam.second ); ReturnType res = factory::NormalizeFactory::normalize ( std::move ( param ) ); diff --git a/alib2abstraction/src/abstraction/OperationAbstraction.hpp b/alib2abstraction/src/abstraction/OperationAbstraction.hpp index d4868846c2..64b73fa944 100644 --- a/alib2abstraction/src/abstraction/OperationAbstraction.hpp +++ b/alib2abstraction/src/abstraction/OperationAbstraction.hpp @@ -15,6 +15,9 @@ namespace abstraction { class OperationAbstraction : public std::enable_shared_from_this < OperationAbstraction > { +protected: + virtual bool run ( ) = 0; + public: virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) = 0; virtual bool detachInput ( unsigned index ) = 0; @@ -24,7 +27,6 @@ public: virtual bool inputsReady ( ) const = 0; virtual bool inputsAttached ( ) const = 0; - virtual bool run ( ) = 0; virtual bool eval ( ) = 0; virtual unsigned numberOfParams ( ) const = 0; virtual bool cached ( ) const = 0; diff --git a/alib2abstraction/src/abstraction/PackingAbstraction.hpp b/alib2abstraction/src/abstraction/PackingAbstraction.hpp index cf45b9060c..9f9ab75764 100644 --- a/alib2abstraction/src/abstraction/PackingAbstraction.hpp +++ b/alib2abstraction/src/abstraction/PackingAbstraction.hpp @@ -83,13 +83,7 @@ public: } virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - - return m_abstractions [ m_resultId ]->run ( ); + return m_abstractions [ m_resultId ]->eval ( ); } virtual bool eval ( ) override { @@ -99,7 +93,7 @@ public: if ( this->cached ( ) ) return true; - return m_abstractions [ m_resultId ]->eval ( ); + return this->run ( ); } virtual unsigned numberOfParams ( ) const override { diff --git a/alib2abstraction/src/abstraction/ReferenceAbstraction.hpp b/alib2abstraction/src/abstraction/ReferenceAbstraction.hpp index 0fadebed6b..7103c96158 100644 --- a/alib2abstraction/src/abstraction/ReferenceAbstraction.hpp +++ b/alib2abstraction/src/abstraction/ReferenceAbstraction.hpp @@ -16,12 +16,6 @@ template < class Type > class ReferenceAbstraction : public UnaryOperationAbstraction < Type *, Type & > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); this->m_data = & retrieveValue < Type & > ( param.first, param.second ); diff --git a/alib2abstraction/src/abstraction/SetAbstraction.hpp b/alib2abstraction/src/abstraction/SetAbstraction.hpp index 98dfc3b032..e749433002 100644 --- a/alib2abstraction/src/abstraction/SetAbstraction.hpp +++ b/alib2abstraction/src/abstraction/SetAbstraction.hpp @@ -18,12 +18,6 @@ template < class ParamType > class SetAbstraction : public AnyaryOperationAbstraction < ext::set < ParamType >, ParamType > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - ext::set < ParamType > theSet; for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : this->m_params ) { theSet.insert ( abstraction::retrieveValue < ParamType > ( param.first, param.second ) ); diff --git a/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp b/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp index 4e2b74fe07..665258ed2b 100644 --- a/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp +++ b/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp @@ -22,12 +22,6 @@ public: } virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - - if ( this->cached ( ) ) - return true; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); m_ostream << retrieveValue < const ParamType & > ( param.first, param.second ) << std::endl; return true; diff --git a/alib2abstraction/src/abstraction/WrapperAbstraction.hpp b/alib2abstraction/src/abstraction/WrapperAbstraction.hpp index 28311daad4..b32bfb3ad7 100644 --- a/alib2abstraction/src/abstraction/WrapperAbstraction.hpp +++ b/alib2abstraction/src/abstraction/WrapperAbstraction.hpp @@ -18,7 +18,7 @@ class BaseWrapperAbstraction : public OperationAbstraction { protected: std::function < std::shared_ptr < OperationAbstraction > ( ParamTypes ... ) > m_WrapperFinder; - std::optional < std::shared_ptr < OperationAbstraction > > m_data; + std::shared_ptr < OperationAbstraction > m_abstraction; ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( ParamTypes ) > m_params; public: @@ -93,7 +93,7 @@ public: } virtual bool isReady ( ) const override { - return ( bool ) m_data && m_data.value ( )->isReady ( ); + return ( bool ) m_abstraction && m_abstraction->isReady ( ); } virtual bool cached ( ) const override { @@ -106,10 +106,18 @@ public: virtual std::shared_ptr < abstraction::OperationAbstraction > getProxyAbstraction ( ) override { if ( this->isReady ( ) ) - return this->m_data.value ( )->getProxyAbstraction ( ); + return this->m_abstraction->getProxyAbstraction ( ); else throw std::domain_error ( "Proxy abstraction not avaiable before evaluation." ); } + + void attachInputsToAbstraction ( ) { + for ( unsigned index = 0; index < sizeof ... ( ParamTypes ); ++ index ) + if ( ! this->m_abstraction->attachInput ( this->m_params [ index ].first, index, this->m_params [ index ].second, true ) ) + throw std::invalid_argument ( "Can't connect param " + this->m_abstraction->getParamType ( index ) + " at " + ext::to_string ( index ) + " of wrapped algorithm with result of type " + this->m_params [ index ].first->getReturnType ( ) + "." ); + + } + }; template < class ReturnType, class ... ParamTypes > @@ -119,17 +127,14 @@ public: } virtual bool run ( ) override { - if ( ! this->m_data ) - this->m_data = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params ); + this->m_abstraction = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params ); - std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.value ( ); - if ( abstraction->getReturnTypeIndex ( ) != this->getReturnTypeIndex ( ) ) + if ( this->m_abstraction->getReturnTypeIndex ( ) != this->getReturnTypeIndex ( ) ) throw std::domain_error ( "Expected and provided types do not match" ); - for ( unsigned index = 0; index < sizeof ... ( ParamTypes ); ++ index ) - abstraction->attachInput ( this->m_params [ index ].first, index, this->m_params [ index ].second, true ); + this->attachInputsToAbstraction ( ); - return abstraction->run ( ); + return this->m_abstraction->eval ( ); } virtual ext::type_index getReturnTypeIndex ( ) const override { @@ -152,14 +157,11 @@ public: } virtual bool run ( ) override { - if ( ! this->m_data ) - this->m_data = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params ); + this->m_abstraction = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params ); - std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.value ( ); - for ( unsigned index = 0; index < sizeof ... ( ParamTypes ); ++ index ) - abstraction->attachInput ( this->m_params [ index ].first, index, this->m_params [ index ].second, true ); + this->attachInputsToAbstraction ( ); - return abstraction->run ( ); + return this->m_abstraction->eval ( ); } virtual ext::type_index getReturnTypeIndex ( ) const override { @@ -168,7 +170,7 @@ public: virtual ext::type_index getRuntimeReturnTypeIndex ( ) const override { if ( this->isReady ( ) ) - return this->m_data.value ( )->getProxyAbstraction ( )->getRuntimeReturnTypeIndex ( ); + return this->m_abstraction->getProxyAbstraction ( )->getRuntimeReturnTypeIndex ( ); else throw std::domain_error ( "Runtime type unknown before evaluation." ); } diff --git a/alib2raw/src/abstraction/RawReaderAbstraction.hpp b/alib2raw/src/abstraction/RawReaderAbstraction.hpp index bcce1b0da4..dbfb0a470e 100644 --- a/alib2raw/src/abstraction/RawReaderAbstraction.hpp +++ b/alib2raw/src/abstraction/RawReaderAbstraction.hpp @@ -17,9 +17,6 @@ template < class ReturnType > class RawReaderAbstraction : public UnaryOperationAbstraction < ReturnType, std::string && > { public: virtual bool run ( ) override { - if ( this->isReady ( ) ) - return true; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); ReturnType res = factory::RawDataFactory::fromString ( abstraction::retrieveValue < std::string && > ( param.first, param.second ) ); this->m_data = std::move ( res ); diff --git a/alib2raw/src/abstraction/RawWriterAbstraction.hpp b/alib2raw/src/abstraction/RawWriterAbstraction.hpp index 0f322dd53b..768c62de8f 100644 --- a/alib2raw/src/abstraction/RawWriterAbstraction.hpp +++ b/alib2raw/src/abstraction/RawWriterAbstraction.hpp @@ -17,9 +17,6 @@ template < class ParamType > class RawWriterAbstraction : public UnaryOperationAbstraction < std::string, const ParamType & > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); this->m_data = factory::RawDataFactory::toString ( abstraction::retrieveValue < const ParamType & > ( param.first, param.second ) ); return true; diff --git a/alib2str/src/abstraction/StringReaderAbstraction.hpp b/alib2str/src/abstraction/StringReaderAbstraction.hpp index 8fa35e4dda..a37910068a 100644 --- a/alib2str/src/abstraction/StringReaderAbstraction.hpp +++ b/alib2str/src/abstraction/StringReaderAbstraction.hpp @@ -17,9 +17,6 @@ template < class ReturnType > class StringReaderAbstraction : public UnaryOperationAbstraction < ReturnType, std::string && > { public: virtual bool run ( ) override { - if ( this->isReady ( ) ) - return true; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); ReturnType res = factory::StringDataFactory::fromString ( abstraction::retrieveValue < std::string && > ( param.first, param.second ) ); this->m_data = std::move ( res ); diff --git a/alib2str/src/abstraction/StringWriterAbstraction.hpp b/alib2str/src/abstraction/StringWriterAbstraction.hpp index 77a7198b41..6ea7f82ec6 100644 --- a/alib2str/src/abstraction/StringWriterAbstraction.hpp +++ b/alib2str/src/abstraction/StringWriterAbstraction.hpp @@ -17,9 +17,6 @@ template < class ParamType > class StringWriterAbstraction : public UnaryOperationAbstraction < std::string, const ParamType & > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); this->m_data = factory::StringDataFactory::toString ( abstraction::retrieveValue < const ParamType & > ( param.first, param.second ) ); return true; diff --git a/alib2xml/src/abstraction/XmlComposerAbstraction.hpp b/alib2xml/src/abstraction/XmlComposerAbstraction.hpp index 28699eedc3..4871cc25c5 100644 --- a/alib2xml/src/abstraction/XmlComposerAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlComposerAbstraction.hpp @@ -17,9 +17,6 @@ template < class ParamType > class XmlComposerAbstraction : public UnaryOperationAbstraction < ext::deque < sax::Token >, const ParamType & > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); this->m_data = factory::XmlDataFactory::toTokens ( abstraction::retrieveValue < const ParamType & > ( param.first, param.second ) ); return true; diff --git a/alib2xml/src/abstraction/XmlParserAbstraction.hpp b/alib2xml/src/abstraction/XmlParserAbstraction.hpp index d01bdac8d6..14748eb013 100644 --- a/alib2xml/src/abstraction/XmlParserAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlParserAbstraction.hpp @@ -19,9 +19,6 @@ class XmlParserAbstraction : public UnaryOperationAbstraction < ReturnType, ext: public: virtual bool run ( ) override { - if ( this->isReady ( ) ) - return true; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); ReturnType res = factory::XmlDataFactory::fromTokens ( abstraction::retrieveValue < ext::deque < sax::Token > && > ( param.first, param.second ) ); this->m_data = std::move ( res ); diff --git a/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp b/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp index f3e0a35b33..2da5fa59f6 100644 --- a/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp @@ -16,9 +16,6 @@ namespace abstraction { class XmlTokensComposerAbstraction : public BinaryOperationAbstraction < void, const ext::deque < sax::Token > &, const std::string & > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param2 = std::get < 0 > ( this->m_params ); std::pair < std::shared_ptr < OperationAbstraction >, bool > & param1 = std::get < 1 > ( this->m_params ); sax::SaxComposeInterface::composeFile ( abstraction::retrieveValue < const std::string & > ( param1.first, param1.second ), abstraction::retrieveValue < const ext::deque < sax::Token > & > ( param2.first, param2.second ) ); diff --git a/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp b/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp index 5aa5e16aa2..25c879dc60 100644 --- a/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp @@ -16,9 +16,6 @@ namespace abstraction { class XmlTokensParserAbstraction : public UnaryOperationAbstraction < ext::deque < sax::Token >, const std::string & > { public: virtual bool run ( ) override { - if ( ! this->inputsReady ( ) ) - return false; - std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); this->m_data = sax::SaxParseInterface::parseFile ( abstraction::retrieveValue < const std::string & > ( param.first, param.second ) ); return true; -- GitLab