cereal
A C++11 library for serialization
Public Member Functions | Public Attributes | Related Functions | List of all members
cereal::NameValuePair< T > Class Template Reference

For holding name value pairs. More...

#include </home/shane/workspace/cereal/include/cereal/details/helpers.hpp>

Inheritance diagram for cereal::NameValuePair< T >:
cereal::detail::NameValuePairCore

Public Member Functions

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

Public Attributes

char const * name
 
Type value
 

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.
 
template<class T >
NameValuePair< T > make_nvp (const char *name, T &&value)
 Creates a name value pair.
 
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.
 
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.
 

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) );
}
};

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.

Constructor & Destructor Documentation

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.

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