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


目录
相关文章
|
12月前
|
人工智能 Rust 运维
这个神器让你白嫖ClaudeOpus 4.5,Gemini 3!还能接Claude Code等任意平台
加我进AI讨论学习群,公众号右下角“联系方式”文末有老金的 开源知识库地址·全免费
11546 22
|
存储 NoSQL 区块链
开源:LMDB 操作工具:lmcmd
本文介绍了 LMDB(一种高效的键值存储数据库)和基于 Python 开发的命令行工具 `lmcmd`。由于 LMDB 使用二进制文件存储,管理和调试不便,因此开发了 `lmcmd`,提供了类似 Redis 的命令行操作界面,支持数据库操作、数据导入导出和查找等功能。文章涵盖了 `lmcmd` 的安装、连接数据库和常用命令(如 `set`、`get`、`export` 等)示例。最后强调了开源工具的价值,鼓励用户反馈和改进。
876 1
|
网络安全 开发工具 git
解决 Git 访问 GitHub 时的 SSL 错误
通过上述步骤,可以有效解决 Git 访问 GitHub 时的 SSL 错误。推荐优先更新 CA 证书和正确配置 Git 使用 CA 证书,避免禁用 SSL 验证。如果问题持续,可以切换到 SSH 方式访问 GitHub,确保连接的安全性和稳定性。希望这些内容对您的学习和工作有所帮助。
8525 4
|
测试技术 调度 Android开发
Flutter系列:关于ensureInitialized()
Flutter系列:关于ensureInitialized()
709 5
|
算法 前端开发 C++
C++基础知识(八:STL标准库 deque )
deque在C++的STL(Standard Template Library)中是一个非常强大的容器,它的全称是“Double-Ended Queue”,即双端队列。deque结合了数组和链表的优点,提供了在两端进行高效插入和删除操作的能力,同时保持了随机访问的特性。
843 1
|
C# Windows 开发者
超越选择焦虑:深入解析WinForms、WPF与UWP——谁才是打造顶级.NET桌面应用的终极利器?从开发效率到视觉享受,全面解读三大框架优劣,助你精准匹配项目需求,构建完美桌面应用生态系统
【8月更文挑战第31天】.NET框架为开发者提供了多种桌面应用开发选项,包括WinForms、WPF和UWP。WinForms简单易用,适合快速开发基本应用;WPF提供强大的UI设计工具和丰富的视觉体验,支持XAML,易于实现复杂布局;UWP专为Windows 10设计,支持多设备,充分利用现代硬件特性。本文通过示例代码详细介绍这三种框架的特点,帮助读者根据项目需求做出明智选择。以下是各框架的简单示例代码,便于理解其基本用法。
1567 0
|
存储 弹性计算 人工智能
2024阿里云99计划2核2G服务器99元/年,新购续费都是99元
2024阿里云99计划2核2G服务器99元/年,新购续费都是99元
|
编译器 C语言 C++
CMake基础(9)使用Clang编译
CMake基础(9)使用Clang编译
2374 0
|
编解码 网络协议 程序员
【RTP 传输协议】实时视频传输的艺术:深入探索 RTP 协议及其在 C++ 中的实现
【RTP 传输协议】实时视频传输的艺术:深入探索 RTP 协议及其在 C++ 中的实现
2693 0