Boost logo

Boost Users :

Subject: [Boost-users] Serialize a Tree
From: ÕÅì¿ (luckyzhangwei_at_[hidden])
Date: 2009-07-08 03:14:18


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?



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net