mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 20:00:02 +00:00
Add support for using array of scalar as key field in Cpp (#7623)
* add support for using array of scalar as key field * update cmakelist and test.cpp to include the tests * update bazel rule * address comments * clang format * delete comment * delete comment * address the rest of the commnets * address comments * update naming in test file * format build file * buildifier * make keycomparelessthan call keycomparewithvalue * update to use flatbuffer array instead of raw pointer * clang * format * revert format * revert format * update * run generate_code.py * run code generator * revert changes by generate_code.py * fist run make flatc and then run generate_code.py Co-authored-by: Wen Sun <sunwen@google.com>
This commit is contained in:
21
tests/key_field/key_field_sample.fbs
Normal file
21
tests/key_field/key_field_sample.fbs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace keyfield.sample;
|
||||
|
||||
struct Baz {
|
||||
a: [uint8:4] (key); // A fixed-sized array of uint8 as a Key
|
||||
b: uint8 ;
|
||||
}
|
||||
|
||||
struct Bar {
|
||||
a: [float:3] (key); // A fixed-sized array of float as a Key
|
||||
b: uint8;
|
||||
}
|
||||
|
||||
table FooTable {
|
||||
a: int;
|
||||
b: int;
|
||||
c: string (key);
|
||||
d: [Baz];
|
||||
e: [Bar];
|
||||
}
|
||||
root_type FooTable;
|
||||
|
||||
260
tests/key_field/key_field_sample_generated.h
Normal file
260
tests/key_field/key_field_sample_generated.h
Normal file
@@ -0,0 +1,260 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
|
||||
#ifndef FLATBUFFERS_GENERATED_KEYFIELDSAMPLE_KEYFIELD_SAMPLE_H_
|
||||
#define FLATBUFFERS_GENERATED_KEYFIELDSAMPLE_KEYFIELD_SAMPLE_H_
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
// Ensure the included flatbuffers.h is the same version as when this file was
|
||||
// generated, otherwise it may not be compatible.
|
||||
static_assert(FLATBUFFERS_VERSION_MAJOR == 22 &&
|
||||
FLATBUFFERS_VERSION_MINOR == 10 &&
|
||||
FLATBUFFERS_VERSION_REVISION == 26,
|
||||
"Non-compatible flatbuffers version included");
|
||||
|
||||
namespace keyfield {
|
||||
namespace sample {
|
||||
|
||||
struct Baz;
|
||||
|
||||
struct Bar;
|
||||
|
||||
struct FooTable;
|
||||
struct FooTableBuilder;
|
||||
|
||||
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(1) Baz FLATBUFFERS_FINAL_CLASS {
|
||||
private:
|
||||
uint8_t a_[4];
|
||||
uint8_t b_;
|
||||
|
||||
public:
|
||||
Baz()
|
||||
: a_(),
|
||||
b_(0) {
|
||||
}
|
||||
Baz(uint8_t _b)
|
||||
: a_(),
|
||||
b_(flatbuffers::EndianScalar(_b)) {
|
||||
}
|
||||
Baz(flatbuffers::span<const uint8_t, 4> _a, uint8_t _b)
|
||||
: b_(flatbuffers::EndianScalar(_b)) {
|
||||
flatbuffers::CastToArray(a_).CopyFromSpan(_a);
|
||||
}
|
||||
const flatbuffers::Array<uint8_t, 4> *a() const {
|
||||
return &flatbuffers::CastToArray(a_);
|
||||
}
|
||||
bool KeyCompareLessThan(const Baz * const o) const {
|
||||
return KeyCompareWithValue(o->a()) < 0;
|
||||
}
|
||||
int KeyCompareWithValue(const flatbuffers::Array<uint8_t, 4> *_a) const {
|
||||
for (auto i = 0; i < a()->size(); i++) {
|
||||
const auto a_l = a_[i];
|
||||
const auto a_r = _a->Get(i);
|
||||
if(a_l != a_r)
|
||||
return static_cast<int>(a_l > a_r) - static_cast<int>(a_l < a_r);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
uint8_t b() const {
|
||||
return flatbuffers::EndianScalar(b_);
|
||||
}
|
||||
};
|
||||
FLATBUFFERS_STRUCT_END(Baz, 5);
|
||||
|
||||
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Bar FLATBUFFERS_FINAL_CLASS {
|
||||
private:
|
||||
float a_[3];
|
||||
uint8_t b_;
|
||||
int8_t padding0__; int16_t padding1__;
|
||||
|
||||
public:
|
||||
Bar()
|
||||
: a_(),
|
||||
b_(0),
|
||||
padding0__(0),
|
||||
padding1__(0) {
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
}
|
||||
Bar(uint8_t _b)
|
||||
: a_(),
|
||||
b_(flatbuffers::EndianScalar(_b)),
|
||||
padding0__(0),
|
||||
padding1__(0) {
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
}
|
||||
Bar(flatbuffers::span<const float, 3> _a, uint8_t _b)
|
||||
: b_(flatbuffers::EndianScalar(_b)),
|
||||
padding0__(0),
|
||||
padding1__(0) {
|
||||
flatbuffers::CastToArray(a_).CopyFromSpan(_a);
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
}
|
||||
const flatbuffers::Array<float, 3> *a() const {
|
||||
return &flatbuffers::CastToArray(a_);
|
||||
}
|
||||
bool KeyCompareLessThan(const Bar * const o) const {
|
||||
return KeyCompareWithValue(o->a()) < 0;
|
||||
}
|
||||
int KeyCompareWithValue(const flatbuffers::Array<float, 3> *_a) const {
|
||||
for (auto i = 0; i < a()->size(); i++) {
|
||||
const auto a_l = a_[i];
|
||||
const auto a_r = _a->Get(i);
|
||||
if(a_l != a_r)
|
||||
return static_cast<int>(a_l > a_r) - static_cast<int>(a_l < a_r);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
uint8_t b() const {
|
||||
return flatbuffers::EndianScalar(b_);
|
||||
}
|
||||
};
|
||||
FLATBUFFERS_STRUCT_END(Bar, 16);
|
||||
|
||||
struct FooTable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef FooTableBuilder Builder;
|
||||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
|
||||
VT_A = 4,
|
||||
VT_B = 6,
|
||||
VT_C = 8,
|
||||
VT_D = 10,
|
||||
VT_E = 12
|
||||
};
|
||||
int32_t a() const {
|
||||
return GetField<int32_t>(VT_A, 0);
|
||||
}
|
||||
int32_t b() const {
|
||||
return GetField<int32_t>(VT_B, 0);
|
||||
}
|
||||
const flatbuffers::String *c() const {
|
||||
return GetPointer<const flatbuffers::String *>(VT_C);
|
||||
}
|
||||
bool KeyCompareLessThan(const FooTable * const o) const {
|
||||
return *c() < *o->c();
|
||||
}
|
||||
int KeyCompareWithValue(const char *_c) const {
|
||||
return strcmp(c()->c_str(), _c);
|
||||
}
|
||||
const flatbuffers::Vector<const keyfield::sample::Baz *> *d() const {
|
||||
return GetPointer<const flatbuffers::Vector<const keyfield::sample::Baz *> *>(VT_D);
|
||||
}
|
||||
const flatbuffers::Vector<const keyfield::sample::Bar *> *e() const {
|
||||
return GetPointer<const flatbuffers::Vector<const keyfield::sample::Bar *> *>(VT_E);
|
||||
}
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<int32_t>(verifier, VT_A, 4) &&
|
||||
VerifyField<int32_t>(verifier, VT_B, 4) &&
|
||||
VerifyOffsetRequired(verifier, VT_C) &&
|
||||
verifier.VerifyString(c()) &&
|
||||
VerifyOffset(verifier, VT_D) &&
|
||||
verifier.VerifyVector(d()) &&
|
||||
VerifyOffset(verifier, VT_E) &&
|
||||
verifier.VerifyVector(e()) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
|
||||
struct FooTableBuilder {
|
||||
typedef FooTable Table;
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_a(int32_t a) {
|
||||
fbb_.AddElement<int32_t>(FooTable::VT_A, a, 0);
|
||||
}
|
||||
void add_b(int32_t b) {
|
||||
fbb_.AddElement<int32_t>(FooTable::VT_B, b, 0);
|
||||
}
|
||||
void add_c(flatbuffers::Offset<flatbuffers::String> c) {
|
||||
fbb_.AddOffset(FooTable::VT_C, c);
|
||||
}
|
||||
void add_d(flatbuffers::Offset<flatbuffers::Vector<const keyfield::sample::Baz *>> d) {
|
||||
fbb_.AddOffset(FooTable::VT_D, d);
|
||||
}
|
||||
void add_e(flatbuffers::Offset<flatbuffers::Vector<const keyfield::sample::Bar *>> e) {
|
||||
fbb_.AddOffset(FooTable::VT_E, e);
|
||||
}
|
||||
explicit FooTableBuilder(flatbuffers::FlatBufferBuilder &_fbb)
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
flatbuffers::Offset<FooTable> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<FooTable>(end);
|
||||
fbb_.Required(o, FooTable::VT_C);
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<FooTable> CreateFooTable(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
int32_t a = 0,
|
||||
int32_t b = 0,
|
||||
flatbuffers::Offset<flatbuffers::String> c = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<const keyfield::sample::Baz *>> d = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<const keyfield::sample::Bar *>> e = 0) {
|
||||
FooTableBuilder builder_(_fbb);
|
||||
builder_.add_e(e);
|
||||
builder_.add_d(d);
|
||||
builder_.add_c(c);
|
||||
builder_.add_b(b);
|
||||
builder_.add_a(a);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline flatbuffers::Offset<FooTable> CreateFooTableDirect(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
int32_t a = 0,
|
||||
int32_t b = 0,
|
||||
const char *c = nullptr,
|
||||
std::vector<keyfield::sample::Baz> *d = nullptr,
|
||||
std::vector<keyfield::sample::Bar> *e = nullptr) {
|
||||
auto c__ = c ? _fbb.CreateString(c) : 0;
|
||||
auto d__ = d ? _fbb.CreateVectorOfSortedStructs<keyfield::sample::Baz>(d) : 0;
|
||||
auto e__ = e ? _fbb.CreateVectorOfSortedStructs<keyfield::sample::Bar>(e) : 0;
|
||||
return keyfield::sample::CreateFooTable(
|
||||
_fbb,
|
||||
a,
|
||||
b,
|
||||
c__,
|
||||
d__,
|
||||
e__);
|
||||
}
|
||||
|
||||
inline const keyfield::sample::FooTable *GetFooTable(const void *buf) {
|
||||
return flatbuffers::GetRoot<keyfield::sample::FooTable>(buf);
|
||||
}
|
||||
|
||||
inline const keyfield::sample::FooTable *GetSizePrefixedFooTable(const void *buf) {
|
||||
return flatbuffers::GetSizePrefixedRoot<keyfield::sample::FooTable>(buf);
|
||||
}
|
||||
|
||||
inline bool VerifyFooTableBuffer(
|
||||
flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifyBuffer<keyfield::sample::FooTable>(nullptr);
|
||||
}
|
||||
|
||||
inline bool VerifySizePrefixedFooTableBuffer(
|
||||
flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifySizePrefixedBuffer<keyfield::sample::FooTable>(nullptr);
|
||||
}
|
||||
|
||||
inline void FinishFooTableBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<keyfield::sample::FooTable> root) {
|
||||
fbb.Finish(root);
|
||||
}
|
||||
|
||||
inline void FinishSizePrefixedFooTableBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<keyfield::sample::FooTable> root) {
|
||||
fbb.FinishSizePrefixed(root);
|
||||
}
|
||||
|
||||
} // namespace sample
|
||||
} // namespace keyfield
|
||||
|
||||
#endif // FLATBUFFERS_GENERATED_KEYFIELDSAMPLE_KEYFIELD_SAMPLE_H_
|
||||
Reference in New Issue
Block a user