mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
* Issue #4799 fixed. Generator for KeyCompareWithValue is extracted. * format fix
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
f19803d364
commit
8ea293b988
@@ -1535,6 +1535,41 @@ class CppGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
// Generate CompareWithValue method for a key field.
|
||||
void GenKeyFieldMethods(const FieldDef &field) {
|
||||
FLATBUFFERS_ASSERT(field.key);
|
||||
const bool is_string = (field.value.type.base_type == BASE_TYPE_STRING);
|
||||
|
||||
code_ += " bool KeyCompareLessThan(const {{STRUCT_NAME}} *o) const {";
|
||||
if (is_string) {
|
||||
// use operator< of flatbuffers::String
|
||||
code_ += " return *{{FIELD_NAME}}() < *o->{{FIELD_NAME}}();";
|
||||
} else {
|
||||
code_ += " return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();";
|
||||
}
|
||||
code_ += " }";
|
||||
|
||||
if (is_string) {
|
||||
code_ += " int KeyCompareWithValue(const char *val) const {";
|
||||
code_ += " return strcmp({{FIELD_NAME}}()->c_str(), val);";
|
||||
code_ += " }";
|
||||
} else {
|
||||
FLATBUFFERS_ASSERT(IsScalar(field.value.type.base_type));
|
||||
auto type = GenTypeBasic(field.value.type, false);
|
||||
if (parser_.opts.scoped_enums && field.value.type.enum_def &&
|
||||
IsScalar(field.value.type.base_type)) {
|
||||
type = GenTypeGet(field.value.type, " ", "const ", " *", true);
|
||||
}
|
||||
// Returns {field<val: -1, field==val: 0, field>val: +1}.
|
||||
code_.SetValue("KEY_TYPE", type);
|
||||
code_ += " int KeyCompareWithValue({{KEY_TYPE}} val) const {";
|
||||
code_ +=
|
||||
" return static_cast<int>({{FIELD_NAME}}() > val) - "
|
||||
"static_cast<int>({{FIELD_NAME}}() < val);";
|
||||
code_ += " }";
|
||||
}
|
||||
}
|
||||
|
||||
// Generate an accessor struct, builder structs & function for a table.
|
||||
void GenTable(const StructDef &struct_def) {
|
||||
if (parser_.opts.generate_object_based_api) { GenNativeTable(struct_def); }
|
||||
@@ -1712,46 +1747,16 @@ class CppGenerator : public BaseGenerator {
|
||||
code_ +=
|
||||
" flexbuffers::Reference {{FIELD_NAME}}_flexbuffer_root()"
|
||||
" const {";
|
||||
code_ += " auto v = {{FIELD_NAME}}();";
|
||||
code_ += " return flexbuffers::GetRoot(v->Data(), v->size());";
|
||||
// Both Data() and size() are const-methods, therefore call order doesn't matter.
|
||||
code_ +=
|
||||
" return flexbuffers::GetRoot({{FIELD_NAME}}()->Data(), "
|
||||
"{{FIELD_NAME}}()->size());";
|
||||
code_ += " }";
|
||||
}
|
||||
|
||||
// Generate a comparison function for this field if it is a key.
|
||||
if (field.key) {
|
||||
const bool is_string = (field.value.type.base_type == BASE_TYPE_STRING);
|
||||
|
||||
code_ += " bool KeyCompareLessThan(const {{STRUCT_NAME}} *o) const {";
|
||||
if (is_string) {
|
||||
code_ += " return *{{FIELD_NAME}}() < *o->{{FIELD_NAME}}();";
|
||||
} else {
|
||||
code_ += " return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();";
|
||||
}
|
||||
code_ += " }";
|
||||
|
||||
if (is_string) {
|
||||
code_ += " int KeyCompareWithValue(const char *val) const {";
|
||||
code_ += " return strcmp({{FIELD_NAME}}()->c_str(), val);";
|
||||
code_ += " }";
|
||||
} else {
|
||||
auto type = GenTypeBasic(field.value.type, false);
|
||||
if (parser_.opts.scoped_enums && field.value.type.enum_def &&
|
||||
IsScalar(field.value.type.base_type)) {
|
||||
type = GenTypeGet(field.value.type, " ", "const ", " *", true);
|
||||
}
|
||||
|
||||
code_.SetValue("KEY_TYPE", type);
|
||||
code_ += " int KeyCompareWithValue({{KEY_TYPE}} val) const {";
|
||||
code_ += " const auto key = {{FIELD_NAME}}();";
|
||||
code_ += " if (key < val) {";
|
||||
code_ += " return -1;";
|
||||
code_ += " } else if (key > val) {";
|
||||
code_ += " return 1;";
|
||||
code_ += " } else {";
|
||||
code_ += " return 0;";
|
||||
code_ += " }";
|
||||
code_ += " }";
|
||||
}
|
||||
GenKeyFieldMethods(field);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2558,22 +2563,7 @@ class CppGenerator : public BaseGenerator {
|
||||
|
||||
// Generate a comparison function for this field if it is a key.
|
||||
if (field.key) {
|
||||
code_ += " bool KeyCompareLessThan(const {{STRUCT_NAME}} *o) const {";
|
||||
code_ += " return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();";
|
||||
code_ += " }";
|
||||
auto type = GenTypeBasic(field.value.type, false);
|
||||
if (parser_.opts.scoped_enums && field.value.type.enum_def &&
|
||||
IsScalar(field.value.type.base_type)) {
|
||||
type = GenTypeGet(field.value.type, " ", "const ", " *", true);
|
||||
}
|
||||
|
||||
code_.SetValue("KEY_TYPE", type);
|
||||
code_ += " int KeyCompareWithValue({{KEY_TYPE}} val) const {";
|
||||
code_ += " const auto key = {{FIELD_NAME}}();";
|
||||
code_ +=
|
||||
" return static_cast<int>(key > val) - static_cast<int>(key < "
|
||||
"val);";
|
||||
code_ += " }";
|
||||
GenKeyFieldMethods(field);
|
||||
}
|
||||
}
|
||||
code_.SetValue("NATIVE_NAME", Name(struct_def));
|
||||
|
||||
@@ -30,14 +30,14 @@ func NewMonsterStorageClient(cc *grpc.ClientConn) MonsterStorageClient {
|
||||
func (c *monsterStorageClient) Store(ctx context.Context, in *flatbuffers.Builder,
|
||||
opts... grpc.CallOption) (* Stat, error) {
|
||||
out := new(Stat)
|
||||
err := grpc.Invoke(ctx, "/Example.MonsterStorage/Store", in, out, c.cc, opts...)
|
||||
err := grpc.Invoke(ctx, "/MyGame.Example.MonsterStorage/Store", in, out, c.cc, opts...)
|
||||
if err != nil { return nil, err }
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *monsterStorageClient) Retrieve(ctx context.Context, in *flatbuffers.Builder,
|
||||
opts... grpc.CallOption) (MonsterStorage_RetrieveClient, error) {
|
||||
stream, err := grpc.NewClientStream(ctx, &_MonsterStorage_serviceDesc.Streams[0], c.cc, "/Example.MonsterStorage/Retrieve", opts...)
|
||||
stream, err := grpc.NewClientStream(ctx, &_MonsterStorage_serviceDesc.Streams[0], c.cc, "/MyGame.Example.MonsterStorage/Retrieve", opts...)
|
||||
if err != nil { return nil, err }
|
||||
x := &monsterStorageRetrieveClient{stream}
|
||||
if err := x.ClientStream.SendMsg(in); err != nil { return nil, err }
|
||||
@@ -77,7 +77,7 @@ func _MonsterStorage_Store_Handler(srv interface{}, ctx context.Context,
|
||||
if interceptor == nil { return srv.(MonsterStorageServer).Store(ctx, in) }
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/Example.MonsterStorage/Store",
|
||||
FullMethod: "/MyGame.Example.MonsterStorage/Store",
|
||||
}
|
||||
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
@@ -108,7 +108,7 @@ func (x *monsterStorageRetrieveServer) Send(m *flatbuffers.Builder) error {
|
||||
|
||||
|
||||
var _MonsterStorage_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "Example.MonsterStorage",
|
||||
ServiceName: "MyGame.Example.MonsterStorage",
|
||||
HandlerType: (*MonsterStorageServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
|
||||
Binary file not shown.
@@ -341,8 +341,7 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
|
||||
return id() < o->id();
|
||||
}
|
||||
int KeyCompareWithValue(uint32_t val) const {
|
||||
const auto key = id();
|
||||
return static_cast<int>(key > val) - static_cast<int>(key < val);
|
||||
return static_cast<int>(id() > val) - static_cast<int>(id() < val);
|
||||
}
|
||||
uint32_t distance() const {
|
||||
return flatbuffers::EndianScalar(distance_);
|
||||
@@ -635,14 +634,7 @@ struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
return id() < o->id();
|
||||
}
|
||||
int KeyCompareWithValue(uint64_t val) const {
|
||||
const auto key = id();
|
||||
if (key < val) {
|
||||
return -1;
|
||||
} else if (key > val) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return static_cast<int>(id() > val) - static_cast<int>(id() < val);
|
||||
}
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
@@ -999,8 +991,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_FLEX);
|
||||
}
|
||||
flexbuffers::Reference flex_flexbuffer_root() const {
|
||||
auto v = flex();
|
||||
return flexbuffers::GetRoot(v->Data(), v->size());
|
||||
return flexbuffers::GetRoot(flex()->Data(), flex()->size());
|
||||
}
|
||||
const flatbuffers::Vector<const Test *> *test5() const {
|
||||
return GetPointer<const flatbuffers::Vector<const Test *> *>(VT_TEST5);
|
||||
|
||||
@@ -14,7 +14,7 @@ class Color {
|
||||
const Color._(this.value);
|
||||
|
||||
factory Color.fromValue(int value) {
|
||||
if (value == null) return null;
|
||||
if (value == null) value = 0;
|
||||
if (!values.containsKey(value)) {
|
||||
throw new StateError('Invalid value $value for bit flag enum Color');
|
||||
}
|
||||
@@ -52,7 +52,7 @@ class AnyTypeId {
|
||||
const AnyTypeId._(this.value);
|
||||
|
||||
factory AnyTypeId.fromValue(int value) {
|
||||
if (value == null) return null;
|
||||
if (value == null) value = 0;
|
||||
if (!values.containsKey(value)) {
|
||||
throw new StateError('Invalid value $value for bit flag enum AnyTypeId');
|
||||
}
|
||||
@@ -428,8 +428,8 @@ class Stat {
|
||||
final int _bcOffset;
|
||||
|
||||
String get id => const fb.StringReader().vTableGet(_bc, _bcOffset, 4, null);
|
||||
int get val => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 6, null);
|
||||
int get count => const fb.Uint16Reader().vTableGet(_bc, _bcOffset, 8, null);
|
||||
int get val => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 6, 0);
|
||||
int get count => const fb.Uint16Reader().vTableGet(_bc, _bcOffset, 8, 0);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -524,7 +524,7 @@ class Referrable {
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
int get id => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 4, null);
|
||||
int get id => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 4, 0);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -607,7 +607,7 @@ class Monster {
|
||||
String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 10, null);
|
||||
List<int> get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 14, null);
|
||||
Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 8));
|
||||
AnyTypeId get testType => new AnyTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 18, null));
|
||||
AnyTypeId get testType => new AnyTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 18, 0));
|
||||
dynamic get test {
|
||||
switch (testType?.value) {
|
||||
case 1: return Monster.reader.vTableGet(_bc, _bcOffset, 20, null);
|
||||
@@ -624,15 +624,15 @@ class Monster {
|
||||
Monster get enemy => Monster.reader.vTableGet(_bc, _bcOffset, 28, null);
|
||||
List<int> get testnestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 30, null);
|
||||
Stat get testempty => Stat.reader.vTableGet(_bc, _bcOffset, 32, null);
|
||||
bool get testbool => const fb.BoolReader().vTableGet(_bc, _bcOffset, 34, null);
|
||||
int get testhashs32Fnv1 => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 36, null);
|
||||
int get testhashu32Fnv1 => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 38, null);
|
||||
int get testhashs64Fnv1 => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 40, null);
|
||||
int get testhashu64Fnv1 => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 42, null);
|
||||
int get testhashs32Fnv1a => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 44, null);
|
||||
int get testhashu32Fnv1a => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 46, null);
|
||||
int get testhashs64Fnv1a => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 48, null);
|
||||
int get testhashu64Fnv1a => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 50, null);
|
||||
bool get testbool => const fb.BoolReader().vTableGet(_bc, _bcOffset, 34, false);
|
||||
int get testhashs32Fnv1 => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 36, 0);
|
||||
int get testhashu32Fnv1 => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 38, 0);
|
||||
int get testhashs64Fnv1 => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 40, 0);
|
||||
int get testhashu64Fnv1 => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 42, 0);
|
||||
int get testhashs32Fnv1a => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 44, 0);
|
||||
int get testhashu32Fnv1a => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 46, 0);
|
||||
int get testhashs64Fnv1a => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 48, 0);
|
||||
int get testhashu64Fnv1a => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 50, 0);
|
||||
List<bool> get testarrayofbools => const fb.ListReader<bool>(const fb.BoolReader()).vTableGet(_bc, _bcOffset, 52, null);
|
||||
double get testf => const fb.Float32Reader().vTableGet(_bc, _bcOffset, 54, 3.14159);
|
||||
double get testf2 => const fb.Float32Reader().vTableGet(_bc, _bcOffset, 56, 3.0);
|
||||
@@ -645,12 +645,12 @@ class Monster {
|
||||
List<double> get vectorOfDoubles => const fb.ListReader<double>(const fb.Float64Reader()).vTableGet(_bc, _bcOffset, 70, null);
|
||||
my_game.InParentNamespace get parentNamespaceTest => my_game.InParentNamespace.reader.vTableGet(_bc, _bcOffset, 72, null);
|
||||
List<Referrable> get vectorOfReferrables => const fb.ListReader<Referrable>(Referrable.reader).vTableGet(_bc, _bcOffset, 74, null);
|
||||
int get singleWeakReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 76, null);
|
||||
int get singleWeakReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 76, 0);
|
||||
List<int> get vectorOfWeakReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 78, null);
|
||||
List<Referrable> get vectorOfStrongReferrables => const fb.ListReader<Referrable>(Referrable.reader).vTableGet(_bc, _bcOffset, 80, null);
|
||||
int get coOwningReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 82, null);
|
||||
int get coOwningReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 82, 0);
|
||||
List<int> get vectorOfCoOwningReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 84, null);
|
||||
int get nonOwningReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 86, null);
|
||||
int get nonOwningReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 86, 0);
|
||||
List<int> get vectorOfNonOwningReferences => const fb.ListReader<int>(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 88, null);
|
||||
|
||||
@override
|
||||
@@ -1157,14 +1157,14 @@ class TypeAliases {
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
int get i8 => const fb.Int8Reader().vTableGet(_bc, _bcOffset, 4, null);
|
||||
int get u8 => const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 6, null);
|
||||
int get i16 => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, null);
|
||||
int get u16 => const fb.Uint16Reader().vTableGet(_bc, _bcOffset, 10, null);
|
||||
int get i32 => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 12, null);
|
||||
int get u32 => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 14, null);
|
||||
int get i64 => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 16, null);
|
||||
int get u64 => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 18, null);
|
||||
int get i8 => const fb.Int8Reader().vTableGet(_bc, _bcOffset, 4, 0);
|
||||
int get u8 => const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 6, 0);
|
||||
int get i16 => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, 0);
|
||||
int get u16 => const fb.Uint16Reader().vTableGet(_bc, _bcOffset, 10, 0);
|
||||
int get i32 => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 12, 0);
|
||||
int get u32 => const fb.Uint32Reader().vTableGet(_bc, _bcOffset, 14, 0);
|
||||
int get i64 => const fb.Int64Reader().vTableGet(_bc, _bcOffset, 16, 0);
|
||||
int get u64 => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 18, 0);
|
||||
double get f32 => const fb.Float32Reader().vTableGet(_bc, _bcOffset, 20, 0.0);
|
||||
double get f64 => const fb.Float64Reader().vTableGet(_bc, _bcOffset, 22, 0.0);
|
||||
List<int> get v8 => const fb.ListReader<int>(const fb.Int8Reader()).vTableGet(_bc, _bcOffset, 24, null);
|
||||
|
||||
@@ -12,7 +12,7 @@ class EnumInNestedNS {
|
||||
const EnumInNestedNS._(this.value);
|
||||
|
||||
factory EnumInNestedNS.fromValue(int value) {
|
||||
if (value == null) return null;
|
||||
if (value == null) value = 0;
|
||||
if (!values.containsKey(value)) {
|
||||
throw new StateError('Invalid value $value for bit flag enum EnumInNestedNS');
|
||||
}
|
||||
@@ -59,7 +59,7 @@ class TableInNestedNS {
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
int get foo => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 4, null);
|
||||
int get foo => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 4, 0);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
|
||||
@@ -21,7 +21,7 @@ class TableInFirstNS {
|
||||
final int _bcOffset;
|
||||
|
||||
namespace_a_namespace_b.TableInNestedNS get fooTable => namespace_a_namespace_b.TableInNestedNS.reader.vTableGet(_bc, _bcOffset, 4, null);
|
||||
EnumInNestedNS get fooEnum => new EnumInNestedNS.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 6, null));
|
||||
EnumInNestedNS get fooEnum => new EnumInNestedNS.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 6, 0));
|
||||
namespace_a_namespace_b.StructInNestedNS get fooStruct => namespace_a_namespace_b.StructInNestedNS.reader.vTableGet(_bc, _bcOffset, 8, null);
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user