mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
weak_ptr seems to work for poly types
This commit is contained in:
@@ -31,7 +31,6 @@
|
||||
#include <cereal/types/memory.hpp>
|
||||
#include <cereal/details/polymorphic_impl.hpp>
|
||||
#include <cereal/details/util.hpp>
|
||||
#include <iostream>
|
||||
//! Registers a polymorphic type with cereal
|
||||
/*! Polymorphic types must be registered before pointers
|
||||
to them can be serialized. This also assumes that
|
||||
@@ -99,9 +98,7 @@ namespace cereal
|
||||
ar.registerPolymorphicName(nameid, name);
|
||||
}
|
||||
else
|
||||
{
|
||||
name = ar.getPolymorphicName(nameid);
|
||||
}
|
||||
|
||||
auto & bindingMap = detail::StaticObject<detail::InputBindingMap<Archive>>::getInstance().map;
|
||||
|
||||
@@ -152,8 +149,7 @@ namespace cereal
|
||||
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
|
||||
save( Archive & ar, std::weak_ptr<T> const & ptr )
|
||||
{
|
||||
std::cout << "poly weak" << std::endl;
|
||||
auto sptr = ptr.lock();
|
||||
auto const sptr = ptr.lock();
|
||||
ar( sptr );
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,8 @@ struct Base
|
||||
|
||||
struct MyType : public Base
|
||||
{
|
||||
int x;
|
||||
|
||||
void foo() {}
|
||||
|
||||
template<class Archive>
|
||||
@@ -69,18 +71,24 @@ CEREAL_REGISTER_TYPE(MyType);
|
||||
|
||||
struct YourType : public Base
|
||||
{
|
||||
YourType(int xx) : x(xx) {}
|
||||
YourType() : x(-1) {}
|
||||
int x;
|
||||
|
||||
void foo() {}
|
||||
|
||||
template<class Archive>
|
||||
void save(Archive & ar) const
|
||||
{
|
||||
std::cout << "Saving YourType" << std::endl;
|
||||
ar( x );
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void load(Archive & ar)
|
||||
{
|
||||
std::cout << "Loading YourType" << std::endl;
|
||||
ar( x );
|
||||
}
|
||||
};
|
||||
CEREAL_REGISTER_TYPE(YourType);
|
||||
@@ -94,7 +102,7 @@ int main()
|
||||
cereal::BinaryOutputArchive oarchive(ostream);
|
||||
|
||||
std::shared_ptr<Base> ptr1 = std::make_shared<MyType>();
|
||||
std::shared_ptr<Base> ptr2 = std::make_shared<YourType>();
|
||||
std::shared_ptr<Base> ptr2 = std::make_shared<YourType>(33);
|
||||
std::unique_ptr<Base> ptr3(new MyType);
|
||||
std::weak_ptr<Base> ptr4 = ptr2;
|
||||
|
||||
@@ -104,8 +112,6 @@ int main()
|
||||
oarchive(ptr4);
|
||||
}
|
||||
|
||||
|
||||
|
||||
{
|
||||
std::ifstream istream("rtti.txt");
|
||||
cereal::BinaryInputArchive iarchive(istream);
|
||||
@@ -121,4 +127,5 @@ int main()
|
||||
iarchive(ptr4);
|
||||
}
|
||||
|
||||
//std::remove("rtti.txt");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user