mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-24 07:31:48 +00:00
Support binary search for struct in cpp (#4245)
* Support binary search for struct in cpp CreateVectorOfSortedStruct is provided for convenience. * fix continuous-integration error * add generated files * compile Ability.cs in csharp test * compile Ability.cs in csharp * modify according to code review
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
89041a1686
commit
a5cc2092a6
@@ -23,6 +23,8 @@ struct TestSimpleTableWithEnumT;
|
||||
|
||||
struct Vec3;
|
||||
|
||||
struct Ability;
|
||||
|
||||
struct Stat;
|
||||
struct StatT;
|
||||
|
||||
@@ -247,6 +249,44 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS {
|
||||
};
|
||||
STRUCT_END(Vec3, 32);
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
|
||||
private:
|
||||
uint32_t id_;
|
||||
uint32_t distance_;
|
||||
|
||||
public:
|
||||
Ability() {
|
||||
memset(this, 0, sizeof(Ability));
|
||||
}
|
||||
Ability(const Ability &_o) {
|
||||
memcpy(this, &_o, sizeof(Ability));
|
||||
}
|
||||
Ability(uint32_t _id, uint32_t _distance)
|
||||
: id_(flatbuffers::EndianScalar(_id)),
|
||||
distance_(flatbuffers::EndianScalar(_distance)) {
|
||||
}
|
||||
uint32_t id() const {
|
||||
return flatbuffers::EndianScalar(id_);
|
||||
}
|
||||
void mutate_id(uint32_t _id) {
|
||||
flatbuffers::WriteScalar(&id_, _id);
|
||||
}
|
||||
bool KeyCompareLessThan(const Ability *o) const {
|
||||
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);
|
||||
}
|
||||
uint32_t distance() const {
|
||||
return flatbuffers::EndianScalar(distance_);
|
||||
}
|
||||
void mutate_distance(uint32_t _distance) {
|
||||
flatbuffers::WriteScalar(&distance_, _distance);
|
||||
}
|
||||
};
|
||||
STRUCT_END(Ability, 8);
|
||||
|
||||
} // namespace Example
|
||||
|
||||
namespace Example2 {
|
||||
@@ -480,6 +520,7 @@ struct MonsterT : public flatbuffers::NativeTable {
|
||||
float testf2;
|
||||
float testf3;
|
||||
std::vector<std::string> testarrayofstring2;
|
||||
std::vector<Ability> testarrayofsortedstruct;
|
||||
MonsterT()
|
||||
: mana(150),
|
||||
hp(100),
|
||||
@@ -530,7 +571,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
VT_TESTF = 54,
|
||||
VT_TESTF2 = 56,
|
||||
VT_TESTF3 = 58,
|
||||
VT_TESTARRAYOFSTRING2 = 60
|
||||
VT_TESTARRAYOFSTRING2 = 60,
|
||||
VT_TESTARRAYOFSORTEDSTRUCT = 62
|
||||
};
|
||||
const Vec3 *pos() const {
|
||||
return GetStruct<const Vec3 *>(VT_POS);
|
||||
@@ -722,6 +764,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *mutable_testarrayofstring2() {
|
||||
return GetPointer<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *>(VT_TESTARRAYOFSTRING2);
|
||||
}
|
||||
const flatbuffers::Vector<const Ability *> *testarrayofsortedstruct() const {
|
||||
return GetPointer<const flatbuffers::Vector<const Ability *> *>(VT_TESTARRAYOFSORTEDSTRUCT);
|
||||
}
|
||||
flatbuffers::Vector<const Ability *> *mutable_testarrayofsortedstruct() {
|
||||
return GetPointer<flatbuffers::Vector<const Ability *> *>(VT_TESTARRAYOFSORTEDSTRUCT);
|
||||
}
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<Vec3>(verifier, VT_POS) &&
|
||||
@@ -766,6 +814,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, VT_TESTARRAYOFSTRING2) &&
|
||||
verifier.Verify(testarrayofstring2()) &&
|
||||
verifier.VerifyVectorOfStrings(testarrayofstring2()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, VT_TESTARRAYOFSORTEDSTRUCT) &&
|
||||
verifier.Verify(testarrayofsortedstruct()) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
MonsterT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const;
|
||||
@@ -872,13 +922,16 @@ struct MonsterBuilder {
|
||||
void add_testarrayofstring2(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> testarrayofstring2) {
|
||||
fbb_.AddOffset(Monster::VT_TESTARRAYOFSTRING2, testarrayofstring2);
|
||||
}
|
||||
void add_testarrayofsortedstruct(flatbuffers::Offset<flatbuffers::Vector<const Ability *>> testarrayofsortedstruct) {
|
||||
fbb_.AddOffset(Monster::VT_TESTARRAYOFSORTEDSTRUCT, testarrayofsortedstruct);
|
||||
}
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb)
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
MonsterBuilder &operator=(const MonsterBuilder &);
|
||||
flatbuffers::Offset<Monster> Finish() {
|
||||
const auto end = fbb_.EndTable(start_, 29);
|
||||
const auto end = fbb_.EndTable(start_, 30);
|
||||
auto o = flatbuffers::Offset<Monster>(end);
|
||||
fbb_.Required(o, Monster::VT_NAME);
|
||||
return o;
|
||||
@@ -914,12 +967,14 @@ inline flatbuffers::Offset<Monster> CreateMonster(
|
||||
float testf = 3.14159f,
|
||||
float testf2 = 3.0f,
|
||||
float testf3 = 0.0f,
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> testarrayofstring2 = 0) {
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> testarrayofstring2 = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<const Ability *>> testarrayofsortedstruct = 0) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_testhashu64_fnv1a(testhashu64_fnv1a);
|
||||
builder_.add_testhashs64_fnv1a(testhashs64_fnv1a);
|
||||
builder_.add_testhashu64_fnv1(testhashu64_fnv1);
|
||||
builder_.add_testhashs64_fnv1(testhashs64_fnv1);
|
||||
builder_.add_testarrayofsortedstruct(testarrayofsortedstruct);
|
||||
builder_.add_testarrayofstring2(testarrayofstring2);
|
||||
builder_.add_testf3(testf3);
|
||||
builder_.add_testf2(testf2);
|
||||
@@ -976,7 +1031,8 @@ inline flatbuffers::Offset<Monster> CreateMonsterDirect(
|
||||
float testf = 3.14159f,
|
||||
float testf2 = 3.0f,
|
||||
float testf3 = 0.0f,
|
||||
const std::vector<flatbuffers::Offset<flatbuffers::String>> *testarrayofstring2 = nullptr) {
|
||||
const std::vector<flatbuffers::Offset<flatbuffers::String>> *testarrayofstring2 = nullptr,
|
||||
const std::vector<const Ability *> *testarrayofsortedstruct = nullptr) {
|
||||
return MyGame::Example::CreateMonster(
|
||||
_fbb,
|
||||
pos,
|
||||
@@ -1006,7 +1062,8 @@ inline flatbuffers::Offset<Monster> CreateMonsterDirect(
|
||||
testf,
|
||||
testf2,
|
||||
testf3,
|
||||
testarrayofstring2 ? _fbb.CreateVector<flatbuffers::Offset<flatbuffers::String>>(*testarrayofstring2) : 0);
|
||||
testarrayofstring2 ? _fbb.CreateVector<flatbuffers::Offset<flatbuffers::String>>(*testarrayofstring2) : 0,
|
||||
testarrayofsortedstruct ? _fbb.CreateVector<const Ability *>(*testarrayofsortedstruct) : 0);
|
||||
}
|
||||
|
||||
flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
|
||||
@@ -1134,6 +1191,7 @@ inline void Monster::UnPackTo(MonsterT *_o, const flatbuffers::resolver_function
|
||||
{ auto _e = testf2(); _o->testf2 = _e; };
|
||||
{ auto _e = testf3(); _o->testf3 = _e; };
|
||||
{ auto _e = testarrayofstring2(); if (_e) { _o->testarrayofstring2.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayofstring2[_i] = _e->Get(_i)->str(); } } };
|
||||
{ auto _e = testarrayofsortedstruct(); if (_e) { _o->testarrayofsortedstruct.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayofsortedstruct[_i] = *_e->Get(_i); } } };
|
||||
}
|
||||
|
||||
inline flatbuffers::Offset<Monster> Monster::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher) {
|
||||
@@ -1171,6 +1229,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
auto _testf2 = _o->testf2;
|
||||
auto _testf3 = _o->testf3;
|
||||
auto _testarrayofstring2 = _o->testarrayofstring2.size() ? _fbb.CreateVectorOfStrings(_o->testarrayofstring2) : 0;
|
||||
auto _testarrayofsortedstruct = _o->testarrayofsortedstruct.size() ? _fbb.CreateVectorOfStructs(_o->testarrayofsortedstruct) : 0;
|
||||
return MyGame::Example::CreateMonster(
|
||||
_fbb,
|
||||
_pos,
|
||||
@@ -1200,7 +1259,8 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
_testf,
|
||||
_testf2,
|
||||
_testf3,
|
||||
_testarrayofstring2);
|
||||
_testarrayofstring2,
|
||||
_testarrayofsortedstruct);
|
||||
}
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type) {
|
||||
|
||||
Reference in New Issue
Block a user