游戏开发程序员

TinySerializer框架的用法

2017-04-26  本文已影响30人  davidpp

0. 简介

实现了一个可扩展的微型C++序列化框架tiny-serializer

项目地址:https://github.com/david-pp/tiny-serializer

依赖:

1. 用法(静态的ProtoSerializer

注意:本节演示代码位于该项目的:exmaple/demo_serialize.cpp

1.1 序列化基本类型

支持下面的类型:

注意:同种类型的可以随意调整,不会影响序列化。如:uint32_t序列化的数据,使用uint8_t/uint64_t也可以进行正常的反序列化。

例子:

1.2 序列化Protobuf生成的结构

        PlayerProto p1;
        p1.set_id(1024);
        p1.set_name("david");
        p1.add_quests(1);
        p1.add_quests(2);
        p1.mutable_weapon()->set_type(2);
        p1.mutable_weapon()->set_name("Sword");


        std::string data = serialize(p1);

        PlayerProto p2;
        if (deserialize(p2, data))
            std::cout << p2.ShortDebugString() << std::endl;
        else
            std::cout << "error happens !" << std::endl;

1.3 序列化STL容器

支持的容器如下:

注意:

例子:

1.4 序列化用户自定义类型

对用户自定义类型序列化的支持,分两种方式:

例子

2. 反射式用法(动态的ProtoDynSerializer

对于基本类型、STL、Protobuf生成的类型,ProtoDynSerializerProtoSerializer的处理是一样的,区别在于用户定义类型的用法。

注意:

2.1 基本类型、STL、Protobuf生成类型的序列化

        PlayerProto p1;
        p1.set_id(1024);
        p1.set_name("david");
        p1.add_quests(1);
        p1.add_quests(2);
        p1.mutable_weapon()->set_type(2);
        p1.mutable_weapon()->set_name("Sword");
    
    
        std::string data = serialize<ProtoDynSerializer>(p1);
    
        PlayerProto p2;
        if (deserialize<ProtoDynSerializer>(p2, data))
            std::cout << p2.ShortDebugString() << std::endl;
        else
            std::cout << "error happens !" << std::endl;
    ```


### 2.2 用户自定义类型的序列化

- 用户自定义类型:

    ```c++
    struct Weapon {
        uint32_t type = 0;
        std::string name = "";
    };
    
    struct Player {
        uint32_t id = 0;
        std::string name = "";
        std::vector<uint32_t> quests;
        Weapon weapon;
        std::map<uint32_t, Weapon> weapons;
        std::map<uint32_t, std::vector<Weapon>> weapons_map;
    };
    ```
- 用户自定义类型到`Proto`的映射

    ```c++
    // Mapping
    RUN_ONCE(Mapping) {
    
        ProtoMappingFactory::instance().declare<Weapon>("Weapon")
                .property<ProtoDynSerializer>("type", &Weapon::type, 1)
                .property<ProtoDynSerializer>("name", &Weapon::name, 2);
    
        ProtoMappingFactory::instance().declare<Player>("Player", "PlayerDynProto")
                .property<ProtoDynSerializer>("id", &Player::id, 1)
                .property<ProtoDynSerializer>("name", &Player::name, 2)
                .property<ProtoDynSerializer>("quests", &Player::quests, 3)
                .property<ProtoDynSerializer>("weapon", &Player::weapon, 4)
                .property<ProtoDynSerializer>("weapon2", &Player::weapons, 5)
                .property<ProtoDynSerializer>("weapons_map", &Player::weapons_map, 6);
    
        // MUST!!! CREATE DESCRIPTORS
        ProtoMappingFactory::instance().createAllProtoDescriptor();
    }
    ```
    
    执行`ProtoMappingFactory::instance().createAllProtoDefine()`,可以得到它们映射的Proto定义:
    
    ```proto
    // Weapon -> WeaponDynProto
    message WeaponDynProto {
      optional bytes type = 1;
      optional bytes name = 2;
    }
    
    // Player -> PlayerDynProto
    message PlayerDynProto {
      optional bytes id = 1;
      optional bytes name = 2;
      optional bytes quests = 3;
      optional bytes weapon = 4;
      optional bytes weapon2 = 5;
      optional bytes weapons_map = 6;
    }
    ```

- 序列化/反序列化

    ```c++
    try {
        Player p;
        p.init();
    
        std::string data = serialize<ProtoDynSerializer>(p);
    
        Player p2;
        deserialize<ProtoDynSerializer>(p2, data);
        p2.dump();
    } catch (const std::exception& e) {
        std::cout << "Error Happens: " << e.what() << std::endl;
    }
    ```

## 3.静态 vs. 动态

- 推荐使用静态玩法:
     - 静态执行效率高于动态。
     - 静态出错会在编译器暴露,而动态出错时会在运行时抛出异常。
     
- 动态的好处:用户自定义类型使用起来更加方便,不要额外定义Proto和实现序列化的约定。


- 性能简单对比:动态/静态 ~= 1.3倍

    ```bash
    $ time `./demo_serialize_dyn 10000 > /dev/null`
    
    real    0m7.522s
    user    0m7.051s
    sys     0m0.435s
    
    $ time `./demo_serialize 10000 > /dev/null`
    
    real    0m5.858s
    user    0m5.460s
    sys     0m0.373s
    ```



### 4. 进一步了解

 - [TinySerializer的设计](https://github.com/david-pp/tiny-serializer/blob/master/tiny-serializer-design.md)
上一篇下一篇

猜你喜欢

热点阅读