[C++17] Add compile-time reflection for fields. (#6324)
authorDavid P. Sicilia <dpacbach@users.noreply.github.com>
Fri, 5 Mar 2021 18:01:40 +0000 (13:01 -0500)
committerGitHub <noreply@github.com>
Fri, 5 Mar 2021 18:01:40 +0000 (10:01 -0800)
commita69815f72c7432a838f7163ecdcf6b30c8b3bf35
treeab61ecc8a9808584ee7fc70869f2a0e212fdf916
parent4033ff5892ed6a5e083811fa13c7f9094d46d5db
[C++17] Add compile-time reflection for fields. (#6324)

* [C++17] Add compile-time reflection for fields.

Included in this commit is the following:

  - The C++ generator has been modified so that,
    when in C++17 mode, it will emit Table and
    Struct field traits that can be used at com-
    pile time as a form of static reflection. This
    includes field types, field names, and a tuple
    of field getter results.

  - Diffs to the cpp17 generated files. No other
    generated files are affected.

  - A unit test that also serves as an example. It
    demonstrates how to use the full power of this
    reflection to implement a full recursive
    JSON-like stringifier for Flatbuffers types,
    but without needing any runtime access to the
    *.fbs definition files; the computation is
    done using only static reflection.

Tested on Linux with gcc 10.2.0.

Fixes #6285.

* Fix int-conversion warning on MSVC.

* Try to fix std::to_string ambiguity on MSVC.

* Fix clang-format diffs.

* Fix more clang-format diffs.

* Fix last clang-format diff.

* Enable C++17 build/test for VC 19 platform in CI.

* Forgot to add value to cmake command line variable.

* Various fixes/changes in response to @vglavnyy's feedback.

* Replace "fields pack" with index-based getters.

* Fix MSVC error.

* Fix clang-format diffs.

* getter_for method returns result instead of address-of-getter.

* Next round of reviewer suggestions.

* Use type instead of hardcoded struct name.

* Fix clang-format diff.

* Add test for FieldType since it is not used in the stringify test.

* Add fields_number field to Traits struct.

* Add --cpp-static-reflection flag and put those features behind it.

* Fix clang-format diffs.

* Remove <tuple> include.
.github/workflows/build.yml
include/flatbuffers/idl.h
src/flatc.cpp
src/idl_gen_cpp.cpp
tests/cpp17/generated_cpp17/monster_test_generated.h
tests/cpp17/generated_cpp17/optional_scalars_generated.h
tests/cpp17/stringify_util.h [new file with mode: 0644]
tests/cpp17/test_cpp17.cpp
tests/generate_code.sh