Skip to content
Snippets Groups Projects
Commit b441900a authored by Jan Trávníček's avatar Jan Trávníček
Browse files

Abstraction: make run function private

parent 57d085d4
No related branches found
No related tags found
1 merge request!47Redesign abstraction
Showing
with 24 additions and 92 deletions
...@@ -25,12 +25,6 @@ public: ...@@ -25,12 +25,6 @@ public:
} }
   
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->inputsReady ( ) )
return false;
if ( this->cached ( ) )
return true;
this->template run_helper < ParamTypes ... > ( m_callback, this->m_params ); this->template run_helper < ParamTypes ... > ( m_callback, this->m_params );
return true; return true;
} }
......
...@@ -16,12 +16,6 @@ template < class ReturnType, class ParamType > ...@@ -16,12 +16,6 @@ template < class ReturnType, class ParamType >
class CastAbstraction : public UnaryOperationAbstraction < ReturnType, const ParamType & > { class CastAbstraction : public UnaryOperationAbstraction < ReturnType, const ParamType & > {
public: public:
virtual bool run ( ) override { 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 ); 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 ) ); this->m_data = ReturnType ( retrieveValue < const ParamType & > ( param.first, param.second ) );
   
......
...@@ -26,12 +26,6 @@ public: ...@@ -26,12 +26,6 @@ public:
} }
   
virtual bool run ( ) override { 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; 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 > > ( ); std::shared_ptr < OperationAbstraction > reference = std::make_shared < ReferenceAbstraction < typename std::remove_reference < ObjectType >::type > > ( );
......
...@@ -18,12 +18,6 @@ template < class ReturnType, class ParamType > ...@@ -18,12 +18,6 @@ template < class ReturnType, class ParamType >
class NormalizeAbstraction : public UnaryOperationAbstraction < ReturnType, ParamType && > { class NormalizeAbstraction : public UnaryOperationAbstraction < ReturnType, ParamType && > {
public: public:
virtual bool run ( ) override { 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 ); std::pair < std::shared_ptr < OperationAbstraction >, bool > & rawParam = std::get < 0 > ( this->m_params );
ParamType && param = retrieveValue < ParamType && > ( rawParam.first, rawParam.second ); ParamType && param = retrieveValue < ParamType && > ( rawParam.first, rawParam.second );
ReturnType res = factory::NormalizeFactory::normalize ( std::move ( param ) ); ReturnType res = factory::NormalizeFactory::normalize ( std::move ( param ) );
......
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
namespace abstraction { namespace abstraction {
   
class OperationAbstraction : public std::enable_shared_from_this < OperationAbstraction > { class OperationAbstraction : public std::enable_shared_from_this < OperationAbstraction > {
protected:
virtual bool run ( ) = 0;
public: public:
virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) = 0; virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) = 0;
virtual bool detachInput ( unsigned index ) = 0; virtual bool detachInput ( unsigned index ) = 0;
...@@ -24,7 +27,6 @@ public: ...@@ -24,7 +27,6 @@ public:
   
virtual bool inputsReady ( ) const = 0; virtual bool inputsReady ( ) const = 0;
virtual bool inputsAttached ( ) const = 0; virtual bool inputsAttached ( ) const = 0;
virtual bool run ( ) = 0;
virtual bool eval ( ) = 0; virtual bool eval ( ) = 0;
virtual unsigned numberOfParams ( ) const = 0; virtual unsigned numberOfParams ( ) const = 0;
virtual bool cached ( ) const = 0; virtual bool cached ( ) const = 0;
......
...@@ -83,13 +83,7 @@ public: ...@@ -83,13 +83,7 @@ public:
} }
   
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->inputsReady ( ) ) return m_abstractions [ m_resultId ]->eval ( );
return false;
if ( this->cached ( ) )
return true;
return m_abstractions [ m_resultId ]->run ( );
} }
   
virtual bool eval ( ) override { virtual bool eval ( ) override {
...@@ -99,7 +93,7 @@ public: ...@@ -99,7 +93,7 @@ public:
if ( this->cached ( ) ) if ( this->cached ( ) )
return true; return true;
   
return m_abstractions [ m_resultId ]->eval ( ); return this->run ( );
} }
   
