Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions include/flatbuffers/reflection_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can we not just make this the only version? I believe const char * passed into a char * will work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a great suggestion. Will implement.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const char * passed to char * generally won't work?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant to write it the other way around.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew what you meant :) it just depends on if the argument match considers it close enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, no, this is the root of the issue. the original version is a const char* only -- the issue is that a const char* maps onto the templated version template<typename StringType> int KeyCompareWithValue(const StringType&)

So the two options are either

  1. providing an explicit overload for char*, or
  2. adding SFINAE conditions to the template deduction (which is a bit gross in C++11)

return KeyCompareWithValue(static_cast<const char *>(_key));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _key) const {
if (key()->c_str() < _key) return -1;
Expand Down Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down Expand Up @@ -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<const char *>(_filename));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _filename) const {
if (filename()->c_str() < _filename) return -1;
Expand Down
6 changes: 6 additions & 0 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char "
"*>(_{{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<typename StringType>";
Expand Down
3 changes: 3 additions & 0 deletions tests/cpp17/generated_cpp17/monster_test_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down
3 changes: 3 additions & 0 deletions tests/key_field/key_field_sample_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *>(_c));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _c) const {
if (c()->c_str() < _c) return -1;
Expand Down
22 changes: 22 additions & 0 deletions tests/key_field_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Baz> bazs;
Expand Down
1 change: 1 addition & 0 deletions tests/key_field_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace flatbuffers {
namespace tests {

void CallLookupByKeyOnString();
void FixedSizedScalarKeyInStructTest();
void StructKeyInStructTest();
void NestedStructKeyInStructTest();
Expand Down
3 changes: 3 additions & 0 deletions tests/monster_test_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down
3 changes: 3 additions & 0 deletions tests/monster_test_suffix/monster_test_suffix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *>(_name));
}
template<typename StringType>
int KeyCompareWithValue(const StringType& _name) const {
if (name()->c_str() < _name) return -1;
Expand Down
1 change: 1 addition & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,7 @@ int FlatBufferTests(const std::string& tests_data_path) {
JsonUnsortedArrayTest();
VectorSpanTest();
NativeInlineTableVectorTest();
CallLookupByKeyOnString();
FixedSizedScalarKeyInStructTest();
StructKeyInStructTest();
NestedStructKeyInStructTest();
Expand Down
Loading