Radium Engine  1.5.20
Loading...
Searching...
No Matches
Ra::Core::VariableSet Class Reference

Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T). More...

#include <Core/Containers/VariableSet.hpp>

+ Inheritance diagram for Ra::Core::VariableSet:
+ Collaboration diagram for Ra::Core::VariableSet:

Classes

class  DynamicVisitor
 Base class for visitors with configurable per-type callbacks. Visiting will be prepared at running time by dynamically adding visitor operators for each type one want to visit in the container. The visitor accept type based on either the added operators or an external acceptance functor which can be associated/changed at runtime. This functor is only allowed to reduce the number of visited type as only type for which a visitor operator is registered can be visited. The visitor operators should be any callable that accept to be called using f(const std::string&, T&[, std::any&&]) More...
 
class  DynamicVisitorBase
 Base class for dynamically configurable visitors Users can implement this interface to build custom visitors without any restriction. To ease visitor configuration, see class DynamicVisitor. More...
 
struct  StaticVisitor
 Base class for visitors with static supported types. Visiting will be prepared at compile time by unfolding type list and generating all the required function calls for the visit. Any class that defines the same alias for a public member "types" can be used as a visitor. More...
 

Public Types

template<typename T >
using VariableContainer = std::map<std::string, T>
 Container type for the mapping name->value of variables with type T.
 
template<typename T >
using Variable = typename VariableContainer<T>::value_type
 Variable type as stored in the VariableSet.
 
template<typename T >
using VariableType = typename Variable<T>::second_type
 Type of the variable value.
 
template<typename T >
using VariableHandle = typename VariableContainer<T>::iterator
 Handle of a variable A handle on a variable with type T is an iterator into the BaseContainer. De-referencing the handle give access to a non const pair <const std::string, T> (BaseContainer<T>::value_type). VariableHandle validity follows the rules of BaseContainer<T>::iterator validity.
 
template<typename H >
using VariableTypeFromHandle = typename std::iterator_traits<H>::value_type::second_type
 Type of the variable referenced by a VariableHandle.
 

Public Member Functions

template<typename F , typename U >
void visitStatic (F &&visitor, U &userParams) const
 
template<typename F , typename U >
void visitStatic (F &&visitor, U &&userParams) const
 
 VariableSet ()
 
 VariableSet (const VariableSet &other) noexcept
 A VariableSet is copyable.
 
 VariableSet (VariableSet &&other) noexcept
 A VariableSet is movable.
 
auto operator= (const VariableSet &other) -> VariableSet &
 Copy assignment operator.
 
auto operator= (VariableSet &&other) noexcept -> VariableSet &
 Move assignment operator.
 
void clear ()
 remove all elements from the container
 
void mergeKeepVariables (const VariableSet &from)
 Merge the VariableSet from into this.
 
void mergeReplaceVariables (const VariableSet &from)
 Merge the VariableSet from into this.
 
size_t size () const
 Gets the total number of variables (of any type)
 
auto getStoredTypes () const -> const std::vector< std::type_index > &
 Gets the stored data type.
 
template<typename T >
auto insertVariable (const std::string &name, const T &value) -> std::pair< VariableHandle< T >, bool >
 Add a variable, i.e. an association name->value, into the container.
 
template<typename T >
auto getVariable (const std::string &name) const -> const T &
 get the value of the given variable
 
template<typename T >
auto getVariable (const std::string &name) -> T &
 
template<typename T >
auto getVariableHandle (const std::string &name) const -> const VariableHandle< T >
 get the handle on the variable with the given name
 
template<typename H >
bool isHandleValid (const H &handle) const
 Test the validity of a handle.
 
template<typename T >
auto setVariable (const std::string &name, const T &value) -> std::pair< VariableHandle< T >, bool >
 reset (or set if the variable does not exist yet) the value of the variable.
 
template<typename T >
bool deleteVariable (const std::string &name)
 Remove a variable, i.e. a name->value association.
 
template<typename H >
bool deleteVariable (H &handle)
 delete a variable from its handle
 
template<typename T >
auto existsVariable (const std::string &name) const -> Utils::optional< VariableHandle< T > >
 test the existence of the given variable
 
template<typename T >
auto existsVariableType () const -> Utils::optional< VariableContainer< T > * >
 Test if the storage supports a given variable type.
 
template<typename T >
bool deleteAllVariables ()
 Removes all variables of the given type.
 
template<typename T >
auto getAllVariables () const -> VariableContainer< T > &
 Get the whole container for variables of a given type.
 
template<typename H >
auto getAllVariablesFromHandle (const H &handle) -> VariableContainer< VariableTypeFromHandle< H > > &
 Get the whole container for variables of the same type than the given handled variable.
 
template<typename T >
size_t numberOf () const
 Get the number of variables of the given type.
 
