iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nrt::GenericBag< FirstT, OtherT...> Class Template Reference

#include <nrt/Core/Design/GenericBag.H>

template<class FirstT, class... OtherT>
class nrt::GenericBag< FirstT, OtherT...>

A class for holding a bag of unique, unordered components.

This class is designed to hold an unordered set of distinct type components. Any two types are considered distinct if std::is_same<std::decay<T1>, std::decay<T2>> would return false.

Element access is provided by get<Type>(), which returns a reference to the first item of type Type in the bag as ordered in the template parameter list. Since types should be distinct, the first instance of type Type will be the only instance of type Type. Behavior is undefined if two of the same type occur in the bag.

nrt::GenericBag<int, float, double> bag(1, 2.0f, 3.0);
std::cout << bag.get<double>() << std::endl; // displays 3

Attempting to access a type that does not exist in the bag will result in a compile time error.

In addition, a bag can be assigned to another bag of arbitrary type and only the fields in their intersection will be assigned. In the case where no fields are shared, no operation will be carried out.

nrt::GenericBag<int, float, double> bag1(1, 2.0f, 3.0);
nrt::GenericBag<float, bool> bag2( 99.0f, true );
bag1 = bag2;
std::cout << bag1.get<float>() << std::endl; // displays 99
std::cout << bag1.get<int>() << std::endl; // displays 1

The ordering of elements does not matter for this operation.

nrt::GenericBag<int, bool> bag1;
nrt::GenericBag<bool, int> bag2;
bag1 = bag2;
bag2 = bag1;

Explicit reference types can be used to specialize a GenericBag and can be accessed with or without specifying a reference, since access to types only requires that the types are equivalent on a decayed basis.

int x = 3;
nrt::GenericBag<int&> bag1( x );
std::cout << bag1.get<int>() << " " << bag1.get<int&>() << std::endl;
// displays 3 3

In addition, types specified as a reference will be considered equivalent to non reference types during assignment.

Assignment in the other direction to a reference type will change the original source of the reference.

int x = 3;
nrt::GenricBag<int&> bag1( x );
nrt::GenericBag<int, double> bag2( 4, 3.7 );
bag2 = bag1;
std::cout << bag2.get<int>() << std::endl; // displays 3
bag2.get<int>() = 33;
bag1 = bag2;
std::cout << x << std::endl; // displays 33

Note that since we only care about decayed types when getting types out or doing assignment, the following is perfectly valid:

nrt::GenericBag<int, const bool> bag1( 3, true );
nrt::GenericBag<const int, bool> bag2 = bag1; // valid, const int matches with int, bool matches with const bool
int x = 3;
nrt::GenericBag<const int &> bag3( x );
int & y = bag3.get<int>(); // int will match to const int &, but this will be invalid since int & cannot be bound to const int &

Definition at line 140 of file GenericBag.H.

Inheritance diagram for nrt::GenericBag< FirstT, OtherT...>:
nrt::StreamableGenericBag< FirstT, OtherT...>

Public Member Functions

Constructors
 GenericBag ()
 Initializes all elements to their default values.
 
 GenericBag (FirstT const &first, OtherT const &...other)
 
template<class FirstT2 , class... OtherT2>
 GenericBag (GenericBag< FirstT2, OtherT2...> const &bag)
 Explicit copy constructor. More...
 
 GenericBag (GenericBag<> const &bag)
 Explicit copy from an empty bag.
 
template<class FirstT2 , class... OtherT2>
 GenericBag (StreamableGenericBag< FirstT2, OtherT2...> const &bag)
 Explicit copy constructor. More...
 
 GenericBag (StreamableGenericBag<> const &bag)
 Explicit copy from an empty bag.
 
template<class FirstT2 , class... OtherT2>
 GenericBag (GenericBag< FirstT2, OtherT2...> &&bag)
 Move constructor. More...
 
 GenericBag (GenericBag<> &&bag)
 Move from empty bag.
 
template<class FirstT2 , class... OtherT2>
 GenericBag (StreamableGenericBag< FirstT2, OtherT2...> &&bag)
 Move constructor. More...
 
 GenericBag (StreamableGenericBag<> &&bag)
 Move from empty bag.
 
Element Access and Type Information

Access to individual elements is possible by either specifying a specific type, which will be matched on an std::decay basis, or a specific index in the bag.

template<class T >
getReferenceType< T, FirstT,
OtherT...>::type 
get ()
 Return a reference to the first instance of type T encountered in the bag. More...
 
template<class T >
std::decay< T >::type const & get () const
 Return a const reference to the first instance of type T. More...
 
template<size_t Index>
getIndexType< Index,
GenericBag >::type & 
get ()
 Return a reference to the type at the given index.
 
template<size_t Index>
getIndexType< Index,
GenericBag >::type const & 
get () const
 Return a const reference to the type at the given index.
 
