|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Serialize a Treei want to serialize a self-defined tree to a binary file and vice-versa. 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(); 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? _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
|
|
Re: Serialize a TreeTo 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@...> wrote in message news:c7b1c0080907080014u7f117ec7s8b0b874d115c235@...... i want to serialize a self-defined tree to a binary file and vice-versa. The data structure is as follows: _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
| Free embeddable forum powered by Nabble | Forum Help |