template<typename F >
void visit (F &&visitor) const
 Visit the container using a user defined visitor.
 
template<typename F , typename T >
void visit (F &&visitor, T &userParams) const
 overload of the static visit method to allow a parameter pass by reference
 
template<typename F , typename T >
void visit (F &&visitor, T &&userParams) const
 overload of the static visit method to allow a parameter pass by rvalue reference
 

Detailed Description

Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T).

The container is compatible with any type, from simple arithmetic type to wrapped references to any other type.

"Variables" can be added, accessed and removed from the container given their type, name and value.

Given a type, the container gives access to the mapping name->value for all variables with the given type.

Stored variables can be referenced using a VariableHandle returned when a variable is inserted in the container or fetch giving the type and the name of the variable. Validity of the variable handle follows the same rules than the std::iterator attached to the underlying mapping storage (std::map<std::string, T>::iterator for the actual implementation).

From a variable handle (possibly invalid), the mapping name->value for all variables with the same type can be fetched. This allows generic, type agnostic, usage of the container as soon as a variable handle (even invalid) is available.

The container could be visited to apply processing, transformation, ... on all or part of the variables. The accept functions of the visitor are defined by the availability of a callable object whose profile is compatible with the visited types.

Visiting the container can be made

  • using a statically typed visitor : accepted types are defined at compile time, as Ra::Core::Utils::TypeList, and the visit is pre-processed by the compiler.
  • using a dynamically configurable visitor where functor accepting types can be added/removed at runtime. This kind of visit is a little more expensive while being more configurable. Tests using empty processing (to evaluate only the cost of visiting the collection) on different types showed a visit from 5 to 8 times slower. This penalty becomes quite low as soon as the processing during the visit is more complex

The visitor of the collection can accept one user parameter to be forwarded to each visit function. This parameter could be of any type, knowing the same parameter will be forwarded to all processing method when visiting a variable. Some constraints on user provided parameter depends on the visiting strategy

  • For static visitors, this parameter is strongly typed and ALL the visiting function should be called with the profile void(const std::string&, [const]T[&], [const]U&&), for any variable type T and the user parameter type U.
  • For dynamic visitors, as they rely on type erasure pattern, the user parameter should be of any type U but this type is erased when calling the visiting function. So, visiting functions must be callable with the profile void(const std::string&, [const]T[&], [const]std::anyU&&) knowing that the std::any contains a parameter of type U.
Note
relies on C++ standard libraries extensions, version 2 or recommended implementation on compilers that do not provide this extension https://en.cppreference.com/w/cpp/experimental/lib_extensions_2
See also
VariableSet unit test source code for examples of usage of this container

Definition at line 71 of file VariableSet.hpp.

Member Typedef Documentation

◆ Variable

template<typename T >
using Ra::Core::VariableSet::Variable = typename VariableContainer<T>::value_type

Variable type as stored in the VariableSet.

Definition at line 80 of file VariableSet.hpp.

◆ VariableContainer

Container type for the mapping name->value of variables with type T.

Definition at line 76 of file VariableSet.hpp.

◆ VariableHandle

template<typename T >
using Ra::Core::VariableSet::VariableHandle = typename VariableContainer<T>::iterator

Handle of a variable A handle on a variable with type T is an iterator into the BaseContainer. De-referencing the handle give access to a non const pair <const std::string, T> (BaseContainer<T>::value_type). VariableHandle validity follows the rules of BaseContainer<T>::iterator validity.

Definition at line 92 of file VariableSet.hpp.

◆ VariableType

template<typename T >
using Ra::Core::VariableSet::VariableType = typename Variable<T>::second_type

Type of the variable value.

Definition at line 84 of file VariableSet.hpp.

◆ VariableTypeFromHandle

template<typename H >
using Ra::Core::VariableSet::VariableTypeFromHandle = typename std::iterator_traits<H>::value_type::second_type

Type of the variable referenced by a VariableHandle.

Note
This is the same as the one denoted by the type T in VariableHandle<T> or in VariableType<T>

Definition at line 98 of file VariableSet.hpp.

Constructor & Destructor Documentation

◆ VariableSet() [1/3]

Ra::Core::VariableSet::VariableSet ( )
inline

Constructors, destructors

Definition at line 103 of file VariableSet.hpp.

◆ VariableSet() [2/3]

Ra::Core::VariableSet::VariableSet ( const VariableSet & other)
inlinenoexcept

A VariableSet is copyable.

Definition at line 106 of file VariableSet.hpp.

◆ VariableSet() [3/3]

Ra::Core::VariableSet::VariableSet ( VariableSet && other)
inlinenoexcept

A VariableSet is movable.

Definition at line 111 of file VariableSet.hpp.

+ Here is the call graph for this function:

Member Function Documentation

◆ clear()

