Skip to content

Instantly share code, notes, and snippets.

@nekko1119
Created November 24, 2013 07:40
Show Gist options
  • Save nekko1119/7624501 to your computer and use it in GitHub Desktop.
Save nekko1119/7624501 to your computer and use it in GitHub Desktop.
std::shared_ptrをシリアライズしたり、intラッパーしたり、殴り書き。
#include <iostream>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/shared_ptr_helper.hpp>
#include <memory>
struct Integer
{
int held = 0;
operator int() const { return held; }
Integer(int value) : held(value) {}
Integer() = default;
};
using MyType = Integer;
using SharedPtr = boost::shared_ptr<MyType>;
class Hoge
{
public:
Hoge(int d, double& r, SharedPtr s) : data_(d), ref_(r), sp_(s) {}
int data() const { return data_; }
double const& ref() const { return ref_; }
SharedPtr const& ptr() const { return sp_; }
private:
friend boost::serialization::access;
template <typename Archive>
void serialize(Archive&, unsigned int) {}
int data_;
double& ref_;
SharedPtr sp_;
};
namespace boost
{
namespace serialization
{
template <typename Archive>
void serialize(Archive& a, Integer& i, unsigned int)
{
a & boost::serialization::make_nvp("integer", i.held);
}
template<class Archive, class T>
inline void serialize(
Archive & ar,
std::shared_ptr< T > &t,
const unsigned int file_version
)
{
// correct shared_ptr serialization depends upon object tracking
// being used.
BOOST_STATIC_ASSERT(
boost::serialization::tracking_level< T >::value
!= boost::serialization::track_never
);
boost::serialization::split_free(ar, t, file_version);
}
template<class Archive, class T>
inline void save(
Archive & ar,
const std::shared_ptr< T > &t,
const unsigned int /* file_version */
)
{
// The most common cause of trapping here would be serializing
// something like shared_ptr<int>. This occurs because int
// is never tracked by default. Wrap int in a trackable type
BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
const T * t_ptr = t.get();
ar << boost::serialization::make_nvp("px", t_ptr);
}
template<class Archive, class T>
inline void load(
Archive & ar,
std::shared_ptr< T > &t,
const unsigned int /*file_version*/
)
{
// The most common cause of trapping here would be serializing
// something like shared_ptr<int>. This occurs because int
// is never tracked by default. Wrap int in a trackable type
BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
T* r;
ar >> boost::serialization::make_nvp("px", r);
t.reset(r);
}
template <typename Archive>
void save_construct_data(Archive& ar, Hoge const* p, unsigned int)
{
std::cout << "save_construct_data\n";
auto const& d = p->data();// ar << xxxや、make_nvpは左辺値参照を引数に取るので、右辺値を左辺値に変換する必要が有る
double r = p->ref();
ar << boost::serialization::make_nvp("data", d);
ar << boost::serialization::make_nvp("ref", r);
ar << boost::serialization::make_nvp("ptr", p->ptr());
}
template <typename Archive>
void load_construct_data(Archive& ar, Hoge* p, unsigned int)
{
std::cout << "load_construct_data\n";
int d;
static double r;
SharedPtr sp;
ar >> boost::serialization::make_nvp("data", d);
ar >> boost::serialization::make_nvp("ref", r);
ar >> boost::serialization::make_nvp("ptr", sp);
::new(p) Hoge(d, r, sp);
}
}
}
#include <fstream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
int main()
{
{
double r = 3.14;
SharedPtr sp{new MyType(1119)};
Hoge h{42, r, sp};
auto* p = &h;
std::ofstream ofs{"hoge.xml"};
std::cout << "xml_orchive\n";
boost::archive::xml_oarchive oa{ofs};
std::cout << "make_nvp\n";
oa << boost::serialization::make_nvp("Hoge", p);
std::cout << "-----------------------\n";
}
{
Hoge* p = nullptr;
std::ifstream ifs{"hoge.xml"};
std::cout << "xml_iarchive\n";
boost::archive::xml_iarchive ia{ifs};
std::cout << "make_nvp\n";
ia >> boost::serialization::make_nvp("Hoge", p);
std::cout << p->data() << ", " << p->ref() << ", " << *p->ptr() << std::endl;
delete p;
std::cout << "-----------------------\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment