Fix dereference operator of VectorIterator to structures (#8425)

For Vector or Array of structures the dereference operator of an
iterator returns the pointer to the structure. However, IndirectHelper,
which is used in the implementation of this operator, is instantiated
in the way that the IndirectHelper::Read returns structure by value.

This is because, Vector and Array instantiate IndirectHelper with
const T*, but VectorIterator instantiates IndirectHelper with T. There
are three IndirectHelper template definition: first for T, second for
Offset<T> and the last one for const T*. Those have different
IndirectHelper:Read implementations and (more importantly) return type.
This is the reason of mismatch in VectorIterator::operator* between
return type declaration and what was exactly returned.

That is, for Array<T,...> where T is scalar the VectorIterator is
instantiated as VectorIterator<T, T>, dereference operator returns T
and its implementation uses IndirectHelper<T> which Read function
returns T.
When T is not scalar, then VectorIterator is instantiated as
VectorIterator<T, const T *>, dereference operator returns const T * and
its implementation uses IndirectHelper<T> which Read function returns T.

The fix is done as follows:
* implement type trait is_specialization_of_Offset and
 is_specialization_of_Offset64,
* change partial specialization of IndirectHelper with const T * that
 it is instantiated by T and enabled only if T is not scalar and not
 specialization of Offset or Offset64,
* remove type differentiation (due to scalar) from Array..

The above makes the IndirectHelper able to correctly instantiate itself
basing only on T. Thus, the instantiation in VectorIterator correctly
instantiate IndirectHelper::Read function, especially the return type.
3 files changed
tree: 43efebbee618623140d38c2f1380a29fe24fae59
  1. .bazelci/
  2. .github/
  3. android/
  4. bazel/
  5. benchmarks/
  6. CMake/
  7. conan/
  8. dart/
  9. docs/
  10. examples/
  11. go/
  12. goldens/
  13. grpc/
  14. include/
  15. java/
  16. js/
  17. kotlin/
  18. lobster/
  19. lua/
  20. mjs/
  21. net/
  22. nim/
  23. php/
  24. python/
  25. reflection/
  26. rust/
  27. samples/
  28. scripts/
  29. snap/
  30. src/
  31. swift/
  32. tests/
  33. ts/
  34. .bazelignore
  35. .bazelrc
  36. .clang-format
  37. .clang-tidy
  38. .editorconfig
  39. .eslintrc.js
  40. .gitattributes
  41. .gitignore
  42. .npmrc
  43. BUILD.bazel
  44. build_defs.bzl
  45. CHANGELOG.md
  46. CMakeLists.txt
  47. composer.json
  48. conanfile.py
  49. CONTRIBUTING.md
  50. extensions.bzl
  51. FlatBuffers.podspec
  52. Formatters.md
  53. LICENSE
  54. MODULE.bazel
  55. package.json
  56. Package.swift
  57. pnpm-lock.yaml
  58. README.md
  59. SECURITY.md
  60. swift.swiftformat
  61. tsconfig.json
  62. tsconfig.mjs.json
  63. typescript.bzl
README.md

logo FlatBuffers

Build status BuildKite status Fuzzing Status Discord Chat Twitter Follow Twitter Follow

FlatBuffers is a cross platform serialization library architected for maximum memory efficiency. It allows you to directly access serialized data without parsing/unpacking it first, while still having great forwards/backwards compatibility.

Quick Start

  1. Build the compiler for flatbuffers (flatc)

    Use cmake to create the build files for your platform and then perform the compilation (Linux example).

    cmake -G "Unix Makefiles"
    make -j
    
  2. Define your flatbuffer schema (.fbs)

    Write the schema to define the data you want to serialize. See monster.fbs for an example.

  3. Generate code for your language(s)

    Use the flatc compiler to take your schema and generate language-specific code:

    ./flatc --cpp --rust monster.fbs
    

    Which generates monster_generated.h and monster_generated.rs files.

  4. Serialize data

    Use the generated code, as well as the FlatBufferBuilder to construct your serialized buffer. (C++ example)

  5. Transmit/store/save Buffer

    Use your serialized buffer however you want. Send it to someone, save it for later, etc...

  6. Read the data

    Use the generated accessors to read the data from the serialized buffer.

    It doesn't need to be the same language/schema version, FlatBuffers ensures the data is readable across languages and schema versions. See the Rust example reading the data written by C++.

Documentation

Go to our landing page to browse our documentation.

Supported operating systems

  • Windows
  • macOS
  • Linux
  • Android
  • And any others with a recent C++ compiler (C++ 11 and newer)

Supported programming languages

Code generation and runtime libraries for many popular languages.

  1. C
  2. C++ - snapcraft.io
  3. C# - nuget.org
  4. Dart - pub.dev
  5. Go - go.dev
  6. Java - Maven
  7. JavaScript - NPM
  8. Kotlin
  9. Lobster
  10. Lua
  11. PHP
  12. Python - PyPI
  13. Rust - crates.io
  14. Swift - swiftpackageindex
  15. TypeScript - NPM
  16. Nim

Versioning

FlatBuffers does not follow traditional SemVer versioning (see rationale) but rather uses a format of the date of the release.

Contribution

To contribute to this project, see CONTRIBUTING.

Community

Security

Please see our Security Policy for reporting vulnerabilities.

Licensing

Flatbuffers is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.