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