cereal
A C++11 library for serialization
 All Classes Files Functions Variables Typedefs Enumerations Groups
Static Public Member Functions | List of all members
cereal::LoadAndAllocate< T > Struct Template Reference

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

#include <cereal/access.hpp>

Static Public Member Functions

static void load_and_allocate (...)
 Called by cereal if no default constructor exists to load and allocate data simultaneously. More...
 

Detailed Description

template<class T>
struct cereal::LoadAndAllocate< 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 LoadAndAllocate for your class, and provide an appropriate load_and_allocate method.

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

struct MyType
{
MyType( int x ); // note: no default ctor
int myX;
// Define a serialize or save/load pair as you normally would
template <class Archive>
void serialize( Archive & ar )
{
ar( myX );
}
};
// Provide a specialization for LoadAndAllocate for your type
namespace cereal
{
template <> struct LoadAndAllocate<MyType>
{
// load_and_allocate will be passed the archive that you will be loading
// from and should return a raw pointer to a dynamically allocated instance
// of your type.
//
// This will be captured by a smart pointer of some type and you need not
// worry about managing the memory
template <class Archive>
static MyType * load_and_allocate( Archive & ar )
{
int x;
ar( x );
return new MyType( x );
}
};
} // end namespace cereal
Template Parameters
TThe type to specialize for

Member Function Documentation

template<class T >
static void cereal::LoadAndAllocate< T >::load_and_allocate (   ...)
inlinestatic

Called by cereal if no default constructor exists to load and allocate 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: