• FlatBuffer入门笔记


    FlatBuffer入门笔记

    1 flatbuffer资料

    flatbuffer下载地址:https://github.com/google/flatbuffers

    flatbuffer官方使用文档:https://google.github.io/flatbuffers/index.html#flatbuffers_overview

    flatbuffer官方测试用例:https://google.github.io/flatbuffers/flatbuffers_guide_use_cpp.html

    2 编写IDL|Schema

    2.1 IDL测试文件

      引用官方测试使用的IDL

     1 // Example IDL file for our monster's schema.
     2 namespace MyGame.Sample;
     3 enum Color:byte { Red = 0, Green, Blue = 2 }
     4 union Equipment { Weapon } // Optionally add more tables.
     5 struct Vec3 {
     6   x:float;
     7   y:float;
     8   z:float;
     9 }
    10 table Monster {
    11   pos:Vec3; // Struct.
    12   mana:short = 150;
    13   hp:short = 100;
    14   name:string;
    15   friendly:bool = false (deprecated);
    16   inventory:[ubyte];  // Vector of scalars.
    17   color:Color = Blue; // Enum.
    18   weapons:[Weapon];   // Vector of tables.
    19   equipped:Equipment; // Union.
    20   path:[Vec3];        // Vector of structs.
    21 }
    22 table Weapon {
    23   name:string;
    24   damage:short;
    25 }
    26 root_type Monster;

    2.2  生成桩文件

      利用flatc.exe生成IDL对应的桩代码,命令格式为

    1 flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] [ -S ] FILES...  [ -- FILES...]
    1 flatc --cpp monster.fbs

      flatbuffer使用模板编程,仅生成h文件。对应的文件名为filename_generated.h。这里生成monster_generated.h文件。

    2.3 IDL数据类型

    2.3.1 Table

      Table是FlatBuffer定义的主要数据类型,一个Table包含一个名称(如2.1的Monster),以及一组字段(如2.1的Monster)。每个字段由名称、类型及默认值组成。

      FlatBuffer每个字段都是可选的,这由FlatBUffer的线性实现机制决定:空闲字段仅填充默认占位值,而不占用分配size。

    2.3.2 Struct

      Struct只包含数值类型与其他struct,与Table相比,Struct使用更少的存储空间以及更快的访问速度。

    2.3.3 Type

      内建的数值类型有:

    • 8 bit: byte (int8), ubyte (uint8), bool
    • 16 bit: short (int16), ushort (uint16)
    • 32 bit: int (int32), uint (uint32), float (float32)
    • 64 bit: long (int64), ulong (uint64), double (float64)

      内建的非数值类型有:

    • vector,用[]表示
    • string
    • 指向其他Table、struct、enum、unions的引用

    2.3.4 Enums

      定义了一系列的命名常量,可以给定默认值。第一个变量的默认值为0。

     2.3.5 Namespaces

      可以定义嵌套的namespace,用.分割。

    2.3.6 Root type

      定义序列化的root table或者struct。

    3 序列化与反序列化

      对2.1例子的序列化和反序列化过程实现在sample_binary.cpp。

    3.1 序列化

     1  // Build up a serialized buffer algorithmically:
     2   flatbuffers::FlatBufferBuilder builder;                               //申请一个flatbuffer
     3 
     4   // First, lets serialize some weapons for the Monster: A 'sword' and an 'axe'.
     5   auto weapon_one_name = builder.CreateString("Sword");                 //在buffer中申请string
     6   short weapon_one_damage = 3;
     7 
     8   auto weapon_two_name = builder.CreateString("Axe");                   //在buffer中申请string
     9   short weapon_two_damage = 5;
    10 
    11   // Use the `CreateWeapon` shortcut to create Weapons with all fields set.
    12   auto sword = CreateWeapon(builder, weapon_one_name, weapon_one_damage); //桩代码中实现
    13   auto axe = CreateWeapon(builder, weapon_two_name, weapon_two_damage);   //
    14 
    15   // Create a FlatBuffer's `vector` from the `std::vector`.
    16   std::vector<flatbuffers::Offset<Weapon>> weapons_vector;
    17   weapons_vector.push_back(sword);
    18   weapons_vector.push_back(axe);
    19   auto weapons = builder.CreateVector(weapons_vector);                    //在buffer中序列化vector
    20 
    21   // Second, serialize the rest of the objects needed by the Monster.
    22   auto position = Vec3(1.0f, 2.0f, 3.0f);                               
    23 
    24   auto name = builder.CreateString("MyMonster");
    25 
    26   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    27   auto inventory = builder.CreateVector(inv_data, 10);
    28 
    29   // Shortcut for creating monster with all fields set:
    30   auto orc = CreateMonster(builder, &position, 150, 80, name, inventory,
    31                            Color_Red, weapons, Equipment_Weapon, axe.Union());
    32 
    33   builder.Finish(orc);  // Serialize the root of the object.              //序列化对象root
    34 
    35   // We now have a FlatBuffer we can store on disk or send over a network.
    36 
    37   // ** file/network code goes here :) **

    3.2 反序列化

     1 // access builder.GetBufferPointer() for builder.GetSize() bytes
     2 
     3   // Instead, we're going to access it right away (as if we just received it).
     4 
     5   // Get access to the root:
     6   auto monster = GetMonster(builder.GetBufferPointer());                  //获取根对象指针
     7 
     8   // Get and test some scalar types from the FlatBuffer.
     9   assert(monster->hp() == 80);
    10   assert(monster->mana() == 150);  // default
    11   assert(monster->name()->str() == "MyMonster");
    12 
    13   // Get and test a field of the FlatBuffer's `struct`.
    14   auto pos = monster->pos();
    15   assert(pos);
    16   assert(pos->z() == 3.0f);
    17   (void)pos;
    18 
    19   // Get a test an element from the `inventory` FlatBuffer's `vector`.
    20   auto inv = monster->inventory();
    21   assert(inv);
    22   assert(inv->Get(9) == 9);
    23   (void)inv;
    24 
    25   // Get and test the `weapons` FlatBuffers's `vector`.
    26   std::string expected_weapon_names[] = { "Sword", "Axe" };
    27   short expected_weapon_damages[] = { 3, 5 };
    28   auto weps = monster->weapons();
    29   for (unsigned int i = 0; i < weps->size(); i++) {
    30     assert(weps->Get(i)->name()->str() == expected_weapon_names[i]);
    31     assert(weps->Get(i)->damage() == expected_weapon_damages[i]);
    32   }
    33   (void)expected_weapon_names;
    34   (void)expected_weapon_damages;
    35 
    36   // Get and test the `Equipment` union (`equipped` field).
    37   assert(monster->equipped_type() == Equipment_Weapon);
    38   auto equipped = static_cast<const Weapon *>(monster->equipped());
    39   assert(equipped->name()->str() == "Axe");
    40   assert(equipped->damage() == 5);
    41   (void)equipped;

    4 FlatBuffer原理?网上找的一些文档

      https://www.jianshu.com/p/fa999434776a

  • 相关阅读:
    题目:心灵的抚慰
    间谍网络
    tarjan算法模版
    rect
    学校食堂
    题目:自然的雪糕
    Winform里众多上下文菜单的控制要怎么做?
    ubuntu更新列表
    瞧,Silverlight是如何被部署到客户机的
    盖茨十条成功箴言 值得一生铭记
  • 原文地址:https://www.cnblogs.com/hgwang/p/9964151.html
Copyright © 2020-2023  润新知