Merge "Generate C++ function returning the file_identifier of a flatbuffer" into ub-games-master

This commit is contained in:
Wouter van Oortmerssen 2014-12-09 02:00:58 +00:00 committed by Android (Google) Code Review
commit 19361a58ec
3 changed files with 19 additions and 10 deletions

View File

@ -638,21 +638,27 @@ std::string GenerateCPP(const Parser &parser,
"return verifier.VerifyBuffer<";
code += parser.root_struct_def->name + ">(); }\n\n";
if (parser.file_identifier_.length()) {
// Return the identifier
code += "inline const char *" + parser.root_struct_def->name;
code += "Identifier() { return \"" + parser.file_identifier_;
code += "\"; }\n\n";
// Check if a buffer has the identifier.
code += "inline bool " + parser.root_struct_def->name;
code += "BufferHasIdentifier(const void *buf) { return flatbuffers::";
code += "BufferHasIdentifier(buf, ";
code += parser.root_struct_def->name + "Identifier()); }\n\n";
}
// Finish a buffer with a given root object:
code += "inline void Finish" + parser.root_struct_def->name;
code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<";
code += parser.root_struct_def->name + "> root) { fbb.Finish(root";
if (parser.file_identifier_.length())
code += ", \"" + parser.file_identifier_ + "\"";
code += ", " + parser.root_struct_def->name + "Identifier()";
code += "); }\n\n";
if (parser.file_identifier_.length()) {
// Check if a buffer has the identifier.
code += "inline bool " + parser.root_struct_def->name;
code += "BufferHasIdentifier(const void *buf) { return flatbuffers::";
code += "BufferHasIdentifier(buf, \"" + parser.file_identifier_;
code += "\"); }\n\n";
}
}
CloseNestedNameSpaces(name_space, &code);

View File

@ -240,9 +240,11 @@ inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<
inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer<Monster>(); }
inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<Monster> root) { fbb.Finish(root, "MONS"); }
inline const char *MonsterIdentifier() { return "MONS"; }
inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, "MONS"); }
inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, MonsterIdentifier()); }
inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<Monster> root) { fbb.Finish(root, MonsterIdentifier()); }
} // namespace Example
} // namespace MyGame

View File

@ -117,6 +117,7 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
flatbuf.length());
TEST_EQ(VerifyMonsterBuffer(verifier), true);
TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
TEST_EQ(MonsterBufferHasIdentifier(flatbuf.c_str()), true);
// Access the buffer from the root.