
i want to serialize a self-defined tree to a binary file and vice-versa. The data structure is as follows: class Node { public: Node(); Node(int type,const string &name); ~Node(); string getNodeName(); int getNodeType(); Node *getNodeParent(); bool setNodeName(string &name); bool setNodeType(int type); bool setNodeParent(Node *parent); ............................................................. private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & type; ar & name; } int type; string name; Node *parent; public: vector<Node *> children; // children list of current node vector<AttribNode *> attribList; // attribute list of current node }; class AttribNode { public: AttribNode(); AttribNode(string attribName,int attribType,const char* value); string getAttribName(); int getAttribType(); private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & name; ar & type; ar & asciiValue; ar & binSize; } string name; int type; string asciiValue; long binSize; char *binValue; }; the difficult is how to serialize the relation between different Nodes.and the binValue. while in the memory, there has a tree.the save function is as follows: bool ConfigRegistry::saveConfigTreeToLocal() { ofstream ofs("D:\\Config.bin",ios::binary); boost::archive::binary_oarchive oa(ofs); queue<Node *> nodeQueue; nodeQueue.push((Node *)root); while(!nodeQueue.empty()) { Node * temp = nodeQueue.front(); for (int i = 0;i < temp->children.size();i++) { nodeQueue.push(temp->children.at(i)); } oa << *temp; int attribNum = temp->attribList.size(); oa << attribNum; for(int i =0;i < temp->attribList.size();i++) { oa << *(temp->attribList.at(i)); if (temp->attribList.at(i)->getAttribType() == ATTRIBUTE_TYPE_BINARY) { char * ptr = temp->attribList.at(i)->binValue; long size = temp->attribList.at(i)->binSize; oa << ptr[size]; } } int childrenNum = temp->children.size(); oa << childrenNum; nodeQueue.pop(); } return true; } bool ConfigRegistry::getConfigTreeFromLocal() { ofstream ifs("D:\\Config.bin",ios::binary); boost::archive::binary_oarchive ia(ifs); int childNum = 0; int attribNum = 0; queue<Node *> nodeQueue; root = (void *)new Node(); Node *curNode = (Node *)root; nodeQueue.push(curNode); while(!nodeQueue.empty()) { ia >> *(curNode); ia >> attribNum; while(attribNum--) { AttribNode * attribNode = new AttribNode(); ia >> *attribNode; if (attribNode->getAttribType() == ATTRIBUTE_TYPE_BINARY) { attribNode->binValue = new char[attribNode->binSize]; ia >> attribNode->binValue; } else { attribNode->binValue = NULL; attribNode->binSize = 0; } curNode->attribList.push_back(attribNode); } ia >> childNum; while (childNum--) { Node * newNode = new Node(); curNode->addChild(newNode); nodeQueue.push(newNode); } nodeQueue.pop(); curNode = nodeQueue.front(); } return true; } the saveConfigTreeToLocal() is okay. but the getConfigTreeFromLocal() is error.y? how can i serialize the self-defined tree above?

To serialize the tree use template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & type; ar & name; ar & parent; } I'm not sure what binValue is supposed to be but the following might work better here template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & name; ar & type; ar & attribute_list; } string name; int type; string attribute_list; .. "??" <luckyzhangwei@gmail.com> wrote in message news:c7b1c0080907080014u7f117ec7s8b0b874d115c235@mail.gmail.com... i want to serialize a self-defined tree to a binary file and vice-versa. The data structure is as follows:
participants (2)
-
Robert Ramey
-
张炜