diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index 32ee1aa6bec..5b2108b4050 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -275,6 +275,9 @@ struct KeyValue FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_key) const { return strcmp(key()->c_str(), _key); } + int KeyCompareWithValue(char *_key) const { + return KeyCompareWithValue(static_cast(_key)); + } template int KeyCompareWithValue(const StringType& _key) const { if (key()->c_str() < _key) return -1; @@ -473,6 +476,9 @@ struct Enum FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -632,6 +638,9 @@ struct Field FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -857,6 +866,9 @@ struct Object FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -1016,6 +1028,9 @@ struct RPCCall FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -1139,6 +1154,9 @@ struct Service FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -1265,6 +1283,9 @@ struct SchemaFile FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_filename) const { return strcmp(filename()->c_str(), _filename); } + int KeyCompareWithValue(char *_filename) const { + return KeyCompareWithValue(static_cast(_filename)); + } template int KeyCompareWithValue(const StringType& _filename) const { if (filename()->c_str() < _filename) return -1; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 51bb8204901..5bf70e5f25d 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2608,6 +2608,12 @@ class CppGenerator : public BaseGenerator { code_ += " int KeyCompareWithValue(const char *_{{FIELD_NAME}}) const {"; code_ += " return strcmp({{FIELD_NAME}}()->c_str(), _{{FIELD_NAME}});"; code_ += " }"; + // Crate an explicit overload for char*. + code_ += " int KeyCompareWithValue(char *_{{FIELD_NAME}}) const {"; + code_ += + " return KeyCompareWithValue(static_cast(_{{FIELD_NAME}}));"; + code_ += " }"; // Compares key against any string-like object (e.g. std::string_view or // std::string) that implements operator< comparison with const char*. code_ += " template"; diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index bb6efe1b938..1224df67fad 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -1447,6 +1447,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/key_field/key_field_sample_generated.h b/tests/key_field/key_field_sample_generated.h index 4a9a4d45a3b..0872896ac28 100644 --- a/tests/key_field/key_field_sample_generated.h +++ b/tests/key_field/key_field_sample_generated.h @@ -598,6 +598,9 @@ struct FooTable FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_c) const { return strcmp(c()->c_str(), _c); } + int KeyCompareWithValue(char *_c) const { + return KeyCompareWithValue(static_cast(_c)); + } template int KeyCompareWithValue(const StringType& _c) const { if (c()->c_str() < _c) return -1; diff --git a/tests/key_field_test.cpp b/tests/key_field_test.cpp index db5a0d5d3c0..7bbc758ac9e 100644 --- a/tests/key_field_test.cpp +++ b/tests/key_field_test.cpp @@ -12,6 +12,28 @@ namespace tests { using namespace keyfield::sample; +void CallLookupByKeyOnString() { + flatbuffers::FlatBufferBuilder fbb; + const auto foo = keyfield::sample::CreateFooTable( + fbb, 1, 2, fbb.CreateString("TEST_STRING")); + fbb.Finish(foo); + uint8_t* buf = fbb.GetBufferPointer(); + auto foo_table = GetFooTable(buf); + + TEST_EQ(foo_table->KeyCompareWithValue("TEST_STRING"), 0); + TEST_NE(foo_table->KeyCompareWithValue("AAA"), 0); + + // now compare with std::string + TEST_EQ(foo_table->KeyCompareWithValue(std::string{"TEST_STRING"}), 0); + TEST_NE(foo_table->KeyCompareWithValue(std::string{"AAA"}), 0); + + // now compare with a mutable char* + char mutable_str[] = "TEST_STRING"; + TEST_EQ(foo_table->KeyCompareWithValue(mutable_str), 0); + char mutable_str2[] = "AAA"; + TEST_NE(foo_table->KeyCompareWithValue(mutable_str2), 0); +} + void FixedSizedScalarKeyInStructTest() { flatbuffers::FlatBufferBuilder fbb; std::vector bazs; diff --git a/tests/key_field_test.h b/tests/key_field_test.h index be335a4f2d5..f3730da7ece 100644 --- a/tests/key_field_test.h +++ b/tests/key_field_test.h @@ -4,6 +4,7 @@ namespace flatbuffers { namespace tests { +void CallLookupByKeyOnString(); void FixedSizedScalarKeyInStructTest(); void StructKeyInStructTest(); void NestedStructKeyInStructTest(); diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 7c994e5c160..7e0748cc4b3 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -1463,6 +1463,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/monster_test_suffix/ext_only/monster_test_generated.hpp b/tests/monster_test_suffix/ext_only/monster_test_generated.hpp index 91c5e32f174..06933d2c9b3 100644 --- a/tests/monster_test_suffix/ext_only/monster_test_generated.hpp +++ b/tests/monster_test_suffix/ext_only/monster_test_generated.hpp @@ -1454,6 +1454,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h b/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h index 91c5e32f174..06933d2c9b3 100644 --- a/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h +++ b/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h @@ -1454,6 +1454,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/monster_test_suffix/monster_test_suffix.hpp b/tests/monster_test_suffix/monster_test_suffix.hpp index 91c5e32f174..06933d2c9b3 100644 --- a/tests/monster_test_suffix/monster_test_suffix.hpp +++ b/tests/monster_test_suffix/monster_test_suffix.hpp @@ -1454,6 +1454,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/test.cpp b/tests/test.cpp index c94d21592f7..3122df9270f 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1829,6 +1829,7 @@ int FlatBufferTests(const std::string& tests_data_path) { JsonUnsortedArrayTest(); VectorSpanTest(); NativeInlineTableVectorTest(); + CallLookupByKeyOnString(); FixedSizedScalarKeyInStructTest(); StructKeyInStructTest(); NestedStructKeyInStructTest();