problem with serialization (got my example)
Here is my short [kind of?] example. If it is too long, sorry, but I felt that I had to populate my vector with somewhat substantial vector. Also, in my previous post, I said that the other types that class Builder serialized only serialized std::string and booleans, but they also serialize std::vector<std::string>. I don't think it would make any difference since they are all either primitive types or types defined by the standard, but just to clarify. boost_serialization_test.hpp: #ifndef DATA_CLASSES_H #define DATA_CLASSES_H #include <fstream> #include <string> #include <vector> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/shared_ptr.hpp> class Program { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & sources; ar & target; ar & indent; ar & buildVars; ar & buildVarsValues; } public: Program() : className("Program") { } std::string sources; std::string target; bool indent; std::vector<std::string> buildVars; std::vector<std::string> buildVarsValues; std::string className; }; class Object { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & sources; ar & target; ar & indent; ar & buildVars; ar & buildVarsValues; } public: Object() : className("Object") { } std::string className; std::string sources; std::string target; bool indent; std::vector<std::string> buildVars; std::vector<std::string> buildVarsValues; }; class Java { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & srcDir; ar & classDir; ar & indent; } public: Java() : className("Java") { } std::string className; std::string srcDir; std::string classDir; bool indent; }; class Comment { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & content; } public: std::string content; }; class Print { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & content; ar & indent; } public: Print() : className("Print") { } std::string className; std::string content; bool indent; }; class Variable { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & name; ar & content; ar & indent; } public: Variable() : className("Variable") { } std::string className; std::string name; std::string content; bool indent; }; class Library { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & sources; ar & target; ar & indent; ar & buildVars; ar & buildVarsValues; } public: Library() : className("Library") { } std::string className; std::string sources; std::string target; bool indent; std::vector<std::string> buildVars; std::vector<std::string> buildVarsValues; }; class SharedLibrary { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & sources; ar & target; ar & indent; ar & buildVars; ar & buildVarsValues; } public: SharedLibrary() : className("SharedLibrary") { } std::string className; std::string sources; std::string target; bool indent; std::vector<std::string> buildVars; std::vector<std::string> buildVarsValues; }; class Node { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & nodes; ar & file; ar & dir; ar & content; ar & indent; ar & name; } public: Node() : className("Node") { } std::string className; std::string nodes; std::string file; std::string dir; std::string content; bool indent; std::string name; }; class Import { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & import; } public: Import() : className("Import") { } std::string className; std::string import; }; class Code { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & code; ar & indent; } public: Code() : className("Code") { } std::string className; std::string code; bool indent; }; class SourceSignatures { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & indent; ar & type; } public: SourceSignatures() : className("SourceSignatures") { } std::string className; bool indent; std::string type; }; class TargetSignatures { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & indent; ar & type; } public: TargetSignatures() : className("TargetSignatures") { } std::string className; bool indent; std::string type; }; class SetOption { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & indent; ar & option; ar & value; } public: SetOption() : className("SetOption") { } std::string className; bool indent; std::string option; std::string value; }; class Ignore { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & indent; ar & target; ar & file; } public: Ignore() : className("Ignore") { } std::string className; bool indent; std::string target; std::string file; }; class Depend { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & indent; ar & target; ar & file; } public: Depend() : className("Depend") { } std::string className; bool indent; std::string target; std::string file; }; class EnvironmentVariable { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & indent; ar & name; ar & buildVars; ar & buildVarsValues; } public: EnvironmentVariable() : className("EnvironmentVariable") { } std::string className; bool indent; std::string name; std::vector<std::string> buildVars; std::vector<std::string> buildVarsValues; }; class Builder { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & type; switch(type) { case 0: ar & program; break; case 1: ar & object; break; case 2: ar & java; break; case 3: ar & comment; break; case 4: ar & print; break; case 5: ar & variable; break; case 6: ar & library; break; case 7: ar & sharedLibrary; break; case 8: ar & node; break; case 9: ar & import; break; case 10: ar & code; break; case 11: ar & sourceSignatures; break; case 12: ar & targetSignatures; break; case 13: ar & setOption; break; case 14: ar & ignore; break; case 15: ar & depend; break; case 16: ar & environmentVariable; break; } } public: boost::shared_ptr<Program> program; boost::shared_ptr<Object> object; boost::shared_ptr<Java> java; boost::shared_ptr<Comment> comment; boost::shared_ptr<Print> print; boost::shared_ptr<Variable> variable; boost::shared_ptr<Library> library; boost::shared_ptr<SharedLibrary> sharedLibrary; boost::shared_ptr<Node> node; boost::shared_ptr<Import> import; boost::shared_ptr<Code> code; boost::shared_ptr<SourceSignatures> sourceSignatures; boost::shared_ptr<TargetSignatures> targetSignatures; boost::shared_ptr<SetOption> setOption; boost::shared_ptr<Ignore> ignore; boost::shared_ptr<Depend> depend; boost::shared_ptr<EnvironmentVariable> environmentVariable; int type; }; /* BOOST_CLASS_TRACKING(Program, boost::serialization::track_never) BOOST_CLASS_TRACKING(Object, boost::serialization::track_never) BOOST_CLASS_TRACKING(Java, boost::serialization::track_never) BOOST_CLASS_TRACKING(Comment, boost::serialization::track_never) BOOST_CLASS_TRACKING(Print, boost::serialization::track_never) BOOST_CLASS_TRACKING(Variable, boost::serialization::track_never) BOOST_CLASS_TRACKING(Library, boost::serialization::track_never) BOOST_CLASS_TRACKING(SharedLibrary, boost::serialization::track_never) BOOST_CLASS_TRACKING(Node, boost::serialization::track_never) BOOST_CLASS_TRACKING(Import, boost::serialization::track_never) BOOST_CLASS_TRACKING(Code, boost::serialization::track_never) BOOST_CLASS_TRACKING(SourceSignatures, boost::serialization::track_never) BOOST_CLASS_TRACKING(TargetSignatures, boost::serialization::track_never) BOOST_CLASS_TRACKING(SetOption, boost::serialization::track_never) BOOST_CLASS_TRACKING(Ignore, boost::serialization::track_never) BOOST_CLASS_TRACKING(Depend, boost::serialization::track_never) BOOST_CLASS_TRACKING(EnvironmentVariable, boost::serialization::track_never) BOOST_CLASS_TRACKING(Builder, boost::serialization::track_never)*/ #endif boost_serialization_test.cpp: #include "boost_serialization_test.hpp" int main() { std::ofstream fout("file"); boost::archive::text_oarchive fout_ar(fout); std::vector<Builder> data; Builder *builder = new Builder[10]; for(int i = 0; i < 10; i++) { builder[i].type = i; switch(i) { case 0: builder[i].program->sources = "bshfdvhjvcds"; break; case 1: builder[i].object->sources = "bshfdvhjvcds"; break; case 2: builder[i].java->srcDir = "bshfdvhjvcds"; break; case 3: builder[i].comment->content = "bshfdvhjvcds"; break; case 4: builder[i].print->content = "bshfdvhjvcds"; break; case 5: builder[i].variable->content = "bshfdvhjvcds"; break; case 6: builder[i].library->sources = "bshfdvhjvcds"; break; case 7: builder[i].sharedLibrary->sources = "bshfdvhjvcds"; break; case 8: builder[i].node->content = "bshfdvhjvcds"; break; case 9: builder[i].import->import = "bshfdvhjvcds"; break; case 10: builder[i].code->code = "bshfdvhjvcds"; break; case 11: builder[i].sourceSignatures->type = "jsdbifbviugvdbuyvf"; break; case 12: builder[i].targetSignatures->type = "jsdbifbviugvdbuyvf"; break; case 13: builder[i].setOption->option = "jsdbifbviugvdbuyvf"; break; case 14: builder[i].ignore->target = "jsdbifbviugvdbuyvf"; break; case 15: builder[i].depend->target = "jsdbifbviugvdbuyvf"; break; case 16: builder[i].environmentVariable->name = "vhdbfubvugvsdfuvy"; break; } } for(int i = 0; i < 10; i++) { data.push_back(builder[i]); } fout_ar << data; } Thanks a lot. Benjamin Lau P/s By the way, the /**/ around the BOOST_CLASS_TRACKING were from an earlier attempt to correct the problem, but with or without the macros, the same error messages appear.
Below are the changes I made in order for this compile: a) pass a const reference to ar << b) include headers for serialized types. Robert Ramey #include <boost/serialization/vector.hpp> // include serialization for vector #include <boost/serialization/shared_ptr.hpp> // include serialization for shared_ptr void save_data(const std::vector<Builder> & data){ std::ofstream fout("file"); boost::archive::text_oarchive fout_ar(fout); fout_ar << data; } int main() { std::vector<Builder> data; Builder *builder = new Builder[10]; for(int i = 0; i < 10; i++) { builder[i].type = i; switch(i) { case 0: builder[i].program->sources = "bshfdvhjvcds"; break; case 1: builder[i].object->sources = "bshfdvhjvcds"; break; case 2: builder[i].java->srcDir = "bshfdvhjvcds"; break; case 3: builder[i].comment->content = "bshfdvhjvcds"; break; case 4: builder[i].print->content = "bshfdvhjvcds"; break; case 5: builder[i].variable->content = "bshfdvhjvcds"; break; case 6: builder[i].library->sources = "bshfdvhjvcds"; break; case 7: builder[i].sharedLibrary->sources = "bshfdvhjvcds"; break; case 8: builder[i].node->content = "bshfdvhjvcds"; break; case 9: builder[i].import->import = "bshfdvhjvcds"; break; case 10: builder[i].code->code = "bshfdvhjvcds"; break; case 11: builder[i].sourceSignatures->type = "jsdbifbviugvdbuyvf"; break; case 12: builder[i].targetSignatures->type = "jsdbifbviugvdbuyvf"; break; case 13: builder[i].setOption->option = "jsdbifbviugvdbuyvf"; break; case 14: builder[i].ignore->target = "jsdbifbviugvdbuyvf"; break; case 15: builder[i].depend->target = "jsdbifbviugvdbuyvf"; break; case 16: builder[i].environmentVariable->name = "vhdbfubvugvsdfuvy"; break; } } for(int i = 0; i < 10; i++) { data.push_back(builder[i]); } save_data(data); }
participants (2)
-
Benjamin Lau
-
Robert Ramey