Commit a0bf238b authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen Committed by Android (Google) Code Review

Merge changes I2de7d14d,I56392340 into ub-games-master

* changes:
  Added accessor for file_extension in generated code.
  Added missing --gen-mutable to CMakeLists.txt
parents fc7e8af5 37e6efe1
......@@ -104,7 +104,7 @@ function(compile_flatbuffers_schema_to_cpp SRC_FBS)
string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
add_custom_command(
OUTPUT ${GEN_HEADER}
COMMAND flatc -c -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
COMMAND flatc -c --gen-mutable -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
DEPENDS flatc)
endfunction()
......
......@@ -50,18 +50,27 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
: x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)) { }
float x() const { return flatbuffers::EndianScalar(x_); }
void mutate_x(float x) { flatbuffers::WriteScalar(&x_, x); }
float y() const { return flatbuffers::EndianScalar(y_); }
void mutate_y(float y) { flatbuffers::WriteScalar(&y_, y); }
float z() const { return flatbuffers::EndianScalar(z_); }
void mutate_z(float z) { flatbuffers::WriteScalar(&z_, z); }
};
STRUCT_END(Vec3, 12);
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
const Vec3 *pos() const { return GetStruct<const Vec3 *>(4); }
Vec3 *mutable_pos() { return GetStruct<Vec3 *>(4); }
int16_t mana() const { return GetField<int16_t>(6, 150); }
bool mutate_mana(int16_t mana) { return SetField(6, mana); }
int16_t hp() const { return GetField<int16_t>(8, 100); }
bool mutate_hp(int16_t hp) { return SetField(8, hp); }
const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(10); }
flatbuffers::String *mutable_name() { return GetPointer<flatbuffers::String *>(10); }
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
flatbuffers::Vector<uint8_t> *mutable_inventory() { return GetPointer<flatbuffers::Vector<uint8_t> *>(14); }
Color color() const { return static_cast<Color>(GetField<int8_t>(16, 2)); }
bool mutate_color(Color color) { return SetField(16, static_cast<int8_t>(color)); }
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<Vec3>(verifier, 4 /* pos */) &&
......@@ -120,6 +129,8 @@ inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, An
inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot<Monster>(buf); }
inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer<Monster>(); }
inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<Monster> root) { fbb.Finish(root); }
......
......@@ -717,6 +717,13 @@ std::string GenerateCPP(const Parser &parser,
code += name + "Identifier()); }\n\n";
}
if (parser.file_extension_.length()) {
// Return the extension
code += "inline const char *" + name;
code += "Extension() { return \"" + parser.file_extension_;
code += "\"; }\n\n";
}
// Finish a buffer with a given root object:
code += "inline void Finish" + name;
code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<";
......
......@@ -332,6 +332,8 @@ inline const char *MonsterIdentifier() { return "MONS"; }
inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, MonsterIdentifier()); }
inline const char *MonsterExtension() { return "mon"; }
inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<Monster> root) { fbb.Finish(root, MonsterIdentifier()); }
} // namespace Example
......
......@@ -135,6 +135,7 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length) {
TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
TEST_EQ(strcmp(MonsterExtension(), "mon"), 0);
// Access the buffer from the root.
auto monster = GetMonster(flatbuf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment