c++ - Able to serialize with boost but unable to deserialize std::shared_ptr -
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) { // raw pointer has 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 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 , open binary archive 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 archive serial_writer << *tree; // archive , stream closed when destructors 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 , open binary archive 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 archive serial_reader >> *tree; // archive , stream closed when destructors called }else if(reader.fail()){ reader.clear(); } return true; } i have written , verified successful serialization file fail deserialize , usable object.
whether writing in text or binary, can verify serialized output correct but, reason, serialize output not deserialize , left empty object when loading.
have @ these links, might provide 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
Comments
Post a Comment