Radium Engine  1.5.0
VariableSet.cpp
1 #include <Core/Containers/VariableSet.hpp>
2 #include <Core/CoreMacros.hpp>
3 
4 // inspirations :
5 // Radium dataflow dynamic type management
6 // Radium attribArray
7 // factory design pattern
8 // visitor design pattern
9 // https://en.cppreference.com/w/cpp/utility/any/type
10 // https://gieseanw.wordpress.com/2017/05/03/a-true-heterogeneous-container-in-c/
11 namespace Ra {
12 namespace Core {
13 
14 auto VariableSet::VariableSetFunctions::getInstance() -> VariableSetFunctions* {
15  static VariableSet::VariableSetFunctions instance;
16  return &instance;
17 }
18 
20  m_variables = other.m_variables;
21  m_typeIndexToVtableIndex = other.m_typeIndexToVtableIndex;
22  m_storedType = other.m_storedType;
23  return *this;
24 }
25 
26 auto VariableSet::operator=( VariableSet&& other ) noexcept -> VariableSet& {
27  m_variables = std::move( other.m_variables );
28  m_typeIndexToVtableIndex = std::move( other.m_typeIndexToVtableIndex );
29  m_storedType = std::move( other.m_storedType );
30  return *this;
31 }
32 
34  m_variables.clear();
35 }
36 
38  for ( const auto& type : from.getStoredTypes() ) {
39  const auto& index = from.m_typeIndexToVtableIndex.at( type );
40  from.m_vtable->m_mergeKeepFunctions[index]( from, *this );
41  }
42 }
43 
45  for ( const auto& type : from.getStoredTypes() ) {
46  const auto& index = from.m_typeIndexToVtableIndex.at( type );
47  from.m_vtable->m_mergeReplaceFunctions[index]( from, *this );
48  }
49 }
50 
51 size_t VariableSet::size() const {
52  size_t sum { 0 };
53  for ( const auto& [type, index] : m_typeIndexToVtableIndex ) {
54  sum += m_vtable->m_sizeFunctions[index]( *this );
55  }
56  return sum;
57 }
58 
59 bool VariableSet::DynamicVisitor::accept( const std::type_index& id ) const {
60  return m_visitorOperator.find( id ) != m_visitorOperator.cend();
61 }
62 
63 void VariableSet::DynamicVisitor::operator()( std::any&& in, std::any&& userParam ) const {
64  m_visitorOperator.at( std::type_index( in.type() ) )( in, std::forward<std::any>( userParam ) );
65 }
66 
67 } // namespace Core
68 } // namespace Ra
bool accept(const std::type_index &id) const override
Acceptance function for the visitor.
Definition: VariableSet.cpp:59
void operator()(std::any &&in, std::any &&userParam) const override
Execute a visiting operator on accepted types.
Definition: VariableSet.cpp:63
Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T...
Definition: VariableSet.hpp:72
auto getStoredTypes() const -> const std::vector< std::type_index > &
Gets the stored data type.
void mergeKeepVariables(const VariableSet &from)
Merge the VariableSet from into this.
Definition: VariableSet.cpp:37
void clear()
remove all elements from the container
Definition: VariableSet.cpp:33
void mergeReplaceVariables(const VariableSet &from)
Merge the VariableSet from into this.
Definition: VariableSet.cpp:44
size_t size() const
Gets the total number of variables (of any type)
Definition: VariableSet.cpp:51
auto operator=(const VariableSet &other) -> VariableSet &
Copy assignment operator.
Definition: VariableSet.cpp:19
Definition: Cage.cpp:3