diff --git a/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp b/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp index 8f90a0a43da1648c86fb292ab5600d6d1d442492..c237e4f3aa5e69952b5fbe37d20f41c9718f0150 100644 --- a/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp +++ b/alib2abstraction/src/abstraction/AlgorithmAbstraction.hpp @@ -8,8 +8,10 @@ #ifndef _ALGORITHM_ABSTRACTION_HPP_ #define _ALGORITHM_ABSTRACTION_HPP_ -#include <abstraction/NaryOperationAbstraction.hpp> #include <alib/memory> + +#include <abstraction/NaryOperationAbstraction.hpp> + #include <registry/Registry.h> namespace abstraction { @@ -29,7 +31,7 @@ public: if ( this->cached ( ) ) return true; - this->run_helper ( m_callback, this->m_params, this->m_moves, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); + this->template run_helper < ParamTypes ... > ( m_callback, this->m_params, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); return true; } diff --git a/alib2abstraction/src/abstraction/AnyaryOperationAbstraction.hpp b/alib2abstraction/src/abstraction/AnyaryOperationAbstraction.hpp index 146d14ca5ff47c1d983364ffa0871623443da9f2..fd705dc40ae9aa7095ffa333abec6fdc4326b004 100644 --- a/alib2abstraction/src/abstraction/AnyaryOperationAbstraction.hpp +++ b/alib2abstraction/src/abstraction/AnyaryOperationAbstraction.hpp @@ -8,8 +8,11 @@ #ifndef _ANYARY_OPERATION_ABSTRACTION_HPP_ #define _ANYARY_OPERATION_ABSTRACTION_HPP_ -#include <abstraction/ValueOperationAbstraction.hpp> #include <alib/memory> + +#include <abstraction/ValueOperationAbstraction.hpp> +#include <common/AbstractionHelpers.hpp> + #include <registry/Registry.h> namespace abstraction { @@ -17,20 +20,21 @@ namespace abstraction { template < class ReturnType, class ParamType > class AnyaryOperationAbstraction : public ValueOperationAbstraction < ReturnType > { protected: - ext::vector < std::pair < std::shared_ptr < ValueProvider < ParamType > >, bool > > m_params; + ext::vector < std::pair < std::shared_ptr < OperationAbstraction >, bool > > m_params; private: - virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move ) override { - std::shared_ptr < ValueProvider < ParamType > > validData = std::dynamic_pointer_cast < ValueProvider < ParamType > > ( input->getProxyAbstraction ( ) ); - if ( validData ) { - if ( m_params.size ( ) < index + 1 ) - m_params.resize ( index + 1 ); + virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) override { + if ( input == nullptr ) + return false; - m_params [ index ] = std::make_pair ( validData, move ); - return true; - } else { + if ( checkInput && ! abstraction::CheckInput < ValueProvider < ParamType > >::checkInput ( input->getProxyAbstraction ( ), 0 /* Note: yes index zero */ ) ) return false; - } + + if ( m_params.size ( ) < index + 1 ) + m_params.resize ( index + 1 ); + + m_params [ index ] = std::make_pair ( input, move ); + return true; } virtual bool detachInput ( unsigned index ) override { @@ -48,7 +52,7 @@ public: } virtual bool inputsReady ( ) const override { - for ( const auto & param : m_params ) + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) if ( ! ( bool ) param.first || ! param.first->isReady ( ) ) return false; @@ -56,7 +60,7 @@ public: } virtual bool inputsAttached ( ) const override { - for ( const auto & param : m_params ) + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) if ( ! ( bool ) param.first ) return false; @@ -70,7 +74,7 @@ public: if ( this->cached ( ) ) return true; - for ( const std::pair < std::shared_ptr < ValueProvider < ParamType > >, bool > & param : m_params ) + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) if ( ! param.first->eval ( ) ) return false; diff --git a/alib2abstraction/src/abstraction/CastAbstraction.hpp b/alib2abstraction/src/abstraction/CastAbstraction.hpp index 0f97f9aaebca1c484c813804257ee5750eb29c0a..c1b1a7d2e1e8e6968ea920147dad2fd648c96e14 100644 --- a/alib2abstraction/src/abstraction/CastAbstraction.hpp +++ b/alib2abstraction/src/abstraction/CastAbstraction.hpp @@ -13,7 +13,7 @@ namespace abstraction { template < class ReturnType, class ParamType > -class CastAbstraction : public UnaryOperationAbstraction < ReturnType, ParamType > { +class CastAbstraction : public UnaryOperationAbstraction < ReturnType, const ParamType & > { public: virtual bool run ( ) override { if ( ! this->inputsReady ( ) ) @@ -22,7 +22,8 @@ public: if ( this->cached ( ) ) return true; - this->m_data = ReturnType ( std::get < 0 > ( this->m_params )->getConstValueReference ( ) ); + 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 ) ); return true; } diff --git a/alib2abstraction/src/abstraction/MemberAbstraction.hpp b/alib2abstraction/src/abstraction/MemberAbstraction.hpp index a8ec3a2b2e8e13fdcd611affeba2507a8eb62710..3dc85c6ecb05c09db6dbf54449725a685193e901 100644 --- a/alib2abstraction/src/abstraction/MemberAbstraction.hpp +++ b/alib2abstraction/src/abstraction/MemberAbstraction.hpp @@ -8,8 +8,10 @@ #ifndef _MEMBER_ABSTRACTION_HPP_ #define _MEMBER_ABSTRACTION_HPP_ -#include <abstraction/NaryOperationAbstraction.hpp> #include <alib/memory> + +#include <abstraction/NaryOperationAbstraction.hpp> + #include <registry/Registry.h> namespace abstraction { @@ -29,7 +31,7 @@ public: if ( this->cached ( ) ) return true; - this->member_run_helper ( m_callback, this->m_params, this->m_moves, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); + this->template member_run_helper < ObjectType, ParamTypes ... > ( m_callback, this->m_params, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); return true; } diff --git a/alib2abstraction/src/abstraction/NaryOperationAbstraction.hpp b/alib2abstraction/src/abstraction/NaryOperationAbstraction.hpp index ecdec0cf73ad3abe79e33471d2ee297e0786996b..625962c3f85f2e62ec5bdb28dc422f1e1e028158 100644 --- a/alib2abstraction/src/abstraction/NaryOperationAbstraction.hpp +++ b/alib2abstraction/src/abstraction/NaryOperationAbstraction.hpp @@ -8,9 +8,12 @@ #ifndef _NARY_OPERATION_ABSTRACTION_HPP_ #define _NARY_OPERATION_ABSTRACTION_HPP_ -#include <abstraction/ValueOperationAbstraction.hpp> #include <alib/tuple> #include <alib/memory> +#include <alib/array> + +#include <abstraction/ValueOperationAbstraction.hpp> + #include <registry/Registry.h> namespace abstraction { @@ -18,65 +21,57 @@ namespace abstraction { template < class ReturnType, class ... ParamTypes > class NaryOperationAbstraction : public ValueOperationAbstraction < ReturnType > { protected: - ext::tuple < std::shared_ptr < ValueProvider < ParamTypes > > ... > m_params; - std::array < bool, sizeof ... ( ParamTypes ) > m_moves; + ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( ParamTypes ) > m_params; private: - virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move ) override { - if ( index >= m_moves.size ( ) ) + virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) override { + if ( index >= m_params.size ( ) ) throw std::invalid_argument ( "Parameter index " + ext::to_string ( index ) + " out of bounds."); - auto attachCallback = [ & ] ( auto & param ) { - if ( param != nullptr ) - return false; + if ( input == nullptr ) + return false; - typename std::decay < decltype ( param )>::type validData = std::dynamic_pointer_cast < typename std::decay < decltype ( param ) >::type::element_type > ( input->getProxyAbstraction ( ) ); - if ( validData ) { - m_moves [ index ] = move; - param = validData; - return true; - } else { - return false; - } - }; + if ( checkInput && ! abstraction::CheckInput < ValueProvider < ParamTypes > ... >::checkInput ( input->getProxyAbstraction ( ), index ) ) + return false; - return ext::call_on_nth < bool > ( m_params, index, attachCallback ); + m_params [ index ].first = input; + m_params [ index ].second = move; + + return true; } virtual bool detachInput ( unsigned index ) override { - if ( index >= m_moves.size ( ) ) + if ( index >= m_params.size ( ) ) throw std::invalid_argument ( "Parameter index " + ext::to_string ( index ) + " out of bounds."); - m_moves [ index ] = false; - - auto detachCallback = [ & ] ( auto & param ) { - param = nullptr; - return true; - }; + m_params [ index ].first = nullptr; + m_params [ index ].second = false; - return ext::call_on_nth < bool > ( m_params, index, detachCallback ); + return true; } public: NaryOperationAbstraction ( ) { - for ( unsigned i = 0; i < sizeof ... ( ParamTypes ); ++ i ) - m_moves [ i ] = false; + for ( unsigned i = 0; i < sizeof ... ( ParamTypes ); ++ i ) { + m_params [ i ].first = nullptr; + m_params [ i ].second = false; + } } virtual bool inputsReady ( ) const override { - auto readyCallback = [ ] ( const auto & param ) { - return ( bool ) param && param->isReady ( ); - }; + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) + if ( ! param.first || ! param.first->isReady ( ) ) + return false; - return ext::all_of ( m_params, readyCallback ); + return true; } virtual bool inputsAttached ( ) const override { - auto attachedCallback = [ ] ( const auto & param ) { - return ( bool ) param; - }; + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) + if ( ! param.first ) + return false; - return ext::all_of ( m_params, attachedCallback ); + return true; } virtual bool eval ( ) override { @@ -86,12 +81,9 @@ public: if ( this->cached ( ) ) return true; - auto evalCallback = [ ] ( const auto & param ) { - return param->eval ( ); - }; - - if ( ! ext::all_of ( m_params, evalCallback ) ) - return false; + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) + if ( ! param.first->eval ( ) ) + return false; return this->run ( ); } @@ -101,10 +93,7 @@ public: } virtual ext::type_index getParamTypeIndex ( unsigned index ) const override { - auto callback = [ & ] ( auto & param ) { - return ext::type_index ( typeid ( typename std::decay < decltype ( param ) >::type::element_type::return_type ) ); - }; - return ext::call_on_nth < ext::type_index > ( m_params, index, callback ); + return abstraction::ParamType < ParamTypes ... >::paramType ( index ); } }; diff --git a/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp b/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp index e81156b1d5b715243923754db5f3f5b4057a5534..63cc53532a72e6600b626cee2a9dfc47f51c85b7 100644 --- a/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp +++ b/alib2abstraction/src/abstraction/NormalizeAbstraction.hpp @@ -15,7 +15,7 @@ namespace abstraction { template < class ReturnType, class ParamType > -class NormalizeAbstraction : public UnaryOperationAbstraction < ReturnType, ParamType > { +class NormalizeAbstraction : public UnaryOperationAbstraction < ReturnType, ParamType && > { public: virtual bool run ( ) override { if ( ! this->inputsReady ( ) ) @@ -24,7 +24,8 @@ public: if ( this->cached ( ) ) return true; - ParamType && param = std::get < 0 > ( this->m_params )->getValue ( std::get < 0 > ( this->m_moves ) ); + 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 ) ); this->m_data = std::move ( res ); diff --git a/alib2abstraction/src/abstraction/OperationAbstraction.hpp b/alib2abstraction/src/abstraction/OperationAbstraction.hpp index a0a78d926c4e1f7f10b6f6e789fa338647d66fb3..d4868846c278417b94eb882a296ceea04aa200c6 100644 --- a/alib2abstraction/src/abstraction/OperationAbstraction.hpp +++ b/alib2abstraction/src/abstraction/OperationAbstraction.hpp @@ -16,7 +16,7 @@ namespace abstraction { class OperationAbstraction : public std::enable_shared_from_this < OperationAbstraction > { public: - virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move ) = 0; + virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) = 0; virtual bool detachInput ( unsigned index ) = 0; virtual ~OperationAbstraction ( ) noexcept { diff --git a/alib2abstraction/src/abstraction/PackingAbstraction.hpp b/alib2abstraction/src/abstraction/PackingAbstraction.hpp index db0d65d8e24d89b6b53b06abc89a1f6c52f20135..cf45b9060c06d2d12cdb8497929a97be0a02d640 100644 --- a/alib2abstraction/src/abstraction/PackingAbstraction.hpp +++ b/alib2abstraction/src/abstraction/PackingAbstraction.hpp @@ -8,9 +8,11 @@ #ifndef _PACKING_ABSTRACTION_HPP_ #define _PACKING_ABSTRACTION_HPP_ -#include <abstraction/ValueOperationAbstraction.hpp> #include <alib/memory> #include <alib/array> + +#include <abstraction/ValueOperationAbstraction.hpp> + #include <registry/Registry.h> namespace abstraction { @@ -31,7 +33,7 @@ public: } bool setInnerConnection ( unsigned sourceId, unsigned targetId, unsigned paramPosition, bool move ) { - return m_abstractions [ targetId ]->attachInput ( m_abstractions [ sourceId ], paramPosition, move ); + return m_abstractions [ targetId ]->attachInput ( m_abstractions [ sourceId ], paramPosition, move, true ); } bool clearInnerConnection ( unsigned targetId, unsigned paramPosition ) { @@ -43,11 +45,11 @@ public: } private: - virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move ) override { + virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) override { bool res = true; for ( const ConnectionTarget & target : m_connections [ index ] ) - res &= m_abstractions [ target.targetId ]->attachInput ( input, target.paramPosition, move ); + res &= m_abstractions [ target.targetId ]->attachInput ( input, target.paramPosition, move, checkInput ); if ( ! res ) this->detachInput ( index ); diff --git a/alib2abstraction/src/abstraction/SetAbstraction.hpp b/alib2abstraction/src/abstraction/SetAbstraction.hpp index 21054f0af83ec09ca1b4eb4c9fbd6cfb5a0fe88b..98dfc3b0322f50dc1e76f87d45f454e3514ec491 100644 --- a/alib2abstraction/src/abstraction/SetAbstraction.hpp +++ b/alib2abstraction/src/abstraction/SetAbstraction.hpp @@ -8,9 +8,10 @@ #ifndef _SET_ABSTRACTION_HPP_ #define _SET_ABSTRACTION_HPP_ -#include <abstraction/AnyaryOperationAbstraction.hpp> #include <alib/memory> +#include <abstraction/AnyaryOperationAbstraction.hpp> + namespace abstraction { template < class ParamType > @@ -24,8 +25,8 @@ public: return true; ext::set < ParamType > theSet; - for ( const std::pair < std::shared_ptr < ValueProvider < ParamType > >, bool > & param : this->m_params ) { - theSet.insert ( param.first->getValue ( param.second ) ); + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : this->m_params ) { + theSet.insert ( abstraction::retrieveValue < ParamType > ( param.first, param.second ) ); } this->m_data = theSet; diff --git a/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp b/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp index c9d87a6c938138d31138517d7b6e8546b090d57b..d10a33fe3109fce3fbca963daefb17afcd03d72f 100644 --- a/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp +++ b/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp @@ -8,13 +8,16 @@ #ifndef _VALUE_OPERATION_ABSTRACTION_HPP_ #define _VALUE_OPERATION_ABSTRACTION_HPP_ -#include <abstraction/OperationAbstraction.hpp> #include <alib/tuple> #include <alib/memory> #include <alib/typeindex> #include <alib/variant> + +#include <abstraction/OperationAbstraction.hpp> #include <abstraction/ValueProvider.hpp> +#include <common/AbstractionHelpers.hpp> + namespace abstraction { class UnspecifiedType { @@ -34,24 +37,22 @@ protected: mutable ext::variant < void, ReturnType > m_data; public: - template < typename Callable, typename Object, typename ... Ts, size_t ... Indexes > - inline void member_run_helper ( Callable callback, const ext::tuple < Object, Ts ... > & inputs, const std::array < bool, 1 + sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename Object, typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void member_run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, 1 + sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) - m_data = callback ( & std::get < 0 > ( inputs )->getValue ( false ), std::get < Indexes + 1 > ( inputs )->getValue ( std::get < Indexes + 1 > ( moves ) ) ... ); + m_data = callback ( & abstraction::retrieveValue < Object > ( std::get < 0 > ( inputs ).first, std::get < 0 > ( inputs ).second ), abstraction::retrieveValue < ParamTypes > ( std::get < Indexes + 1 > ( inputs ).first, std::get < Indexes + 1 > ( inputs ).second ) ... ); } - template < typename Callable, typename ... Ts, size_t ... Indexes > - inline void run_helper ( Callable callback, const ext::tuple < Ts ... > & inputs, const std::array < bool, sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) - m_data = callback ( std::get < Indexes > ( inputs )->getValue ( std::get < Indexes > ( moves ) ) ... ); + m_data = callback ( abstraction::retrieveValue < ParamTypes > ( std::get < Indexes > ( inputs ).first, std::get < Indexes > ( inputs ).second ) ... ); } virtual bool isReady ( ) const override { @@ -88,24 +89,22 @@ protected: mutable ext::variant < void, std::reference_wrapper < ReturnType > > m_data; public: - template < typename Callable, typename Object, typename ... Ts, size_t ... Indexes > - inline void member_run_helper ( Callable callback, const ext::tuple < Object, Ts ... > & inputs, const std::array < bool, 1 + sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename Object, typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void member_run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, 1 + sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) - m_data = std::reference_wrapper < ReturnType > ( callback ( & std::get < 0 > ( inputs )->getValue ( false ), std::get < Indexes + 1 > ( inputs )->getValue ( std::get < Indexes + 1 > ( moves ) ) ... ) ); + m_data = std::reference_wrapper < ReturnType > ( callback ( & abstraction::retrieveValue < Object > ( std::get < 0 > ( inputs ).first, std::get < 0 > ( inputs ).second ), abstraction::retrieveValue < ParamTypes > ( std::get < Indexes + 1 > ( inputs ).first, std::get < Indexes + 1 > ( inputs ).second ) ... ) ); } - template < typename Callable, typename ... Ts, size_t ... Indexes > - inline void run_helper ( Callable callback, const ext::tuple < Ts ... > & inputs, const std::array < bool, sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) - m_data = std::reference_wrapper < ReturnType > ( callback ( std::get < Indexes > ( inputs )->getValue ( std::get < Indexes > ( moves ) ) ... ) ); + m_data = std::reference_wrapper < ReturnType > ( callback ( abstraction::retrieveValue < ParamTypes > ( std::get < Indexes > ( inputs ).first, std::get < Indexes > ( inputs ).second ) ... ) ); } virtual bool isReady ( ) const override { @@ -138,24 +137,22 @@ protected: mutable ext::variant < void, std::reference_wrapper < const ReturnType > > m_data; public: - template < typename Callable, typename Object, typename ... Ts, size_t ... Indexes > - inline void member_run_helper ( Callable callback, const ext::tuple < Object, Ts ... > & inputs, const std::array < bool, 1 + sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename Object, typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void member_run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, 1 + sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) - m_data = std::reference_wrapper < const ReturnType > ( callback ( & std::get < 0 > ( inputs )->getValue ( false ), std::get < Indexes + 1 > ( inputs )->getValue ( std::get < Indexes + 1 > ( moves ) ) ... ) ); + m_data = std::reference_wrapper < const ReturnType > ( callback ( & abstraction::retrieveValue < Object > ( std::get < 0 > ( inputs ).first, std::get < 0 > ( inputs ).second ), abstraction::retrieveValue < ParamTypes > ( std::get < Indexes + 1 > ( inputs ).first, std::get < Indexes + 1 > ( inputs ).second ) ... ) ); } - template < typename Callable, typename ... Ts, size_t ... Indexes > - inline void run_helper ( Callable callback, const ext::tuple < Ts ... > & inputs, const std::array < bool, sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) - m_data = std::reference_wrapper < const ReturnType > ( callback ( std::get < Indexes > ( inputs )->getValue ( std::get < Indexes > ( moves ) ) ... ) ); + m_data = std::reference_wrapper < const ReturnType > ( callback ( abstraction::retrieveValue < ParamTypes > ( std::get < Indexes > ( inputs ).first, std::get < Indexes > ( inputs ).second ) ... ) ); } virtual bool isReady ( ) const override { @@ -192,26 +189,24 @@ protected: mutable ext::variant < void, std::reference_wrapper < ReturnType > > m_data; public: - template < typename Callable, typename Object, typename ... Ts, size_t ... Indexes > - inline void member_run_helper ( Callable callback, const ext::tuple < Object, Ts ... > & inputs, const std::array < bool, 1 + sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename Object, typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void member_run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, 1 + sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) { - ReturnType && res = callback ( & std::get < 0 > ( inputs )->getValue ( false ), std::get < Indexes + 1 > ( inputs )->getValue ( std::get < Indexes + 1 > ( moves ) ) ... ); + ReturnType && res = callback ( & abstraction::retrieveValue < Object > ( std::get < 0 > ( inputs ).first, std::get < 0 > ( inputs ).second ), abstraction::retrieveValue < ParamTypes > ( std::get < Indexes + 1 > ( inputs ).first, std::get < Indexes + 1 > ( inputs ).second ) ... ); m_data = std::reference_wrapper < ReturnType > ( res ); } } - template < typename Callable, typename ... Ts, size_t ... Indexes > - inline void run_helper ( Callable callback, const ext::tuple < Ts ... > & inputs, const std::array < bool, sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) { - ReturnType && res = callback ( std::get < Indexes > ( inputs )->getValue ( std::get < Indexes > ( moves ) ) ... ); + ReturnType && res = callback ( abstraction::retrieveValue < ParamTypes > ( std::get < Indexes > ( inputs ).first, std::get < Indexes > ( inputs ).second ) ... ); m_data = std::reference_wrapper < ReturnType > ( res ); } } @@ -246,26 +241,24 @@ protected: mutable ext::variant < void, std::reference_wrapper < const ReturnType > > m_data; public: - template < typename Callable, typename Object, typename ... Ts, size_t ... Indexes > - inline void member_run_helper ( Callable callback, const ext::tuple < Object, Ts ... > & inputs, const std::array < bool, 1 + sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename Object, typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void member_run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, 1 + sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) { - const ReturnType && res = callback ( & std::get < 0 > ( inputs )->getValue ( false ), std::get < Indexes + 1 > ( inputs )->getValue ( std::get < Indexes + 1 > ( moves ) ) ... ); + const ReturnType && res = callback ( & abstraction::retrieveValue < Object > ( std::get < 0 > ( inputs ).first, std::get < 0 > ( inputs ).second ), abstraction::retrieveValue < ParamTypes > ( std::get < Indexes + 1 > ( inputs ).first, std::get < Indexes + 1 > ( inputs ).second ) ... ); m_data = std::reference_wrapper < const ReturnType > ( res ); } } - template < typename Callable, typename ... Ts, size_t ... Indexes > - inline void run_helper ( Callable callback, const ext::tuple < Ts ... > & inputs, const std::array < bool, sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; if ( ! isReady ( ) ) { - const ReturnType && res = callback ( std::get < Indexes > ( inputs )->getValue ( std::get < Indexes > ( moves ) ) ... ); + const ReturnType && res = callback ( abstraction::retrieveValue < ParamTypes > ( std::get < Indexes > ( inputs ).first, std::get < Indexes > ( inputs ).second ) ... ); m_data = std::reference_wrapper < const ReturnType > ( res ); } } @@ -293,22 +286,20 @@ public: template < > class ValueOperationAbstraction < void > : public OperationAbstraction { public: - template < typename Callable, typename Object, typename ... Ts, size_t ... Indexes > - inline void member_run_helper ( Callable callback, const ext::tuple < Object, Ts ... > & inputs, const std::array < bool, 1 + sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < typename Object, typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void member_run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, 1 + sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; - callback ( & std::get < 0 > ( inputs )->getValue ( false ), std::get < Indexes + 1 > ( inputs )->getValue ( std::get < Indexes + 1 > ( moves ) ) ... ); + callback ( & abstraction::retrieveValue < Object > ( std::get < 0 > ( inputs ).first, std::get < 0 > ( inputs ).second ), abstraction::retrieveValue < ParamTypes > ( std::get < Indexes + 1 > ( inputs ).first, std::get < Indexes + 1 > ( inputs ).second ) ... ); } - template < typename Callable, typename ... Ts, size_t ... Indexes > - inline void run_helper ( Callable callback, const ext::tuple < Ts ... > & inputs, const std::array < bool, sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) const { + template < typename ... ParamTypes, typename Callable, size_t ... Indexes > + inline void run_helper ( Callable callback, const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( Indexes ) > & inputs, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) inputs; - ( void ) moves; - callback ( std::get < Indexes > ( inputs )->getValue ( std::get < Indexes > ( moves ) ) ... ); + callback ( abstraction::retrieveValue < ParamTypes > ( std::get < Indexes > ( inputs ).first, std::get < Indexes > ( inputs ).second ) ... ); } virtual bool isReady ( ) const override { diff --git a/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp b/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp index c5bafe2c40bb49c57332b9ec7fa20bdafb2411dc..4e2b74fe0796763d0b53aadbebc92abae91de4ad 100644 --- a/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp +++ b/alib2abstraction/src/abstraction/ValuePrinterAbstraction.hpp @@ -25,7 +25,11 @@ public: if ( ! this->inputsReady ( ) ) return false; - m_ostream << std::get < 0 > ( this->m_params )->getConstValueReference ( ) << std::endl; + 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/ValueProvider.hpp b/alib2abstraction/src/abstraction/ValueProvider.hpp index 843c405f54e02cd931e3e74b791bc3bc32caf848..6bc7a301d59a03b651d6365bba477e61d344eec9 100644 --- a/alib2abstraction/src/abstraction/ValueProvider.hpp +++ b/alib2abstraction/src/abstraction/ValueProvider.hpp @@ -9,6 +9,7 @@ #define _VALUE_PROVIDER_HPP_ #include <exception> + #include <alib/utility> namespace abstraction { @@ -19,8 +20,6 @@ protected: virtual Type & getData ( ) const = 0; public: - typedef Type return_type; - template < class T = Type > typename std::enable_if < std::is_copy_constructible < T >::value && std::is_move_constructible < T >::value, T >::type getValue ( bool move ) const { if ( move ) @@ -64,8 +63,6 @@ protected: virtual Type & getData ( ) const = 0; public: - typedef Type return_type; - Type & getValueReference ( ) const { return getData ( ); } @@ -85,8 +82,6 @@ protected: virtual const Type & getConstData ( ) const = 0; public: - typedef Type return_type; - const Type & getConstValueReference ( ) const { return getConstData ( ); } @@ -106,8 +101,6 @@ protected: virtual Type & getData ( ) const = 0; public: - typedef Type return_type; - Type && getRValueReference ( ) const { return std::move ( getData ( ) ); } @@ -130,8 +123,6 @@ protected: virtual const Type & getConstData ( ) const = 0; public: - typedef Type return_type; - const Type && getConstRValueReference ( ) const { return std::move ( getConstData ( ) ); } diff --git a/alib2abstraction/src/abstraction/WrapperAbstraction.hpp b/alib2abstraction/src/abstraction/WrapperAbstraction.hpp index 8d44f8aeb16f91d230ccb64f0c898393e2747d4f..094e8ed44e79f01893be12d9732caf7a79543860 100644 --- a/alib2abstraction/src/abstraction/WrapperAbstraction.hpp +++ b/alib2abstraction/src/abstraction/WrapperAbstraction.hpp @@ -18,76 +18,68 @@ protected: std::function < std::shared_ptr < OperationAbstraction > ( ParamTypes ... ) > m_WrapperFinder; ext::variant < void, std::shared_ptr < OperationAbstraction > > m_data; - ext::tuple < std::shared_ptr < ValueProvider < ParamTypes > > ... > m_params; - std::array < bool, sizeof ... ( ParamTypes ) > m_moves; + ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( ParamTypes ) > m_params; public: BaseWrapperAbstraction ( std::function < std::shared_ptr < OperationAbstraction > ( ParamTypes ... ) > wrapperFinder ) : m_WrapperFinder ( wrapperFinder ) { - for ( unsigned i = 0; i < sizeof ... ( ParamTypes ); ++ i ) - m_moves [ i ] = false; + for ( unsigned i = 0; i < sizeof ... ( ParamTypes ); ++ i ) { + m_params [ i ].first = nullptr; + m_params [ i ].second = false; + } } private: - virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move ) override { - if ( index >= m_moves.size ( ) ) + virtual bool attachInput ( const std::shared_ptr < OperationAbstraction > & input, unsigned index, bool move, bool checkInput ) override { + if ( index >= m_params.size ( ) ) throw std::invalid_argument ( "Parameter index " + ext::to_string ( index ) + " out of bounds."); - auto attachCallback = [ & ] ( auto & param ) { - if ( param != nullptr ) - return false; + if ( input == nullptr ) + return false; - typename std::decay < decltype ( param )>::type validData = std::dynamic_pointer_cast < typename std::decay < decltype ( param ) >::type::element_type > ( input->getProxyAbstraction ( ) ); - if ( validData ) { - m_moves [ index ] = move; - param = validData; - return true; - } else { - return false; - } - }; + if ( checkInput && ! CheckInput < ValueProvider < ParamTypes > ... >::checkInput ( input->getProxyAbstraction ( ), index ) ) + return false; - return ext::call_on_nth < bool > ( m_params, index, attachCallback ); + m_params [ index ].first = input; + m_params [ index ].second = move; + + return true; } virtual bool detachInput ( unsigned index ) override { - if ( index >= m_moves.size ( ) ) + if ( index >= m_params.size ( ) ) throw std::invalid_argument ( "Parameter index " + ext::to_string ( index ) + " out of bounds."); - m_moves [ index ] = false; - - auto detachCallback = [ & ] ( auto & param ) { - param = nullptr; - return true; - }; + m_params [ index ].first = nullptr; + m_params [ index ].second = false; - return ext::call_on_nth < bool > ( m_params, index, detachCallback ); + return true; } public: virtual bool inputsReady ( ) const override { - auto readyCallback = [ ] ( const auto & param ) { - return ( bool ) param && param->isReady ( ); - }; + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) + if ( ! param.first || ! param.first->isReady ( ) ) + return false; - return ext::all_of ( m_params, readyCallback ); + return true; } virtual bool inputsAttached ( ) const override { - auto attachedCallback = [ ] ( const auto & param ) { - return ( bool ) param; - }; + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) + if ( ! param.first ) + return false; - return ext::all_of ( m_params, attachedCallback ); + return true; } + protected: - template < typename ... Ts, size_t ... Indexes > - inline void finder_helper ( const ext::tuple < Ts ... > & params, const std::array < bool, sizeof ... ( Indexes ) > moves, std::index_sequence < Indexes ... > ) { + template < size_t ... Indexes > + inline void finder_helper ( const ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( Indexes ) > & params, std::index_sequence < Indexes ... > ) { /* make unused parameter warning go away in case of sizeof ... ( Ts ) == 0 */ ( void ) params; - ( void ) moves; - m_data = m_WrapperFinder ( std::get < Indexes > ( params )->getValue ( std::get < Indexes > ( moves ) ) ... ); + m_data = m_WrapperFinder ( abstraction::retrieveValue < ParamTypes > ( std::get < Indexes > ( params ).first, std::get < Indexes > ( params ).second ) ... ); } public: @@ -98,12 +90,9 @@ public: if ( this->cached ( ) ) return true; - auto evalCallback = [ ] ( const auto & param ) { - return param->eval ( ); - }; - - if ( ! ext::all_of ( m_params, evalCallback ) ) - return false; + for ( const std::pair < std::shared_ptr < OperationAbstraction >, bool > & param : m_params ) + if ( ! param.first->eval ( ) ) + return false; return this->run ( ); } @@ -121,10 +110,7 @@ public: } virtual ext::type_index getParamTypeIndex ( unsigned index ) const override { - auto callback = [ & ] ( auto & param ) { - return ext::type_index ( typeid ( typename std::decay < decltype ( param ) >::type::element_type::return_type ) ); - }; - return ext::call_on_nth < ext::type_index > ( m_params, index, callback ); + return ParamType < ParamTypes ... >::paramType ( index ); } virtual std::shared_ptr < abstraction::OperationAbstraction > getProxyAbstraction ( ) override { @@ -143,18 +129,14 @@ public: virtual bool run ( ) override { if ( this->m_data.template is < void > ( ) ) - this->finder_helper ( this->m_params, this->m_moves, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); - - auto getParam = [ & ] ( auto & param ) { - return std::dynamic_pointer_cast < abstraction::OperationAbstraction > ( param ); - }; + this->finder_helper ( this->m_params, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); - std::shared_ptr < OperationAbstraction > abstraction = this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( ); + std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( ); if ( 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 ( ext::call_on_nth < std::shared_ptr < abstraction::OperationAbstraction > > ( this->m_params, index, getParam ), index, this->m_moves [ index ] ); + abstraction->attachInput ( this->m_params [ index ].first, index, this->m_params [ index ].second, true ); return abstraction->run ( ); } @@ -180,15 +162,11 @@ public: virtual bool run ( ) override { if ( this->m_data.template is < void > ( ) ) - this->finder_helper ( this->m_params, this->m_moves, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); - - auto getParam = [ & ] ( auto & param ) { - return std::dynamic_pointer_cast < abstraction::OperationAbstraction > ( param ); - }; + this->finder_helper ( this->m_params, std::make_index_sequence < sizeof ... ( ParamTypes ) > { } ); - std::shared_ptr < OperationAbstraction > abstraction = this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( ); + std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( ); for ( unsigned index = 0; index < sizeof ... ( ParamTypes ); ++ index ) - abstraction->attachInput ( ext::call_on_nth < std::shared_ptr < abstraction::OperationAbstraction > > ( this->m_params, index, getParam ), index, this->m_moves [ index ] ); + abstraction->attachInput ( this->m_params [ index ].first, index, this->m_params [ index ].second, true ); return abstraction->run ( ); } diff --git a/alib2abstraction/src/common/AbstractionHelpers.hpp b/alib2abstraction/src/common/AbstractionHelpers.hpp new file mode 100644 index 0000000000000000000000000000000000000000..5e27a2fb7b903cf599c44e0ea7dfdd7f8d552531 --- /dev/null +++ b/alib2abstraction/src/common/AbstractionHelpers.hpp @@ -0,0 +1,65 @@ +#ifndef _ABSTRACTION_HELPERS_HPP_ +#define _ABSTRACTION_HELPERS_HPP_ + +#include <alib/tuple> +#include <alib/array> +#include <alib/typeinfo> + +namespace abstraction { + +template < class ParamType > +std::shared_ptr < ValueProvider < ParamType > > translateParam ( const std::shared_ptr < OperationAbstraction > & param ) { + std::shared_ptr < ValueProvider < ParamType > > res = std::dynamic_pointer_cast < ValueProvider < ParamType > > ( param->getProxyAbstraction ( ) ); + if ( ! res ) + throw std::invalid_argument ( "Abstraction does not provide value of type " + ext::to_string < ParamType > ( ) + " but " + param->getReturnType ( ) + "." ); + return res; +} + +template < class ParamType > +ParamType retrieveValue ( const std::shared_ptr < OperationAbstraction > & param, bool move ) { + return translateParam < ParamType > ( param )->getValue ( move ); +} + +template < class ... Params > +struct CheckInput; + +template < > +struct CheckInput < > { + static bool checkInput ( const std::shared_ptr < OperationAbstraction > &, unsigned ) { + throw std::logic_error ( "Out of types to check" ); + } +}; + +template < class Param, class ... Params > +struct CheckInput < Param, Params ... > { + static bool checkInput ( const std::shared_ptr < OperationAbstraction > & operation, unsigned index ) { + if ( index == 0 ) + return ( bool ) std::dynamic_pointer_cast < typename std::decay < Param >::type > ( operation->getProxyAbstraction ( ) ); + else + return CheckInput < Params ... >::checkInput ( operation, index - 1 ); + } +}; + +template < class ... Params > +struct ParamType; + +template < > +struct ParamType < > { + static ext::type_index paramType ( unsigned ) { + throw std::logic_error ( "Out of types to check" ); + } +}; + +template < class Param, class ... Params > +struct ParamType < Param, Params ... > { + static ext::type_index paramType ( unsigned index ) { + if ( index == 0 ) + return ext::type_index ( typeid ( typename std::decay < Param >::type ) ); + else + return ParamType < Params ... >::paramType ( index - 1); + } +}; + +} /* namespace abstraction */ + +#endif // _ABSTRACTION_HELPERS_HPP_ diff --git a/alib2abstraction/src/common/AlgorithmHelper.cpp b/alib2abstraction/src/common/AlgorithmHelper.cpp index af08f9e4db34d0988995c72c274baf2fa5e76e31..053452984fd570938f347a34bfe8d60f57668750 100644 --- a/alib2abstraction/src/common/AlgorithmHelper.cpp +++ b/alib2abstraction/src/common/AlgorithmHelper.cpp @@ -33,7 +33,7 @@ std::shared_ptr < abstraction::OperationAbstraction > AlgorithmHelper::eval ( co i = 0; for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : casted_params ) { - if ( ! algo->attachInput ( param, i, moves [ i ] ) ) + if ( ! algo->attachInput ( param, i, moves [ i ], true ) ) throw std::invalid_argument ( "Can't connect param " + algo->getParamType ( i ) + " at " + ext::to_string ( i ) + " of algorithm " + name + " with result of type " + param->getReturnType ( ) + "." ); ++ i; } @@ -43,7 +43,7 @@ std::shared_ptr < abstraction::OperationAbstraction > AlgorithmHelper::eval ( co if ( abstraction::Registry::hasNormalize ( algo->getReturnType ( ) ) ) { std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( algo->getReturnType ( ) ); - if ( ! normalized->attachInput ( algo, 0, true ) ) + if ( ! normalized->attachInput ( algo, 0, true, true ) ) throw std::invalid_argument ( "Can't connect param at 0 of normalize of algorithm " + name + " with result of type " + algo->getReturnType ( ) + "." ); if ( ! normalized->eval ( ) ) throw std::invalid_argument ( "Eval of normalize of algorithm " + name + " failed." ); diff --git a/alib2abstraction/src/common/CastHelper.cpp b/alib2abstraction/src/common/CastHelper.cpp index d85f7e76275006260fd3b401a247034d2b284848..6b710be76f5269920584f9beeea0a9efbc743ce7 100644 --- a/alib2abstraction/src/common/CastHelper.cpp +++ b/alib2abstraction/src/common/CastHelper.cpp @@ -10,14 +10,14 @@ std::shared_ptr < abstraction::OperationAbstraction > CastHelper::eval ( const s } std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getCastAbstraction ( type, param->getReturnType ( ) ); - if ( ! res->attachInput ( param, 0, move ) ) - throw std::invalid_argument ( "Can't connect param at 0 of cast to " + type + " with result of type " + param->getReturnType ( ) + "." ); + if ( ! res->attachInput ( param, 0, move, true ) ) + throw std::invalid_argument ( "Can't connect param " + res->getParamType ( 0 ) + " at 0 of cast to " + type + " with result of type " + param->getReturnType ( ) + "." ); if ( ! res->eval ( ) ) throw std::invalid_argument ( "Eval of cast to " + type + " failed." ); if ( abstraction::Registry::hasNormalize ( res->getReturnType ( ) ) ) { std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( res->getReturnType ( ) ); - if ( ! normalized->attachInput ( res, 0, true ) ) + if ( ! normalized->attachInput ( res, 0, true, true ) ) throw std::invalid_argument ( "Can't connect param at 0 of normalize of cast to " + type + " with result of type " + res->getReturnType ( ) + "." ); if ( ! normalized->eval ( ) ) throw std::invalid_argument ( "Eval of normalize of cast to " + type + " failed." ); diff --git a/alib2cli/src/ast/statements/ContainerStatement.cpp b/alib2cli/src/ast/statements/ContainerStatement.cpp index 92675cf8c4de18d910da2d708466ad583622096f..976d2c0f2b4c9a830759580569cbff540c75abae 100644 --- a/alib2cli/src/ast/statements/ContainerStatement.cpp +++ b/alib2cli/src/ast/statements/ContainerStatement.cpp @@ -39,7 +39,7 @@ std::shared_ptr < abstraction::OperationAbstraction > ContainerStatement::transl i = 0; for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : casted_params ) { - if ( ! algo->attachInput ( param, i, moves [ i ] ) ) + if ( ! algo->attachInput ( param, i, moves [ i ], true ) ) throw std::invalid_argument ( "Can't connect param at " + ext::to_string ( i ) + " of algorithm " + m_container + " with result of type " + param->getReturnType ( ) + "." ); ++ i; } diff --git a/alib2cli/src/ast/statements/FileStatement.cpp b/alib2cli/src/ast/statements/FileStatement.cpp index 3d4d27b5ed5c50335cebdba8ea43b1edf04190db..12a39fe967b796beb03e44f3a559d7dd044fb665 100644 --- a/alib2cli/src/ast/statements/FileStatement.cpp +++ b/alib2cli/src/ast/statements/FileStatement.cpp @@ -27,7 +27,7 @@ std::shared_ptr < abstraction::OperationAbstraction > FileStatement::translateAn std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::InputFileRegistry::getAbstraction ( filetype, type, templateParams ); std::shared_ptr < abstraction::ImmediateValueAbstraction < std::string > > file = std::make_shared < abstraction::ImmediateValueAbstraction < std::string > > ( m_file->eval ( environment ) ); - res->attachInput ( file, 0, false ); + res->attachInput ( file, 0, false, true ); res->eval ( ); return res; diff --git a/alib2cli/src/ast/statements/ResultFileStatement.h b/alib2cli/src/ast/statements/ResultFileStatement.h index 099a5514b543b100e96a92f7ab959e8a6737d309..dc376cd389c3722431960e05165af4badf991daf 100644 --- a/alib2cli/src/ast/statements/ResultFileStatement.h +++ b/alib2cli/src/ast/statements/ResultFileStatement.h @@ -23,8 +23,8 @@ public: std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::OutputFileRegistry::getAbstraction ( filetype, prev->getReturnType ( ) ); std::shared_ptr < abstraction::ImmediateValueAbstraction < std::string > > file = std::make_shared < abstraction::ImmediateValueAbstraction < std::string > > ( m_file->eval ( environment ) ); - res->attachInput ( file, 0, false ); - res->attachInput ( prev, 1, false ); + res->attachInput ( file, 0, false, true ); + res->attachInput ( prev, 1, false, true ); res->eval ( ); return res; diff --git a/alib2cli/src/ast/statements/ResultPrintStatement.h b/alib2cli/src/ast/statements/ResultPrintStatement.h index 494ca35d45a77641efc913ae979667df9eb8dcaf..57d4b0463a093958eca10aebe4648b0bbe302c4a 100644 --- a/alib2cli/src/ast/statements/ResultPrintStatement.h +++ b/alib2cli/src/ast/statements/ResultPrintStatement.h @@ -18,7 +18,7 @@ public: if ( res->numberOfParams ( ) == 0 ) return res; - if ( ! res->attachInput ( prev, 0, false ) ) + if ( ! res->attachInput ( prev, 0, false, true ) ) throw std::invalid_argument ( "Can't connect param at 0 of result print statement with result of type " + prev->getReturnType ( ) + "." ); if ( ! res->eval ( ) ) throw std::invalid_argument ( "Eval of result print statement failed." ); diff --git a/alib2cli/src/environment/Environment.h b/alib2cli/src/environment/Environment.h index 592b99629cf4055ff7857e31558ab9f805ca1bab..062d08bb22e8c7af62f5f621b37bfec15714216b 100644 --- a/alib2cli/src/environment/Environment.h +++ b/alib2cli/src/environment/Environment.h @@ -69,7 +69,7 @@ public: template < class T > const T & getVariable ( const std::string & name ) const { - std::shared_ptr < abstraction::ValueProvider < const T & > > ptr = std::dynamic_pointer_cast < abstraction::ValueProvider < const T & > > ( getVariableInt ( name ) ); + std::shared_ptr < abstraction::ValueProvider < const T & > > ptr = abstraction::translateParam < const T & > ( getVariableInt ( name ) ); if ( ! ptr ) throw exception::CommonException ( "Invalid variable type. Requested: " + ext::to_string < T > ( ) + ", actual : " + getVariableInt ( name )->getRuntimeReturnType ( ) ); return ptr->getConstValueReference ( ); diff --git a/alib2gui/src/Converter.cpp b/alib2gui/src/Converter.cpp index b8fdca01b030daa66a53bd8a577c943c4b834756..7cade598599235e20a6551c6cf68daea9a346140 100644 --- a/alib2gui/src/Converter.cpp +++ b/alib2gui/src/Converter.cpp @@ -19,7 +19,7 @@ namespace Converter { std::optional<QString> toString(const std::shared_ptr<abstraction::OperationAbstraction>& data) { try { auto res = abstraction::StringWriterRegistry::getAbstraction(data->getReturnType()); - res->attachInput(data, 0, true); + res->attachInput(data, 0, true, true); res->eval(); auto& value = dynamic_cast<abstraction::ValueProvider<std::string>&>(*res); @@ -64,7 +64,7 @@ namespace Converter { std::optional<QString> toXML(const std::shared_ptr<abstraction::OperationAbstraction>& data, bool indent) { try { auto res = abstraction::XmlRegistry::getXmlComposerAbstraction(data->getReturnType()); - res->attachInput(data, 0, true); + res->attachInput(data, 0, true, true); res->eval(); auto& value = dynamic_cast<abstraction::ValueProvider<ext::deque<sax::Token>>&>(*res); @@ -109,7 +109,7 @@ namespace Converter { auto tokensAbstraction = std::make_shared<abstraction::ImmediateValueAbstraction<ext::deque<sax::Token>>>( tokens); - automaton->attachInput(tokensAbstraction, 0, true); + automaton->attachInput(tokensAbstraction, 0, true, true); return automaton; } catch (const exception::CommonException& e) { diff --git a/alib2raw/src/abstraction/RawReaderAbstraction.hpp b/alib2raw/src/abstraction/RawReaderAbstraction.hpp index 64f35ea4d8a16203c1b24a7cfdcfc6369f541d94..bcce1b0da45164ea5b59a033a09ff9a1164b849d 100644 --- a/alib2raw/src/abstraction/RawReaderAbstraction.hpp +++ b/alib2raw/src/abstraction/RawReaderAbstraction.hpp @@ -20,7 +20,8 @@ public: if ( this->isReady ( ) ) return true; - ReturnType res = factory::RawDataFactory::fromString ( std::get < 0 > ( this->m_params )->getRValueReference ( ) ); + 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 ); return true; } diff --git a/alib2raw/src/abstraction/RawWriterAbstraction.hpp b/alib2raw/src/abstraction/RawWriterAbstraction.hpp index aeda8654c270620830189221f19dc5c395297e78..0f322dd53b91718186bc111de152d8ec84b5c594 100644 --- a/alib2raw/src/abstraction/RawWriterAbstraction.hpp +++ b/alib2raw/src/abstraction/RawWriterAbstraction.hpp @@ -20,7 +20,8 @@ public: if ( ! this->inputsReady ( ) ) return false; - this->m_data = factory::RawDataFactory::toString ( std::get < 0 > ( this->m_params )->getConstValueReference ( ) ); + 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 70cc75ea6d109dc4a5a5aa1f074d2453b920e738..8fa35e4ddaa4f9f47d01a6cb84a766572d74e083 100644 --- a/alib2str/src/abstraction/StringReaderAbstraction.hpp +++ b/alib2str/src/abstraction/StringReaderAbstraction.hpp @@ -20,7 +20,8 @@ public: if ( this->isReady ( ) ) return true; - ReturnType res = factory::StringDataFactory::fromString ( std::get < 0 > ( this->m_params )->getRValueReference ( ) ); + 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 ); return true; } diff --git a/alib2str/src/abstraction/StringWriterAbstraction.hpp b/alib2str/src/abstraction/StringWriterAbstraction.hpp index b69e34e1ab0200c45dcc5ac255417b7d2543ee3a..77a7198b41a39a9d092b0f52e71e928e40557a51 100644 --- a/alib2str/src/abstraction/StringWriterAbstraction.hpp +++ b/alib2str/src/abstraction/StringWriterAbstraction.hpp @@ -20,7 +20,8 @@ public: if ( ! this->inputsReady ( ) ) return false; - this->m_data = factory::StringDataFactory::toString ( std::get < 0 > ( this->m_params )->getConstValueReference ( ) ); + 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 52c5972c71c1bf0710e092c738b8b062cf6aeab4..28699eedc36d8b04296a8d613852918e60316b62 100644 --- a/alib2xml/src/abstraction/XmlComposerAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlComposerAbstraction.hpp @@ -20,7 +20,8 @@ public: if ( ! this->inputsReady ( ) ) return false; - this->m_data = factory::XmlDataFactory::toTokens ( std::get < 0 > ( this->m_params )->getConstValueReference ( ) ); + 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 6a221f532de5e4ceba98999f92df7ee07e1a8d19..d01bdac8d61404cf65da4e5872542e4de49d9aef 100644 --- a/alib2xml/src/abstraction/XmlParserAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlParserAbstraction.hpp @@ -22,7 +22,8 @@ public: if ( this->isReady ( ) ) return true; - ReturnType res = factory::XmlDataFactory::fromTokens ( std::get < 0 > ( this->m_params )->getRValueReference ( ) ); + 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 ); return true; } @@ -30,7 +31,8 @@ public: template < > inline bool XmlParserAbstraction < exception::CommonException >::run ( ) { - exception::CommonException ex = factory::XmlDataFactory::fromTokens ( std::get < 0 > ( this->m_params )->getRValueReference ( ) ); + std::pair < std::shared_ptr < OperationAbstraction >, bool > & param = std::get < 0 > ( this->m_params ); + exception::CommonException ex = factory::XmlDataFactory::fromTokens ( abstraction::retrieveValue < ext::deque < sax::Token > && > ( param.first, param.second ) ); throw ex; } diff --git a/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp b/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp index 91fb2d65fa3e97ab9994d314a75fe830c73a0976..f3e0a35b3376ecae5136858afe25c3600b55cab9 100644 --- a/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlTokensComposerAbstraction.hpp @@ -19,7 +19,9 @@ public: if ( ! this->inputsReady ( ) ) return false; - sax::SaxComposeInterface::composeFile ( std::get < 1 > ( this->m_params )->getConstValueReference ( ), std::get < 0 > ( this->m_params )->getConstValueReference ( ) ); + 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 ) ); return true; } }; diff --git a/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp b/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp index b2ed62c6a241408f94053effc869ee8b6a51db1d..5aa5e16aa2a0e38d17edd1ed00916b84a500c0f1 100644 --- a/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp +++ b/alib2xml/src/abstraction/XmlTokensParserAbstraction.hpp @@ -19,7 +19,8 @@ public: if ( ! this->inputsReady ( ) ) return false; - this->m_data = sax::SaxParseInterface::parseFile ( std::get < 0 > ( this->m_params )->getConstValueReference ( ) ); + 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; } };