CppUsage: address requested changes.

idl_gen_cpp.cpp: fix formatting, keep CreateVectorOfStrings for normal string cases.
This commit is contained in:
Luca Longinotti 2019-03-05 18:09:32 +01:00
parent 371d4e0b79
commit 2f0402f9ff
2 changed files with 16 additions and 12 deletions

View File

@ -270,8 +270,8 @@ custom string types; the `--cpp-str-flex-ctor` argument to `flatc` or the
per field attribute `cpp_str_flex_ctor` can be used to change this behavior, per field attribute `cpp_str_flex_ctor` can be used to change this behavior,
so that the custom string type is constructed by passing the pointer and so that the custom string type is constructed by passing the pointer and
length of the FlatBuffers String. The custom string class will require a length of the FlatBuffers String. The custom string class will require a
constructor in the following format: custom_str_class(const char *, uint32_t). constructor in the following format: custom_str_class(const char *, size_t).
Please note that the character array is not guaranteed to be NUL terminated, Please note that the character array is not guaranteed to be NULL terminated,
you should always use the provided size to determine end of string. you should always use the provided size to determine end of string.
## Reflection (& Resizing) ## Reflection (& Resizing)

View File

@ -496,13 +496,13 @@ class CppGenerator : public BaseGenerator {
// Return a C++ type from the table in idl.h // Return a C++ type from the table in idl.h
std::string GenTypeBasic(const Type &type, bool user_facing_type) const { std::string GenTypeBasic(const Type &type, bool user_facing_type) const {
static const char *const ctypename[] = { static const char *const ctypename[] = {
// clang-format off // clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \ #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \ RTYPE) \
#CTYPE, #CTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD #undef FLATBUFFERS_TD
// clang-format on // clang-format on
}; };
if (user_facing_type) { if (user_facing_type) {
if (type.enum_def) return WrapInNameSpace(*type.enum_def); if (type.enum_def) return WrapInNameSpace(*type.enum_def);
@ -581,7 +581,7 @@ class CppGenerator : public BaseGenerator {
bool FlexibleStringConstructor(const FieldDef *field) { bool FlexibleStringConstructor(const FieldDef *field) {
auto attr = field ? (field->attributes.Lookup("cpp_str_flex_ctor") != nullptr) : false; auto attr = field ? (field->attributes.Lookup("cpp_str_flex_ctor") != nullptr) : false;
auto ret = attr ? attr : parser_.opts.cpp_object_api_string_flexible_constructor; auto ret = attr ? attr : parser_.opts.cpp_object_api_string_flexible_constructor;
return (ret && NativeString(field) != "std::string"); // Only for custom string types. return ret && NativeString(field) != "std::string"; // Only for custom string types.
} }
std::string GenTypeNativePtr(const std::string &type, const FieldDef *field, std::string GenTypeNativePtr(const std::string &type, const FieldDef *field,
@ -2341,13 +2341,17 @@ class CppGenerator : public BaseGenerator {
auto vector_type = field.value.type.VectorType(); auto vector_type = field.value.type.VectorType();
switch (vector_type.base_type) { switch (vector_type.base_type) {
case BASE_TYPE_STRING: { case BASE_TYPE_STRING: {
// Use by-function serialization to emulate CreateVectorOfStrings(); if (NativeString(&field) == "std::string") {
// this works also with non-std strings. code += "_fbb.CreateVectorOfStrings(" + value + ")";
code += "_fbb.CreateVector<flatbuffers::Offset<flatbuffers::String>> "; } else {
code += "(" + value + ".size(), "; // Use by-function serialization to emulate CreateVectorOfStrings();
code += "[](size_t i, _VectorArgs *__va) { "; // this works also with non-std strings.
code += "return __va->__fbb->CreateString(__va->_" + value + "[i]);"; code += "_fbb.CreateVector<flatbuffers::Offset<flatbuffers::String>> ";
code += " }, &_va )"; code += "(" + value + ".size(), ";
code += "[](size_t i, _VectorArgs *__va) { ";
code += "return __va->__fbb->CreateString(__va->_" + value + "[i]);";
code += " }, &_va )";
}
break; break;
} }
case BASE_TYPE_STRUCT: { case BASE_TYPE_STRUCT: {