void Ra::Core::VariableSet::clear ( )

remove all elements from the container

Definition at line 33 of file VariableSet.cpp.

+ Here is the call graph for this function:

◆ deleteAllVariables()

template<typename T >
bool Ra::Core::VariableSet::deleteAllVariables ( )

Removes all variables of the given type.

Template Parameters
TThe type of variable to remove
Returns
true if the type is deleted, false if it was not managed before the call. Unregister all the functions associated with the type and remove the storage of existing variables. If several variables are still stored, they will be destroyed.

Definition at line 756 of file VariableSet.hpp.

◆ deleteVariable() [1/2]

template<typename T >
bool Ra::Core::VariableSet::deleteVariable ( const std::string & name)

Remove a variable, i.e. a name->value association.

Returns
true if the variable was removed, false if
Precondition
The element name must exists with type T. If not verified (assert in debug mode) std::bad_any_cast exception could be thrown by the underlying management of type erasure

Definition at line 651 of file VariableSet.hpp.

◆ deleteVariable() [2/2]

template<typename H >
bool Ra::Core::VariableSet::deleteVariable ( H & handle)

delete a variable from its handle

Template Parameters
HType of the handle. Expected to be VariableHandle<T> for some variable type T
Parameters
handlethe variable handle
Returns
true variable was removed, false if not. If the variable was removed, handle is invalidated
Precondition
the handle must be valid. If not verified (assert in debug mode) std::bad_any_cast exception could be thrown by the underlying management of type erasure

Definition at line 662 of file VariableSet.hpp.

◆ existsVariable()

template<typename T >
auto Ra::Core::VariableSet::existsVariable ( const std::string & name) const -> Utils::optional<VariableHandle<T>>

test the existence of the given variable

Returns
an optional variable handle which contains a value if a variable with the given name and type exists in the storage.

Definition at line 670 of file VariableSet.hpp.

◆ existsVariableType()

template<typename T >
auto Ra::Core::VariableSet::existsVariableType ( ) const -> Utils::optional<VariableContainer<T>*>

Test if the storage supports a given variable type.

Operators acting on a per type basis

Template Parameters
TThe type of variable to test
Returns
an optional, empty if the type does not exists in the VariableSet or whose value is a non owning pointer to the variable collection if it exists. This non owning pointer remains valid as long as the VariableSet exists and contains the given type.

Definition at line 749 of file VariableSet.hpp.

+ Here is the call graph for this function:

◆ getAllVariables()

template<typename T >
auto Ra::Core::VariableSet::getAllVariables ( ) const -> VariableContainer<T>&

Get the whole container for variables of a given type.

Template Parameters
TThe variable type to get
Returns
a reference to the storage of the mapping name->value for the given type.
Precondition
existsVariableType<T>(). If not verified (assert in debug mode) std::bad_any_cast exception will be thrown by the underlying management of type erasure

Definition at line 765 of file VariableSet.hpp.

◆ getAllVariablesFromHandle()

template<typename H >
auto Ra::Core::VariableSet::getAllVariablesFromHandle ( const H & handle) -> VariableContainer<VariableTypeFromHandle<H>>&

Get the whole container for variables of the same type than the given handled variable.

Template Parameters
HType of the variable handle, should be VariableHandle<T> for some type T
Parameters
handlethe handle to an existing variable
Returns
a reference to the storage of the mapping name->value for the given type.
Precondition
existsVariableType<HandledType<H>>(). If not verified (assert in debug mode) std::bad_any_cast exception will be thrown by the underlying management of type erasure

Definition at line 774 of file VariableSet.hpp.

◆ getStoredTypes()

auto Ra::Core::VariableSet::getStoredTypes ( ) const -> const std::vector<std::type_index>&
inline

Gets the stored data type.

Returns
constant vector of type_index

Definition at line 147 of file VariableSet.hpp.

◆ getVariable() [1/2]

template<typename T >
auto Ra::Core::VariableSet::getVariable ( const std::string & name) -> T&

Definition at line 618 of file VariableSet.hpp.

◆ getVariable() [2/2]

template<typename T >
auto Ra::Core::VariableSet::getVariable ( const std::string & name) const -> const T&

get the value of the given variable

Returns
a reference to the value.
Precondition
The element name must exists with type T. If not verified (assert in debug mode) std::bad_any_cast exception could be thrown by the underlying management of type erasure

Definition at line 623 of file VariableSet.hpp.

◆ getVariableHandle()

template<typename T >
auto Ra::Core::VariableSet::getVariableHandle ( const std::string & name) const -> const VariableHandle<T>

get the handle on the variable with the given name

Template Parameters
Tthe type of the variable
Parameters
namethe name of a variable
Returns
an handle which can be de-referenced to obtain a std::pair<const std::string, T> representing the name and the value of the variable.

