Skip to content

Commit

Permalink
Renaming load_and_allocate to load_and_construct
Browse files Browse the repository at this point in the history
Anything associated this that used the verb allocate has been replaced with construct

closes #59
  • Loading branch information
AzothAmmo committed Feb 21, 2014
1 parent 4a3c285 commit 30a22fe
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 137 deletions.
70 changes: 35 additions & 35 deletions include/cereal/access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ namespace cereal
{
//! 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. You can also
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 LoadAndAllocate must be placed within the cereal namespace:
The specialization of LoadAndConstruct must be placed within the cereal namespace:
@code{.cpp}
struct MyType
Expand All @@ -59,20 +59,20 @@ namespace cereal
}
};
// Provide a specialization for LoadAndAllocate for your type
// Provide a specialization for LoadAndConstruct for your type
namespace cereal
{
template <> struct LoadAndAllocate<MyType>
template <> struct LoadAndConstruct<MyType>
{
// load_and_allocate will be passed the archive that you will be loading
// from as well as an allocate object which you can use as if it were the
// 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_allocate( Archive & ar, cereal::allocate<MyType> & allocate )
static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct )
{
int x;
ar( x );
allocate( x );
construct( x );
}
};
} // end namespace cereal
Expand All @@ -81,30 +81,30 @@ 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_allocate function.
use member serialize functions and a static member load_and_construct function.
@tparam T The type to specialize for
@ingroup Access */
template <class T>
struct LoadAndAllocate
struct LoadAndConstruct
{
//! Called by cereal if no default constructor exists to load and allocate data simultaneously
//! 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 */
static std::false_type load_and_allocate(...)
static std::false_type load_and_construct(...)
{ return std::false_type(); }
};

// forward decl for allocate
// forward decl for construct
//! @cond PRIVATE_NEVERDEFINED
namespace memory_detail{ template <class Ar, class T> struct LoadAndAllocateLoadWrapper; }
namespace memory_detail{ template <class Ar, class T> struct LoadAndConstructLoadWrapper; }
//! @endcond

