flatbuffers 使用问题记录

简介: flatbuffers 使用问题记录

1.  命名空间的问题



namespace 1.0.3 版本包含文件类型前面不需要加命名空间,但是1.1.0 中包含需要在类型前加命名空间


include必须放在namespace前面


例如:

include “aa.fbs”
namespace IM.test;
foo.fbc
namespace foo;
struct Foo { f: uint; }
bar.fbc
include "foo.fbc";
namespace bar;
struct Bar { foo: Foo; }
flatc -c bar.fbc will fail with bar.fbc:3:0: error: structs_ may contain only scalar or struct fields
修改方式:
struct Bar { foo: Foo; } ->  struct Bar { foo: foo.Foo; }  或者 将 struct 修改成 table
struct UserInfo{
        user_id:uint;
                    name:string
}
error: structs_ may contain only scalar or struct fields


2. struct 和 table的区别



http://www.coder4.com/archives/4386


基本类型:


8 bit: byte ubyte bool

16 bit: short ushort

32 bit: int uint float

64 bit: long ulong double


复杂类型:


数组 (用中括号表示 [type]). 不支持嵌套数组,可以用table实现

字符串 string, 支持 UTF-8 或者 7-bit ASCII. 对于其他编码可以用数组 [byte]或者[ubyte]表示。

Struct 只支持基本类型或者嵌套Struct

Table 类似Struct,但是可以支持任何类型。


3. roottype的问题及多个table的解决方式



https://github.com/google/flatbuffers/issues/65  


Why the need for a Root


1) a commit was pushed yesterday that adds GetRootAs functions for all tables, not just the root_type.


2) generally no. this is a strongly types system, meaning you need to know the kind of buffer you're dealing with. If you want to use this in a context where you want to have multiple different root types, you have these options:

a) make your root type a table that contains a union of all possible sub-roots.

b) prefix flatbuffers with your own file header

c) use flatbuffer's built-in file indentification feature, which hasn't been ported to Java yet. I'll get to that.


3) That's a bug, the 1 should actually read: Any.Monster. I'll fix that.


多个消息一个文件中,但是root_type 只能有一个,解决方式如下:

namespace TestApp;
union Msg {TestObj, Hello}
struct KV {
    key: ulong;
    value: double;
}
table TestObj {
    id:ulong;
    name:string;
    flag:ubyte = 0;
    list:[ulong];
    kv:KV;
}
table Hello {
    id:uint;
    name:string;
}
table RootMsg{
    any:Msg;
}
root_type RootMsg;


具体样例可以参见:https://github.com/DavadDi/study_example/tree/master/flatbuffers/multi_table


4. enum不生成name的前缀


flatc -c --no-prefix -b aa.fbs



5. 其他问题


enum的默认值只能从0开始


由于table中的字段全部为可选,因此所有返回指针的地方都必须判断是否为空指针

#define STR(ptr)  (ptr!=nullptr)?ptr->c_str():""
std::string = STR(user_info->user_name());


目录
相关文章
|
fastjson 前端开发
巧用fastjson自定义序列化类实现字段的转换
项目中突然需要增加另一个字段的查找,而这个查找需要查另一张表的记录。 但现在产品很多地方都要增加该字段,如何最快的实现该功能呢。 办法如下: 通过fastjson序列化时,增加该字段的序列化类,该序列化类通过CODE查找名称,并序列化到前端。
5895 0
|
10月前
|
存储 JSON C#
【Unity 3D】C#从JSON文件中读取、解析、保存数据(附源码)
【Unity 3D】C#从JSON文件中读取、解析、保存数据(附源码)
406 0
|
存储 编译器 C语言
【C语言】 数据的存储 -- 数据类型介绍 -- 存储 -- 浮点型在内存中的存储,很详细也很重要,不明白的一定要看2
【C语言】 数据的存储 -- 数据类型介绍 -- 存储 -- 浮点型在内存中的存储,很详细也很重要,不明白的一定要看2
|
存储 C语言
【C语言】 数据的存储 -- 数据类型介绍 -- 存储 -- 浮点型在内存中的存储,很详细也很重要,不明白的一定要看3
【C语言】 数据的存储 -- 数据类型介绍 -- 存储 -- 浮点型在内存中的存储,很详细也很重要,不明白的一定要看3
【C语言】 数据的存储 -- 数据类型介绍 -- 存储 -- 浮点型在内存中的存储,很详细也很重要,不明白的一定要看3
|
存储 C++
[Eigen中文文档] 原始缓冲区接口:Map 类
本节解释了如何使用“原始”C/C++ 数组。这在各种情况下都很有用,特别是在将向量和矩阵从其他库“导入”到 Eigen 中时。
218 0
|
存储 编译器 C语言
【C语言】 数据的存储 -- 数据类型介绍 -- 存储 -- 浮点型在内存中的存储,很详细也很重要,不明白的一定要看1
【C语言】 数据的存储 -- 数据类型介绍 -- 存储 -- 浮点型在内存中的存储,很详细也很重要,不明白的一定要看1
|
easyexcel
EasyExcel低版本中数据行中包含空数据会跳过导致数据对应不上的问题解析
EasyExcel低版本中数据行中包含空数据会跳过导致数据对应不上的问题解析
578 0
|
机器学习/深度学习 JSON 网络协议
结构体 map 切片序列化|学习笔记
快速学习结构体 map 切片序列化
|
C++
C++ STL map使用的注意事项记录
map.count与map[]的区别 假如我们建立一个从int到int的映射: 那我们通常会这么写: map<int,int> mp,表示键值的映射 那访问不存在的键与访问存在的键会发生什么呢?
161 0
C++ STL map使用的注意事项记录
ADI
|
算法 JavaScript 前端开发
[记录]我的数据结构学习路径
[记录]我的数据结构学习路径
ADI
116 0