virtual unsigned numberOfParams ( ) const override { virtual unsigned numberOfParams ( ) const override {
......
...@@ -16,12 +16,6 @@ template < class Type > ...@@ -16,12 +16,6 @@ template < class Type >
class ReferenceAbstraction : public UnaryOperationAbstraction < Type *, Type & > { class ReferenceAbstraction : public UnaryOperationAbstraction < Type *, Type & > {
public: public:
virtual bool run ( ) override { 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 ); std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params );
this->m_data = & retrieveValue < Type & > ( param.first, param.second ); this->m_data = & retrieveValue < Type & > ( param.first, param.second );
   
......
...@@ -18,12 +18,6 @@ template < class ParamType > ...@@ -18,12 +18,6 @@ template < class ParamType >
class SetAbstraction : public AnyaryOperationAbstraction < ext::set < ParamType >, ParamType > { class SetAbstraction : public AnyaryOperationAbstraction < ext::set < ParamType >, ParamType > {
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->inputsReady ( ) )
return false;
if ( this->cached ( ) )
return true;
ext::set < ParamType > theSet; ext::set < ParamType > theSet;
for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : this->m_params ) { for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : this->m_params ) {
theSet.insert ( abstraction::retrieveValue < ParamType > ( param.first, param.second ) ); theSet.insert ( abstraction::retrieveValue < ParamType > ( param.first, param.second ) );
......
...@@ -22,12 +22,6 @@ public: ...@@ -22,12 +22,6 @@ public:
} }
   
virtual bool run ( ) override { 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 ); 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; m_ostream << retrieveValue < const ParamType & > ( param.first, param.second ) << std::endl;
return true; return true;
......
...@@ -18,7 +18,7 @@ class BaseWrapperAbstraction : public OperationAbstraction { ...@@ -18,7 +18,7 @@ class BaseWrapperAbstraction : public OperationAbstraction {
protected: protected:
std::function < std::shared_ptr < OperationAbstraction > ( ParamTypes ... ) > m_WrapperFinder; 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; ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( ParamTypes ) > m_params;
   
public: public:
...@@ -93,7 +93,7 @@ public: ...@@ -93,7 +93,7 @@ public:
} }
   
virtual bool isReady ( ) const override { 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 { virtual bool cached ( ) const override {
...@@ -106,10 +106,18 @@ public: ...@@ -106,10 +106,18 @@ public:
   
virtual std::shared_ptr < abstraction::OperationAbstraction > getProxyAbstraction ( ) override { virtual std::shared_ptr < abstraction::OperationAbstraction > getProxyAbstraction ( ) override {
if ( this->isReady ( ) ) if ( this->isReady ( ) )
return this->m_data.value ( )->getProxyAbstraction ( ); return this->m_abstraction->getProxyAbstraction ( );
else else
throw std::domain_error ( "Proxy abstraction not avaiable before evaluation." ); 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 > template < class ReturnType, class ... ParamTypes >
...@@ -119,17 +127,14 @@ public: ...@@ -119,17 +127,14 @@ public:
} }
   
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->m_data ) this->m_abstraction = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params );
this->m_data = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params );
   
std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.value ( ); if ( this->m_abstraction->getReturnTypeIndex ( ) != this->getReturnTypeIndex ( ) )
if ( abstraction->getReturnTypeIndex ( ) != this->getReturnTypeIndex ( ) )
throw std::domain_error ( "Expected and provided types do not match" ); throw std::domain_error ( "Expected and provided types do not match" );
   
for ( unsigned index = 0; index < sizeof ... ( ParamTypes ); ++ index ) this->attachInputsToAbstraction ( );
abstraction->attachInput ( this->m_params [ index ].first, index, this->m_params [ index ].second, true );
   
return abstraction->run ( ); return this->m_abstraction->eval ( );
} }
   
virtual ext::type_index getReturnTypeIndex ( ) const override { virtual ext::type_index getReturnTypeIndex ( ) const override {
...@@ -152,14 +157,11 @@ public: ...@@ -152,14 +157,11 @@ public:
} }
   
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->m_data ) this->m_abstraction = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params );
this->m_data = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params );
   
std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.value ( ); this->attachInputsToAbstraction ( );
for ( unsigned index = 0; index < sizeof ... ( ParamTypes ); ++ index )
abstraction->attachInput ( this->m_params [ index ].first, index, this->m_params [ index ].second, true );
   
return abstraction->run ( ); return this->m_abstraction->eval ( );
} }
   
