tidying up

This commit is contained in:
Shane Grant
2013-06-28 11:39:34 -07:00
parent a5cc31bdb0
commit 196023c4ce
5 changed files with 121 additions and 18 deletions

View File

@@ -39,8 +39,6 @@
namespace cereal namespace cereal
{ {
static const int32_t msb_32bit = 0x80000000;
// ###################################################################### // ######################################################################
//! An exception class thrown when things go wrong at runtime //! An exception class thrown when things go wrong at runtime
struct Exception : public std::runtime_error struct Exception : public std::runtime_error
@@ -72,7 +70,6 @@ namespace cereal
size() call. In either case, any constness will be stripped away */ size() call. In either case, any constness will be stripped away */
NameValuePair( char const * n, T && v ) : name(n), value(const_cast<Type>(v)) {} NameValuePair( char const * n, T && v ) : name(n), value(const_cast<Type>(v)) {}
//std::string name;
char const * name; char const * name;
Type value; Type value;
}; };
@@ -91,6 +88,9 @@ namespace cereal
return {name, std::forward<T>(value)}; return {name, std::forward<T>(value)};
} }
//! Creates a name value pair for the variable T, using the same name
#define CEREAL_NVP(T) ::cereal::make_nvp(#T, T)
// ###################################################################### // ######################################################################
//! A wrapper around data that can be serialized in a binary fashion //! A wrapper around data that can be serialized in a binary fashion
template <class T> template <class T>
@@ -115,9 +115,6 @@ namespace cereal
return {std::forward<T>(data), size}; return {std::forward<T>(data), size};
} }
//! Creates a name value pair for the variable T, using the same name
#define CEREAL_NVP(T) ::cereal::make_nvp(#T, T)
// ###################################################################### // ######################################################################
//! Called before a type is serialized to set up any special archive state //! Called before a type is serialized to set up any special archive state
//! for processing some type //! for processing some type
@@ -155,6 +152,8 @@ namespace cereal
// forward decls for polymorphic support // forward decls for polymorphic support
template <class Archive, class T> struct polymorphic_serialization_support; template <class Archive, class T> struct polymorphic_serialization_support;
struct adl_tag; struct adl_tag;
static const int32_t msb_32bit = 0x80000000;
} }
//! Registers a specific Archive type with cereal //! Registers a specific Archive type with cereal
@@ -194,7 +193,7 @@ namespace cereal
{ {
auto ptrId = itsCurrentPointerId++; auto ptrId = itsCurrentPointerId++;
itsSharedPointerMap.insert( {addr, ptrId} ); itsSharedPointerMap.insert( {addr, ptrId} );
return ptrId | msb_32bit; // mask MSB to be 1 return ptrId | detail::msb_32bit; // mask MSB to be 1
} }
else else
return id->second; return id->second;
@@ -312,7 +311,6 @@ namespace cereal
//! The id to be given to the next pointer //! The id to be given to the next pointer
std::size_t itsCurrentPointerId; std::size_t itsCurrentPointerId;
}; // class OutputArchive }; // class OutputArchive
// ###################################################################### // ######################################################################
@@ -345,7 +343,7 @@ namespace cereal
void registerSharedPointer(uint32_t const id, std::shared_ptr<void> ptr) void registerSharedPointer(uint32_t const id, std::shared_ptr<void> ptr)
{ {
uint32_t const stripped_id = id & ~msb_32bit; uint32_t const stripped_id = id & ~detail::msb_32bit;
itsSharedPointerMap.insert( {stripped_id, ptr} ); itsSharedPointerMap.insert( {stripped_id, ptr} );
} }
@@ -459,7 +457,6 @@ namespace cereal
//! Maps from addresses to pointer ids //! Maps from addresses to pointer ids
std::unordered_map<std::size_t, std::shared_ptr<void>> itsSharedPointerMap; std::unordered_map<std::size_t, std::shared_ptr<void>> itsSharedPointerMap;
}; // class InputArchive }; // class InputArchive
} // namespace cereal } // namespace cereal

View File

@@ -40,7 +40,7 @@ namespace cereal
uint32_t id = ar.registerSharedPointer( ptr.get() ); uint32_t id = ar.registerSharedPointer( ptr.get() );
ar( id ); ar( id );
if( id & msb_32bit ) if( id & detail::msb_32bit )
{ {
ar( *ptr ); ar( *ptr );
} }
@@ -56,7 +56,7 @@ namespace cereal
ar( id ); ar( id );
if( id & msb_32bit ) if( id & detail::msb_32bit )
{ {
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) ); ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) );
ar.registerSharedPointer(id, ptr); ar.registerSharedPointer(id, ptr);
@@ -77,7 +77,7 @@ namespace cereal
ar( id ); ar( id );
if( id & msb_32bit ) if( id & detail::msb_32bit )
{ {
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) ); ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) );
ar( *ptr ); ar( *ptr );
@@ -134,7 +134,6 @@ namespace cereal
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) ); ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) );
ar( *ptr ); ar( *ptr );
} }
} // namespace cereal } // namespace cereal
#endif // CEREAL_TYPES_SHARED_PTR_HPP_ #endif // CEREAL_TYPES_SHARED_PTR_HPP_

View File

