Search code examples
c++flatbuffers

Using custom enums in a FlatBuffer


I'm using the C++ API for FlatBuffers to store and retrieve preferences for a macOS application written in Objective-C.

Many of the preferences that I'm storing are typedef'd enums that I use throughout the application. Is there anyway to use those existing enums within a FlatBuffer schema such that I can use them as default values?

For example, I'd like to do the following:

  table Preferences {
    layout:int = FLLayoutModeList;
    sidebar:int = FLSidebarElementInspector;
  }

Where FLLayoutModeList and FLSidebarElementInspector are defined as FLLayoutMode and FLSidebarElement enums respectively in my Objective-C.

Can this be done using the C++ native types interface? Note that I'm only reading and writing this FlatBuffer from my own code. It is not being shared with other applications nor does it require any cross-language support, if that matters.


Solution

  • There is no way for a schema to include definitions from something that is not a schema, i.e. C++ or Objective C code. The only way to not have to duplicate them is to define them in a FlatBuffers schema and use the generated code from there. It does not work the other way around.

    Note that if you're using Objective C (as opposed to Objective C++) you could use the flatcc schema compiler to generate pure C, which may work better.