virtual ext::type_index getReturnTypeIndex ( ) const override { virtual ext::type_index getReturnTypeIndex ( ) const override {
...@@ -168,7 +170,7 @@ public: ...@@ -168,7 +170,7 @@ public:
   
virtual ext::type_index getRuntimeReturnTypeIndex ( ) const override { virtual ext::type_index getRuntimeReturnTypeIndex ( ) const override {
if ( this->isReady ( ) ) if ( this->isReady ( ) )
return this->m_data.value ( )->getProxyAbstraction ( )->getRuntimeReturnTypeIndex ( ); return this->m_abstraction->getProxyAbstraction ( )->getRuntimeReturnTypeIndex ( );
else else
throw std::domain_error ( "Runtime type unknown before evaluation." ); throw std::domain_error ( "Runtime type unknown before evaluation." );
} }
......
...@@ -17,9 +17,6 @@ template < class ReturnType > ...@@ -17,9 +17,6 @@ template < class ReturnType >
class RawReaderAbstraction : public UnaryOperationAbstraction < ReturnType, std::string && > { class RawReaderAbstraction : public UnaryOperationAbstraction < ReturnType, std::string && > {
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( this->isReady ( ) )
return true;
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); 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 ) ); ReturnType res = factory::RawDataFactory::fromString ( abstraction::retrieveValue < std::string && > ( param.first, param.second ) );
this->m_data = std::move ( res ); this->m_data = std::move ( res );
......
...@@ -17,9 +17,6 @@ template < class ParamType > ...@@ -17,9 +17,6 @@ template < class ParamType >
class RawWriterAbstraction : public UnaryOperationAbstraction < std::string, const ParamType & > { class RawWriterAbstraction : public UnaryOperationAbstraction < std::string, const ParamType & > {
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->inputsReady ( ) )
return false;
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); 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 ) ); this->m_data = factory::RawDataFactory::toString ( abstraction::retrieveValue < const ParamType & > ( param.first, param.second ) );
return true; return true;
......
...@@ -17,9 +17,6 @@ template < class ReturnType > ...@@ -17,9 +17,6 @@ template < class ReturnType >
class StringReaderAbstraction : public UnaryOperationAbstraction < ReturnType, std::string && > { class StringReaderAbstraction : public UnaryOperationAbstraction < ReturnType, std::string && > {
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( this->isReady ( ) )
return true;
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); 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 ) ); ReturnType res = factory::StringDataFactory::fromString ( abstraction::retrieveValue < std::string && > ( param.first, param.second ) );
this->m_data = std::move ( res ); this->m_data = std::move ( res );
......
...@@ -17,9 +17,6 @@ template < class ParamType > ...@@ -17,9 +17,6 @@ template < class ParamType >
class StringWriterAbstraction : public UnaryOperationAbstraction < std::string, const ParamType & > { class StringWriterAbstraction : public UnaryOperationAbstraction < std::string, const ParamType & > {
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->inputsReady ( ) )
return false;
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); 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 ) ); this->m_data = factory::StringDataFactory::toString ( abstraction::retrieveValue < const ParamType & > ( param.first, param.second ) );
return true; return true;
......
...@@ -17,9 +17,6 @@ template < class ParamType > ...@@ -17,9 +17,6 @@ template < class ParamType >
class XmlComposerAbstraction : public UnaryOperationAbstraction < ext::deque < sax::Token >, const ParamType & > { class XmlComposerAbstraction : public UnaryOperationAbstraction < ext::deque < sax::Token >, const ParamType & > {
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->inputsReady ( ) )
return false;
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); 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 ) ); this->m_data = factory::XmlDataFactory::toTokens ( abstraction::retrieveValue < const ParamType & > ( param.first, param.second ) );
return true; return true;
......
...@@ -19,9 +19,6 @@ class XmlParserAbstraction : public UnaryOperationAbstraction < ReturnType, ext: ...@@ -19,9 +19,6 @@ class XmlParserAbstraction : public UnaryOperationAbstraction < ReturnType, ext:
   
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( this->isReady ( ) )
return true;
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); 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 ) ); ReturnType res = factory::XmlDataFactory::fromTokens ( abstraction::retrieveValue < ext::deque < sax::Token > && > ( param.first, param.second ) );
this->m_data = std::move ( res ); this->m_data = std::move ( res );
......
...@@ -16,9 +16,6 @@ namespace abstraction { ...@@ -16,9 +16,6 @@ namespace abstraction {
class XmlTokensComposerAbstraction : public BinaryOperationAbstraction < void, const ext::deque < sax::Token > &, const std::string & > { class XmlTokensComposerAbstraction : public BinaryOperationAbstraction < void, const ext::deque < sax::Token > &, const std::string & > {
public: public:
virtual bool run ( ) override { 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 > & param2 = std::get < 0 > ( this->m_params );
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param1 = std::get < 1 > ( 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 ) ); sax::SaxComposeInterface::composeFile ( abstraction::retrieveValue < const std::string & > ( param1.first, param1.second ), abstraction::retrieveValue < const ext::deque < sax::Token > & > ( param2.first, param2.second ) );
......
...@@ -16,9 +16,6 @@ namespace abstraction { ...@@ -16,9 +16,6 @@ namespace abstraction {
class XmlTokensParserAbstraction : public UnaryOperationAbstraction < ext::deque < sax::Token >, const std::string & > { class XmlTokensParserAbstraction : public UnaryOperationAbstraction < ext::deque < sax::Token >, const std::string & > {
public: public:
virtual bool run ( ) override { virtual bool run ( ) override {
if ( ! this->inputsReady ( ) )
return false;
std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); 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 ) ); this->m_data = sax::SaxParseInterface::parseFile ( abstraction::retrieveValue < const std::string & > ( param.first, param.second ) );
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment