As we all know, boost has a serialization inside. But there's a fundamental difference between me and boost. Boost's serialization is to read and write an object and a stream through the overloaded operator, while I automatically generate the corresponding read and write action by recording the member variables of the class.
But my serialization library has two drawbacks, that is, the efficiency is not particularly high, because I have a common object that can convert to and from XML. Although this is not necessary, it is only my personal hobby. The second disadvantage is that serialization will produce intrusive code. Use the following methods:
First, transform the class that needs to be serialize to make it a serializable Object:
1 class TestTree : public VL_SerializableObject
2 {
3 public:
4 VInt IntegerMember;
5 VUnicodeString StringMember;
6 VL_AutoPtr<TestTree> LeftTree;
7 VL_AutoPtr<TestTree> RightTree;
8
9 TestTree()
10 {
11 VL_REGISTER_SERIALIZABLE_FIELD(TestTree,IntegerMember);
12 VL_REGISTER_SERIALIZABLE_FIELD(TestTree,StringMember);
13 VL_REGISTER_SERIALIZABLE_FIELD(TestTree,LeftTree);
14 VL_REGISTER_SERIALIZABLE_FIELD(TestTree,RightTree);
15 }
16 };
Second, construct a vl_serializer and register the class:
1 VL_Serializer Serializer;
2 VL_REGISTER_SERIALIZABLE_CLASS(&Serializer,TestTree);
完成了!现在可以尝试将一个对象通过Serializer转换成XML:
1 VL_AutoPtr<TestTree> Root=new TestTree;
2 Root->IntegerMember=10;
3 Root->StringMember=L"Root";
4
5 VL_AutoPtr<TestTree> Left=new TestTree;
6 Left->IntegerMember=20;
7 Left->StringMember=L"Left";
8
9 VL_AutoPtr<TestTree> Right=new TestTree;
10 Right->IntegerMember=30;
11 Right->StringMember=L"Right";
12
13 Root->LeftTree=Left;
14 Root->RightTree=Right;
15
16 VL_CommonObject SerializedObject=Serializer.Serialize(Root.Object());
17 VL_XMLDocument Document;
18 Document.GetRootElement()->SetName(L"SerializedObject");
19 SerializedObject.SaveToXML(Document.GetRootElement());
20 VL_FileStream Stream(FileName,VL_FileStream::vfomWrite);
21 Document.Save(&Stream,true);