cereal
A C++11 library for serialization
Static Public Member Functions | List of all members
cereal::LoadAndConstruct< T > Struct Template Reference

A class that allows cereal to load smart pointers to types that have no default constructor. More...

#include </build/libcereal-1.1.2/include/cereal/access.hpp>

Static Public Member Functions

static std::false_type load_and_construct (...)
 Called by cereal if no default constructor exists to load and construct data simultaneously. More...
 

Detailed Description

template<class T>
struct cereal::LoadAndConstruct< T >

A class that allows cereal to load smart pointers to types that have no default constructor.

If your class does not have a default constructor, cereal will not be able to load any smart pointers to it unless you overload LoadAndConstruct for your class, and provide an appropriate load_and_construct method. You can also choose to define a member static function instead of specializing this class.

The specialization of LoadAndConstruct must be placed within the cereal namespace:

struct MyType
{
MyType( int x ); // note: no default ctor
int myX;
// Define a serialize or load/save pair as you normally would
template <class Archive>
void serialize( Archive & ar )
{
ar( myX );
}
};
// Provide a specialization for LoadAndConstruct for your type
namespace cereal
{
template <> struct LoadAndConstruct<MyType>
{
// load_and_construct will be passed the archive that you will be loading
// from as well as a construct object which you can use as if it were the
// constructor for your type. cereal will handle all memory management for you.
template <class Archive>
static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct )
{
int x;
ar( x );
construct( x );
}
};
} // end namespace cereal

Please note that just as in using external serialization functions, you cannot get access to non-public members of your class by befriending cereal::access. If you have the ability to modify the class you wish to serialize, it is recommended that you use member serialize functions and a static member load_and_construct function.

Template Parameters
TThe type to specialize for

Member Function Documentation

template<class T >
static std::false_type cereal::LoadAndConstruct< T >::load_and_construct (   ...)
inlinestatic

Called by cereal if no default constructor exists to load and construct data simultaneously.

Overloads of this should return a pointer to T and expect an archive as a parameter


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