Fix for load_and_allocate in regards to issue #42

load_and_allocate did not properly enter into the 'data' NVP that the
ptr_wrapper creates for unique/shared ptr.  When loading these types,
we now go through a wrapper struct to force entry into an extra node to
resolve this issue.

Changes to unittests are for an issue compiling with g++-4.7.3 under
Ubuntu where steady_clock::now() is not defined for some reason
This commit is contained in:
Shane Grant
2014-01-07 11:38:28 -08:00
parent 4de892eaf8
commit 30836ce2bf
2 changed files with 47 additions and 3 deletions

View File

@@ -54,6 +54,23 @@ namespace cereal
{ {
return {std::forward<T>(t)}; return {std::forward<T>(t)};
} }
//! A struct that acts as a wrapper around calling load_andor_allocate
/*! The purpose of this is to allow a load_and_allocate call to properly enter into the
'data' NVP of the ptr_wrapper
@internal */
template <class Archive, class T>
struct LoadAndAllocateLoadWrapper
{
LoadAndAllocateLoadWrapper() = default;
inline void serialize( Archive & ar )
{
ptr = ::cereal::detail::Load<T, Archive>::load_andor_allocate( ar );
}
T * ptr;
};
} }
//! Saving std::shared_ptr for non polymorphic types //! Saving std::shared_ptr for non polymorphic types
@@ -140,8 +157,10 @@ namespace cereal
if( id & detail::msb_32bit ) if( id & detail::msb_32bit )
{ {
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) ); // Use wrapper to enter into "data" nvp
ar.registerSharedPointer(id, ptr); memory_detail::LoadAndAllocateLoadWrapper<Archive, T> loadWrapper;
ar( loadWrapper );
ptr.reset( loadWrapper.ptr );
} }
else else
{ {
@@ -205,7 +224,12 @@ namespace cereal
auto & ptr = wrapper.ptr; auto & ptr = wrapper.ptr;
if( isValid ) if( isValid )
ptr.reset( detail::Load<T, Archive>::load_andor_allocate( ar ) ); {
// Use wrapper to enter into "data" nvp
memory_detail::LoadAndAllocateLoadWrapper<Archive, T> loadWrapper;
ar( loadWrapper );
ptr.reset( loadWrapper.ptr );
}
else else
ptr.reset( nullptr ); ptr.reset( nullptr );
} }

View File

@@ -2570,11 +2570,15 @@ void test_chrono()
for(int ii=0; ii<100; ++ii) for(int ii=0; ii<100; ++ii)
{ {
auto o_timePoint1 = std::chrono::system_clock::now(); auto o_timePoint1 = std::chrono::system_clock::now();
#ifndef CEREAL_OLDER_GCC
auto o_timePoint2 = std::chrono::steady_clock::now(); auto o_timePoint2 = std::chrono::steady_clock::now();
#endif // CEREAL_OLDER_GCC
auto o_timePoint3 = std::chrono::high_resolution_clock::now(); auto o_timePoint3 = std::chrono::high_resolution_clock::now();
auto o_duration1 = std::chrono::system_clock::now() - o_timePoint1; auto o_duration1 = std::chrono::system_clock::now() - o_timePoint1;
#ifndef CEREAL_OLDER_GCC
auto o_duration2 = std::chrono::steady_clock::now() - o_timePoint2; auto o_duration2 = std::chrono::steady_clock::now() - o_timePoint2;
#endif // CEREAL_OLDER_GCC
auto o_duration3 = std::chrono::high_resolution_clock::now() - o_timePoint3; auto o_duration3 = std::chrono::high_resolution_clock::now() - o_timePoint3;
std::ostringstream os; std::ostringstream os;
@@ -2582,18 +2586,26 @@ void test_chrono()
OArchive oar(os); OArchive oar(os);
oar(o_timePoint1); oar(o_timePoint1);
#ifndef CEREAL_OLDER_GCC
oar(o_timePoint2); oar(o_timePoint2);
#endif // CEREAL_OLDER_GCC
oar(o_timePoint3); oar(o_timePoint3);
oar(o_duration1); oar(o_duration1);
#ifndef CEREAL_OLDER_GCC
oar(o_duration2); oar(o_duration2);
#endif // CEREAL_OLDER_GCC
oar(o_duration3); oar(o_duration3);
} }
decltype(o_timePoint1) i_timePoint1; decltype(o_timePoint1) i_timePoint1;
#ifndef CEREAL_OLDER_GCC
decltype(o_timePoint2) i_timePoint2; decltype(o_timePoint2) i_timePoint2;
#endif // CEREAL_OLDER_GCC
decltype(o_timePoint3) i_timePoint3; decltype(o_timePoint3) i_timePoint3;
decltype(o_duration1) i_duration1; decltype(o_duration1) i_duration1;
#ifndef CEREAL_OLDER_GCC
decltype(o_duration2) i_duration2; decltype(o_duration2) i_duration2;
#endif // CEREAL_OLDER_GCC
decltype(o_duration3) i_duration3; decltype(o_duration3) i_duration3;
std::istringstream is(os.str()); std::istringstream is(os.str());
@@ -2601,18 +2613,26 @@ void test_chrono()
IArchive iar(is); IArchive iar(is);
iar(i_timePoint1); iar(i_timePoint1);
#ifndef CEREAL_OLDER_GCC
iar(i_timePoint2); iar(i_timePoint2);
#endif // CEREAL_OLDER_GCC
iar(i_timePoint3); iar(i_timePoint3);
iar(i_duration1); iar(i_duration1);
#ifndef CEREAL_OLDER_GCC
iar(i_duration2); iar(i_duration2);
#endif // CEREAL_OLDER_GCC
iar(i_duration3); iar(i_duration3);
} }
BOOST_CHECK( o_timePoint1 == i_timePoint1 ); BOOST_CHECK( o_timePoint1 == i_timePoint1 );
#ifndef CEREAL_OLDER_GCC
BOOST_CHECK( o_timePoint2 == i_timePoint2 ); BOOST_CHECK( o_timePoint2 == i_timePoint2 );
#endif // CEREAL_OLDER_GCC
BOOST_CHECK( o_timePoint3 == i_timePoint3 ); BOOST_CHECK( o_timePoint3 == i_timePoint3 );
BOOST_CHECK( o_duration1 == i_duration1 ); BOOST_CHECK( o_duration1 == i_duration1 );
#ifndef CEREAL_OLDER_GCC
BOOST_CHECK( o_duration2 == i_duration2 ); BOOST_CHECK( o_duration2 == i_duration2 );
#endif // CEREAL_OLDER_GCC
BOOST_CHECK( o_duration3 == i_duration3 ); BOOST_CHECK( o_duration3 == i_duration3 );
} }
} }