Added weak_ptr support

This commit is contained in:
=
2013-06-13 16:07:36 -07:00
parent 58abf1d2b9
commit b4afce223b
2 changed files with 31 additions and 28 deletions

View File

@@ -6,18 +6,6 @@
namespace cereal
{
/*
*
*
*
*
*
*
*
*
*
*
*/
//! Saving std::shared_ptr to binary
template <class T>
void save( BinaryOutputArchive & ar, std::shared_ptr<T> const & ptr )
@@ -53,6 +41,24 @@ namespace cereal
ptr = std::static_pointer_cast<T>(ar.getSharedPointer(id));
}
}
//! Saving std::weak_ptr to binary
template <class T>
void save( BinaryOutputArchive & ar, std::weak_ptr<T> const & ptr )
{
auto sptr = ptr.lock();
ar & sptr;
}
//! Loading std::weak_ptr to binary
template <class T>
void load( BinaryInputArchive & ar, std::weak_ptr<T> & ptr )
{
std::shared_ptr<T> sptr;
ar & sptr;
ptr = sptr;
}
} // namespace cereal
#endif // CEREAL_BINARY_ARCHIVE_SHARED_PTR_HPP_

View File

@@ -3,7 +3,7 @@
#include <cereal/binary_archive/binary_archive.hpp>
#include <cereal/binary_archive/string.hpp>
#include <cereal/json_archive/json_archive.hpp>
#include <cereal/binary_archive/shared_ptr.hpp>
#include <cereal/binary_archive/memory.hpp>
#include <cxxabi.h>
#include <sstream>
@@ -145,32 +145,29 @@ int main()
{
std::ofstream os("ptr.txt");
cereal::BinaryOutputArchive archive(os);
std::shared_ptr<int> xptr1 = std::make_shared<int>(5);
std::shared_ptr<int> xptr2 = xptr1;
std::shared_ptr<int> yptr1 = std::make_shared<int>(6);
std::shared_ptr<int> yptr2 = yptr1;
std::shared_ptr<std::shared_ptr<int>> xptr1 = std::make_shared<std::shared_ptr<int>>(std::make_shared<int>(5));
std::shared_ptr<int> xptr2 = *xptr1;
std::weak_ptr<int> wptr2 = xptr2;
archive & xptr1;
archive & xptr2;
archive & yptr1;
archive & yptr2;
archive & wptr2;
}
{
std::ifstream is("ptr.txt");
cereal::BinaryInputArchive archive(is);
std::shared_ptr<int> xptr1;
std::shared_ptr<std::shared_ptr<int>> xptr1;
std::shared_ptr<int> xptr2;
std::shared_ptr<int> yptr1;
std::shared_ptr<int> yptr2;
std::weak_ptr<int> wptr2;
archive & xptr1;
archive & xptr2;
archive & yptr1;
archive & yptr2;
archive & wptr2;
assert(xptr1.get() == xptr2.get());
assert(yptr1.get() == yptr2.get());
std::cout << *xptr1 << " " << *xptr2 << std::endl;
std::cout << *yptr1 << " " << *yptr2 << std::endl;
std::cout << **xptr1 << std::endl;
std::cout << *xptr2 << std::endl;
std::cout << (*xptr1).get() << " == " << xptr2.get() << " ? " << ((*xptr1).get() == xptr2.get()) << std::endl;
std::cout << *(wptr2.lock()) << std::endl;
std::cout << (wptr2.lock().get() == xptr2.get()) << std::endl;
}