mc_rtc::DataStore Struct Reference

Generic data store. More...

#include <mc_rtc/DataStore.h>

Public Member Functions

 DataStore ()=default
 
 DataStore (const DataStore &)=delete
 
DataStoreoperator= (const DataStore &)=delete
 
 DataStore (DataStore &&)=default
 
DataStoreoperator= (DataStore &&)=default
 
bool has (const std::string &name) const noexcept
 Checks whether an object is in the datastore. More...
 
std::vector< std::string > keys () const noexcept
 Returns all objects in the datastore. More...
 
template<typename T >
T & get (const std::string &name)
 Get a reference to an object on the datastore. More...
 
template<typename T >
const T & get (const std::string &name) const
 const variant of get More...
 
template<typename T >
void get (const std::string &name, T &data)
 Assign value from the datastore if it exists, leave value unchanged otherwise. More...
 
template<typename T >
const T & get (const std::string &name, const T &defaultValue) const
 Gets a value from the datastore if it exists or a default value otherwise. More...
 
template<typename T >
void assign (const std::string &name, const T &data)
 Copies an object to an existing datastore object. More...
 
template<typename T , typename... ArgsT, typename... Args>
T & make (const std::string &name, Args &&... args)
 Creates an object on the datastore and returns a reference to it. More...
 
template<typename T >
auto make_call (const std::string &name, T fn) -> typename internal::lambda_traits< T >::fn_t &
 Creates an object on the datastore and returns a reference to it. More...
 
template<typename T , typename... ArgsT, typename... Args>
T & make_initializer (const std::string &name, Args &&... args)
 Creates an object on the datastore using list initialization and returns a reference to it. More...
 
template<typename RetT , typename... FuncArgsT, typename... ArgsT, typename std::enable_if< sizeof...(FuncArgsT)==sizeof...(ArgsT) &&!std::is_same< std::tuple< FuncArgsT... >, std::tuple< ArgsT... >>::value, int >::type = 0>
RetT call (const std::string &name, ArgsT &&... args) const
 Calls a function that was registered in the datastore and returns this call result. More...
 
template<typename RetT = void, typename... ArgsT>
RetT call (const std::string &name, ArgsT &&... args) const
 Calls a function that was registered in the datastore and returns this call result. More...
 
void remove (const std::string &name) noexcept
 Removes an object from the datastore. More...
 
void clear () noexcept
 Remove all entries in the datastore. More...
 
const std::string & name () const noexcept
 Name of this datastore. More...
 
void name (const std::string &name) noexcept
 Sets this datastore's name. More...
 

Detailed Description

Generic data store.

This class allows to store and retrieve C++ objects. It is intended to be used as a way to share arbitrary data between various parts of the framework (states, plugins, observers, etc...)

DataStore store;
// Creates a vector of 4 double with value 42 on the datastore
store.make<std::vector<double>>("Data", 4, 42);
...
auto & data = store.get<std::vector<double>>("Data");
// Use the object
data[3] = 0;
...
// Get another reference to the object
auto & data2 = store.get<std::vector<double>>("Data");
data2.push_back(0);
// vector now has size 5
log::info(data.size());

When retrieving an object using get<Type>, checks are performed to ensure that the stored type and Type are compatible.

To handle inheritance, you need to explicitely provide the class hierarchy:

struct A {};
struct B : public A {};
// Creating an inherited object and checking virtual inheritance
store.make<B, A>("data");
auto & base = store.get<A>("data");
auto & derived = store.get<B>("data");

Constructor & Destructor Documentation

◆ DataStore() [1/3]

mc_rtc::DataStore::DataStore ( )
default

◆ DataStore() [2/3]

mc_rtc::DataStore::DataStore ( const DataStore )
delete

◆ DataStore() [3/3]

mc_rtc::DataStore::DataStore ( DataStore &&  )
default

Member Function Documentation

◆ assign()

template<typename T >
void mc_rtc::DataStore::assign ( const std::string &  name,
const T &  data 
)
inline

Copies an object to an existing datastore object.

Parameters
nameName of the stored object
dataData to copy on the datastore
See also
get(std::string)

◆ call() [1/2]

template<typename RetT , typename... FuncArgsT, typename... ArgsT, typename std::enable_if< sizeof...(FuncArgsT)==sizeof...(ArgsT) &&!std::is_same< std::tuple< FuncArgsT... >, std::tuple< ArgsT... >>::value, int >::type = 0>
RetT mc_rtc::DataStore::call ( const std::string &  name,
ArgsT &&...  args 
) const
inline

Calls a function that was registered in the datastore and returns this call result.

This is syntactic sugar for getting the function object and calling it