template<class T >
getReferenceType< T, FirstT,
OtherT...>::type 
tryGet ()
 Attempts to return a reference to the first instance of type T encountered in the bag. More...
 
template<class T >
std::decay< T >::type const & tryGet () const
 Attempts to get a constant reference to the first instance of Type T. More...
 
template<class T >
bool const hasType () const
 Checks whether the GenericBag contains a certain type. More...
 
Assignment Operators
template<class FirstT2 , class... OtherT2>
GenericBag & operator= (GenericBag< FirstT2, OtherT2...> const &bag)
 Assignment operator. More...
 
GenericBag & operator= (GenericBag<> const &bag)
 Assign to empty bag. More...
 
template<class FirstT2 , class... OtherT2>
GenericBag & operator= (StreamableGenericBag< FirstT2, OtherT2...> const &bag)
 Assignment operator. More...
 
GenericBag & operator= (StreamableGenericBag<> const &bag)
 Assign to empty bag. More...
 
template<class FirstT2 , class... OtherT2>
GenericBag & operator= (GenericBag< FirstT2, OtherT2...> &&bag)
 Move Assignment operator. More...
 
GenericBag & operator= (GenericBag<> &&bag)
 Move assign to empty bag. More...
 
template<class FirstT2 , class... OtherT2>
GenericBag & operator= (StreamableGenericBag< FirstT2, OtherT2...> &&bag)
 Move Assignment operator. More...
 
GenericBag & operator= (StreamableGenericBag<> &&bag)
 Move assign to empty bag. More...
 
Advanced operations

Advanced operations include things such as applying a function to each element in a bag, either in place or returning a new bag of some type.

template<class... RetTypes, class Functor >
GenericBag< RetTypes...> applyFunction (Functor &&f) const
 Apply a function to every element in the bag. More...
 
template<class Functor >
void applyFunctionInPlace (Functor &&f)
 Apply a function in place to every element in the bag. More...
 

Protected Types

typedef FirstT FirstType
 Make FirstT available to helper functionality.
 

Protected Member Functions

FirstT & first ()
 Returns the first element held by this GenericBag.
 
FirstT const & first () const
 Const version of above.
 
GenericBag< OtherT...> & other ()
 Returns the other elements held by this GenericBag.
 
GenericBag< OtherT...> const & other () const
 Const version of above.
 
template<class Archive >
void serialize (Archive &ar)
 

Static Protected Attributes

static constexpr int height = GenericBag<OtherT...>::height + 1
 Our 'height' in the bag.
 

Friends

template<class... Args>
class GenericBag
 
template<size_t H, class RT , class D1 , class D2 >
struct getClassFromBag
 
template<size_t H, class RT , class D1 , class D2 >
struct tryGetClassFromBag
 
template<size_t H1, size_t H2, class FT2 , class... OT2>
struct bagOuterSearch
 
template<size_t H1, class A1 , class D1 , class D2 >
struct bagInnerSearch
 
template<size_t H1, size_t H2, class FT2 , class... OT2>
struct bagOuterSearchMove
 
template<size_t H1, class A1 , class D1 , class D2 >
struct bagInnerSearchMove
 
template<size_t H, class RT >
struct getIndexFromBag
 
template<class... Args>
class StreamableGenericBag
 
template<size_t H>
struct streamGenericBagContents
 
struct initiateGenericBagStream
 
template<size_t H, class F , class RT >
struct expandAndApply
 
template<class T , class... Args>
struct getReferenceType
 
template<class D1 , class D2 , class... Args>
struct getReferenceTypeImpl
 
class cereal::access
 

Constructor & Destructor Documentation

template<class FirstT , class... OtherT>
nrt::GenericBag< FirstT, OtherT...>::GenericBag ( FirstT const &  first,
OtherT const &...  other 
)

Initialize all elements to the provided values in the same order as template parameters

Parameters
firstInitial value for the first type
otherInitial values in corresponding order to other types
template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
nrt::GenericBag< FirstT, OtherT...>::GenericBag ( GenericBag< FirstT2, OtherT2...> const &  bag)
explicit

Explicit copy constructor.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values.

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
nrt::GenericBag< FirstT, OtherT...>::GenericBag ( StreamableGenericBag< FirstT2, OtherT2...> const &  bag)
explicit

Explicit copy constructor.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
nrt::GenericBag< FirstT, OtherT...>::GenericBag ( GenericBag< FirstT2, OtherT2...> &&  bag)

Move constructor.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
nrt::GenericBag< FirstT, OtherT...>::GenericBag ( StreamableGenericBag< FirstT2, OtherT2...> &&  bag)

Move constructor.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

Member Function Documentation

template<class FirstT , class... OtherT>
template<class T >
getReferenceType<T, FirstT, OtherT...>::type nrt::GenericBag< FirstT, OtherT...>::get ( )

Return a reference to the first instance of type T encountered in the bag.

