我可能会推荐现成的指针容器。有
指针容器
这是直截了当的:
Live On Coliru
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <fstream>
#include <boost/ptr_container/ptr_unordered_map.hpp>
#include <boost/ptr_container/serialize_ptr_unordered_map.hpp>
struct Base {
virtual ~Base() noexcept = default;
virtual int get_x() const = 0;
friend auto& operator<<(std::ostream& os, Base const& b) {
return os << typeid(b).name() << "[" << b.get_x() << "]";
}
};
struct A : Base { int get_x() const override { return 0; } };
struct B : Base { int get_x() const override { return 1; } };
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Base)
BOOST_CLASS_EXPORT(A)
BOOST_CLASS_EXPORT(B)
namespace boost { namespace serialization {
template <class Ar> void serialize(Ar& ar, Base&, unsigned) { }
template <class Ar> void serialize(Ar& ar, A& a, unsigned) { ar& base_object<Base>(a); }
template <class Ar> void serialize(Ar& ar, B& b, unsigned) { ar& base_object<Base>(b); }
}} // namespace boost::serialization
template <typename Map>
inline void save_map(std::string const& filename, Map const& map) {
std::ofstream ofs(filename, std::ios::binary);
boost::archive::binary_oarchive archive(ofs, boost::archive::no_codecvt);
archive << map;
}
template <typename Map>
inline void load_map(std::string const& filename, Map& map) {
std::ifstream ifs(filename, std::ios::binary);
boost::archive::binary_iarchive archive(ifs, boost::archive::no_codecvt);
archive >> map;
}
#include <iostream>
int main()
{
using Dict = boost::ptr_unordered_map<int, Base>;
{
Dict dict;
dict.insert(100, std::make_unique<A>());
dict.insert(200, std::make_unique<B>());
dict.insert(300, std::make_unique<B>());
dict.insert(400, std::make_unique<A>());
save_map("test.bin", dict);
}
{
Dict roundtrip;
load_map("test.bin", roundtrip);
for (auto const& [k,v]: roundtrip) {
std::cout << k << ": " << *v << "\n";
}
}
}
打印例如
100: 1A[0]
200: 1B[1]
300: 1B[1]
400: 1A[0]
而且没有内存泄漏。
自己动手
这有一些好处(更现代的界面有时在其他点上更“指针”界面):
Live On Coliru
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/boost_unordered_map.hpp>
#include <boost/serialization/unique_ptr.hpp>
#include <boost/serialization/export.hpp>
#include <fstream>
struct Base {
virtual ~Base() noexcept = default;
virtual int get_x() const = 0;
friend auto& operator<<(std::ostream& os, Base const& b) {
return os << typeid(b).name() << "[" << b.get_x() << "]";
}
};
struct A : Base { int get_x() const override { return 0; } };
struct B : Base { int get_x() const override { return 1; } };
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Base)
BOOST_CLASS_EXPORT(A)
BOOST_CLASS_EXPORT(B)
namespace boost { namespace serialization {
template <class Ar> void serialize(Ar& ar, Base&, unsigned) { }
template <class Ar> void serialize(Ar& ar, A& a, unsigned) { ar& base_object<Base>(a); }
template <class Ar> void serialize(Ar& ar, B& b, unsigned) { ar& base_object<Base>(b); }
}} // namespace boost::serialization
template <typename Map>
inline void save_map(std::string const& filename, Map const& map) {
std::ofstream ofs(filename, std::ios::binary);
boost::archive::binary_oarchive archive(ofs, boost::archive::no_codecvt);
archive << map;
}
template <typename Map>
inline void load_map(std::string const& filename, Map& map) {
std::ifstream ifs(filename, std::ios::binary);
boost::archive::binary_iarchive archive(ifs, boost::archive::no_codecvt);
archive >> map;
}
#include <iostream>
int main()
{
using Dict = boost::unordered_map<int, std::unique_ptr<Base> >;
{
Dict dict;
dict.emplace(100, std::make_unique<A>());
dict.emplace(200, std::make_unique<B>());
dict.emplace(300, std::make_unique<B>());
dict.emplace(400, std::make_unique<A>());
save_map("test.bin", dict);
}
{
Dict roundtrip;
load_map("test.bin", roundtrip);
for (auto const& [k,v]: roundtrip) {
std::cout << k << ": " << *v << "\n";
}
}
}
打印,同样没有内存泄漏,类似于:
100: 1A[0]
200: 1B[1]
300: 1B[1]
400: 1A[0]