In this version you must specify the argument types for the function you are calling. This is especially useful when the types deduced from the arguments do not match the function type (typically passing a non-const reference to a function expecting a const reference.

Parameters
nameName of the stored function
Template Parameters
FunArgsTTypes of arguments expected by the function
Parameters
argsArguments passed to the function

◆ call() [2/2]

template<typename RetT = void, typename... ArgsT>
RetT mc_rtc::DataStore::call ( const std::string &  name,
ArgsT &&...  args 
) const
inline

Calls a function that was registered in the datastore and returns this call result.

This is syntaxic sugar for getting the function object and calling it

This version deduces the functions' arguments from the arguments passed to the call. This follows C++ rules for deducing types from universal reference collapsing except for arithmetic types where the type is deduced to be of value type.

If you need to call a callback that takes an arithmetic type by reference then you must specify the arguments' type explicitely

Parameters
nameName of the stored object

@params args Arguments passed to the function

◆ clear()

void mc_rtc::DataStore::clear ( )
inlinenoexcept

Remove all entries in the datastore.

◆ get() [1/4]

template<typename T >
T& mc_rtc::DataStore::get ( const std::string &  name)
inline

Get a reference to an object on the datastore.

Parameters
nameName of the stored oject
Returns
Reference to the stored object
Exceptions
std::runtime_errorwhen the type of T does not match the one defined upon creation.

make

◆ get() [2/4]

template<typename T >
const T& mc_rtc::DataStore::get ( const std::string &  name) const
inline

const variant of get

◆ get() [3/4]

template<typename T >
const T& mc_rtc::DataStore::get ( const std::string &  name,
const T &  defaultValue 
) const
inline

Gets a value from the datastore if it exists or a default value otherwise.

Parameters
nameName of the stored data
defaultValueDefault value used if the data is not in the datastore
Returns
Stored data if it exists, defaultValue otherwise.

◆ get() [4/4]

template<typename T >
void mc_rtc::DataStore::get ( const std::string &  name,
T &  data 
)
inline

Assign value from the datastore if it exists, leave value unchanged otherwise.

Parameters
nameName of the stored data
dataReference to the external object to assign with the stored data

◆ has()

bool mc_rtc::DataStore::has ( const std::string &  name) const
inlinenoexcept

Checks whether an object is in the datastore.

Parameters
nameName of the stored object
Returns
true when the object is in the datastore

◆ keys()

std::vector<std::string> mc_rtc::DataStore::keys ( ) const
inlinenoexcept

Returns all objects in the datastore.

◆ make()

template<typename T , typename... ArgsT, typename... Args>
T& mc_rtc::DataStore::make ( const std::string &  name,
Args &&...  args 
)
inline

Creates an object on the datastore and returns a reference to it.

Parameters
nameName of the stored object.
argsParameters to be passed to the object's constructor
Returns
A reference to the constructed object
Exceptions
std::runtime_errorif an object with the same name already exists in the datastore.

◆ make_call()

template<typename T >
auto mc_rtc::DataStore::make_call ( const std::string &  name,
fn 
) -> typename internal::lambda_traits<T>::fn_t &
inline

Creates an object on the datastore and returns a reference to it.

This overload should only work with lambda-like objects and transform the lambda into an equivalent std::function

Parameters
nameName of the stored object
fnFunction that will be stored in the datastore
Returns
A reference to the constructed object

◆ make_initializer()

template<typename T , typename... ArgsT, typename... Args>
T& mc_rtc::DataStore::make_initializer ( const std::string &  name,
Args &&...  args 
)
inline

Creates an object on the datastore using list initialization and returns a reference to it.

Parameters
nameName of the stored object.
argsParameters to be passed to the object using list initialization
Returns
A reference to the constructed object

◆ name() [1/2]

const std::string& mc_rtc::DataStore::name ( ) const
inlinenoexcept

Name of this datastore.

◆ name() [2/2]

void mc_rtc::DataStore::name ( const std::string &  name)
inlinenoexcept

Sets this datastore's name.

Parameters
nameName for the datastore

◆ operator=() [1/2]

DataStore& mc_rtc::DataStore::operator= ( const DataStore )
delete

◆ operator=() [2/2]

DataStore& mc_rtc::DataStore::operator= ( DataStore &&  )
default

◆ remove()

void mc_rtc::DataStore::remove ( const std::string &  name)
inlinenoexcept

Removes an object from the datastore.

Parameters
nameName of the stored object

The documentation for this struct was generated from the following file:
mc_rtc::log::info
void info(Args &&... args)
Definition: logging.h:75
mc_rtc::DataStore::DataStore
DataStore()=default