//! Used to allocate types with no default constructor
//! Used to construct types with no default constructor
/*! When serializing a type that has no default constructor, cereal
will attempt to call either the class static function load_and_allocate
or the appropriate template specialization of LoadAndAllocate. cereal
will attempt to call either the class static function load_and_construct
or the appropriate template specialization of LoadAndConstruct. cereal
will pass that function a reference to the archive as well as a reference
to an allocate object which should be used to perform the allocation once
to a construct object which should be used to perform the allocation once
data has been appropriately loaded.
@code{.cpp}
Expand All @@ -124,32 +124,32 @@ namespace cereal
}
template <class Archive>
static void load_and_allocate( Archive & ar, cereal::allocate<MyType> & allocate )
static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct )
{
int x, y;
ar( x, y );
// use allocate object to initialize with loaded data
allocate( x, y );
// use construct object to initialize with loaded data
construct( x, y );
// access to member variables and functions via -> operator
ar( allocate->notInConstructor );
ar( construct->notInConstructor );
// could also do the above section by:
double z;
ar( z );
allocate->notInConstructor = z;
construct->notInConstructor = z;
}
};
@endcode
@tparam T The class type being serialized
*/
template <class T>
class allocate
class construct
{
public:
//! Allocate and initialize the type T with the given arguments
//! Construct and initialize the type T with the given arguments
/*! This will forward all arguments to the underlying type T,
calling an appropriate constructor.
Expand All @@ -162,7 +162,7 @@ namespace cereal
void operator()( Args && ... args )
{
if( itsValid )
throw Exception("Attempting to allocate an already initialized object");
throw Exception("Attempting to construct an already initialized object");

new (itsPtr) T( std::forward<Args>( args )... );
itsValid = true;
Expand All @@ -183,7 +183,7 @@ namespace cereal

//! Returns a raw pointer to the initialized underlying object
/*! This is mainly intended for use with passing an instance of
an allocated object to cereal::base_class.
a constructed object to cereal::base_class.
It is strongly recommended to avoid using this function in
any other circumstance.
Expand All @@ -195,11 +195,11 @@ namespace cereal
}

private:
template <class A, class B> friend struct ::cereal::memory_detail::LoadAndAllocateLoadWrapper;
template <class A, class B> friend struct ::cereal::memory_detail::LoadAndConstructLoadWrapper;

allocate( T * p ) : itsPtr( p ), itsValid( false ) {}
allocate( allocate const & ) = delete;
allocate & operator=( allocate const & ) = delete;
construct( T * p ) : itsPtr( p ), itsValid( false ) {}
construct( construct const & ) = delete;
construct & operator=( construct const & ) = delete;

T * itsPtr;
bool itsValid;
Expand Down Expand Up @@ -263,13 +263,13 @@ namespace cereal
{ t.load(ar, version); }

template <class T>
static std::false_type load_and_allocate(...)
static std::false_type load_and_construct(...)
{ return std::false_type(); }

template<class T, class Archive> inline
static auto load_and_allocate(Archive & ar, ::cereal::allocate<T> & allocate) -> decltype(T::load_and_allocate(ar, allocate))
static auto load_and_construct(Archive & ar, ::cereal::construct<T> & construct) -> decltype(T::load_and_construct(ar, construct))
{
T::load_and_allocate( ar, allocate );
T::load_and_construct( ar, construct );
}
};

Expand Down
44 changes: 22 additions & 22 deletions include/cereal/details/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,22 @@ namespace cereal
struct has_non_member_versioned_##name : std::integral_constant<bool, detail::has_non_member_versioned_##name##_impl<T, A>::value> {}

// ######################################################################
// Member load_and_allocate
// Member load_and_construct
template<typename T, typename A>
struct has_member_load_and_allocate :
std::integral_constant<bool, std::is_same<decltype( access::load_and_allocate<T>( std::declval<A&>(), std::declval< ::cereal::allocate<T>&>() ) ), void>::value> {};
struct has_member_load_and_construct :
std::integral_constant<bool, std::is_same<decltype( access::load_and_construct<T>( std::declval<A&>(), std::declval< ::cereal::construct<T>&>() ) ), void>::value> {};

// ######################################################################
// Non Member load_and_allocate
// Non Member load_and_construct
template<typename T, typename A>
struct has_non_member_load_and_allocate : std::integral_constant<bool,
std::is_same<decltype( LoadAndAllocate<T>::load_and_allocate( std::declval<A&>(), std::declval< ::cereal::allocate<T>&>() ) ), void>::value> {};
struct has_non_member_load_and_construct : std::integral_constant<bool,
std::is_same<decltype( LoadAndConstruct<T>::load_and_construct( std::declval<A&>(), std::declval< ::cereal::construct<T>&>() ) ), void>::value> {};

// ######################################################################
// Has either a member or non member allocate
template<typename T, typename A>
struct has_load_and_allocate : std::integral_constant<bool,
has_member_load_and_allocate<T, A>::value || has_non_member_load_and_allocate<T, A>::value>
struct has_load_and_construct : std::integral_constant<bool,
has_member_load_and_construct<T, A>::value || has_non_member_load_and_construct<T, A>::value>
{ };

// ######################################################################
Expand Down Expand Up @@ -580,47 +580,47 @@ namespace cereal
// ######################################################################
namespace detail
{
template <class T, class A, bool Member = traits::has_member_load_and_allocate<T, A>::value, bool NonMember = traits::has_non_member_load_and_allocate<T, A>::value>
struct Load
template <class T, class A, bool Member = traits::has_member_load_and_construct<T, A>::value, bool NonMember = traits::has_non_member_load_and_construct<T, A>::value>
struct Construct
{
static_assert( !sizeof(T), "Cereal detected both member and non member load_and_allocate functions!" );
static T * load_andor_allocate( A & /*ar*/, allocate<T> & /*allocate*/ )
static_assert( !sizeof(T), "Cereal detected both member and non member load_and_construct functions!" );
static T * load_andor_construct( A & /*ar*/, construct<T> & /*construct*/ )
{ return nullptr; }
};

template <class T, class A>
struct Load<T, A, false, false>
struct Construct<T, A, false, false>
{
static_assert( std::is_default_constructible<T>::value,
"Trying to serialize a an object with no default constructor.\n\n"
"Types must either be default constructible or define either a member or non member Construct function.\n"
"Construct functions generally have the signature:\n\n"
"template <class Archive>\n"
"static void load_and_allocate(Archive & ar, cereal::allocate<T> & allocate)\n"
"static void load_and_construct(Archive & ar, cereal::construct<T> & construct)\n"
"{\n"
" var a;\n"
" ar( a )\n"
" allocate( a );\n"
" construct( a );\n"
"}\n\n" );
static T * load_andor_allocate()
static T * load_andor_construct()
{ return new T(); }
};

template <class T, class A>
struct Load<T, A, true, false>
struct Construct<T, A, true, false>
{
static void load_andor_allocate( A & ar, allocate<T> & allocate )
static void load_andor_construct( A & ar, construct<T> & construct )
{
access::load_and_allocate<T>( ar, allocate );
access::load_and_construct<T>( ar, construct );
}
};

template <class T, class A>
struct Load<T, A, false, true>
struct Construct<T, A, false, true>
{
static void load_andor_allocate( A & ar, allocate<T> & allocate )
static void load_andor_construct( A & ar, construct<T> & construct )
{
LoadAndAllocate<T>::load_and_allocate( ar, allocate );
LoadAndConstruct<T>::load_and_construct( ar, construct );
}
};
} // namespace detail
Expand Down
Loading

0 comments on commit 30a22fe

Please sign in to comment.