NDDEM
Public Member Functions | Public Attributes | Private Types | Private Member Functions | Related Functions | List of all members
cereal::NameValuePair< T > Class Template Reference

For holding name value pairs. More...

#include <helpers.hpp>

+ Inheritance diagram for cereal::NameValuePair< T >:

Public Member Functions

 NameValuePair (char const *n, T &&v)
 Constructs a new NameValuePair. More...
 

Public Attributes

char constname
 
Type value
 

Private Types

using Type = typename std::conditional< std::is_array< typename std::remove_reference< T >::type >::value, typename std::remove_cv< T >::type, typename std::conditional< std::is_lvalue_reference< T >::value, T, typename std::decay< T >::type >::type >::type
 

Private Member Functions

NameValuePairoperator= (NameValuePair const &)=delete
 

Related Functions

(Note that these are not member functions.)

template<class T >
NameValuePair< T > make_nvp (std::string const &name, T &&value)
 Creates a name value pair. More...
 
template<class T >
NameValuePair< T > make_nvp (const char *name, T &&value)
 Creates a name value pair. More...
 
template<class Archive , class T >
std::enable_if< std::is_same< Archive,::cereal::BinaryInputArchive >::value||std::is_same< Archive,::cereal::BinaryOutputArchive >::value, T && >::type make_nvp (const char *, T &&value)
 A specialization of make_nvp<> that simply forwards the value for binary archives. More...
 
template<class Archive , class T >
std::enable_if<!std::is_same< Archive,::cereal::BinaryInputArchive >::value &&!std::is_same< Archive,::cereal::BinaryOutputArchive >::value, NameValuePair< T > >::type make_nvp (const char *name, T &&value)
 A specialization of make_nvp<> that actually creates an nvp for non-binary archives. More...
 

Detailed Description

template<class T>
class cereal::NameValuePair< T >

For holding name value pairs.

This pairs a name (some string) with some value such that an archive can potentially take advantage of the pairing.

In serialization functions, NameValuePairs are usually created like so:

struct MyStruct
{
int a, b, c, d, e;
template<class Archive>
void serialize(Archive & archive)
{
archive( CEREAL_NVP(a),
CEREAL_NVP(e) );
}
};
#define CEREAL_NVP(T)
Creates a name value pair for the variable T with the same name as the variable.
Definition: cereal.hpp:72
uint d
void serialize(Archive &, std::less< T > &)
Saving for std::less.
Definition: functional.hpp:39
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181

Alternatively, you can give you data members custom names like so:

struct MyStruct
{
int a, b, my_embarrassing_variable_name, d, e;
template<class Archive>
void serialize(Archive & archive)
{
archive( CEREAL_NVP(a),
cereal::make_nvp("var", my_embarrassing_variable_name) );
CEREAL_NVP(e) );
}
};

There is a slight amount of overhead to creating NameValuePairs, so there is a third method which will elide the names when they are not used by the Archive:

struct MyStruct
{
int a, b;
template<class Archive>
void serialize(Archive & archive)
{
archive( cereal::make_nvp<Archive>(a),
cereal::make_nvp<Archive>(b) );
}
};

This third method is generally only used when providing generic type support. Users writing their own serialize functions will normally explicitly control whether they want to use NVPs or not.

Member Typedef Documentation

◆ Type

template<class T >
using cereal::NameValuePair< T >::Type = typename std::conditional<std::is_array<typename std::remove_reference<T>::type>::value, typename std::remove_cv<T>::type, typename std::conditional<std::is_lvalue_reference<T>::value, T, typename std::decay<T>::type>::type>::type
private

Constructor & Destructor Documentation

◆ NameValuePair()

template<class T >
cereal::NameValuePair< T >::NameValuePair ( char const n,
T &&  v 
)
inline

Constructs a new NameValuePair.

Parameters
nThe name of the pair
vThe value to pair. Ideally this should be an l-value reference so that the value can be both loaded and saved to. If you pass an r-value reference, the NameValuePair will store a copy of it instead of a reference. Thus you should only pass r-values in cases where this makes sense, such as the result of some size() call.

Member Function Documentation

◆ operator=()

template<class T >
NameValuePair& cereal::NameValuePair< T >::operator= ( NameValuePair< T > const )
privatedelete

Friends And Related Function Documentation

◆ make_nvp() [1/4]

template<class Archive , class T >
std::enable_if< std::is_same< Archive,::cereal::BinaryInputArchive >::value||std::is_same< Archive,::cereal::BinaryOutputArchive >::value, T && >::type make_nvp ( const char *  ,
T &&  value 
)
related

A specialization of make_nvp<> that simply forwards the value for binary archives.

◆ make_nvp() [2/4]

template<class T >
NameValuePair< T > make_nvp ( const char *  name,
T &&  value 
)
related

Creates a name value pair.

◆ make_nvp() [3/4]

template<class Archive , class T >
std::enable_if<!std::is_same< Archive,::cereal::BinaryInputArchive >::value &&!std::is_same< Archive,::cereal::BinaryOutputArchive >::value, NameValuePair< T > >::type make_nvp ( const char *  name,
T &&  value 
)
related

A specialization of make_nvp<> that actually creates an nvp for non-binary archives.

◆ make_nvp() [4/4]

template<class T >
NameValuePair< T > make_nvp ( std::string const name,
T &&  value 
)
related

Creates a name value pair.

Member Data Documentation

◆ name

template<class T >
char const* cereal::NameValuePair< T >::name

◆ value

template<class T >
Type cereal::NameValuePair< T >::value

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