google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[C++] flatc generates invalid Code for union field accessors, when --cpp-field-case-style is used

stefan301 opened this issue · comments

flatc generates invalid code for the following schema, when --cpp-field-case-style upper or --cpp-field-case-style lower is used:

table AttributeValueInt {
	v : int;
}

table AttributeValueString {
	v : string;
}

union U_AttributeValue {
	AttributeValueInt,
	AttributeValueString
}

table Attribute {
	name:string;
	value:U_AttributeValue (required);
}

The command

flatc.exe --cpp --cpp-field-case-style upper Attribute.fbs

generates this code:

struct Attribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
  typedef AttributeBuilder Builder;
  enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
    VT_NAME = 4,
    VT_VALUE_TYPE = 6,
    VT_VALUE = 8
  };
  const flatbuffers::String *Name() const {
    return GetPointer<const flatbuffers::String *>(VT_NAME);
  }
  U_AttributeValue Value_type() const {
    return static_cast<U_AttributeValue>(GetField<uint8_t>(VT_VALUE_TYPE, 0));
  }
  const void *Value() const {
    return GetPointer<const void *>(VT_VALUE);
  }
  template<typename T> const T *Value_as() const;
  const AttributeValueInt *Value_as_AttributeValueInt() const {
    return value_type() == U_AttributeValue_AttributeValueInt ? static_cast<const AttributeValueInt *>(Value()) : nullptr;
  }
  const AttributeValueString *Value_as_AttributeValueString() const {
    return value_type() == U_AttributeValue_AttributeValueString ? static_cast<const AttributeValueString *>(Value()) : nullptr;
  }

Value_as_AttributeValueInt() and Value_as_AttributeValueString() call value_type(), while its name is Value_type().

Fixed by PR #7211