mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
@@ -155,7 +155,7 @@ namespace cereal
|
|||||||
// Non Member load_and_allocate
|
// Non Member load_and_allocate
|
||||||
template<typename T, typename A>
|
template<typename T, typename A>
|
||||||
struct has_non_member_load_and_allocate : std::integral_constant<bool,
|
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>&>() ) ), T*>::value> {};
|
std::is_same<decltype( LoadAndAllocate<T>::load_and_allocate( std::declval<A&>(), std::declval<::cereal::allocate<T>&>() ) ), void>::value> {};
|
||||||
|
|
||||||
// ######################################################################
|
// ######################################################################
|
||||||
// Has either a member or non member allocate
|
// Has either a member or non member allocate
|
||||||
|
|||||||
@@ -250,22 +250,22 @@ namespace cereal
|
|||||||
|
|
||||||
if( isValid )
|
if( isValid )
|
||||||
{
|
{
|
||||||
// allocate into unique ptr to handle exceptions
|
|
||||||
// pretend this is the real deal
|
|
||||||
// after we get it back, transfer to proper shared ptr
|
|
||||||
|
|
||||||
// Storage type for the pointer - since we can't default construct this type,
|
// Storage type for the pointer - since we can't default construct this type,
|
||||||
// we'll allocate it using std::aligned_storage and use a custom deleter
|
// we'll allocate it using std::aligned_storage
|
||||||
using ST = typename std::aligned_storage<sizeof(T)>::type;
|
using ST = typename std::aligned_storage<sizeof(T)>::type;
|
||||||
|
|
||||||
|
// Allocate storage - note the ST type so that deleter is correct if
|
||||||
|
// an exception is thrown before we are initialized
|
||||||
std::unique_ptr<ST> stPtr( new ST() );
|
std::unique_ptr<ST> stPtr( new ST() );
|
||||||
|
|
||||||
// Use wrapper to enter into "data" nvp of ptr_wrapper
|
// Use wrapper to enter into "data" nvp of ptr_wrapper
|
||||||
memory_detail::LoadAndAllocateLoadWrapper<Archive, T> loadWrapper( reinterpret_cast<T *>( stPtr.get() ) );
|
memory_detail::LoadAndAllocateLoadWrapper<Archive, T> loadWrapper( reinterpret_cast<T *>( stPtr.get() ) );
|
||||||
|
|
||||||
|
// Initialize storage
|
||||||
ar( loadWrapper );
|
ar( loadWrapper );
|
||||||
|
|
||||||
ptr.reset( stPtr.release() );
|
// Transfer ownership to correct unique_ptr type
|
||||||
|
ptr.reset( reinterpret_cast<T *>( stPtr.release() ) );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ptr.reset( nullptr );
|
ptr.reset( nullptr );
|
||||||
|
|||||||
129
unittests.cpp
129
unittests.cpp
@@ -1145,6 +1145,135 @@ BOOST_AUTO_TEST_CASE( json_memory_cycles )
|
|||||||
test_memory_cycles<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
|
test_memory_cycles<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ######################################################################
|
||||||
|
struct OneLA
|
||||||
|
{
|
||||||
|
OneLA( int xx ) : x( xx ) {}
|
||||||
|
|
||||||
|
int x;
|
||||||
|
|
||||||
|
template <class Archive>
|
||||||
|
void serialize( Archive & ar )
|
||||||
|
{ ar( x ); }
|
||||||
|
|
||||||
|
template <class Archive>
|
||||||
|
static void load_and_allocate( Archive & ar, cereal::allocate<OneLA> & allocate )
|
||||||
|
{
|
||||||
|
int xx;
|
||||||
|
ar( xx );
|
||||||
|
allocate( xx );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==( OneLA const & other ) const
|
||||||
|
{ return x == other.x; }
|
||||||
|
};
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, OneLA const & s)
|
||||||
|
{
|
||||||
|
os << "[" << s.x << "]";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TwoLA
|
||||||
|
{
|
||||||
|
TwoLA( int xx ) : x( xx ) {}
|
||||||
|
|
||||||
|
int x;
|
||||||
|
|
||||||
|
template <class Archive>
|
||||||
|
void serialize( Archive & ar )
|
||||||
|
{ ar( x ); }
|
||||||
|
|
||||||
|
bool operator==( TwoLA const & other ) const
|
||||||
|
{ return x == other.x; }
|
||||||
|
};
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, TwoLA const & s)
|
||||||
|
{
|
||||||
|
os << "[" << s.x << "]";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace cereal
|
||||||
|
{
|
||||||
|
template <>
|
||||||
|
struct LoadAndAllocate<TwoLA>
|
||||||
|
{
|
||||||
|
template <class Archive>
|
||||||
|
static void load_and_allocate( Archive & ar, cereal::allocate<TwoLA> & allocate )
|
||||||
|
{
|
||||||
|
int xx;
|
||||||
|
ar( xx );
|
||||||
|
allocate( xx );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class IArchive, class OArchive>
|
||||||
|
void test_memory_load_allocate()
|
||||||
|
{
|
||||||
|
std::random_device rd;
|
||||||
|
std::mt19937 gen(rd());
|
||||||
|
|
||||||
|
for(int ii=0; ii<100; ++ii)
|
||||||
|
{
|
||||||
|
auto o_shared1 = std::make_shared<OneLA>( random_value<int>(gen) );
|
||||||
|
auto o_shared2 = std::make_shared<TwoLA>( random_value<int>(gen) );
|
||||||
|
std::unique_ptr<OneLA> o_unique1( new OneLA( random_value<int>(gen) ) );
|
||||||
|
std::unique_ptr<TwoLA> o_unique2( new TwoLA( random_value<int>(gen) ) );
|
||||||
|
|
||||||
|
std::ostringstream os;
|
||||||
|
{
|
||||||
|
OArchive oar(os);
|
||||||
|
|
||||||
|
oar( o_shared1 );
|
||||||
|
oar( o_shared2 );
|
||||||
|
oar( o_unique1 );
|
||||||
|
oar( o_unique2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
decltype(o_shared1) i_shared1;
|
||||||
|
decltype(o_shared2) i_shared2;
|
||||||
|
decltype(o_unique1) i_unique1;
|
||||||
|
decltype(o_unique2) i_unique2;
|
||||||
|
|
||||||
|
std::istringstream is(os.str());
|
||||||
|
{
|
||||||
|
IArchive iar(is);
|
||||||
|
|
||||||
|
iar( i_shared1 );
|
||||||
|
iar( i_shared2 );
|
||||||
|
iar( i_unique1 );
|
||||||
|
iar( i_unique2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL( *o_shared1, *i_shared1 );
|
||||||
|
BOOST_CHECK_EQUAL( *o_shared2, *i_shared2 );
|
||||||
|
BOOST_CHECK_EQUAL( *o_unique1, *i_unique1 );
|
||||||
|
BOOST_CHECK_EQUAL( *o_unique2, *i_unique2 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE( binary_memory_load_allocate )
|
||||||
|
{
|
||||||
|
test_memory_load_allocate<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE( portable_binary_memory_load_allocate )
|
||||||
|
{
|
||||||
|
test_memory_load_allocate<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE( xml_memory_load_allocate )
|
||||||
|
{
|
||||||
|
test_memory_load_allocate<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE( json_memory_load_allocate )
|
||||||
|
{
|
||||||
|
test_memory_load_allocate<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
|
||||||
|
}
|
||||||
|
|
||||||
// ######################################################################
|
// ######################################################################
|
||||||
template <class IArchive, class OArchive>
|
template <class IArchive, class OArchive>
|
||||||
void test_memory()
|
void test_memory()
|
||||||
|
|||||||
Reference in New Issue
Block a user