Added case for nullptr smart pointers

This commit is contained in:
Randolph Voorhies
2013-06-19 19:04:21 -07:00
parent 8849251bb1
commit 27ef76de78
2 changed files with 16 additions and 3 deletions

View File

@@ -141,7 +141,7 @@ namespace cereal
class OutputArchive class OutputArchive
{ {
public: public:
OutputArchive(ArchiveType * const self) : self(self), itsCurrentPointerId(0) OutputArchive(ArchiveType * const self) : self(self), itsCurrentPointerId(1)
{ } { }
//! Serializes all passed in data //! Serializes all passed in data
@@ -155,6 +155,9 @@ namespace cereal
//! Registers a pointer with the archive //! Registers a pointer with the archive
uint32_t registerSharedPointer( void * addr ) uint32_t registerSharedPointer( void * addr )
{ {
// Handle null pointers by just returning 0
if(addr == 0) return 0;
auto id = itsSharedPointerMap.find( addr ); auto id = itsSharedPointerMap.find( addr );
if( id == itsSharedPointerMap.end() ) if( id == itsSharedPointerMap.end() )
{ {
@@ -302,6 +305,8 @@ namespace cereal
std::shared_ptr<void> getSharedPointer(uint32_t const id) std::shared_ptr<void> getSharedPointer(uint32_t const id)
{ {
if(id == 0) return std::shared_ptr<void>(nullptr);
auto ptr = itsSharedPointerMap.find( id ); auto ptr = itsSharedPointerMap.find( id );
if(ptr == itsSharedPointerMap.end()) if(ptr == itsSharedPointerMap.end())
{ {

View File

@@ -697,8 +697,11 @@ BOOST_AUTO_TEST_CASE( binary_memory )
std::shared_ptr<int> o_xptr2 = o_xptr1; std::shared_ptr<int> o_xptr2 = o_xptr1;
std::shared_ptr<int> o_yptr1 = std::make_shared<int>(random_value<int>(gen)); std::shared_ptr<int> o_yptr1 = std::make_shared<int>(random_value<int>(gen));
std::shared_ptr<int> o_yptr2 = o_yptr1; std::shared_ptr<int> o_yptr2 = o_yptr1;
oar( o_xptr1, o_xptr2); std::shared_ptr<int> o_nullptr1;
oar( o_yptr1, o_yptr2); std::shared_ptr<int> o_nullptr2;
oar( o_xptr1, o_xptr2 );
oar( o_yptr1, o_yptr2 );
oar( o_nullptr1, o_nullptr2 );
std::istringstream is(os.str()); std::istringstream is(os.str());
cereal::BinaryInputArchive iar(is); cereal::BinaryInputArchive iar(is);
@@ -707,8 +710,11 @@ BOOST_AUTO_TEST_CASE( binary_memory )
std::shared_ptr<int> i_xptr2; std::shared_ptr<int> i_xptr2;
std::shared_ptr<int> i_yptr1; std::shared_ptr<int> i_yptr1;
std::shared_ptr<int> i_yptr2; std::shared_ptr<int> i_yptr2;
std::shared_ptr<int> i_nullptr1;
std::shared_ptr<int> i_nullptr2;
iar( i_xptr1, i_xptr2); iar( i_xptr1, i_xptr2);
iar( i_yptr1, i_yptr2); iar( i_yptr1, i_yptr2);
oar( i_nullptr1, i_nullptr2 );
BOOST_CHECK_EQUAL(o_xptr1.get(), o_xptr2.get()); BOOST_CHECK_EQUAL(o_xptr1.get(), o_xptr2.get());
BOOST_CHECK_EQUAL(i_xptr1.get(), i_xptr2.get()); BOOST_CHECK_EQUAL(i_xptr1.get(), i_xptr2.get());
@@ -717,6 +723,8 @@ BOOST_AUTO_TEST_CASE( binary_memory )
BOOST_CHECK_EQUAL(o_yptr1.get(), o_yptr2.get()); BOOST_CHECK_EQUAL(o_yptr1.get(), o_yptr2.get());
BOOST_CHECK_EQUAL(i_yptr1.get(), i_yptr2.get()); BOOST_CHECK_EQUAL(i_yptr1.get(), i_yptr2.get());
BOOST_CHECK_EQUAL(*i_yptr1, *i_yptr2); BOOST_CHECK_EQUAL(*i_yptr1, *i_yptr2);
BOOST_CHECK(!i_nullptr1);
BOOST_CHECK(!i_nullptr2);
} }
} }