forked from BigfootDev/flatbuffers
Document requirement for custom string types to implement empty() and be constructible from std::string.
Add new option --cpp-str-flex-ctor to construct custom string types not via std::string, but (char * + length).
This commit is contained in:
@@ -100,14 +100,18 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
|
||||
" --gen-compare Generate operator== for object-based API types.\n"
|
||||
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
||||
" --gen-generated Add @Generated annotation for Java\n"
|
||||
" --gen-all Generate not just code for the current schema files,\n"
|
||||
" --gen-all Generate not just code for the current schema files,\n"
|
||||
" but for all files it includes as well.\n"
|
||||
" If the language uses a single file for output (by default\n"
|
||||
" the case for C++ and JS), all code will end up in this one\n"
|
||||
" file.\n"
|
||||
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr).\n"
|
||||
" --cpp-str-type T Set object API string type (default std::string).\n"
|
||||
" T::c_str() and T::length() must be supported.\n"
|
||||
" T::c_str(), T::length() and T::empty() must be supported.\n"
|
||||
" The custom type also needs to be constructible from std::string\n"
|
||||
" (see the --cpp-str-flex-ctor option to change this behavior).\n"
|
||||
" --cpp-str-flex-ctor Don't construct custom string types by passing std::string\n"
|
||||
" from Flatbuffers, but (char* + length).\n"
|
||||
" --object-prefix Customise class prefix for C++ object-based API.\n"
|
||||
" --object-suffix Customise class suffix for C++ object-based API.\n"
|
||||
" Default value is \"T\".\n"
|
||||
@@ -247,6 +251,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
||||
} else if (arg == "--cpp-str-type") {
|
||||
if (++argi >= argc) Error("missing type following" + arg, true);
|
||||
opts.cpp_object_api_string_type = argv[argi];
|
||||
} else if (arg == "--cpp-str-flex-ctor") {
|
||||
opts.cpp_object_api_string_flexible_constructor = true;
|
||||
} else if (arg == "--gen-nullable") {
|
||||
opts.gen_nullable = true;
|
||||
} else if (arg == "--gen-generated") {
|
||||
|
||||
@@ -578,6 +578,12 @@ class CppGenerator : public BaseGenerator {
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool FlexibleStringConstructor(const FieldDef *field) {
|
||||
auto attr = field ? (field->attributes.Lookup("cpp_str_flex_ctor") != nullptr) : false;
|
||||
auto ret = attr ? attr : parser_.opts.cpp_object_api_string_flexible_constructor;
|
||||
return (ret && NativeString(field) != "std::string"); // Only for custom string types.
|
||||
}
|
||||
|
||||
std::string GenTypeNativePtr(const std::string &type, const FieldDef *field,
|
||||
bool is_constructor) {
|
||||
auto &ptr_type = PtrType(field);
|
||||
@@ -2138,7 +2144,11 @@ class CppGenerator : public BaseGenerator {
|
||||
bool invector, const FieldDef &afield) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING: {
|
||||
return val + "->str()";
|
||||
if (FlexibleStringConstructor(&afield)) {
|
||||
return NativeString(&afield) + "(" + val + "->c_str(), " + val + "->size())";
|
||||
} else {
|
||||
return val + "->str()";
|
||||
}
|
||||
}
|
||||
case BASE_TYPE_STRUCT: {
|
||||
const auto name = WrapInNameSpace(*type.struct_def);
|
||||
@@ -2169,7 +2179,7 @@ class CppGenerator : public BaseGenerator {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
std::string GenUnpackFieldStatement(const FieldDef &field,
|
||||
const FieldDef *union_field) {
|
||||
|
||||
Reference in New Issue
Block a user