mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
Enums in C++ are now strongly typed.
Accessors and constructors now take enum types rather than ints. Bug: 16570507 Change-Id: I4b50fd64ad2e662ea2481bc0ccea784326fb31c0 Tested: on Linux and Windows.
This commit is contained in:
@@ -71,7 +71,7 @@ add_executable(flatc ${FlatBuffers_Compiler_SRCS})
|
||||
|
||||
function(compile_flatbuffers_schema_to_cpp SRC_FBS)
|
||||
get_filename_component(SRC_FBS_DIR ${SRC_FBS} DIRECTORY)
|
||||
string(REGEX REPLACE ".fbs$" "_generated.h" GEN_HEADER ${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}"
|
||||
@@ -97,6 +97,7 @@ endif()
|
||||
if(FLATBUFFERS_BUILD_TESTS)
|
||||
enable_testing()
|
||||
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION
|
||||
"${CMAKE_CURRENT_BINARY_DIR}")
|
||||
add_test(NAME flattests COMMAND flattests)
|
||||
endif()
|
||||
|
||||
@@ -259,6 +259,13 @@ public:
|
||||
return IndirectHelper<T>::Read(Data(), i);
|
||||
}
|
||||
|
||||
// If this is a Vector of enums, T will be its storage type, not the enum
|
||||
// type. This function makes it convenient to retrieve value with enum
|
||||
// type E.
|
||||
template<typename E> E GetEnum(uoffset_t i) const {
|
||||
return static_cast<E>(Get(i));
|
||||
}
|
||||
|
||||
const void *GetStructFromOffset(size_t o) const {
|
||||
return reinterpret_cast<const void *>(Data() + o);
|
||||
}
|
||||
|
||||
@@ -175,12 +175,13 @@ struct Namespace {
|
||||
|
||||
// Base class for all definition types (fields, structs_, enums_).
|
||||
struct Definition {
|
||||
Definition() : generated(false) {}
|
||||
Definition() : generated(false), defined_namespace(nullptr) {}
|
||||
|
||||
std::string name;
|
||||
std::string doc_comment;
|
||||
SymbolTable<Value> attributes;
|
||||
bool generated; // did we already output code for this definition?
|
||||
Namespace *defined_namespace; // Where it was defined.
|
||||
};
|
||||
|
||||
struct FieldDef : public Definition {
|
||||
@@ -199,8 +200,7 @@ struct StructDef : public Definition {
|
||||
predecl(true),
|
||||
sortbysize(true),
|
||||
minalign(1),
|
||||
bytesize(0),
|
||||
defined_namespace(nullptr)
|
||||
bytesize(0)
|
||||
{}
|
||||
|
||||
void PadLastField(size_t minalign) {
|
||||
@@ -215,7 +215,6 @@ struct StructDef : public Definition {
|
||||
bool sortbysize; // Whether fields come in the declaration or size order.
|
||||
size_t minalign; // What the whole object needs to be aligned to.
|
||||
size_t bytesize; // Size if fixed.
|
||||
Namespace *defined_namespace; // Where it was defined.
|
||||
};
|
||||
|
||||
inline bool IsStruct(const Type &type) {
|
||||
@@ -243,8 +242,9 @@ struct EnumVal {
|
||||
struct EnumDef : public Definition {
|
||||
EnumDef() : is_union(false) {}
|
||||
|
||||
EnumVal *ReverseLookup(int enum_idx) {
|
||||
for (auto it = vals.vec.begin() + static_cast<int>(is_union);
|
||||
EnumVal *ReverseLookup(int enum_idx, bool skip_union_default = true) {
|
||||
for (auto it = vals.vec.begin() + static_cast<int>(is_union &&
|
||||
skip_union_default);
|
||||
it != vals.vec.end(); ++it) {
|
||||
if ((*it)->value == enum_idx) {
|
||||
return *it;
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Sample {
|
||||
struct Vec3;
|
||||
struct Monster;
|
||||
|
||||
enum {
|
||||
enum Color {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2
|
||||
@@ -23,9 +23,9 @@ inline const char **EnumNamesColor() {
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
inline const char *EnumNameColor(Color e) { return EnumNamesColor()[e]; }
|
||||
|
||||
enum {
|
||||
enum Any {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1
|
||||
};
|
||||
@@ -35,7 +35,7 @@ inline const char **EnumNamesAny() {
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
inline const char *EnumNameAny(Any e) { return EnumNamesAny()[e]; }
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type);
|
||||
|
||||
@@ -61,7 +61,7 @@ struct Monster : private flatbuffers::Table {
|
||||
int16_t hp() const { return GetField<int16_t>(8, 100); }
|
||||
const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(10); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
int8_t color() const { return GetField<int8_t>(16, 2); }
|
||||
Color color() const { return static_cast<Color>(GetField<int8_t>(16, 2)); }
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<Vec3>(verifier, 4 /* pos */) &&
|
||||
@@ -84,10 +84,13 @@ struct MonsterBuilder {
|
||||
void add_hp(int16_t hp) { fbb_.AddElement<int16_t>(8, hp, 100); }
|
||||
void add_name(flatbuffers::Offset<flatbuffers::String> name) { fbb_.AddOffset(10, name); }
|
||||
void add_inventory(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory) { fbb_.AddOffset(14, inventory); }
|
||||
void add_color(int8_t color) { fbb_.AddElement<int8_t>(16, color, 2); }
|
||||
void add_color(Color color) { fbb_.AddElement<int8_t>(16, static_cast<int8_t>(color), 2); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
MonsterBuilder &operator=(const MonsterBuilder &);
|
||||
flatbuffers::Offset<Monster> Finish() { return flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 7)); }
|
||||
flatbuffers::Offset<Monster> Finish() {
|
||||
auto o = flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 7));
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb,
|
||||
@@ -96,7 +99,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
int16_t hp = 100,
|
||||
flatbuffers::Offset<flatbuffers::String> name = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory = 0,
|
||||
int8_t color = 2) {
|
||||
Color color = Color_Blue) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_inventory(inventory);
|
||||
builder_.add_name(name);
|
||||
|
||||
@@ -23,18 +23,38 @@
|
||||
namespace flatbuffers {
|
||||
namespace cpp {
|
||||
|
||||
// Ensure that a type is prefixed with its namespace whenever it is used
|
||||
// outside of its namespace.
|
||||
static std::string WrapInNameSpace(const Parser &parser, const Namespace *ns,
|
||||
const std::string &name) {
|
||||
if (parser.namespaces_.back() != ns) {
|
||||
std::string qualified_name;
|
||||
for (auto it = ns->components.begin();
|
||||
it != ns->components.end(); ++it) {
|
||||
qualified_name += *it + "::";
|
||||
}
|
||||
return qualified_name + name;
|
||||
} else {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
// Return a C++ type from the table in idl.h
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static std::string GenTypeBasic(const Parser &parser, const Type &type,
|
||||
bool real_enum) {
|
||||
static const char *ctypename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE) #CTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
return ctypename[type.base_type];
|
||||
return real_enum && type.enum_def
|
||||
? WrapInNameSpace(parser, type.enum_def->defined_namespace,
|
||||
type.enum_def->name)
|
||||
: ctypename[type.base_type];
|
||||
}
|
||||
|
||||
static std::string GenTypeWire(const Parser &parser, const Type &type,
|
||||
const char *postfix);
|
||||
const char *postfix, bool real_enum);
|
||||
|
||||
// Return a C++ pointer type, specialized to the actual struct/table types,
|
||||
// and vector element types.
|
||||
@@ -44,19 +64,10 @@ static std::string GenTypePointer(const Parser &parser, const Type &type) {
|
||||
return "flatbuffers::String";
|
||||
case BASE_TYPE_VECTOR:
|
||||
return "flatbuffers::Vector<" +
|
||||
GenTypeWire(parser, type.VectorType(), "") + ">";
|
||||
GenTypeWire(parser, type.VectorType(), "", false) + ">";
|
||||
case BASE_TYPE_STRUCT: {
|
||||
auto ns = type.struct_def->defined_namespace;
|
||||
if (parser.namespaces_.back() != ns) {
|
||||
std::string qualified_name;
|
||||
for (auto it = ns->components.begin();
|
||||
it != ns->components.end(); ++it) {
|
||||
qualified_name += *it + "::";
|
||||
}
|
||||
return qualified_name + type.struct_def->name;
|
||||
} else {
|
||||
return type.struct_def->name;
|
||||
}
|
||||
return WrapInNameSpace(parser, type.struct_def->defined_namespace,
|
||||
type.struct_def->name);
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
@@ -68,9 +79,9 @@ static std::string GenTypePointer(const Parser &parser, const Type &type) {
|
||||
// Return a C++ type for any type (scalar/pointer) specifically for
|
||||
// building a flatbuffer.
|
||||
static std::string GenTypeWire(const Parser &parser, const Type &type,
|
||||
const char *postfix) {
|
||||
const char *postfix, bool real_enum) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type) + postfix
|
||||
? GenTypeBasic(parser, type, real_enum) + postfix
|
||||
: IsStruct(type)
|
||||
? "const " + GenTypePointer(parser, type) + " *"
|
||||
: "flatbuffers::Offset<" + GenTypePointer(parser, type) + ">" + postfix;
|
||||
@@ -80,7 +91,7 @@ static std::string GenTypeWire(const Parser &parser, const Type &type,
|
||||
// serialized size.
|
||||
static std::string GenTypeSize(const Parser &parser, const Type &type) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
? GenTypeBasic(parser, type, false)
|
||||
: IsStruct(type)
|
||||
? GenTypePointer(parser, type)
|
||||
: "flatbuffers::uoffset_t";
|
||||
@@ -90,9 +101,9 @@ static std::string GenTypeSize(const Parser &parser, const Type &type) {
|
||||
// using a flatbuffer.
|
||||
static std::string GenTypeGet(const Parser &parser, const Type &type,
|
||||
const char *afterbasic, const char *beforeptr,
|
||||
const char *afterptr) {
|
||||
const char *afterptr, bool real_enum) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type) + afterbasic
|
||||
? GenTypeBasic(parser, type, real_enum) + afterbasic
|
||||
: beforeptr + GenTypePointer(parser, type) + afterptr;
|
||||
}
|
||||
|
||||
@@ -110,7 +121,7 @@ static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
|
||||
std::string &code = *code_ptr;
|
||||
std::string &code_post = *code_ptr_post;
|
||||
GenComment(enum_def.doc_comment, code_ptr);
|
||||
code += "enum {\n";
|
||||
code += "enum " + enum_def.name + " {\n";
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
@@ -143,7 +154,7 @@ static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
|
||||
}
|
||||
code += "nullptr };\n return names;\n}\n\n";
|
||||
code += "inline const char *EnumName" + enum_def.name;
|
||||
code += "(int e) { return EnumNames" + enum_def.name + "()[e";
|
||||
code += "(" + enum_def.name + " e) { return EnumNames" + enum_def.name + "()[e";
|
||||
if (enum_def.vals.vec.front()->value)
|
||||
code += " - " + GenEnumVal(enum_def, *enum_def.vals.vec.front(), opts);
|
||||
code += "]; }\n\n";
|
||||
@@ -176,6 +187,18 @@ static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
|
||||
}
|
||||
}
|
||||
|
||||
// Generates a value with optionally a cast applied if the field has a
|
||||
// different underlying type from its interface type (currently only the
|
||||
// case for enums. "from" specify the direction, true meaning from the
|
||||
// underlying type to the interface type.
|
||||
std::string GenUnderlyingCast(const Parser &parser, const FieldDef &field,
|
||||
bool from, const std::string &val) {
|
||||
return field.value.type.enum_def && IsScalar(field.value.type.base_type)
|
||||
? "static_cast<" + GenTypeBasic(parser, field.value.type, from) + ">(" +
|
||||
val + ")"
|
||||
: val;
|
||||
}
|
||||
|
||||
// Generate an accessor struct, builder structs & function for a table.
|
||||
static void GenTable(const Parser &parser, StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
@@ -193,18 +216,21 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) { // Deprecated fields won't be accessible.
|
||||
GenComment(field.doc_comment, code_ptr, " ");
|
||||
code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " *");
|
||||
code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " *",
|
||||
true);
|
||||
code += field.name + "() const { return ";
|
||||
// Call a different accessor for pointers, that indirects.
|
||||
code += IsScalar(field.value.type.base_type)
|
||||
std::string call = IsScalar(field.value.type.base_type)
|
||||
? "GetField<"
|
||||
: (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<");
|
||||
code += GenTypeGet(parser, field.value.type, "", "const ", " *") + ">(";
|
||||
code += NumToString(field.value.offset);
|
||||
call += GenTypeGet(parser, field.value.type, "", "const ", " *", false);
|
||||
call += ">(" + NumToString(field.value.offset);
|
||||
// Default value as second arg for non-pointer types.
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += ", " + field.value.constant;
|
||||
code += "); }\n";
|
||||
call += ", " + field.value.constant;
|
||||
call += ")";
|
||||
code += GenUnderlyingCast(parser, field, true, call);
|
||||
code += "; }\n";
|
||||
auto nested = field.attributes.Lookup("nested_flatbuffer");
|
||||
if (nested) {
|
||||
auto nested_root = parser.structs_.Lookup(nested->constant);
|
||||
@@ -283,15 +309,18 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) {
|
||||
code += " void add_" + field.name + "(";
|
||||
code += GenTypeWire(parser, field.value.type, " ") + field.name;
|
||||
code += GenTypeWire(parser, field.value.type, " ", true) + field.name;
|
||||
code += ") { fbb_.Add";
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += "Element<" + GenTypeWire(parser, field.value.type, "") + ">";
|
||||
else if (IsStruct(field.value.type))
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
code += "Element<" + GenTypeWire(parser, field.value.type, "", false);
|
||||
code += ">";
|
||||
} else if (IsStruct(field.value.type)) {
|
||||
code += "Struct";
|
||||
else
|
||||
} else {
|
||||
code += "Offset";
|
||||
code += "(" + NumToString(field.value.offset) + ", " + field.name;
|
||||
}
|
||||
code += "(" + NumToString(field.value.offset) + ", ";
|
||||
code += GenUnderlyingCast(parser, field, false, field.name);
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += ", " + field.value.constant;
|
||||
code += "); }\n";
|
||||
@@ -327,8 +356,22 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) {
|
||||
code += ",\n " + GenTypeWire(parser, field.value.type, " ");
|
||||
code += field.name + " = " + field.value.constant;
|
||||
code += ",\n " + GenTypeWire(parser, field.value.type, " ", true);
|
||||
code += field.name + " = ";
|
||||
if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) {
|
||||
auto ed = field.value.type.enum_def->ReverseLookup(
|
||||
StringToInt(field.value.constant.c_str()), false);
|
||||
if (ed) {
|
||||
code += WrapInNameSpace(parser,
|
||||
field.value.type.enum_def->defined_namespace,
|
||||
field.value.type.enum_def->name + "_" +
|
||||
ed->name);
|
||||
} else {
|
||||
code += GenUnderlyingCast(parser, field, true, field.value.constant);
|
||||
}
|
||||
} else {
|
||||
code += field.value.constant;
|
||||
}
|
||||
}
|
||||
}
|
||||
code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n";
|
||||
@@ -377,7 +420,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
code += " " + GenTypeGet(parser, field.value.type, " ", "", " ");
|
||||
code += " " + GenTypeGet(parser, field.value.type, " ", "", " ", false);
|
||||
code += field.name + "_;\n";
|
||||
GenPadding(field, [&code, &padding_id](int bits) {
|
||||
code += " int" + NumToString(bits) +
|
||||
@@ -392,7 +435,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (it != struct_def.fields.vec.begin()) code += ", ";
|
||||
code += GenTypeGet(parser, field.value.type, " ", "const ", " &");
|
||||
code += GenTypeGet(parser, field.value.type, " ", "const ", " &", true);
|
||||
code += field.name;
|
||||
}
|
||||
code += ")\n : ";
|
||||
@@ -403,10 +446,13 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
|
||||
auto &field = **it;
|
||||
if (it != struct_def.fields.vec.begin()) code += ", ";
|
||||
code += field.name + "_(";
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += "flatbuffers::EndianScalar(" + field.name + "))";
|
||||
else
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
code += "flatbuffers::EndianScalar(";
|
||||
code += GenUnderlyingCast(parser, field, false, field.name);
|
||||
code += "))";
|
||||
} else {
|
||||
code += field.name + ")";
|
||||
}
|
||||
GenPadding(field, [&code, &padding_id](int bits) {
|
||||
(void)bits;
|
||||
code += ", __padding" + NumToString(padding_id++) + "(0)";
|
||||
@@ -432,12 +478,13 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
GenComment(field.doc_comment, code_ptr, " ");
|
||||
code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " &");
|
||||
code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " &",
|
||||
true);
|
||||
code += field.name + "() const { return ";
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += "flatbuffers::EndianScalar(" + field.name + "_)";
|
||||
else
|
||||
code += field.name + "_";
|
||||
code += GenUnderlyingCast(parser, field, true,
|
||||
IsScalar(field.value.type.base_type)
|
||||
? "flatbuffers::EndianScalar(" + field.name + "_)"
|
||||
: field.name + "_");
|
||||
code += "; }\n";
|
||||
}
|
||||
code += "};\nSTRUCT_END(" + struct_def.name + ", ";
|
||||
|
||||
@@ -371,6 +371,15 @@ void Parser::ParseField(StructDef &struct_def) {
|
||||
ParseSingleValue(field.value);
|
||||
}
|
||||
|
||||
if (type.enum_def &&
|
||||
IsScalar(type.base_type) &&
|
||||
!struct_def.fixed &&
|
||||
!type.enum_def->attributes.Lookup("bit_flags") &&
|
||||
!type.enum_def->ReverseLookup(StringToInt(field.value.constant.c_str())))
|
||||
Error("enum " + type.enum_def->name +
|
||||
" does not have a declaration for this field\'s default of " +
|
||||
field.value.constant);
|
||||
|
||||
field.doc_comment = dc;
|
||||
ParseMetaData(field);
|
||||
field.deprecated = field.attributes.Lookup("deprecated") != nullptr;
|
||||
@@ -703,6 +712,7 @@ void Parser::ParseEnum(bool is_union) {
|
||||
enum_def.name = name;
|
||||
enum_def.doc_comment = dc;
|
||||
enum_def.is_union = is_union;
|
||||
enum_def.defined_namespace = namespaces_.back();
|
||||
if (enums_.Add(name, &enum_def)) Error("enum already exists: " + name);
|
||||
if (is_union) {
|
||||
enum_def.underlying_type.base_type = BASE_TYPE_UTYPE;
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace FlatBuffers.Test
|
||||
|
||||
Monster.StartMonster(fbb);
|
||||
Monster.AddPos(fbb, Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
|
||||
(sbyte)4, (short)5, (sbyte)6));
|
||||
Color.Green, (short)5, (sbyte)6));
|
||||
Monster.AddHp(fbb, (short)80);
|
||||
Monster.AddName(fbb, str);
|
||||
Monster.AddInventory(fbb, inv);
|
||||
@@ -105,7 +105,7 @@ namespace FlatBuffers.Test
|
||||
Assert.AreEqual(3.0f, pos.Z());
|
||||
|
||||
Assert.AreEqual(3.0f, pos.Test1());
|
||||
Assert.AreEqual((sbyte)4, pos.Test2());
|
||||
Assert.AreEqual(Color.Green, pos.Test2());
|
||||
var t = pos.Test3();
|
||||
Assert.AreEqual((short)5, t.A());
|
||||
Assert.AreEqual((sbyte)6, t.B());
|
||||
|
||||
@@ -72,7 +72,7 @@ class JavaTest {
|
||||
|
||||
Monster.startMonster(fbb);
|
||||
Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
|
||||
(byte)4, (short)5, (byte)6));
|
||||
Color.Green, (short)5, (byte)6));
|
||||
Monster.addHp(fbb, (short)80);
|
||||
Monster.addName(fbb, str);
|
||||
Monster.addInventory(fbb, inv);
|
||||
@@ -135,7 +135,7 @@ class JavaTest {
|
||||
TestEq(pos.y(), 2.0f);
|
||||
TestEq(pos.z(), 3.0f);
|
||||
TestEq(pos.test1(), 3.0);
|
||||
TestEq(pos.test2(), (byte)4);
|
||||
TestEq(pos.test2(), Color.Green);
|
||||
Test t = pos.test3();
|
||||
TestEq(t.a(), (short)5);
|
||||
TestEq(t.b(), (byte)6);
|
||||
|
||||
@@ -15,7 +15,7 @@ struct Vec3 (force_align: 16) {
|
||||
y:float;
|
||||
z:float;
|
||||
test1:double;
|
||||
test2:byte;
|
||||
test2:Color;
|
||||
test3:Test;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ struct Test;
|
||||
struct Vec3;
|
||||
struct Monster;
|
||||
|
||||
enum {
|
||||
enum Color {
|
||||
Color_Red = 1,
|
||||
Color_Green = 2,
|
||||
Color_Blue = 8
|
||||
@@ -29,9 +29,9 @@ inline const char **EnumNamesColor() {
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e - Color_Red]; }
|
||||
inline const char *EnumNameColor(Color e) { return EnumNamesColor()[e - Color_Red]; }
|
||||
|
||||
enum {
|
||||
enum Any {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1
|
||||
};
|
||||
@@ -41,7 +41,7 @@ inline const char **EnumNamesAny() {
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
inline const char *EnumNameAny(Any e) { return EnumNamesAny()[e]; }
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type);
|
||||
|
||||
@@ -73,14 +73,14 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 {
|
||||
int16_t __padding2;
|
||||
|
||||
public:
|
||||
Vec3(float x, float y, float z, double test1, int8_t test2, const Test &test3)
|
||||
: x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)), __padding0(0), test1_(flatbuffers::EndianScalar(test1)), test2_(flatbuffers::EndianScalar(test2)), __padding1(0), test3_(test3), __padding2(0) { (void)__padding0; (void)__padding1; (void)__padding2; }
|
||||
Vec3(float x, float y, float z, double test1, Color test2, const Test &test3)
|
||||
: x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)), __padding0(0), test1_(flatbuffers::EndianScalar(test1)), test2_(flatbuffers::EndianScalar(static_cast<int8_t>(test2))), __padding1(0), test3_(test3), __padding2(0) { (void)__padding0; (void)__padding1; (void)__padding2; }
|
||||
|
||||
float x() const { return flatbuffers::EndianScalar(x_); }
|
||||
float y() const { return flatbuffers::EndianScalar(y_); }
|
||||
float z() const { return flatbuffers::EndianScalar(z_); }
|
||||
double test1() const { return flatbuffers::EndianScalar(test1_); }
|
||||
int8_t test2() const { return flatbuffers::EndianScalar(test2_); }
|
||||
Color test2() const { return static_cast<Color>(flatbuffers::EndianScalar(test2_)); }
|
||||
const Test &test3() const { return test3_; }
|
||||
};
|
||||
STRUCT_END(Vec3, 32);
|
||||
@@ -91,8 +91,8 @@ struct Monster : private flatbuffers::Table {
|
||||
int16_t hp() const { return GetField<int16_t>(8, 100); }
|
||||
const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(10); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
int8_t color() const { return GetField<int8_t>(16, 8); }
|
||||
uint8_t test_type() const { return GetField<uint8_t>(18, 0); }
|
||||
Color color() const { return static_cast<Color>(GetField<int8_t>(16, 8)); }
|
||||
Any test_type() const { return static_cast<Any>(GetField<uint8_t>(18, 0)); }
|
||||
const void *test() const { return GetPointer<const void *>(20); }
|
||||
const flatbuffers::Vector<const Test *> *test4() const { return GetPointer<const flatbuffers::Vector<const Test *> *>(22); }
|
||||
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *testarrayofstring() const { return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *>(24); }
|
||||
@@ -141,8 +141,8 @@ struct MonsterBuilder {
|
||||
void add_hp(int16_t hp) { fbb_.AddElement<int16_t>(8, hp, 100); }
|
||||
void add_name(flatbuffers::Offset<flatbuffers::String> name) { fbb_.AddOffset(10, name); }
|
||||
void add_inventory(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory) { fbb_.AddOffset(14, inventory); }
|
||||
void add_color(int8_t color) { fbb_.AddElement<int8_t>(16, color, 8); }
|
||||
void add_test_type(uint8_t test_type) { fbb_.AddElement<uint8_t>(18, test_type, 0); }
|
||||
void add_color(Color color) { fbb_.AddElement<int8_t>(16, static_cast<int8_t>(color), 8); }
|
||||
void add_test_type(Any test_type) { fbb_.AddElement<uint8_t>(18, static_cast<uint8_t>(test_type), 0); }
|
||||
void add_test(flatbuffers::Offset<void> test) { fbb_.AddOffset(20, test); }
|
||||
void add_test4(flatbuffers::Offset<flatbuffers::Vector<const Test *>> test4) { fbb_.AddOffset(22, test4); }
|
||||
void add_testarrayofstring(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> testarrayofstring) { fbb_.AddOffset(24, testarrayofstring); }
|
||||
@@ -165,8 +165,8 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
int16_t hp = 100,
|
||||
flatbuffers::Offset<flatbuffers::String> name = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory = 0,
|
||||
int8_t color = 8,
|
||||
uint8_t test_type = 0,
|
||||
Color color = Color_Blue,
|
||||
Any test_type = Any_NONE,
|
||||
flatbuffers::Offset<void> test = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<const Test *>> test4 = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> testarrayofstring = 0,
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
y: 2,
|
||||
z: 3,
|
||||
test1: 3,
|
||||
test2: 4,
|
||||
test2: Green,
|
||||
test3: {
|
||||
a: 5,
|
||||
b: 6
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
y: 2,
|
||||
z: 3,
|
||||
test1: 3,
|
||||
test2: 4,
|
||||
test2: Green,
|
||||
test3: {
|
||||
a: 5,
|
||||
b: 6
|
||||
|
||||
Binary file not shown.
@@ -63,7 +63,7 @@ void lcg_reset() { lcg_seed = 48271; }
|
||||
std::string CreateFlatBufferTest() {
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
auto vec = Vec3(1, 2, 3, 0, 0, Test(10, 20));
|
||||
auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
|
||||
|
||||
auto name = builder.CreateString("MyMonster");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user