mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
Added weak_ptr support
This commit is contained in:
@@ -6,18 +6,6 @@
|
|||||||
|
|
||||||
namespace cereal
|
namespace cereal
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
//! Saving std::shared_ptr to binary
|
//! Saving std::shared_ptr to binary
|
||||||
template <class T>
|
template <class T>
|
||||||
void save( BinaryOutputArchive & ar, std::shared_ptr<T> const & ptr )
|
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));
|
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
|
} // namespace cereal
|
||||||
|
|
||||||
#endif // CEREAL_BINARY_ARCHIVE_SHARED_PTR_HPP_
|
#endif // CEREAL_BINARY_ARCHIVE_SHARED_PTR_HPP_
|
||||||
|
|
||||||
29
test.cpp
29
test.cpp
@@ -3,7 +3,7 @@
|
|||||||
#include <cereal/binary_archive/binary_archive.hpp>
|
#include <cereal/binary_archive/binary_archive.hpp>
|
||||||
#include <cereal/binary_archive/string.hpp>
|
#include <cereal/binary_archive/string.hpp>
|
||||||
#include <cereal/json_archive/json_archive.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 <cxxabi.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -145,32 +145,29 @@ int main()
|
|||||||
{
|
{
|
||||||
std::ofstream os("ptr.txt");
|
std::ofstream os("ptr.txt");
|
||||||
cereal::BinaryOutputArchive archive(os);
|
cereal::BinaryOutputArchive archive(os);
|
||||||
std::shared_ptr<int> xptr1 = std::make_shared<int>(5);
|
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::shared_ptr<int> xptr2 = *xptr1;
|
||||||
std::shared_ptr<int> yptr1 = std::make_shared<int>(6);
|
std::weak_ptr<int> wptr2 = xptr2;
|
||||||
std::shared_ptr<int> yptr2 = yptr1;
|
|
||||||
archive & xptr1;
|
archive & xptr1;
|
||||||
archive & xptr2;
|
archive & xptr2;
|
||||||
archive & yptr1;
|
archive & wptr2;
|
||||||
archive & yptr2;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::ifstream is("ptr.txt");
|
std::ifstream is("ptr.txt");
|
||||||
cereal::BinaryInputArchive archive(is);
|
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> xptr2;
|
||||||
std::shared_ptr<int> yptr1;
|
std::weak_ptr<int> wptr2;
|
||||||
std::shared_ptr<int> yptr2;
|
|
||||||
archive & xptr1;
|
archive & xptr1;
|
||||||
archive & xptr2;
|
archive & xptr2;
|
||||||
archive & yptr1;
|
archive & wptr2;
|
||||||
archive & yptr2;
|
|
||||||
|
|
||||||
assert(xptr1.get() == xptr2.get());
|
std::cout << **xptr1 << std::endl;
|
||||||
assert(yptr1.get() == yptr2.get());
|
std::cout << *xptr2 << std::endl;
|
||||||
std::cout << *xptr1 << " " << *xptr2 << std::endl;
|
std::cout << (*xptr1).get() << " == " << xptr2.get() << " ? " << ((*xptr1).get() == xptr2.get()) << std::endl;
|
||||||
std::cout << *yptr1 << " " << *yptr2 << std::endl;
|
std::cout << *(wptr2.lock()) << std::endl;
|
||||||
|
std::cout << (wptr2.lock().get() == xptr2.get()) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user