Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ValueHolderInterface.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/typeinfo>
9
10#include <abstraction/Value.hpp>
11
12namespace abstraction {
13
14template < class Type >
16public:
17 using Value::Value;
18
19 virtual void setValue ( const Type & ) = 0;
20
21 virtual Type && getValue ( ) = 0;
22
23};
24
25template < class ParamType >
26ParamType retrieveValue ( const std::shared_ptr < abstraction::Value > & param, bool move = false ) {
27 using Type = std::decay_t < ParamType >;
28
29 std::shared_ptr < ValueHolderInterface < Type > > interface = std::dynamic_pointer_cast < ValueHolderInterface < Type > > ( param->getProxyAbstraction ( ) );
30 if ( ! interface )
31 throw std::invalid_argument ( "Abstraction does not provide value of type " + ext::to_string < ParamType > ( ) + " but " + param->getType ( ) + "." );
32
33/* if constexpr ( std::is_reference_v < ParamType > ) {
34 if ( interface->isTemporary ( ) && ! interface->isRef ( ) && interface->getTemporaryReferences ( ) == 0 )
35 throw std::domain_error ( "Taking reference to a temporary." );
36 }*/
37
38 if constexpr ( std::is_rvalue_reference_v < ParamType > ) {
39 if ( param->isTemporary ( ) || move )
40 return std::move ( interface->getValue ( ) );
41 else
42 throw std::domain_error ( "Cannot bind without move" );
43 } else if constexpr ( std::is_lvalue_reference_v < ParamType > && std::is_const_v < std::remove_reference_t < ParamType > > ) {
44 Type && res = interface->getValue ( );
45 return res;
46 } else if constexpr ( std::is_lvalue_reference_v < ParamType > ) {
47 if ( ( param->isTemporary ( ) || move ) && ! TypeQualifiers::isRef ( param->getTypeQualifiers ( ) ) )
48 throw std::domain_error ( "Cannot bind temporary to non-const reference" );
49 Type && res = interface->getValue ( );
50 return res;
51 } else if constexpr ( std::is_copy_constructible_v < Type > && std::is_move_constructible_v < Type > ) {
52 if ( ! TypeQualifiers::isConst ( param->getTypeQualifiers ( ) ) && ( param->isTemporary ( ) || move ) )
53 return std::move ( interface->getValue ( ) );
54 else {
55 const Type & res = interface->getValue ( );
56 return res;
57 }
58 } else if constexpr ( std::is_copy_constructible_v < Type > && ! std::is_move_constructible_v < Type > ) {
59 if ( ! TypeQualifiers::isConst ( param->getTypeQualifiers ( ) ) && ( param->isTemporary ( ) || move ) )
60 throw std::domain_error ( "Value not move constructible" );
61 else {
62 Type && res = interface->getValue ( );
63 return res;
64 }
65 } else if constexpr ( ! std::is_copy_constructible_v < Type > && std::is_move_constructible_v < Type > ) {
66 if ( ! TypeQualifiers::isConst ( param->getTypeQualifiers ( ) ) && ( param->isTemporary ( ) || move ) )
67 return std::move ( interface->getValue ( ) );
68 else
69 throw std::domain_error ( "Value not copy constructible" );
70 } else { // ! std::is_copy_constructible_v < Type > && ! std::is_move_constructible_v < Type >
71 if ( ! TypeQualifiers::isConst ( param->getTypeQualifiers ( ) ) )
72 throw std::domain_error ( "Value not move constructible" );
73 else
74 throw std::domain_error ( "Value not copy constructible" );
75 }
76}
77
78} /* namespace abstraction */
79
static constexpr bool isConst(TypeQualifierSet arg)
Definition: TypeQualifiers.hpp:35
static constexpr bool isRef(TypeQualifierSet arg)
Definition: TypeQualifiers.hpp:39
Definition: ValueHolderInterface.hpp:15
virtual void setValue(const Type &)=0
Definition: Value.hpp:19
Definition: AlgorithmAbstraction.hpp:11
ParamType retrieveValue(const std::shared_ptr< abstraction::Value > &param, bool move=false)
Definition: ValueHolderInterface.hpp:26
return res
Definition: MinimizeByPartitioning.h:145
Type
Definition: MeasurementTypes.hpp:20