Definition at line 628 of file VariableSet.hpp.

◆ insertVariable()

template<typename T >
auto Ra::Core::VariableSet::insertVariable ( const std::string & name,
const T & value ) -> std::pair<VariableHandle<T>, bool>

Add a variable, i.e. an association name->value, into the container.

Operators acting on a per variable basis

Returns
true if the variable is inserted, false if the name was already associated with another value (of the same type). In this case, keep the old value.

Definition at line 608 of file VariableSet.hpp.

◆ isHandleValid()

template<typename H >
bool Ra::Core::VariableSet::isHandleValid ( const H & handle) const

Test the validity of a handle.

Template Parameters
HType of the handle. Expected to be VariableHandle<T> for some variable type T
Parameters
handlethe variable handle
Returns
true if the handle is valid, false if not.

Definition at line 634 of file VariableSet.hpp.

◆ mergeKeepVariables()

void Ra::Core::VariableSet::mergeKeepVariables ( const VariableSet & from)

Merge the VariableSet from into this.

Parameters
fromthe VariableSet to merge with the current. Existing variable into this, with same name as in from, are kept unchanged
See also
mergeReplaceVariables

Definition at line 37 of file VariableSet.cpp.

+ Here is the call graph for this function:

◆ mergeReplaceVariables()

void Ra::Core::VariableSet::mergeReplaceVariables ( const VariableSet & from)

Merge the VariableSet from into this.

Parameters
fromthe VariableSet to merge with the current. Existing variable into this, with same name as in from, are replaced by from's one.
See also
mergeKeepVariables

Definition at line 44 of file VariableSet.cpp.

+ Here is the call graph for this function:

◆ numberOf()

template<typename T >
size_t Ra::Core::VariableSet::numberOf ( ) const

Get the number of variables of the given type.

Template Parameters
TThe type to test
Returns
the number of variables with type T stored in the container

Definition at line 781 of file VariableSet.hpp.

◆ operator=() [1/2]

auto Ra::Core::VariableSet::operator= ( const VariableSet & other) -> VariableSet&

Copy assignment operator.

Operators acting on a the whole VariableSet

Definition at line 19 of file VariableSet.cpp.

◆ operator=() [2/2]

auto Ra::Core::VariableSet::operator= ( VariableSet && other) -> VariableSet&
noexcept

Move assignment operator.

Definition at line 26 of file VariableSet.cpp.

+ Here is the call graph for this function:

◆ setVariable()

template<typename T >
auto Ra::Core::VariableSet::setVariable ( const std::string & name,
const T & value ) -> std::pair<VariableHandle<T>, bool>

reset (or set if the variable does not exist yet) the value of the variable.

Returns
a pair with the variable handle and a bool : true if the variable value was reset, false if the variable value was set.

Definition at line 640 of file VariableSet.hpp.

◆ size()

size_t Ra::Core::VariableSet::size ( ) const

Gets the total number of variables (of any type)

Definition at line 51 of file VariableSet.cpp.

◆ visit() [1/3]

template<typename F >
void Ra::Core::VariableSet::visit ( F && visitor) const
inline

Visit the container using a user defined visitor.

Template Parameters
FThe type of the visitor to use (could be dynamic or static - see above )
Parameters
visitorThe visitor object to use The type of the visiting functor F should be
  • either derived from VariableSet::StaticVisitor<Type1, Type2, ...> with function operators with profile const operator(const std::string& name, T& value) available for all the requested types Type1, Type2, ...
  • either a user define class exposing a type list to unfold F::types (see Utils::TypeList and VariableSet::StaticVisitor)
  • either derived from DynamicVisitor. In this case, visiting will be less efficient.

Definition at line 907 of file VariableSet.hpp.

◆ visit() [2/3]

template<typename F , typename T >
void Ra::Core::VariableSet::visit ( F && visitor,
T && userParams ) const
inline

overload of the static visit method to allow a parameter pass by rvalue reference

Definition at line 923 of file VariableSet.hpp.

+ Here is the call graph for this function:

◆ visit() [3/3]

template<typename F , typename T >
void Ra::Core::VariableSet::visit ( F && visitor,
T & userParams ) const
inline

overload of the static visit method to allow a parameter pass by reference

Definition at line 915 of file VariableSet.hpp.

+ Here is the call graph for this function:

◆ visitStatic() [1/2]

template<typename F , typename U >
void Ra::Core::VariableSet::visitStatic ( F && visitor,
U && userParams ) const

Definition at line 826 of file VariableSet.hpp.

◆ visitStatic() [2/2]

template<typename F , typename U >
void Ra::Core::VariableSet::visitStatic ( F && visitor,
U & userParams ) const

Definition at line 820 of file VariableSet.hpp.


The documentation for this class was generated from the following files: