From 57d085d46de7c04adf2adf94327407bd00d2635c Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 11 Dec 2018 21:54:49 +0100 Subject: [PATCH] Abstraction: use std optional instead of variant < void, T > --- .../abstraction/ValueOperationAbstraction.hpp | 37 +++++++++---------- .../src/abstraction/WrapperAbstraction.hpp | 17 +++++---- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp b/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp index 52173501d1..e2d8cdbe14 100644 --- a/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp +++ b/alib2abstraction/src/abstraction/ValueOperationAbstraction.hpp @@ -11,7 +11,6 @@ #include <alib/tuple> #include <alib/memory> #include <alib/typeindex> -#include <alib/variant> #include <abstraction/OperationAbstraction.hpp> #include <abstraction/ValueProvider.hpp> @@ -27,14 +26,14 @@ template < class ReturnType > class ValueOperationAbstraction : public OperationAbstraction, public ValueProvider < ReturnType >, public ValueProvider < ReturnType & >, public ValueProvider < const ReturnType & >, public ValueProvider < ReturnType && >, public ValueProvider < const ReturnType && > { protected: virtual ReturnType & getData ( ) const override { - return m_data.template get < ReturnType > ( ); + return m_data.value ( ); } virtual const ReturnType & getConstData ( ) const override { - return m_data.template get < ReturnType > ( ); + return m_data.value ( ); } - mutable ext::variant < void, ReturnType > m_data; + mutable std::optional < ReturnType > m_data; public: template < typename ... ParamTypes, typename Callable > @@ -44,7 +43,7 @@ public: } virtual bool isReady ( ) const override { - return m_data.template is < ReturnType > ( ); + return ( bool ) m_data; } virtual ext::type_index getReturnTypeIndex ( ) const override { @@ -67,14 +66,14 @@ template < class ReturnType > class ValueOperationAbstraction < ReturnType & > : public OperationAbstraction, public ValueProvider < ReturnType & >, public ValueProvider < const ReturnType & >, public ValueProvider < ReturnType && >, public ValueProvider < const ReturnType && > { protected: virtual ReturnType & getData ( ) const override { - return m_data.template get < std::reference_wrapper < ReturnType > > ( ).get ( ); + return m_data->get ( ); } virtual const ReturnType & getConstData ( ) const override { - return m_data.template get < std::reference_wrapper < ReturnType > > ( ).get ( ); + return m_data->get ( ); } - mutable ext::variant < void, std::reference_wrapper < ReturnType > > m_data; + mutable std::optional < std::reference_wrapper < ReturnType > > m_data; public: template < typename ... ParamTypes, typename Callable > @@ -84,7 +83,7 @@ public: } virtual bool isReady ( ) const override { - return m_data.template is < std::reference_wrapper < ReturnType > > ( ); + return ( bool ) m_data; } virtual ext::type_index getReturnTypeIndex ( ) const override { @@ -107,10 +106,10 @@ template < class ReturnType > class ValueOperationAbstraction < const ReturnType & > : public OperationAbstraction, public ValueProvider < const ReturnType & >, public ValueProvider < const ReturnType && > { protected: virtual const ReturnType & getConstData ( ) const override { - return m_data.template get < std::reference_wrapper < const ReturnType > > ( ).get ( ); + return m_data->get ( ); } - mutable ext::variant < void, std::reference_wrapper < const ReturnType > > m_data; + mutable std::optional < std::reference_wrapper < const ReturnType > > m_data; public: template < typename ... ParamTypes, typename Callable > @@ -120,7 +119,7 @@ public: } virtual bool isReady ( ) const override { - return m_data.template is < std::reference_wrapper < const ReturnType > > ( ); + return ( bool ) m_data; } virtual ext::type_index getReturnTypeIndex ( ) const override { @@ -143,14 +142,14 @@ template < class ReturnType > class ValueOperationAbstraction < ReturnType && > : public OperationAbstraction, public ValueProvider < ReturnType & >, public ValueProvider < const ReturnType & >, public ValueProvider < ReturnType && >, public ValueProvider < const ReturnType && > { protected: virtual ReturnType & getData ( ) const override { - return m_data.template get < std::reference_wrapper < ReturnType > > ( ).get ( ); + return m_data->get ( ); } virtual const ReturnType & getConstData ( ) const override { - return m_data.template get < std::reference_wrapper < ReturnType > > ( ).get ( ); + return m_data->get ( ); } - mutable ext::variant < void, std::reference_wrapper < ReturnType > > m_data; + mutable std::optional < std::reference_wrapper < ReturnType > > m_data; public: template < typename ... ParamTypes, typename Callable > @@ -162,7 +161,7 @@ public: } virtual bool isReady ( ) const override { - return m_data.template is < std::reference_wrapper < ReturnType > > ( ); + return ( bool ) m_data; } virtual ext::type_index getReturnTypeIndex ( ) const override { @@ -185,10 +184,10 @@ template < class ReturnType > class ValueOperationAbstraction < const ReturnType && > : public OperationAbstraction, public ValueProvider < const ReturnType & >, public ValueProvider < const ReturnType && > { protected: virtual const ReturnType & getConstData ( ) const override { - return m_data.template get < std::reference_wrapper < const ReturnType > > ( ).get ( ); + return m_data->get ( ); } - mutable ext::variant < void, std::reference_wrapper < const ReturnType > > m_data; + mutable std::optional < std::reference_wrapper < const ReturnType > > m_data; public: template < typename ... ParamTypes, typename Callable > @@ -200,7 +199,7 @@ public: } virtual bool isReady ( ) const override { - return m_data.template is < std::reference_wrapper < const ReturnType > > ( ); + return ( bool ) m_data; } virtual ext::type_index getReturnTypeIndex ( ) const override { diff --git a/alib2abstraction/src/abstraction/WrapperAbstraction.hpp b/alib2abstraction/src/abstraction/WrapperAbstraction.hpp index 3634fcc7a1..28311daad4 100644 --- a/alib2abstraction/src/abstraction/WrapperAbstraction.hpp +++ b/alib2abstraction/src/abstraction/WrapperAbstraction.hpp @@ -9,6 +9,7 @@ #define _WRAPPER_ABSTRACTION_HPP_ #include <abstraction/OperationAbstraction.hpp> +#include <optional> namespace abstraction { @@ -17,7 +18,7 @@ class BaseWrapperAbstraction : public OperationAbstraction { protected: std::function < std::shared_ptr < OperationAbstraction > ( ParamTypes ... ) > m_WrapperFinder; - ext::variant < void, std::shared_ptr < OperationAbstraction > > m_data; + std::optional < std::shared_ptr < OperationAbstraction > > m_data; ext::array < std::pair < std::shared_ptr < OperationAbstraction >, bool >, sizeof ... ( ParamTypes ) > m_params; public: @@ -92,7 +93,7 @@ public: } virtual bool isReady ( ) const override { - return m_data.template is < std::shared_ptr < OperationAbstraction > > ( ) && m_data.template get < std::shared_ptr < OperationAbstraction > > ( )->isReady ( ); + return ( bool ) m_data && m_data.value ( )->isReady ( ); } virtual bool cached ( ) const override { @@ -105,7 +106,7 @@ public: virtual std::shared_ptr < abstraction::OperationAbstraction > getProxyAbstraction ( ) override { if ( this->isReady ( ) ) - return this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( )->getProxyAbstraction ( ); + return this->m_data.value ( )->getProxyAbstraction ( ); else throw std::domain_error ( "Proxy abstraction not avaiable before evaluation." ); } @@ -118,10 +119,10 @@ public: } virtual bool run ( ) override { - if ( this->m_data.template is < void > ( ) ) + if ( ! this->m_data ) this->m_data = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params ); - std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( ); + std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.value ( ); if ( abstraction->getReturnTypeIndex ( ) != this->getReturnTypeIndex ( ) ) throw std::domain_error ( "Expected and provided types do not match" ); @@ -151,10 +152,10 @@ public: } virtual bool run ( ) override { - if ( this->m_data.template is < void > ( ) ) + if ( ! this->m_data ) this->m_data = abstraction::apply < ParamTypes ... > ( this->m_WrapperFinder, this->m_params ); - std::shared_ptr < OperationAbstraction > & abstraction = this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( ); + 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 ); @@ -167,7 +168,7 @@ public: virtual ext::type_index getRuntimeReturnTypeIndex ( ) const override { if ( this->isReady ( ) ) - return this->m_data.template get < std::shared_ptr < OperationAbstraction > > ( )->getProxyAbstraction ( )->getRuntimeReturnTypeIndex ( ); + return this->m_data.value ( )->getProxyAbstraction ( )->getRuntimeReturnTypeIndex ( ); else throw std::domain_error ( "Runtime type unknown before evaluation." ); } -- GitLab