mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 20:31:23 +00:00
[C++] Static assert on Flatbuffers Version (#7203)
* Static assert on Flatbuffers Version * add comment
This commit is contained in:
@@ -16,8 +16,10 @@
|
||||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "flatbuffers/base.h"
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flatc.h"
|
||||
@@ -199,6 +201,25 @@ class CppGenerator : public BaseGenerator {
|
||||
for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw);
|
||||
}
|
||||
|
||||
// Adds code to check that the included flatbuffers.h is of the same version
|
||||
// as the generated code. This check currently looks for exact version match,
|
||||
// as we would guarantee that they are compatible, but in theory a newer
|
||||
// version of flatbuffers.h should work with a old code gen if we do proper
|
||||
// backwards support.
|
||||
void GenFlatbuffersVersionCheck() {
|
||||
code_ +=
|
||||
"// Ensure the included flatbuffers.h is the same version as when this "
|
||||
"file was";
|
||||
code_ += "// generated, otherwise it may not be compatible.";
|
||||
code_ += "static_assert(FLATBUFFERS_VERSION_MAJOR == " +
|
||||
std::to_string(FLATBUFFERS_VERSION_MAJOR) + " &&";
|
||||
code_ += " FLATBUFFERS_VERSION_MINOR == " +
|
||||
std::to_string(FLATBUFFERS_VERSION_MINOR) + " &&";
|
||||
code_ += " FLATBUFFERS_VERSION_REVISION == " +
|
||||
std::to_string(FLATBUFFERS_VERSION_REVISION) + ",";
|
||||
code_ += " \"Non-compatible flatbuffers version included\");";
|
||||
}
|
||||
|
||||
void GenIncludeDependencies() {
|
||||
int num_includes = 0;
|
||||
if (opts_.generate_object_based_api) {
|
||||
@@ -285,6 +306,8 @@ class CppGenerator : public BaseGenerator {
|
||||
|
||||
code_ += "#include \"flatbuffers/flatbuffers.h\"";
|
||||
code_ += "";
|
||||
GenFlatbuffersVersionCheck();
|
||||
code_ += "";
|
||||
|
||||
SetNameSpace(struct_def.defined_namespace);
|
||||
auto name = Name(struct_def);
|
||||
@@ -349,6 +372,8 @@ class CppGenerator : public BaseGenerator {
|
||||
code_ += "#include \"flatbuffers/flexbuffers.h\"";
|
||||
}
|
||||
code_ += "";
|
||||
GenFlatbuffersVersionCheck();
|
||||
code_ += "";
|
||||
|
||||
if (opts_.include_dependence_headers) { GenIncludeDependencies(); }
|
||||
GenExtraIncludes();
|
||||
|
||||
Reference in New Issue
Block a user