mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-27 17:18:07 +00:00
[C++] Add mutable version of LookupByKey and test (#6826)
* [C++] Add mutable version of LookupByKey and test This adds an overload of LookupByKey to allow lookup in sorted Vectors to return a mutable instance of the object (or nullptr if not found). * Fix naming
This commit is contained in:
@@ -376,6 +376,10 @@ template<typename T> class Vector {
|
|||||||
return IndirectHelper<T>::Read(element, 0);
|
return IndirectHelper<T>::Read(element, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename K> mutable_return_type MutableLookupByKey(K key) {
|
||||||
|
return const_cast<mutable_return_type>(LookupByKey(key));
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// This class is only used to access pre-existing data. Don't ever
|
// This class is only used to access pre-existing data. Don't ever
|
||||||
// try to construct these manually.
|
// try to construct these manually.
|
||||||
|
|||||||
@@ -500,6 +500,15 @@ void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
|
|||||||
TEST_EQ(first->hp(), 0);
|
TEST_EQ(first->hp(), 0);
|
||||||
first->mutate_hp(1000);
|
first->mutate_hp(1000);
|
||||||
|
|
||||||
|
// Mutate via LookupByKey
|
||||||
|
TEST_NOTNULL(tables->MutableLookupByKey("Barney"));
|
||||||
|
TEST_EQ(static_cast<Monster *>(nullptr),
|
||||||
|
tables->MutableLookupByKey("DoesntExist"));
|
||||||
|
TEST_EQ(tables->MutableLookupByKey("Barney")->hp(), 1000);
|
||||||
|
TEST_EQ(tables->MutableLookupByKey("Barney")->mutate_hp(0), true);
|
||||||
|
TEST_EQ(tables->LookupByKey("Barney")->hp(), 0);
|
||||||
|
TEST_EQ(tables->MutableLookupByKey("Barney")->mutate_hp(1000), true);
|
||||||
|
|
||||||
// Run the verifier and the regular test to make sure we didn't trample on
|
// Run the verifier and the regular test to make sure we didn't trample on
|
||||||
// anything.
|
// anything.
|
||||||
AccessFlatBufferTest(flatbuf, length);
|
AccessFlatBufferTest(flatbuf, length);
|
||||||
|
|||||||
Reference in New Issue
Block a user