C++ アプリで flatbuffers でシリアライズされたメッセージを読み込む


スキーマでデータ構造を指定できて, ライブラリとしても組み込みやすく C++ と親和性のたかそうな flatbuffers を試します

Cmake で自作アプリに取り込む

flatbuffers のライブラリ自体は, add_subdirectory で簡単に自作アプリに取り込めます.


# [flatbuffers]
set(FLATBUFFERS_BUILD_TESTS Off CACHE INTERNAL "" FORCE)
set(FLATBUFFERS_BUILD_FLATHASH Off CACHE INTERNAL "" FORCE)
set(FLATBUFFERS_INSTALL Off CACHE INTERNAL "" FORCE)

add_subdirectory(${CMAKE_SOURCE_DIR}/third_party/flatbuffers)

...

target_link_libraries(app flatbuffers)

C++ アプリに組み込む

サンプルは flatbuffers のコードを参考にしてみてください.

パースエラーが出る場合は, error_ メンバ変数にエラーメッセージがあります.

パースがうまくいったら, parser.builder_.GetBufferPointer() にそのデータがありますので, flatc で背一斉されたクラスの Get*** に渡せばメッセージが復元できます.

  // load FlatBuffer schema (.fbs) and JSON from disk
  std::string schemafile;
  std::string jsonfile;
  bool ok = flatbuffers::LoadFile("../schema/config.fbs", false, &schemafile) &&
            flatbuffers::LoadFile("../config.json", false, &jsonfile);
  if (!ok) {
    printf("couldn't load files!\n");
    return 1;
  }

  // parse schema first, so we can use it to parse the data after
  flatbuffers::Parser parser;
  const char *include_directories[] = { nullptr };
  ok = parser.Parse(schemafile.c_str(), include_directories) &&
       parser.Parse(jsonfile.c_str(), include_directories);
  if (!ok) {
    std::cerr << "Parse error: " << parser.error_ << std::endl;
    return 1;
  }

  const App::Config *config = App::GetConfig(parser.builder_.GetBufferPointer());
  std::cout << config->pos()->x() << ", " << config->pos()->y() << ", " << config->pos()->z() << std::endl;

TODO

スキーマファイルを .h かなにかにして, 実行時にファイルから読まずに済むようにする

代用案 JSON + JSON schema

テキストのデータが主流であったり, バイナリのデータが少なく base64 にエンコードしてもサイズ的に問題がないのであれば, JSON + JSON schema という手もあります.

TinyGLTF では nlohmann json.hpp と, それをベースにした schema validator を使っています.

ただし, データを直接 C++ のクラスや構造体に変換してくれるものは, Cereal などの別ライブラリを使うか, 自作する必要があります.