This does not perform a strict search for type T. Instead, it looks for any instance of std::decay<T>::type. The return type is then the most compatible reference type for the matched type (e.g. an actual type of const int will return a const reference as opposed to a non const reference). Errors may still be encountered if the ultimate type this is assigned to is not compatible.

Template Parameters
TThe loose type to match (matching is performed on an std::decay basis)
Returns
A reference (const if necessary) to the matched type
template<class FirstT , class... OtherT>
template<class T >
std::decay<T>::type const& nrt::GenericBag< FirstT, OtherT...>::get ( ) const

Return a const reference to the first instance of type T.

This does not perform a strict search for type T. Instead it looks for any instance of std::decay<T>::type. Errors may still be encountered if the types are not assignable.

template<class FirstT , class... OtherT>
template<class T >
getReferenceType<T, FirstT, OtherT...>::type nrt::GenericBag< FirstT, OtherT...>::tryGet ( )

Attempts to return a reference to the first instance of type T encountered in the bag.

This does not perform a strict search for type T. Instead, it looks for any instance of std::decay<T>::type. The return type is then the most compatible reference type for the matched type (e.g. an actual type of const int will return a const reference as opposed to a non const reference). Errors may still be encountered if the ultimate type this is assigned to is not compatible.

Template Parameters
TThe loose type to match (matching is performed on an std::decay basis)
Returns
A reference (const if necessary) to the matched type
Exceptions
GenericBagExceptionIf the item does not exist
template<class FirstT , class... OtherT>
template<class T >
std::decay<T>::type const& nrt::GenericBag< FirstT, OtherT...>::tryGet ( ) const

Attempts to get a constant reference to the first instance of Type T.

This does not perform a strict search for type T. Instead it looks for any instance of std::decay<T>::type. Errors may still be encountered if the types are not assignable.

Exceptions
GenericBagExceptionIf the item does not exist
template<class FirstT , class... OtherT>
template<class T >
bool const nrt::GenericBag< FirstT, OtherT...>::hasType ( ) const

Checks whether the GenericBag contains a certain type.

This does not perform a strict search for type T. Instead it looks for any instance of std::decay<T>::type.

template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( GenericBag< FirstT2, OtherT2...> const &  bag)

Assignment operator.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

template<class FirstT , class... OtherT>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( GenericBag<> const &  bag)

Assign to empty bag.

All fields in common with the parameter bag will be set equal to the parameter's value for that field. All other fields will be left as is.

template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( StreamableGenericBag< FirstT2, OtherT2...> const &  bag)

Assignment operator.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

template<class FirstT , class... OtherT>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( StreamableGenericBag<> const &  bag)

Assign to empty bag.

All fields in common with the parameter bag will be set equal to the parameter's value for that field. All other fields will be left as is.

template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( GenericBag< FirstT2, OtherT2...> &&  bag)

Move Assignment operator.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

template<class FirstT , class... OtherT>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( GenericBag<> &&  bag)

Move assign to empty bag.

All fields in common with the parameter bag will be set equal to the parameter's value for that field. All other fields will be left as is.

template<class FirstT , class... OtherT>
template<class FirstT2 , class... OtherT2>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( StreamableGenericBag< FirstT2, OtherT2...> &&  bag)

Move Assignment operator.

All fields in common with the parameter bag will be set equal; any fields not in common will be given default values

Two fields are considered equal if they match on an std::is_same<std::decay<T1>, std::decay<T2>> basis

template<class FirstT , class... OtherT>
GenericBag& nrt::GenericBag< FirstT, OtherT...>::operator= ( StreamableGenericBag<> &&  bag)

Move assign to empty bag.

All fields in common with the parameter bag will be set equal to the parameter's value for that field. All other fields will be left as is.

template<class FirstT , class... OtherT>
template<class... RetTypes, class Functor >
GenericBag<RetTypes...> nrt::GenericBag< FirstT, OtherT...>::applyFunction ( Functor &&  f) const

Apply a function to every element in the bag.

This allows you to apply a generic function to each element in the bag. The function should be encapsulated in a functor class that has an operator() that accepts any templated type that could be found in the bag. The return type of the function should be directly related to the template type.

If you wish to bind arguments to your function, this should be done in the construction of your functor prior to passing it to this function.

The current bag will be "unwound" into a parameter pack of its elements, each one having had some functon applied to it. A new GenericBag will then be constructed taking this unwound parameter pack as a construction argument, so it must match the types (in order) specified in RetTypes...

The function applied must be a 1:1 mapping from input types to output types.

Template Parameters
FunctorThe functor to use
RetTypesThe types of the resulting GenericBag
Returns
A new GenericBag consisting of the function applied to each element in the original
template<class FirstT , class... OtherT>
template<class Functor >
void nrt::GenericBag< FirstT, OtherT...>::applyFunctionInPlace ( Functor &&  f)

Apply a function in place to every element in the bag.

The function applied should be generic such that it can handle any of the types currently in the bag. See applyFunction for more information.

Template Parameters
FunctorThe functor to use

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