diff options
author | Vladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com> | 2020-10-03 00:27:30 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-02 10:27:30 -0700 |
commit | bbcc85fd4c77c078a5d9ca1680cc0b4b0b5c4c95 (patch) | |
tree | 4016bdda311dd3990ff9e18448a6069543dbbe96 /src | |
parent | 0bdf2fa156f5133b09ddac7beb326b942d524b38 (diff) | |
download | flatbuffers-bbcc85fd4c77c078a5d9ca1680cc0b4b0b5c4c95.tar.gz flatbuffers-bbcc85fd4c77c078a5d9ca1680cc0b4b0b5c4c95.tar.bz2 flatbuffers-bbcc85fd4c77c078a5d9ca1680cc0b4b0b5c4c95.zip |
Fix generation of C++ code with Optional<Enum> (#6155)
Diffstat (limited to 'src')
-rw-r--r-- | src/idl_gen_cpp.cpp | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 260f0223..40435337 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1598,7 +1598,9 @@ class CppGenerator : public BaseGenerator { std::string GetDefaultScalarValue(const FieldDef &field, bool is_ctor) { const auto &type = field.value.type; - if (type.enum_def && IsScalar(type.base_type)) { + if (field.IsScalarOptional()) { + return GenOptionalNull(); + } else if (type.enum_def && IsScalar(type.base_type)) { auto ev = type.enum_def->FindByValue(field.value.constant); if (ev) { return WrapInNameSpace(type.enum_def->defined_namespace, @@ -1620,8 +1622,7 @@ class CppGenerator : public BaseGenerator { return "0"; } } else { - return field.IsScalarOptional() ? GenOptionalNull() - : GenDefaultConstant(field); + return GenDefaultConstant(field); } } @@ -1647,13 +1648,11 @@ class CppGenerator : public BaseGenerator { code_.SetValue("PARAM_VALUE", "nullptr"); } else { const auto &type = field.value.type; - if (field.IsScalarOptional()) { + code_.SetValue("PARAM_VALUE", GetDefaultScalarValue(field, false)); + if (field.IsScalarOptional()) code_.SetValue("PARAM_TYPE", GenOptionalDecl(type)); - code_.SetValue("PARAM_VALUE", GenOptionalNull()); - } else { + else code_.SetValue("PARAM_TYPE", GenTypeWire(type, " ", true)); - code_.SetValue("PARAM_VALUE", GetDefaultScalarValue(field, false)); - } } code_ += "{{PRE}}{{PARAM_TYPE}}{{PARAM_NAME}} = {{PARAM_VALUE}}\\"; } |