boost serialization
namespace boost {
namespace serialization {
template <class Archive, class T>
inline void save
(Archive &archive,
const std::shared_ptr<T> subtree,
const unsigned int file_version)
{
// only the raw pointer has to be saved
const T *const subtree_x = subtree.get();
archive << subtree_x;
}
template <class Archive, class T>
inline void load
(Archive &archive,
std::shared_ptr<T> subtree,
const unsigned int file_version)
{
T *p_subtree;
archive >> p_subtree;
#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
subtree.release();
subtree = std::shared_ptr< T >(p_subtree);
#else
subtree.reset(p_subtree);
#endif
}
template <class Archive, class T>
inline void serialize
(Archive &archive,
std::shared_ptr<T> subtree, // no const or else get compile-time error
const unsigned int file_version)
{
boost::serialization::split_free(archive, subtree, file_version);
}
} // namespace serialization
} // namespace boost
tree class
class Tree{
private:
class TreeNode{
public:
std::shared_ptr<TreeNode> node_factory(const T &new_key, const long &new_index)
{
return std::shared_ptr<TreeNode>(new TreeNode(new_key, new_index));
}
friend class Tree;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &archive, const unsigned int /* file_version */){
archive & key;
archive & index;
archive & left;
archive & right;
}
T key;
long index;
std::shared_ptr<TreeNode> left;
std::shared_ptr<TreeNode> right;
}; // End Tree Node Class Definition
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &archive, const unsigned int version){
archive & root;
}
};
writer
bool save(std::shared_ptr<Tree> &tree, const std::string &search_tree_file_name)
{
// create and open a binary archive for output
std::ofstream writer(search_tree_file_name, std::ofstream::out | std::ofstream::binary);
if(writer){
boost::archive::binary_oarchive serial_writer(writer);
//set_flags(0, true);
// write class instance to archive
serial_writer << *tree;
// archive and stream closed when destructors are called
}else if(writer.fail()){
writer.clear();
}
return true;
}
reader
enter code here bool load(std::shared_ptr<Tree> &tree, const std::string &search_tree_file_name)
{
// create and open a binary archive for output
std::ifstream reader(search_tree_file_name, std::ifstream::in | std::ifstream::binary);
if(reader){
boost::archive::binary_iarchive serial_reader(reader);
// read class state from archive
serial_reader >> *tree;
// archive and stream closed when destructors are called
}else if(reader.fail()){
reader.clear();
}
return true;
}
I have written to and verified the successful serialization to a file but fail to deserialize from and into a usable object.
Whether I am writing in text or binary, I can verify the serialized output is correct but, for some reason, the serialize output does not deserialize and I am left with an empty object when loading.
Have a look at these links, might provide you some clue.
http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/shared_ptr.html
& http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/shared_ptr2.html
Although #Arun provided great documentation references useful for utilizing std::shared_ptr, I instead chose to employ boost::shared_ptr with boost::serialization and it has cured my de-serialization problem.
Related
I need the ability to save/read my data structures in my project, but all the data is in the form of quite complicated and distinct structures themselves which I typically implement through other structs and vectors. I have wrapped all of them up into a single struct so that I have something like
struct master{
std::vector<apprentice_type1> a;
std::vector<apprentice_type2> b; //etc.
std::string label;
};
with other ones defined like
struct apprentice_type1{
vec location;
int point_label;
std::vector<int> relational_data;
};
struct vec{
double x,y,z;
};
So it gets pretty complicated! I was desperately hoping something nice, quick and naive like
master obj;
//write to obj....
std::ofstream ofs("data.dat", std::ios::binary);
ofs.write((char *)&obj, sizeof(obj));
would work, but at present it doesn't seem to. Before I get lost in the debugging rabbit hole is this actually possible the way I'm approaching it or do I need to rethink? If so, how?
Thanks.
If you want an alternative to Boost serialization and have access to a C++11 compiler, you can also check out cereal. It works in a near identical fashion to Boost serialize but is a header only library so there is nothing to link against.
[...] or do I need to rethink? If so, how?
You will probably need to provide a full implementation (i.e. explore the "rabbit-hole").
This is a known problem (stream serialization) and there is no single best-approach solution to it, because most implementations need to solve different needs.
You can do one of the following:
implement std::i/ostream serialization; This means you will go over your classes and implement operator>>(std::istream*, your_type&) and it's inverse operator<<(std::ostream*, your_type&).
implement serialization based on a stream library (like boost.archive).
use a JSON or XML library.
use google protocol buffers (or something else)
roll your own implementation, depending on your needs.
If you go for boost::serialization, here is a little sample:
#include <fstream>
#include <vector>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/vector.hpp>
template <typename T>
inline const boost::serialization::nvp<T> archive_value(const char* name, T& value) {
return boost::serialization::make_nvp(name, value);
}
const unsigned Version = 0;
class Point{
public:
double x,y,z;
private:
template <typename P, typename Archive>
static void serialize(P& p, Archive& ar, const unsigned int version) {
std::cout << "Point\n";
ar & archive_value("X", p.x);
ar & archive_value("Y", p.y);
ar & archive_value("Z", p.z);
}
public:
template <typename Archive>
void serialize(Archive& ar, const unsigned int version) const {
serialize(*this, ar, version);
}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version) {
serialize(*this, ar, version);
}
};
BOOST_CLASS_VERSION(Point, Version)
struct Scene{
std::vector<Point> points;
private:
template <typename S, typename Archive>
static void serialize(S& s, Archive& ar, const unsigned int version) {
std::cout << "Scene\n";
ar & archive_value("Points", s.points);
}
public:
template <typename Archive>
void serialize(Archive& ar, const unsigned int version) const {
serialize(*this, ar, version);
}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version) {
serialize(*this, ar, version);
}
};
BOOST_CLASS_VERSION(Scene, Version)
template <typename Archive>
void register_types(Archive& ar) {
ar.template register_type<Point>();
ar.template register_type<Scene>();
}
int main() {
Scene scene;
scene.points = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 } };
// Output
{
std::ofstream out("test.dat", std::ios_base::binary);
boost::archive::binary_oarchive output(out);
// First the version!
output & archive_value("Version", Version);
// Next the types!
register_types(output);
// Finally the data
output & archive_value("Scene", scene);
}
scene.points = {};
// Input
{
int version;
std::ifstream in("test.dat", std::ios_base::binary);
boost::archive::binary_iarchive input(in);
// First the version!
input & archive_value("Version", Version);
// Next the types!
register_types(input);
// Finally the data
input & archive_value("Scene", scene);
}
for(const auto& p : scene.points)
std::cout << p.x << '\n';
}
Note: The file format may evolve and serialization functions (input and/or output) may get adjustment depending on the file version.
I am trying to use boost's functionality for serializing pointers to primitives (so that I don't have to de-reference and do a deep store myself). However, I get a pile of errors when I try to do it. Here is a simple example of a class that is supposed to contain save and load methods which write and read the class content from a file. This program does not compile:
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <fstream>
class A
{
public:
boost::shared_ptr<int> sp;
int const * p;
int const& get() {return *p;}
void A::Save(char * const filename);
static A * const Load(char * const filename);
//////////////////////////////////
// Boost Serialization:
//
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar,const unsigned int file_version)
{
ar & p & v;
}
};
// save the world to a file:
void A::Save(char * const filename)
{
// create and open a character archive for output
std::ofstream ofs(filename);
// save data to archive
{
boost::archive::text_oarchive oa(ofs);
// write the pointer to file
oa << this;
}
}
// load world from file
A * const A::Load(char * const filename)
{
A * a;
// create and open an archive for input
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
// read class pointer from archive
ia >> a;
return a;
}
int main()
{
}
Note that I am not interested in a solution that dereferences the pointer; I want boost to take care of that for me (many of these classes might be pointing to the same underlying object).
In http://www.boost.org/doc/libs/1_54_0/libs/serialization/doc/index.html:
By default, data types designated primitive by Implementation Level
class serialization trait are never tracked. If it is desired to track
a shared primitive object through a pointer (e.g. a long used as a
reference count), It should be wrapped in a class/struct so that it is
an identifiable type. The alternative of changing the implementation
level of a long would affect all longs serialized in the whole program
- probably not what one would intend.
Hence:
struct Wrapped {
int value;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar,const unsigned int file_version)
{
ar & value;
}
};
boost::shared_ptr<Wrapped> sp;
Wrapped const * p;
A boost::archive_exception is confounding me. The exception details are as follows
Unhandled exception: boost::archive::archive_exception at memory location...
This occurs when attempting the following boost::serialization load operation that essentially works when BinarySearchTree<std::string> but not when BinarySearchTree<int>
typedef boost::variant<BinarySearchTree<std::string>, BinarySearchTree<int>> BinarySearchTreeVariant;
BinarySearchTreeVariant search_tree_;
BinarySearchTree<std::string> string_search_tree_;
BinarySearchTree<int> int_search_tree_;
boost::shared_ptr<BinarySearchTree<std::string>> binary_string_search_tree_;
boost::shared_ptr<BinarySearchTree<int>> binary_int_search_tree_;
search_tree_ = int_search_tree_;
binary_int_search_tree_.reset(new BinarySearchTree<int>());
try{
if(!binary_int_search_tree_->load(binary_int_search_tree_, search_tree_name))
throw CustomException("Load of binary search tree to disk fail");
}
catch(CustomException &custom_exception){ }
The code that defines load is as follows
bool load(boost::shared_ptr<BinarySearchTree> &tree, const std::string &search_tree_file_name)
{
// create and open an archive for output
std::ifstream reader(search_tree_file_name);
if(reader){
boost::archive::text_iarchive serial_reader(reader);
// read class state from archive
serial_reader >> *tree; <<<--- Unhandled Exception
// archive and stream closed when destructors are called
}else if(reader.fail()){
reader.clear();
}
return true;
}
My search tree is defined with boost::serialization as follows
class BinarySearchTree{
private:
class BinarySearchTreeNode{
public:
friend class BinarySearchTree;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &archive, const unsigned int /* file_version */){
archive & key;
archive & index;
archive & left;
archive & right;
}
T key;
long index;
boost::shared_ptr<BinarySearchTreeNode> left;
boost::shared_ptr<BinarySearchTreeNode> right;
}; // End Tree Node Class Definition
boost::shared_ptr<BinarySearchTreeNode> root;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &archive, const unsigned int version){
archive & root;
}
};
Is this question more appropriate for Boost forums? The complete code is referenced below and I do not consider that I have incorrectly attempted to convert an auto_ptr serialization example into a unique_ptr example as compared to other unique_ptr examples on this site. Thus, why would I receive a compilation error deep within a Boost library? I have used Boost serialization on standard containers before with no problems and, although this is a custom adapter, if similar examples have compiled, why not this?
I referenced http://www.boost.org/doc/libs/1_51_0/libs/serialization/example/demo_auto_ptr.cpp in an attempt to serialize my binary tree using a custom adaptor. Below is a code dump representing an sscce.
// disables conversion from 'std::streamsize' to 'size_t', possible loss of data
#pragma warning(disable:4244)
#ifndef BINARY_SEARCH_TREE_H_
#define BINARY_SEARCH_TREE_H_
#include<functional>
#include<memory>
#include<fstream>
#include<boost/archive/binary_oarchive.hpp>
#include<boost/archive/binary_iarchive.hpp>
#define BST_FILE_NAME "tree.dat" // default filename used to save and load
namespace boost {
namespace serialization {
template <class Archive, class T>
inline void save
(Archive &archive,
const std::unique_ptr<T> &subtree,
const unsigned int file_version)
{
// only the raw pointer has to be saved
const T *const subtree_x = subtree.get();
archive << subtree_x;
}
template <class Archive, class T>
inline void load
(Archive &archive,
const std::unique_ptr<T> &subtree,
const unsigned int file_version)
{
T *p_subtree;
archive >> p_subtree;
#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
subtree.release();
subtree = std::unique_ptr< T >(p_subtree);
#else
subtree.reset(p_subtree);
#endif
}
template <class Archive, class T>
inline void serialize
(Archive &archive,
const std::unique_ptr<T> &subtree,
const unsigned int file_version)
{
boost::serialization::split_free(archive, subtree, file_version);
}
} // namespace serialization
} // namespace boost
template <class T = int>
class BinarySearchTree{
class BinarySearchTreeNode{
public:
std::unique_ptr<BinarySearchTreeNode> node_factory(const T &new_key, const T &new_index){
return std::unique_ptr<BinarySearchTreeNode>(new BinarySearchTreeNode(new_key, new_index)); }
BinarySearchTreeNode(BinarySearchTreeNode &&other) : key(other.key), index(other.index), left(std::move(other.left)),
right(std::move(other.right)) {key = index = left = right = nullptr; }
BinarySearchTreeNode &operator=(BinarySearchTreeNode &&rhs) { if(this != rhs) { key = rhs.key; index = rhs.index;
left = std::move(rhs.left); right = std::move(rhs.right);
rhs.key = rhs.index = rhs.left = rhs.right = nullptr;} return *this;}
~BinarySearchTreeNode() {} // Note to self; don't hide the destructor
friend class BinarySearchTree;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &archive, const unsigned int /* file_version */){
archive & key;
archive & index;
archive & left;
archive & right;
}
T key;
long index;
std::unique_ptr<BinarySearchTreeNode> left;
std::unique_ptr<BinarySearchTreeNode> right;
BinarySearchTreeNode() {}
BinarySearchTreeNode(const T &new_key, const T &new_index) :key(new_key), index(new_index),
left(nullptr), right(nullptr) {}
};
std::unique_ptr<BinarySearchTreeNode> root;
std::list<T> tree_keys;
std::list<long> tree_indicies;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &archive, const unsigned int version){
archive & root;
}
BinarySearchTree(const BinarySearchTree &other){}
BinarySearchTree &operator=(const BinarySearchTree &rhs){}
std::unique_ptr<BinarySearchTreeNode> insert(const T &new_key, const T &new_index,
std::unique_ptr<BinarySearchTreeNode> &tree){
if(tree == nullptr){
return root->node_factory(new_key, new_index);
}else if(std::less<T> () (new_key, tree->key)){ // Left insertion
tree->left = insert(new_key, new_index, tree->left);
return std::move(tree);
}else { // Right insertion
tree->right = insert(new_key, new_index, tree->right);
return std::move(tree);
}
}
public:
BinarySearchTree() : root(nullptr) {}
BinarySearchTree(BinarySearchTree &&other) : root(std::move(other.root)) { other.root = nullptr; }
BinarySearchTree &operator=(BinarySearchTree &&rhs) { if(this != rhs) { root = std::move(rhs.root);
rhs.root = nullptr} return *this; }
bool insert_into_tree(const T &new_key, const T &new_index){
if(new_key == NULL){
return false;
}
root = std::move(insert(new_key, new_index, root));
return true;
}
void save(const BinarySearchTree &tree)
{
// create and open a binary archive for output
std::ofstream writer(BST_FILE_NAME, std::ofstream::out | std::ofstream::binary);
if(writer){
boost::archive::binary_oarchive serial_writer(writer);
//set_flags(0, true);
// write class instance to archive
serial_writer << tree;
// archive and stream closed when destructors are called
}else if(writer.fail()){
writer.clear();
}
}
void load(BinarySearchTree &tree)
{
// create and open a binary archive for output
std::ifstream reader(BST_FILE_NAME, std::ifstream::in | std::ifstream::binary);
if(reader){
boost::archive::binary_iarchive serial_reader(reader);
// read class state from archive
serial_reader >> tree;
// archive and stream closed when destructors are called
}else if(reader.fail()){
reader.clear();
}
}
~BinarySearchTree() {}
};
#endif
The code above compiles as does any usage of the tree member functions. Once choosing to call save, that is when the compiler error appears. Here is main:
#include<cstdlib>
#include "physical_view.h"
using namespace std;
int main(int argc, char *argv[]){
BinarySearchTree<> tree;
tree.insert_into_tree(10, 5);
tree.insert_into_tree(5, 15);
tree.insert_into_tree(15, 10); <--------- All is wonderful to here!
tree.save(tree); <---------- Compiler unhappy here!
return EXIT_SUCCESS;
}
How about that compiler error:
Error 1 error C2039: 'serialize' : is not a member of 'std::unique_ptr<_Ty>' c:\boost_12\include\boost-1_53_1\boost\serialization\access.hpp 118
I thank you for helping me resolve this compiler error.
The is that the signature of your serialize and load functions is incorrect. The std::unique_ptr<T> argument needs to be non-const for both of them. Since it can't deduce the types (due to the const), it simply ignores the overload and fails to find it at all.
I have the following problem when I try to call method in "TestSerialize" class during serialization process.
Here is my code:
class TestSerialize
{
public:
std::string GetVal() { return Val + "abc"; }
void SetVal(std::string tVal) { Val = tVal.substr(0, 2); }
protected:
std::string Val;
friend class boost::serialization::access;
template<class Archive> void save(Archive & ar, const unsigned int version) const
{
using boost::serialization::make_nvp;
std::string tVal = GetVal(); // Error here
ar & make_nvp("SC", tVal);
}
template<class Archive> void load(Archive & ar, const unsigned int version)
{
using boost::serialization::make_nvp;
std::string tVal;
ar & make_nvp("SC", tVal);
SetVal(tVal);
}
BOOST_SERIALIZATION_SPLIT_MEMBER();
};
int main()
{
TestSerialize tS;
std::ofstream ofs("test.xml");
boost::archive::xml_oarchive oa(ofs, boost::archive::no_header);
oa << BOOST_SERIALIZATION_NVP(tS);
ofs.close();
return 0;
}
The error that I encountered is:
'TestSerialize::GetVal' : cannot convert 'this' pointer from 'const TestSerialize' to 'TestSerialize &'
This error only happens on "save" but not "load"
I wonder why I get this error. I would like to know what Boost.Serialization do such that we have these two different behaviors.
I use Boost Library 1.47.0
save is a const function and can only call other const functions. GetVal isn't. Change it:
std::string GetVal() const { ... }