@@ -27,6 +27,8 @@
#ifndef CEREAL_TYPES_POLYMORPHIC_HPP_ #ifndef CEREAL_TYPES_POLYMORPHIC_HPP_
#define CEREAL_TYPES_POLYMORPHIC_HPP_ #define CEREAL_TYPES_POLYMORPHIC_HPP_
#include <cereal/cereal.hpp>
#include <memory>
#include <cereal/details/polymorphic_impl.hpp> #include <cereal/details/polymorphic_impl.hpp>
//! Binds a polymorhic type to all registered archives //! Binds a polymorhic type to all registered archives
@@ -47,4 +49,109 @@
>::getInstance().bind(); \ >::getInstance().bind(); \
}} // end namespaces }} // end namespaces
namespace cereal
{
//! Saving std::shared_ptr for polymorphic types
template <class Archive, class T> inline
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
save( Archive & ar, std::shared_ptr<T> const & ptr )
{
uint32_t id = ar.registerSharedPointer( ptr.get() );
ar( id );
if( id & detail::msb_32bit )
{
ar( *ptr );
}
}
//! Loading std::shared_ptr, case when user load and allocate for polymorphic types
template <class Archive, class T> inline
typename std::enable_if<std::is_polymorphic<T>::value
&& traits::has_load_and_allocate<T, Archive>(), void>::type
load( Archive & ar, std::shared_ptr<T> & ptr )
{
uint32_t id;
ar( id );
if( id & detail::msb_32bit )
{
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) );
ar.registerSharedPointer(id, ptr);
}
else
{
ptr = std::static_pointer_cast<T>(ar.getSharedPointer(id));
}
}
//! Loading std::shared_ptr, case when no user load and allocate for polymorphic types
template <class Archive, class T> inline
typename std::enable_if<std::is_polymorphic<T>::value
&& !traits::has_load_and_allocate<T, Archive>(), void>::type
load( Archive & ar, std::shared_ptr<T> & ptr )
{
uint32_t id;
ar( id );
if( id & detail::msb_32bit )
{
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) );
ar( *ptr );
ar.registerSharedPointer(id, ptr);
}
else
{
ptr = std::static_pointer_cast<T>(ar.getSharedPointer(id));
}
}
//! Saving std::weak_ptr for polymorphic types
template <class Archive, class T> inline
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
save( Archive & ar, std::weak_ptr<T> const & ptr )
{
auto sptr = ptr.lock();
ar( sptr );
}
//! Loading std::weak_ptr for polymorphic types
template <class Archive, class T> inline
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
load( Archive & ar, std::weak_ptr<T> & ptr )
{
std::shared_ptr<T> sptr;
ar( sptr );
ptr = sptr;
}
//! Saving std::unique_ptr for polymorphic types
template <class Archive, class T, class D> inline
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
save( Archive & ar, std::unique_ptr<T, D> const & ptr )
{
ar( *ptr );
}
//! Loading std::unique_ptr, case when user provides load_and_allocate for polymorphic types
template <class Archive, class T, class D> inline
typename std::enable_if<std::is_polymorphic<T>::value
&& traits::has_load_and_allocate<T, Archive>(), void>::type
load( Archive & ar, std::unique_ptr<T, D> & ptr )
{
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) );
}
//! Loading std::unique_ptr, case when no load_and_allocate for polymorphic types
template <class Archive, class T, class D> inline
typename std::enable_if<std::is_polymorphic<T>::value
&& !traits::has_load_and_allocate<T, Archive>(), void>::type
load( Archive & ar, std::unique_ptr<T, D> & ptr )
{
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) );
ar( *ptr );
}
} // namespace cereal
#endif // CEREAL_TYPES_POLYMORPHIC_HPP_ #endif // CEREAL_TYPES_POLYMORPHIC_HPP_

View File

@@ -280,7 +280,7 @@ struct PoDChild : PoDStruct
template <class Archive> template <class Archive>
void serialize( Archive & ar ) void serialize( Archive & ar )
{ {
ar( cereal::base_class<PoDStruct>(this), v ); ar( cereal::virtual_base_class<PoDStruct>(this), v );
}; };
template <class Archive> template <class Archive>

View File

@@ -32,7 +32,7 @@
#include <cereal/types/memory.hpp> #include <cereal/types/memory.hpp>
#include <cereal/types/complex.hpp> #include <cereal/types/complex.hpp>
#include <cereal/types/boost_variant.hpp> #include <cereal/types/boost_variant.hpp>
#include <cereal/base_class.hpp> #include <cereal/types/virtual_base_class.hpp>
#include <cxxabi.h> #include <cxxabi.h>
#include <sstream> #include <sstream>
@@ -61,7 +61,7 @@ class Derived : public Base
template <class Archive> template <class Archive>
void save( Archive & ar ) const void save( Archive & ar ) const
{ {
ar( cereal::base_class<Base>(this) ); ar( cereal::virtual_base_class<Base>(this) );
std::cout << "Derived save" << std::endl; std::cout << "Derived save" << std::endl;
ar( y ); ar( y );
} }
@@ -69,7 +69,7 @@ class Derived : public Base
template <class Archive> template <class Archive>
void load( Archive & ar ) void load( Archive & ar )
{ {
ar( cereal::base_class<Base>(this) ); ar( cereal::virtual_base_class<Base>(this) );
std::cout << "Derived load" << std::endl; std::cout << "Derived load" << std::endl;
ar( y ); ar( y );
} }