How to use s11n?

1)what is s11n?

s11n is 3rd lib for serialize/deserialize stl .(stl:standard template library).
you can use s11n for save and load c++ class or struct object at any time.
for example:you define a class A like this

class A{
public:
    int a;
    list<int> m_list;
    do_any_thing(){use a or m_list}
};

when you want to save class A ,you can use file(read,write file),or xml(ReadXmlFile…),but all these
are complexity,and not common.
if you use s11n ,you can do like these:

A a;
a.a=1;
a.m_list.push_back(1);
a.m_list.push_back(2)....

and then :

//now you saved class A into a txt file classA.s11n like this:
s11nlite::save(a,"classA.s11n");

classA.s11n:

#SerialTree 1
s11n_node class=A
{
    a class=int
    {
        v 1
    }
    m_list class=list
    {
        s11n_node class=int
        {
            v 1
        }
        s11n_node class=int
        {
            v 2
        }
    }
}

and you also can save data as xml type,like this:

<!DOCTYPE SerialTree>
<s11n_node class="A">
    <a class="int">
        <v>1</v>
    </a>
    <m_list class="list">
        <s11n_node class="int">
            <v>1</v>
        </s11n_node>
        <s11n_node class="int">
            <v>2</v>
        </s11n_node>
    </m_list>
</s11n_node>

and then ,when you want load file into class A,you can do like this:

A *a=s11nlite::load_serializable<A>("your save filename");

now you can use class A .

2)how can i use s11n?

  1. download s11n from https://s11n.net
    libs11n.xx.tar.gz2
  2. tar jxf libs11n.xx.tar.gz2
  3. cd libs11n.xx ,./configure –prefix=/usr,make ,[sudo] make install
  4. edit your class cpp.
  5. complie:g++ test.cpp -ls11n -o test

3) complete source

test_s11n.cpp

/*************************************************************************
    > File Name: test_s11.cpp
    > Author: hanhj
    > Created Time: 2016年11月23日 星期三 13时23分59秒
 ************************************************************************/
#include<iostream>
using namespace std;
#include <s11n.net/s11n/s11nlite.hpp>
#include <s11n.net/s11n/micro_api.hpp>
#include <s11n.net/s11n/plugin/plugin.hpp>
#include <s11n.net/s11n/proxy/pod/int.hpp>
#include <s11n.net/s11n/proxy/std/list.hpp>
#include <list>
//demo how to use s11n  basicly
class TestClass{
    public:
        int a;
        int b;
    virtual ~TestClass(){}
    //serialize
    virtual bool operator()(s11nlite::node_type &dest)const{
        typedef s11nlite::node_traits NT;
    //  NT::class_name(dest,"TestClass");//not necessary,because this class have regist in follow.otherwise you should place this line,or s11nlite::node_traits::class_name("TestClass");
        NT::set(dest,"a",a);
        NT::set(dest,"b",b);
        return true;
    }
    //deserialize
    virtual bool operator()(const s11nlite::node_type &src){
        typedef s11nlite::node_traits NT;
        this->a=NT::get(src,"a",this->a);
        this->b=NT::get(src,"b",this->b);
        return true;
    }
    virtual int SaveClass(const string &filename);//must be virtual otherwise the drive class will save base class.
    virtual int LoadClass(const string &filename);//must be virtual otherwise the drive class will load base class.

};
//demo how to use s11n normaly
typedef std::list<int>List;
class Ext1:public TestClass{
    public:
        List m_list;
        int a;
    public:
        //serialize
        virtual bool operator()(s11nlite::node_type &dest)const{//normaly you clare serialize fun like this.
        s11nlite::node_traits::class_name(dest,"Ext1");//not necessary, if class have resgist follow.
        s11nlite::serialize_subnode(dest,"a",a);
        s11nlite::serialize_subnode(dest,"m_list",m_list);//s11n will recurse call serialize fun.
            return true;
        }
        //deserialize
        virtual bool operator()(const s11nlite::node_type &src){
            s11nlite::deserialize_subnode(src,"a",a);
            s11nlite::deserialize_subnode(src,"m_list",m_list);
            return true;
        }
};
//i wrap save and load fun for use
int TestClass::SaveClass(const string &filename){
    return s11nlite::save(*this,filename);
}
int TestClass::LoadClass(const string &filename){

    std::auto_ptr<s11nlite::node_type>node(s11nlite::load_node(filename));
    return s11nlite::deserialize(*node,*this);
}

//if in class serialize fun has set class name ,these is not necessary.
//when deserialize class ,if you use s11nlite::load_serializable<Ext1>("file Name"),
//you should define these.if you use auto_ptr,you can do not define it.
#define S11N_TYPE TestClass
#define S11N_TYPE_NAME "TestClass"
#include <s11n.net/s11n/reg_s11n_traits.hpp>
#define S11N_TYPE Ext1
#define S11N_TYPE_NAME "Ext1"
#include <s11n.net/s11n/reg_s11n_traits.hpp>

int main(int arc,char **argv){
    cout<<"serializer_class:"<<s11nlite::serializer_class()<<"\n";
    TestClass tc;
    TestClass *ptc;
    tc.a=1;
    tc.b=2;
    s11nlite::save(tc,"testclass.s11n");
    ptc=s11nlite::load_serializable<TestClass>("testclass.s11n");
    std::cout<<"TestClass.a:"<<ptc->a<<"\n";
    std::cout<<"TestClass.b:"<<ptc->b<<"\n";
    delete ptc;

    Ext1 ex1;
    ex1.m_list.push_back(1);
    ex1.m_list.push_back(2);
    ex1.a=1;
    s11nlite::save(ex1,"ex1.s11n");

    s11nlite::serializer_class("funxml");//now i will set current serializer as funxml 
    s11nlite::save(ex1,"ex1.xml");

    ex1.a=3;
    std::auto_ptr<s11nlite::node_type>node(s11nlite::load_node("ex1.s11n"));
    s11nlite::deserialize(*node,ex1);
    cout<<"Ext1.a:"<<ex1.a<<"\n";
    cout<<"Ext1.m_list:\n";
    List::iterator it;
    it=ex1.m_list.begin();
    while(it!=ex1.m_list.end()){
        cout<<*it<<"\n";
        it++;
    }
    Ext1 *p;
    p=s11nlite::load_serializable<Ext1>("ex1.s11n");
    cout<<p->a<<"\n";
    delete p;
    //i use wraped save and load fun 
    ex1.SaveClass("ex1_class.s11n");
    ex1.a=3;
    ex1.LoadClass("ex1_class.s11n");
    cout<<"Ext1.a:"<<ex1.a<<"\n";
    cout<<"Ext1.m_list:\n";
    it=ex1.m_list.begin();
    while(it!=ex1.m_list.end()){
        cout<<*it<<"\n";
        it++;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值