I do a C + + serialization library (including part of the source code)

Source: Internet
Author: User
Tags serialization

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.