将 native_type 与平面缓冲区一起使用时,如何避免循环 header 依赖?

How can I avoid circular header dependencies when using native_type with flatbuffers?

文档说明如下:

The native_type attribute will replace the usage of the generated class with the given type. So, continuing with the example, the generated code would use vector2 in place of Vec2T for all generated code of the Object-Based API. However, because the native_type is unknown to flatbuffers, the user must provide the following functions to aide in the serialization process:

namespace flatbuffers {
   Vec2 Pack(const vector2& obj);
   vector2 UnPack(const Vec2& obj);
}

然后:

native_include("path") (at file level): Because the native_type attribute can be used to introduce types that are unknown to flatbuffers, it may be necessary to include "external" header files in the generated code. This attribute can be used to directly add an include directive to the top of the generated code that includes the specified path directly.

因此,如果我理解任何内容,我应该“native_include”声明 vector2 的 header,以便生成的 header 文件包含它。好

但是,我应该在哪里声明 Pack/UnPack 函数?! 生成的 header 也需要这些,因此也应包括这些... 但是这些声明需要 Vec2 类型的定义,它在生成的代码中定义。 所以这是循环依赖,无法使用该功能,还是我遗漏了一些重要的东西?

原来我可以前向声明 return 类型,即使它不是指针。

这就是我所做的:

struct Vec2;
namespace flatbuffers {
    Vec2 Pack(const vector2& obj);
    vector2 UnPack(const Vec2& obj);
}