diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index dabc385bb..9cb016969 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1,4 +1,4 @@ -/* + /* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -1650,7 +1650,7 @@ class CppGenerator : public BaseGenerator { const auto &type = field.value.type; code_.SetValue("PARAM_VALUE", GetDefaultScalarValue(field, false)); if (field.IsScalarOptional()) - code_.SetValue("PARAM_TYPE", GenOptionalDecl(type)); + code_.SetValue("PARAM_TYPE", GenOptionalDecl(type) + " "); else code_.SetValue("PARAM_TYPE", GenTypeWire(type, " ", true)); } diff --git a/tests/BUILD b/tests/BUILD index 203a08cd9..ac8f8f4d4 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -20,7 +20,7 @@ cc_test( "test_builder.cpp", "test_builder.h", "union_vector/union_vector_generated.h", - "optional_scalars_generated.h", + "optional_scalars2_generated.h", ], copts = [ "-DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE", @@ -54,7 +54,7 @@ cc_test( ":unicode_test.json", ":union_vector/union_vector.fbs", ":union_vector/union_vector.json", - ":optional_scalars.fbs", + ":optional_scalars2.fbs", ], includes = [ "", diff --git a/tests/cpp17/generated_cpp17/optional_scalars2_generated.h b/tests/cpp17/generated_cpp17/optional_scalars2_generated.h new file mode 100644 index 000000000..1aba093e6 --- /dev/null +++ b/tests/cpp17/generated_cpp17/optional_scalars2_generated.h @@ -0,0 +1,902 @@ +// automatically generated by the FlatBuffers compiler, do not modify + + +#ifndef FLATBUFFERS_GENERATED_OPTIONALSCALARS2_OPTIONAL_SCALARS_H_ +#define FLATBUFFERS_GENERATED_OPTIONALSCALARS2_OPTIONAL_SCALARS_H_ + +#include "flatbuffers/flatbuffers.h" + +namespace optional_scalars { + +struct ScalarStuff; +struct ScalarStuffBuilder; +struct ScalarStuffT; + +inline const flatbuffers::TypeTable *ScalarStuffTypeTable(); + +enum class OptionalByte : int8_t { + None = 0, + One = 1, + Two = 2, + MIN = None, + MAX = Two +}; + +inline const OptionalByte (&EnumValuesOptionalByte())[3] { + static const OptionalByte values[] = { + OptionalByte::None, + OptionalByte::One, + OptionalByte::Two + }; + return values; +} + +inline const char * const *EnumNamesOptionalByte() { + static const char * const names[4] = { + "None", + "One", + "Two", + nullptr + }; + return names; +} + +inline const char *EnumNameOptionalByte(OptionalByte e) { + if (flatbuffers::IsOutRange(e, OptionalByte::None, OptionalByte::Two)) return ""; + const size_t index = static_cast(e); + return EnumNamesOptionalByte()[index]; +} + +struct ScalarStuffT : public flatbuffers::NativeTable { + typedef ScalarStuff TableType; + int8_t just_i8 = 0; + flatbuffers::Optional maybe_i8 = flatbuffers::nullopt; + int8_t default_i8 = 42; + uint8_t just_u8 = 0; + flatbuffers::Optional maybe_u8 = flatbuffers::nullopt; + uint8_t default_u8 = 42; + int16_t just_i16 = 0; + flatbuffers::Optional maybe_i16 = flatbuffers::nullopt; + int16_t default_i16 = 42; + uint16_t just_u16 = 0; + flatbuffers::Optional maybe_u16 = flatbuffers::nullopt; + uint16_t default_u16 = 42; + int32_t just_i32 = 0; + flatbuffers::Optional maybe_i32 = flatbuffers::nullopt; + int32_t default_i32 = 42; + uint32_t just_u32 = 0; + flatbuffers::Optional maybe_u32 = flatbuffers::nullopt; + uint32_t default_u32 = 42; + int64_t just_i64 = 0; + flatbuffers::Optional maybe_i64 = flatbuffers::nullopt; + int64_t default_i64 = 42LL; + uint64_t just_u64 = 0; + flatbuffers::Optional maybe_u64 = flatbuffers::nullopt; + uint64_t default_u64 = 42ULL; + float just_f32 = 0.0f; + flatbuffers::Optional maybe_f32 = flatbuffers::nullopt; + float default_f32 = 42.0f; + double just_f64 = 0.0; + flatbuffers::Optional maybe_f64 = flatbuffers::nullopt; + double default_f64 = 42.0; + bool just_bool = false; + flatbuffers::Optional maybe_bool = flatbuffers::nullopt; + bool default_bool = true; + optional_scalars::OptionalByte just_enum = optional_scalars::OptionalByte::None; + flatbuffers::Optional maybe_enum = flatbuffers::nullopt; + optional_scalars::OptionalByte default_enum = optional_scalars::OptionalByte::One; +}; + +struct ScalarStuff FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef ScalarStuffT NativeTableType; + typedef ScalarStuffBuilder Builder; + struct Traits; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return ScalarStuffTypeTable(); + } + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_JUST_I8 = 4, + VT_MAYBE_I8 = 6, + VT_DEFAULT_I8 = 8, + VT_JUST_U8 = 10, + VT_MAYBE_U8 = 12, + VT_DEFAULT_U8 = 14, + VT_JUST_I16 = 16, + VT_MAYBE_I16 = 18, + VT_DEFAULT_I16 = 20, + VT_JUST_U16 = 22, + VT_MAYBE_U16 = 24, + VT_DEFAULT_U16 = 26, + VT_JUST_I32 = 28, + VT_MAYBE_I32 = 30, + VT_DEFAULT_I32 = 32, + VT_JUST_U32 = 34, + VT_MAYBE_U32 = 36, + VT_DEFAULT_U32 = 38, + VT_JUST_I64 = 40, + VT_MAYBE_I64 = 42, + VT_DEFAULT_I64 = 44, + VT_JUST_U64 = 46, + VT_MAYBE_U64 = 48, + VT_DEFAULT_U64 = 50, + VT_JUST_F32 = 52, + VT_MAYBE_F32 = 54, + VT_DEFAULT_F32 = 56, + VT_JUST_F64 = 58, + VT_MAYBE_F64 = 60, + VT_DEFAULT_F64 = 62, + VT_JUST_BOOL = 64, + VT_MAYBE_BOOL = 66, + VT_DEFAULT_BOOL = 68, + VT_JUST_ENUM = 70, + VT_MAYBE_ENUM = 72, + VT_DEFAULT_ENUM = 74 + }; + int8_t just_i8() const { + return GetField(VT_JUST_I8, 0); + } + bool mutate_just_i8(int8_t _just_i8) { + return SetField(VT_JUST_I8, _just_i8, 0); + } + flatbuffers::Optional maybe_i8() const { + return GetOptional(VT_MAYBE_I8); + } + bool mutate_maybe_i8(int8_t _maybe_i8) { + return SetField(VT_MAYBE_I8, _maybe_i8); + } + int8_t default_i8() const { + return GetField(VT_DEFAULT_I8, 42); + } + bool mutate_default_i8(int8_t _default_i8) { + return SetField(VT_DEFAULT_I8, _default_i8, 42); + } + uint8_t just_u8() const { + return GetField(VT_JUST_U8, 0); + } + bool mutate_just_u8(uint8_t _just_u8) { + return SetField(VT_JUST_U8, _just_u8, 0); + } + flatbuffers::Optional maybe_u8() const { + return GetOptional(VT_MAYBE_U8); + } + bool mutate_maybe_u8(uint8_t _maybe_u8) { + return SetField(VT_MAYBE_U8, _maybe_u8); + } + uint8_t default_u8() const { + return GetField(VT_DEFAULT_U8, 42); + } + bool mutate_default_u8(uint8_t _default_u8) { + return SetField(VT_DEFAULT_U8, _default_u8, 42); + } + int16_t just_i16() const { + return GetField(VT_JUST_I16, 0); + } + bool mutate_just_i16(int16_t _just_i16) { + return SetField(VT_JUST_I16, _just_i16, 0); + } + flatbuffers::Optional maybe_i16() const { + return GetOptional(VT_MAYBE_I16); + } + bool mutate_maybe_i16(int16_t _maybe_i16) { + return SetField(VT_MAYBE_I16, _maybe_i16); + } + int16_t default_i16() const { + return GetField(VT_DEFAULT_I16, 42); + } + bool mutate_default_i16(int16_t _default_i16) { + return SetField(VT_DEFAULT_I16, _default_i16, 42); + } + uint16_t just_u16() const { + return GetField(VT_JUST_U16, 0); + } + bool mutate_just_u16(uint16_t _just_u16) { + return SetField(VT_JUST_U16, _just_u16, 0); + } + flatbuffers::Optional maybe_u16() const { + return GetOptional(VT_MAYBE_U16); + } + bool mutate_maybe_u16(uint16_t _maybe_u16) { + return SetField(VT_MAYBE_U16, _maybe_u16); + } + uint16_t default_u16() const { + return GetField(VT_DEFAULT_U16, 42); + } + bool mutate_default_u16(uint16_t _default_u16) { + return SetField(VT_DEFAULT_U16, _default_u16, 42); + } + int32_t just_i32() const { + return GetField(VT_JUST_I32, 0); + } + bool mutate_just_i32(int32_t _just_i32) { + return SetField(VT_JUST_I32, _just_i32, 0); + } + flatbuffers::Optional maybe_i32() const { + return GetOptional(VT_MAYBE_I32); + } + bool mutate_maybe_i32(int32_t _maybe_i32) { + return SetField(VT_MAYBE_I32, _maybe_i32); + } + int32_t default_i32() const { + return GetField(VT_DEFAULT_I32, 42); + } + bool mutate_default_i32(int32_t _default_i32) { + return SetField(VT_DEFAULT_I32, _default_i32, 42); + } + uint32_t just_u32() const { + return GetField(VT_JUST_U32, 0); + } + bool mutate_just_u32(uint32_t _just_u32) { + return SetField(VT_JUST_U32, _just_u32, 0); + } + flatbuffers::Optional maybe_u32() const { + return GetOptional(VT_MAYBE_U32); + } + bool mutate_maybe_u32(uint32_t _maybe_u32) { + return SetField(VT_MAYBE_U32, _maybe_u32); + } + uint32_t default_u32() const { + return GetField(VT_DEFAULT_U32, 42); + } + bool mutate_default_u32(uint32_t _default_u32) { + return SetField(VT_DEFAULT_U32, _default_u32, 42); + } + int64_t just_i64() const { + return GetField(VT_JUST_I64, 0); + } + bool mutate_just_i64(int64_t _just_i64) { + return SetField(VT_JUST_I64, _just_i64, 0); + } + flatbuffers::Optional maybe_i64() const { + return GetOptional(VT_MAYBE_I64); + } + bool mutate_maybe_i64(int64_t _maybe_i64) { + return SetField(VT_MAYBE_I64, _maybe_i64); + } + int64_t default_i64() const { + return GetField(VT_DEFAULT_I64, 42LL); + } + bool mutate_default_i64(int64_t _default_i64) { + return SetField(VT_DEFAULT_I64, _default_i64, 42LL); + } + uint64_t just_u64() const { + return GetField(VT_JUST_U64, 0); + } + bool mutate_just_u64(uint64_t _just_u64) { + return SetField(VT_JUST_U64, _just_u64, 0); + } + flatbuffers::Optional maybe_u64() const { + return GetOptional(VT_MAYBE_U64); + } + bool mutate_maybe_u64(uint64_t _maybe_u64) { + return SetField(VT_MAYBE_U64, _maybe_u64); + } + uint64_t default_u64() const { + return GetField(VT_DEFAULT_U64, 42ULL); + } + bool mutate_default_u64(uint64_t _default_u64) { + return SetField(VT_DEFAULT_U64, _default_u64, 42ULL); + } + float just_f32() const { + return GetField(VT_JUST_F32, 0.0f); + } + bool mutate_just_f32(float _just_f32) { + return SetField(VT_JUST_F32, _just_f32, 0.0f); + } + flatbuffers::Optional maybe_f32() const { + return GetOptional(VT_MAYBE_F32); + } + bool mutate_maybe_f32(float _maybe_f32) { + return SetField(VT_MAYBE_F32, _maybe_f32); + } + float default_f32() const { + return GetField(VT_DEFAULT_F32, 42.0f); + } + bool mutate_default_f32(float _default_f32) { + return SetField(VT_DEFAULT_F32, _default_f32, 42.0f); + } + double just_f64() const { + return GetField(VT_JUST_F64, 0.0); + } + bool mutate_just_f64(double _just_f64) { + return SetField(VT_JUST_F64, _just_f64, 0.0); + } + flatbuffers::Optional maybe_f64() const { + return GetOptional(VT_MAYBE_F64); + } + bool mutate_maybe_f64(double _maybe_f64) { + return SetField(VT_MAYBE_F64, _maybe_f64); + } + double default_f64() const { + return GetField(VT_DEFAULT_F64, 42.0); + } + bool mutate_default_f64(double _default_f64) { + return SetField(VT_DEFAULT_F64, _default_f64, 42.0); + } + bool just_bool() const { + return GetField(VT_JUST_BOOL, 0) != 0; + } + bool mutate_just_bool(bool _just_bool) { + return SetField(VT_JUST_BOOL, static_cast(_just_bool), 0); + } + flatbuffers::Optional maybe_bool() const { + return GetOptional(VT_MAYBE_BOOL); + } + bool mutate_maybe_bool(bool _maybe_bool) { + return SetField(VT_MAYBE_BOOL, static_cast(_maybe_bool)); + } + bool default_bool() const { + return GetField(VT_DEFAULT_BOOL, 1) != 0; + } + bool mutate_default_bool(bool _default_bool) { + return SetField(VT_DEFAULT_BOOL, static_cast(_default_bool), 1); + } + optional_scalars::OptionalByte just_enum() const { + return static_cast(GetField(VT_JUST_ENUM, 0)); + } + bool mutate_just_enum(optional_scalars::OptionalByte _just_enum) { + return SetField(VT_JUST_ENUM, static_cast(_just_enum), 0); + } + flatbuffers::Optional maybe_enum() const { + return GetOptional(VT_MAYBE_ENUM); + } + bool mutate_maybe_enum(optional_scalars::OptionalByte _maybe_enum) { + return SetField(VT_MAYBE_ENUM, static_cast(_maybe_enum)); + } + optional_scalars::OptionalByte default_enum() const { + return static_cast(GetField(VT_DEFAULT_ENUM, 1)); + } + bool mutate_default_enum(optional_scalars::OptionalByte _default_enum) { + return SetField(VT_DEFAULT_ENUM, static_cast(_default_enum), 1); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_JUST_I8) && + VerifyField(verifier, VT_MAYBE_I8) && + VerifyField(verifier, VT_DEFAULT_I8) && + VerifyField(verifier, VT_JUST_U8) && + VerifyField(verifier, VT_MAYBE_U8) && + VerifyField(verifier, VT_DEFAULT_U8) && + VerifyField(verifier, VT_JUST_I16) && + VerifyField(verifier, VT_MAYBE_I16) && + VerifyField(verifier, VT_DEFAULT_I16) && + VerifyField(verifier, VT_JUST_U16) && + VerifyField(verifier, VT_MAYBE_U16) && + VerifyField(verifier, VT_DEFAULT_U16) && + VerifyField(verifier, VT_JUST_I32) && + VerifyField(verifier, VT_MAYBE_I32) && + VerifyField(verifier, VT_DEFAULT_I32) && + VerifyField(verifier, VT_JUST_U32) && + VerifyField(verifier, VT_MAYBE_U32) && + VerifyField(verifier, VT_DEFAULT_U32) && + VerifyField(verifier, VT_JUST_I64) && + VerifyField(verifier, VT_MAYBE_I64) && + VerifyField(verifier, VT_DEFAULT_I64) && + VerifyField(verifier, VT_JUST_U64) && + VerifyField(verifier, VT_MAYBE_U64) && + VerifyField(verifier, VT_DEFAULT_U64) && + VerifyField(verifier, VT_JUST_F32) && + VerifyField(verifier, VT_MAYBE_F32) && + VerifyField(verifier, VT_DEFAULT_F32) && + VerifyField(verifier, VT_JUST_F64) && + VerifyField(verifier, VT_MAYBE_F64) && + VerifyField(verifier, VT_DEFAULT_F64) && + VerifyField(verifier, VT_JUST_BOOL) && + VerifyField(verifier, VT_MAYBE_BOOL) && + VerifyField(verifier, VT_DEFAULT_BOOL) && + VerifyField(verifier, VT_JUST_ENUM) && + VerifyField(verifier, VT_MAYBE_ENUM) && + VerifyField(verifier, VT_DEFAULT_ENUM) && + verifier.EndTable(); + } + ScalarStuffT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ScalarStuffT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ScalarStuffBuilder { + typedef ScalarStuff Table; + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_just_i8(int8_t just_i8) { + fbb_.AddElement(ScalarStuff::VT_JUST_I8, just_i8, 0); + } + void add_maybe_i8(int8_t maybe_i8) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_I8, maybe_i8); + } + void add_default_i8(int8_t default_i8) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_I8, default_i8, 42); + } + void add_just_u8(uint8_t just_u8) { + fbb_.AddElement(ScalarStuff::VT_JUST_U8, just_u8, 0); + } + void add_maybe_u8(uint8_t maybe_u8) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_U8, maybe_u8); + } + void add_default_u8(uint8_t default_u8) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_U8, default_u8, 42); + } + void add_just_i16(int16_t just_i16) { + fbb_.AddElement(ScalarStuff::VT_JUST_I16, just_i16, 0); + } + void add_maybe_i16(int16_t maybe_i16) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_I16, maybe_i16); + } + void add_default_i16(int16_t default_i16) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_I16, default_i16, 42); + } + void add_just_u16(uint16_t just_u16) { + fbb_.AddElement(ScalarStuff::VT_JUST_U16, just_u16, 0); + } + void add_maybe_u16(uint16_t maybe_u16) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_U16, maybe_u16); + } + void add_default_u16(uint16_t default_u16) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_U16, default_u16, 42); + } + void add_just_i32(int32_t just_i32) { + fbb_.AddElement(ScalarStuff::VT_JUST_I32, just_i32, 0); + } + void add_maybe_i32(int32_t maybe_i32) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_I32, maybe_i32); + } + void add_default_i32(int32_t default_i32) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_I32, default_i32, 42); + } + void add_just_u32(uint32_t just_u32) { + fbb_.AddElement(ScalarStuff::VT_JUST_U32, just_u32, 0); + } + void add_maybe_u32(uint32_t maybe_u32) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_U32, maybe_u32); + } + void add_default_u32(uint32_t default_u32) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_U32, default_u32, 42); + } + void add_just_i64(int64_t just_i64) { + fbb_.AddElement(ScalarStuff::VT_JUST_I64, just_i64, 0); + } + void add_maybe_i64(int64_t maybe_i64) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_I64, maybe_i64); + } + void add_default_i64(int64_t default_i64) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_I64, default_i64, 42LL); + } + void add_just_u64(uint64_t just_u64) { + fbb_.AddElement(ScalarStuff::VT_JUST_U64, just_u64, 0); + } + void add_maybe_u64(uint64_t maybe_u64) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_U64, maybe_u64); + } + void add_default_u64(uint64_t default_u64) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_U64, default_u64, 42ULL); + } + void add_just_f32(float just_f32) { + fbb_.AddElement(ScalarStuff::VT_JUST_F32, just_f32, 0.0f); + } + void add_maybe_f32(float maybe_f32) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_F32, maybe_f32); + } + void add_default_f32(float default_f32) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_F32, default_f32, 42.0f); + } + void add_just_f64(double just_f64) { + fbb_.AddElement(ScalarStuff::VT_JUST_F64, just_f64, 0.0); + } + void add_maybe_f64(double maybe_f64) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_F64, maybe_f64); + } + void add_default_f64(double default_f64) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_F64, default_f64, 42.0); + } + void add_just_bool(bool just_bool) { + fbb_.AddElement(ScalarStuff::VT_JUST_BOOL, static_cast(just_bool), 0); + } + void add_maybe_bool(bool maybe_bool) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_BOOL, static_cast(maybe_bool)); + } + void add_default_bool(bool default_bool) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_BOOL, static_cast(default_bool), 1); + } + void add_just_enum(optional_scalars::OptionalByte just_enum) { + fbb_.AddElement(ScalarStuff::VT_JUST_ENUM, static_cast(just_enum), 0); + } + void add_maybe_enum(optional_scalars::OptionalByte maybe_enum) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_ENUM, static_cast(maybe_enum)); + } + void add_default_enum(optional_scalars::OptionalByte default_enum) { + fbb_.AddElement(ScalarStuff::VT_DEFAULT_ENUM, static_cast(default_enum), 1); + } + explicit ScalarStuffBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + return o; + } +}; + +inline flatbuffers::Offset CreateScalarStuff( + flatbuffers::FlatBufferBuilder &_fbb, + int8_t just_i8 = 0, + flatbuffers::Optional maybe_i8 = flatbuffers::nullopt, + int8_t default_i8 = 42, + uint8_t just_u8 = 0, + flatbuffers::Optional maybe_u8 = flatbuffers::nullopt, + uint8_t default_u8 = 42, + int16_t just_i16 = 0, + flatbuffers::Optional maybe_i16 = flatbuffers::nullopt, + int16_t default_i16 = 42, + uint16_t just_u16 = 0, + flatbuffers::Optional maybe_u16 = flatbuffers::nullopt, + uint16_t default_u16 = 42, + int32_t just_i32 = 0, + flatbuffers::Optional maybe_i32 = flatbuffers::nullopt, + int32_t default_i32 = 42, + uint32_t just_u32 = 0, + flatbuffers::Optional maybe_u32 = flatbuffers::nullopt, + uint32_t default_u32 = 42, + int64_t just_i64 = 0, + flatbuffers::Optional maybe_i64 = flatbuffers::nullopt, + int64_t default_i64 = 42LL, + uint64_t just_u64 = 0, + flatbuffers::Optional maybe_u64 = flatbuffers::nullopt, + uint64_t default_u64 = 42ULL, + float just_f32 = 0.0f, + flatbuffers::Optional maybe_f32 = flatbuffers::nullopt, + float default_f32 = 42.0f, + double just_f64 = 0.0, + flatbuffers::Optional maybe_f64 = flatbuffers::nullopt, + double default_f64 = 42.0, + bool just_bool = false, + flatbuffers::Optional maybe_bool = flatbuffers::nullopt, + bool default_bool = true, + optional_scalars::OptionalByte just_enum = optional_scalars::OptionalByte::None, + flatbuffers::Optional maybe_enum = flatbuffers::nullopt, + optional_scalars::OptionalByte default_enum = optional_scalars::OptionalByte::One) { + ScalarStuffBuilder builder_(_fbb); + builder_.add_default_f64(default_f64); + if(maybe_f64) { builder_.add_maybe_f64(*maybe_f64); } + builder_.add_just_f64(just_f64); + builder_.add_default_u64(default_u64); + if(maybe_u64) { builder_.add_maybe_u64(*maybe_u64); } + builder_.add_just_u64(just_u64); + builder_.add_default_i64(default_i64); + if(maybe_i64) { builder_.add_maybe_i64(*maybe_i64); } + builder_.add_just_i64(just_i64); + builder_.add_default_f32(default_f32); + if(maybe_f32) { builder_.add_maybe_f32(*maybe_f32); } + builder_.add_just_f32(just_f32); + builder_.add_default_u32(default_u32); + if(maybe_u32) { builder_.add_maybe_u32(*maybe_u32); } + builder_.add_just_u32(just_u32); + builder_.add_default_i32(default_i32); + if(maybe_i32) { builder_.add_maybe_i32(*maybe_i32); } + builder_.add_just_i32(just_i32); + builder_.add_default_u16(default_u16); + if(maybe_u16) { builder_.add_maybe_u16(*maybe_u16); } + builder_.add_just_u16(just_u16); + builder_.add_default_i16(default_i16); + if(maybe_i16) { builder_.add_maybe_i16(*maybe_i16); } + builder_.add_just_i16(just_i16); + builder_.add_default_enum(default_enum); + if(maybe_enum) { builder_.add_maybe_enum(*maybe_enum); } + builder_.add_just_enum(just_enum); + builder_.add_default_bool(default_bool); + if(maybe_bool) { builder_.add_maybe_bool(*maybe_bool); } + builder_.add_just_bool(just_bool); + builder_.add_default_u8(default_u8); + if(maybe_u8) { builder_.add_maybe_u8(*maybe_u8); } + builder_.add_just_u8(just_u8); + builder_.add_default_i8(default_i8); + if(maybe_i8) { builder_.add_maybe_i8(*maybe_i8); } + builder_.add_just_i8(just_i8); + return builder_.Finish(); +} + +struct ScalarStuff::Traits { + using type = ScalarStuff; + static auto constexpr Create = CreateScalarStuff; +}; + +flatbuffers::Offset CreateScalarStuff(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline ScalarStuffT *ScalarStuff::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::make_unique(); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ScalarStuff::UnPackTo(ScalarStuffT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = just_i8(); _o->just_i8 = _e; } + { auto _e = maybe_i8(); _o->maybe_i8 = _e; } + { auto _e = default_i8(); _o->default_i8 = _e; } + { auto _e = just_u8(); _o->just_u8 = _e; } + { auto _e = maybe_u8(); _o->maybe_u8 = _e; } + { auto _e = default_u8(); _o->default_u8 = _e; } + { auto _e = just_i16(); _o->just_i16 = _e; } + { auto _e = maybe_i16(); _o->maybe_i16 = _e; } + { auto _e = default_i16(); _o->default_i16 = _e; } + { auto _e = just_u16(); _o->just_u16 = _e; } + { auto _e = maybe_u16(); _o->maybe_u16 = _e; } + { auto _e = default_u16(); _o->default_u16 = _e; } + { auto _e = just_i32(); _o->just_i32 = _e; } + { auto _e = maybe_i32(); _o->maybe_i32 = _e; } + { auto _e = default_i32(); _o->default_i32 = _e; } + { auto _e = just_u32(); _o->just_u32 = _e; } + { auto _e = maybe_u32(); _o->maybe_u32 = _e; } + { auto _e = default_u32(); _o->default_u32 = _e; } + { auto _e = just_i64(); _o->just_i64 = _e; } + { auto _e = maybe_i64(); _o->maybe_i64 = _e; } + { auto _e = default_i64(); _o->default_i64 = _e; } + { auto _e = just_u64(); _o->just_u64 = _e; } + { auto _e = maybe_u64(); _o->maybe_u64 = _e; } + { auto _e = default_u64(); _o->default_u64 = _e; } + { auto _e = just_f32(); _o->just_f32 = _e; } + { auto _e = maybe_f32(); _o->maybe_f32 = _e; } + { auto _e = default_f32(); _o->default_f32 = _e; } + { auto _e = just_f64(); _o->just_f64 = _e; } + { auto _e = maybe_f64(); _o->maybe_f64 = _e; } + { auto _e = default_f64(); _o->default_f64 = _e; } + { auto _e = just_bool(); _o->just_bool = _e; } + { auto _e = maybe_bool(); _o->maybe_bool = _e; } + { auto _e = default_bool(); _o->default_bool = _e; } + { auto _e = just_enum(); _o->just_enum = _e; } + { auto _e = maybe_enum(); _o->maybe_enum = _e; } + { auto _e = default_enum(); _o->default_enum = _e; } +} + +inline flatbuffers::Offset ScalarStuff::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateScalarStuff(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateScalarStuff(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ScalarStuffT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _just_i8 = _o->just_i8; + auto _maybe_i8 = _o->maybe_i8; + auto _default_i8 = _o->default_i8; + auto _just_u8 = _o->just_u8; + auto _maybe_u8 = _o->maybe_u8; + auto _default_u8 = _o->default_u8; + auto _just_i16 = _o->just_i16; + auto _maybe_i16 = _o->maybe_i16; + auto _default_i16 = _o->default_i16; + auto _just_u16 = _o->just_u16; + auto _maybe_u16 = _o->maybe_u16; + auto _default_u16 = _o->default_u16; + auto _just_i32 = _o->just_i32; + auto _maybe_i32 = _o->maybe_i32; + auto _default_i32 = _o->default_i32; + auto _just_u32 = _o->just_u32; + auto _maybe_u32 = _o->maybe_u32; + auto _default_u32 = _o->default_u32; + auto _just_i64 = _o->just_i64; + auto _maybe_i64 = _o->maybe_i64; + auto _default_i64 = _o->default_i64; + auto _just_u64 = _o->just_u64; + auto _maybe_u64 = _o->maybe_u64; + auto _default_u64 = _o->default_u64; + auto _just_f32 = _o->just_f32; + auto _maybe_f32 = _o->maybe_f32; + auto _default_f32 = _o->default_f32; + auto _just_f64 = _o->just_f64; + auto _maybe_f64 = _o->maybe_f64; + auto _default_f64 = _o->default_f64; + auto _just_bool = _o->just_bool; + auto _maybe_bool = _o->maybe_bool; + auto _default_bool = _o->default_bool; + auto _just_enum = _o->just_enum; + auto _maybe_enum = _o->maybe_enum; + auto _default_enum = _o->default_enum; + return optional_scalars::CreateScalarStuff( + _fbb, + _just_i8, + _maybe_i8, + _default_i8, + _just_u8, + _maybe_u8, + _default_u8, + _just_i16, + _maybe_i16, + _default_i16, + _just_u16, + _maybe_u16, + _default_u16, + _just_i32, + _maybe_i32, + _default_i32, + _just_u32, + _maybe_u32, + _default_u32, + _just_i64, + _maybe_i64, + _default_i64, + _just_u64, + _maybe_u64, + _default_u64, + _just_f32, + _maybe_f32, + _default_f32, + _just_f64, + _maybe_f64, + _default_f64, + _just_bool, + _maybe_bool, + _default_bool, + _just_enum, + _maybe_enum, + _default_enum); +} + +inline const flatbuffers::TypeTable *OptionalByteTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + optional_scalars::OptionalByteTypeTable + }; + static const char * const names[] = { + "None", + "One", + "Two" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_ENUM, 3, type_codes, type_refs, nullptr, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *ScalarStuffTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_CHAR, 0, -1 }, + { flatbuffers::ET_CHAR, 0, -1 }, + { flatbuffers::ET_CHAR, 0, -1 }, + { flatbuffers::ET_UCHAR, 0, -1 }, + { flatbuffers::ET_UCHAR, 0, -1 }, + { flatbuffers::ET_UCHAR, 0, -1 }, + { flatbuffers::ET_SHORT, 0, -1 }, + { flatbuffers::ET_SHORT, 0, -1 }, + { flatbuffers::ET_SHORT, 0, -1 }, + { flatbuffers::ET_USHORT, 0, -1 }, + { flatbuffers::ET_USHORT, 0, -1 }, + { flatbuffers::ET_USHORT, 0, -1 }, + { flatbuffers::ET_INT, 0, -1 }, + { flatbuffers::ET_INT, 0, -1 }, + { flatbuffers::ET_INT, 0, -1 }, + { flatbuffers::ET_UINT, 0, -1 }, + { flatbuffers::ET_UINT, 0, -1 }, + { flatbuffers::ET_UINT, 0, -1 }, + { flatbuffers::ET_LONG, 0, -1 }, + { flatbuffers::ET_LONG, 0, -1 }, + { flatbuffers::ET_LONG, 0, -1 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_DOUBLE, 0, -1 }, + { flatbuffers::ET_DOUBLE, 0, -1 }, + { flatbuffers::ET_DOUBLE, 0, -1 }, + { flatbuffers::ET_BOOL, 0, -1 }, + { flatbuffers::ET_BOOL, 0, -1 }, + { flatbuffers::ET_BOOL, 0, -1 }, + { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + optional_scalars::OptionalByteTypeTable + }; + static const char * const names[] = { + "just_i8", + "maybe_i8", + "default_i8", + "just_u8", + "maybe_u8", + "default_u8", + "just_i16", + "maybe_i16", + "default_i16", + "just_u16", + "maybe_u16", + "default_u16", + "just_i32", + "maybe_i32", + "default_i32", + "just_u32", + "maybe_u32", + "default_u32", + "just_i64", + "maybe_i64", + "default_i64", + "just_u64", + "maybe_u64", + "default_u64", + "just_f32", + "maybe_f32", + "default_f32", + "just_f64", + "maybe_f64", + "default_f64", + "just_bool", + "maybe_bool", + "default_bool", + "just_enum", + "maybe_enum", + "default_enum" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 36, type_codes, type_refs, nullptr, nullptr, names + }; + return &tt; +} + +inline const optional_scalars::ScalarStuff *GetScalarStuff(const void *buf) { + return flatbuffers::GetRoot(buf); +} + +inline const optional_scalars::ScalarStuff *GetSizePrefixedScalarStuff(const void *buf) { + return flatbuffers::GetSizePrefixedRoot(buf); +} + +inline ScalarStuff *GetMutableScalarStuff(void *buf) { + return flatbuffers::GetMutableRoot(buf); +} + +inline const char *ScalarStuffIdentifier() { + return "NULL"; +} + +inline bool ScalarStuffBufferHasIdentifier(const void *buf) { + return flatbuffers::BufferHasIdentifier( + buf, ScalarStuffIdentifier()); +} + +inline bool VerifyScalarStuffBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifyBuffer(ScalarStuffIdentifier()); +} + +inline bool VerifySizePrefixedScalarStuffBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifySizePrefixedBuffer(ScalarStuffIdentifier()); +} + +inline const char *ScalarStuffExtension() { + return "mon"; +} + +inline void FinishScalarStuffBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.Finish(root, ScalarStuffIdentifier()); +} + +inline void FinishSizePrefixedScalarStuffBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.FinishSizePrefixed(root, ScalarStuffIdentifier()); +} + +inline std::unique_ptr UnPackScalarStuff( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetScalarStuff(buf)->UnPack(res)); +} + +inline std::unique_ptr UnPackSizePrefixedScalarStuff( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetSizePrefixedScalarStuff(buf)->UnPack(res)); +} + +} // namespace optional_scalars + +#endif // FLATBUFFERS_GENERATED_OPTIONALSCALARS2_OPTIONAL_SCALARS_H_ diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 48fd730af..1df9ced1f 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -57,7 +57,7 @@ set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes --no-fb-import @rem Generate the optional scalar code for tests. ..\%buildtype%\flatc.exe --rust --lobster optional_scalars2.fbs || goto FAIL ..\%buildtype%\flatc.exe --kotlin optional_scalars.fbs || goto FAIL -..\%buildtype%\flatc.exe %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% --cpp optional_scalars.fbs || goto FAIL +..\%buildtype%\flatc.exe %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% --cpp optional_scalars2.fbs || goto FAIL @rem Generate the schema evolution tests ..\%buildtype%\flatc.exe --cpp --scoped-enums %TEST_CPP_FLAGS% -o evolution_test ./evolution_test/evolution_v1.fbs ./evolution_test/evolution_v2.fbs || goto FAIL @@ -74,7 +74,7 @@ set TEST_CPP17_FLAGS=--cpp --cpp-std c++17 -o ./cpp17/generated_cpp17 %TEST_NOIN if NOT "%MONSTER_EXTRA%"=="skip" ( @rem Flag c++17 requires Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher. ..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% -I include_test monster_test.fbs || goto FAIL - ..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% optional_scalars.fbs || goto FAIL + ..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% optional_scalars2.fbs || goto FAIL @rem..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% arrays_test.fbs || goto FAIL @rem..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% native_type_test.fbs || goto FAIL @rem..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% monster_extra.fbs || goto FAIL diff --git a/tests/generate_code.sh b/tests/generate_code.sh index 2ac43ea51..8c1f63e69 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -55,7 +55,7 @@ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_JS_TS_FLAGS -o namespace # Generate optional scalar code for tests. ../flatc --kotlin optional_scalars.fbs # These ones have not added optional enum support. ../flatc --rust --lobster optional_scalars2.fbs -../flatc $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS --cpp optional_scalars.fbs +../flatc $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS --cpp optional_scalars2.fbs # Generate the schema evolution tests ../flatc --cpp --scoped-enums $TEST_CPP_FLAGS -o evolution_test ./evolution_test/evolution_v*.fbs @@ -86,7 +86,7 @@ fi # Flag c++17 requires Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher. TEST_CPP17_FLAGS="--cpp --cpp-std c++17 -o ./cpp17/generated_cpp17 $TEST_NOINCL_FLAGS" ../flatc $TEST_CPP17_FLAGS -I include_test monster_test.fbs -../flatc $TEST_CPP17_FLAGS optional_scalars.fbs +../flatc $TEST_CPP17_FLAGS optional_scalars2.fbs cd ../samples ../flatc --cpp --lobster $TEST_BASE_FLAGS $TEST_CPP_FLAGS monster.fbs diff --git a/tests/optional_scalars_generated.h b/tests/optional_scalars2_generated.h similarity index 93% rename from tests/optional_scalars_generated.h rename to tests/optional_scalars2_generated.h index 60dcf784c..4df11cb95 100644 --- a/tests/optional_scalars_generated.h +++ b/tests/optional_scalars2_generated.h @@ -1,8 +1,8 @@ // automatically generated by the FlatBuffers compiler, do not modify -#ifndef FLATBUFFERS_GENERATED_OPTIONALSCALARS_OPTIONAL_SCALARS_H_ -#define FLATBUFFERS_GENERATED_OPTIONALSCALARS_OPTIONAL_SCALARS_H_ +#ifndef FLATBUFFERS_GENERATED_OPTIONALSCALARS2_OPTIONAL_SCALARS_H_ +#define FLATBUFFERS_GENERATED_OPTIONALSCALARS2_OPTIONAL_SCALARS_H_ #include "flatbuffers/flatbuffers.h" @@ -20,29 +20,32 @@ inline const flatbuffers::TypeTable *ScalarStuffTypeTable(); enum OptionalByte { OptionalByte_None = 0, OptionalByte_One = 1, + OptionalByte_Two = 2, OptionalByte_MIN = OptionalByte_None, - OptionalByte_MAX = OptionalByte_One + OptionalByte_MAX = OptionalByte_Two }; -inline const OptionalByte (&EnumValuesOptionalByte())[2] { +inline const OptionalByte (&EnumValuesOptionalByte())[3] { static const OptionalByte values[] = { OptionalByte_None, - OptionalByte_One + OptionalByte_One, + OptionalByte_Two }; return values; } inline const char * const *EnumNamesOptionalByte() { - static const char * const names[3] = { + static const char * const names[4] = { "None", "One", + "Two", nullptr }; return names; } inline const char *EnumNameOptionalByte(OptionalByte e) { - if (flatbuffers::IsOutRange(e, OptionalByte_None, OptionalByte_One)) return ""; + if (flatbuffers::IsOutRange(e, OptionalByte_None, OptionalByte_Two)) return ""; const size_t index = static_cast(e); return EnumNamesOptionalByte()[index]; } @@ -83,6 +86,7 @@ struct ScalarStuffT : public flatbuffers::NativeTable { flatbuffers::Optional maybe_bool; bool default_bool; optional_scalars::OptionalByte just_enum; + flatbuffers::Optional maybe_enum; optional_scalars::OptionalByte default_enum; ScalarStuffT() : just_i8(0), @@ -119,6 +123,7 @@ struct ScalarStuffT : public flatbuffers::NativeTable { maybe_bool(flatbuffers::nullopt), default_bool(true), just_enum(optional_scalars::OptionalByte_None), + maybe_enum(flatbuffers::nullopt), default_enum(optional_scalars::OptionalByte_One) { } }; @@ -159,6 +164,7 @@ inline bool operator==(const ScalarStuffT &lhs, const ScalarStuffT &rhs) { (lhs.maybe_bool == rhs.maybe_bool) && (lhs.default_bool == rhs.default_bool) && (lhs.just_enum == rhs.just_enum) && + (lhs.maybe_enum == rhs.maybe_enum) && (lhs.default_enum == rhs.default_enum); } @@ -208,7 +214,8 @@ struct ScalarStuff FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_MAYBE_BOOL = 66, VT_DEFAULT_BOOL = 68, VT_JUST_ENUM = 70, - VT_DEFAULT_ENUM = 72 + VT_MAYBE_ENUM = 72, + VT_DEFAULT_ENUM = 74 }; int8_t just_i8() const { return GetField(VT_JUST_I8, 0); @@ -414,6 +421,12 @@ struct ScalarStuff FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool mutate_just_enum(optional_scalars::OptionalByte _just_enum) { return SetField(VT_JUST_ENUM, static_cast(_just_enum), 0); } + flatbuffers::Optional maybe_enum() const { + return GetOptional(VT_MAYBE_ENUM); + } + bool mutate_maybe_enum(optional_scalars::OptionalByte _maybe_enum) { + return SetField(VT_MAYBE_ENUM, static_cast(_maybe_enum)); + } optional_scalars::OptionalByte default_enum() const { return static_cast(GetField(VT_DEFAULT_ENUM, 1)); } @@ -456,6 +469,7 @@ struct ScalarStuff FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_MAYBE_BOOL) && VerifyField(verifier, VT_DEFAULT_BOOL) && VerifyField(verifier, VT_JUST_ENUM) && + VerifyField(verifier, VT_MAYBE_ENUM) && VerifyField(verifier, VT_DEFAULT_ENUM) && verifier.EndTable(); } @@ -570,6 +584,9 @@ struct ScalarStuffBuilder { void add_just_enum(optional_scalars::OptionalByte just_enum) { fbb_.AddElement(ScalarStuff::VT_JUST_ENUM, static_cast(just_enum), 0); } + void add_maybe_enum(optional_scalars::OptionalByte maybe_enum) { + fbb_.AddElement(ScalarStuff::VT_MAYBE_ENUM, static_cast(maybe_enum)); + } void add_default_enum(optional_scalars::OptionalByte default_enum) { fbb_.AddElement(ScalarStuff::VT_DEFAULT_ENUM, static_cast(default_enum), 1); } @@ -587,39 +604,40 @@ struct ScalarStuffBuilder { inline flatbuffers::Offset CreateScalarStuff( flatbuffers::FlatBufferBuilder &_fbb, int8_t just_i8 = 0, - flatbuffers::Optionalmaybe_i8 = flatbuffers::nullopt, + flatbuffers::Optional maybe_i8 = flatbuffers::nullopt, int8_t default_i8 = 42, uint8_t just_u8 = 0, - flatbuffers::Optionalmaybe_u8 = flatbuffers::nullopt, + flatbuffers::Optional maybe_u8 = flatbuffers::nullopt, uint8_t default_u8 = 42, int16_t just_i16 = 0, - flatbuffers::Optionalmaybe_i16 = flatbuffers::nullopt, + flatbuffers::Optional maybe_i16 = flatbuffers::nullopt, int16_t default_i16 = 42, uint16_t just_u16 = 0, - flatbuffers::Optionalmaybe_u16 = flatbuffers::nullopt, + flatbuffers::Optional maybe_u16 = flatbuffers::nullopt, uint16_t default_u16 = 42, int32_t just_i32 = 0, - flatbuffers::Optionalmaybe_i32 = flatbuffers::nullopt, + flatbuffers::Optional maybe_i32 = flatbuffers::nullopt, int32_t default_i32 = 42, uint32_t just_u32 = 0, - flatbuffers::Optionalmaybe_u32 = flatbuffers::nullopt, + flatbuffers::Optional maybe_u32 = flatbuffers::nullopt, uint32_t default_u32 = 42, int64_t just_i64 = 0, - flatbuffers::Optionalmaybe_i64 = flatbuffers::nullopt, + flatbuffers::Optional maybe_i64 = flatbuffers::nullopt, int64_t default_i64 = 42LL, uint64_t just_u64 = 0, - flatbuffers::Optionalmaybe_u64 = flatbuffers::nullopt, + flatbuffers::Optional maybe_u64 = flatbuffers::nullopt, uint64_t default_u64 = 42ULL, float just_f32 = 0.0f, - flatbuffers::Optionalmaybe_f32 = flatbuffers::nullopt, + flatbuffers::Optional maybe_f32 = flatbuffers::nullopt, float default_f32 = 42.0f, double just_f64 = 0.0, - flatbuffers::Optionalmaybe_f64 = flatbuffers::nullopt, + flatbuffers::Optional maybe_f64 = flatbuffers::nullopt, double default_f64 = 42.0, bool just_bool = false, - flatbuffers::Optionalmaybe_bool = flatbuffers::nullopt, + flatbuffers::Optional maybe_bool = flatbuffers::nullopt, bool default_bool = true, optional_scalars::OptionalByte just_enum = optional_scalars::OptionalByte_None, + flatbuffers::Optional maybe_enum = flatbuffers::nullopt, optional_scalars::OptionalByte default_enum = optional_scalars::OptionalByte_One) { ScalarStuffBuilder builder_(_fbb); builder_.add_default_f64(default_f64); @@ -647,6 +665,7 @@ inline flatbuffers::Offset CreateScalarStuff( if(maybe_i16) { builder_.add_maybe_i16(*maybe_i16); } builder_.add_just_i16(just_i16); builder_.add_default_enum(default_enum); + if(maybe_enum) { builder_.add_maybe_enum(*maybe_enum); } builder_.add_just_enum(just_enum); builder_.add_default_bool(default_bool); if(maybe_bool) { builder_.add_maybe_bool(*maybe_bool); } @@ -705,6 +724,7 @@ inline void ScalarStuff::UnPackTo(ScalarStuffT *_o, const flatbuffers::resolver_ { auto _e = maybe_bool(); _o->maybe_bool = _e; } { auto _e = default_bool(); _o->default_bool = _e; } { auto _e = just_enum(); _o->just_enum = _e; } + { auto _e = maybe_enum(); _o->maybe_enum = _e; } { auto _e = default_enum(); _o->default_enum = _e; } } @@ -750,6 +770,7 @@ inline flatbuffers::Offset CreateScalarStuff(flatbuffers::FlatBuffe auto _maybe_bool = _o->maybe_bool; auto _default_bool = _o->default_bool; auto _just_enum = _o->just_enum; + auto _maybe_enum = _o->maybe_enum; auto _default_enum = _o->default_enum; return optional_scalars::CreateScalarStuff( _fbb, @@ -787,11 +808,13 @@ inline flatbuffers::Offset CreateScalarStuff(flatbuffers::FlatBuffe _maybe_bool, _default_bool, _just_enum, + _maybe_enum, _default_enum); } inline const flatbuffers::TypeTable *OptionalByteTypeTable() { static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_CHAR, 0, 0 }, { flatbuffers::ET_CHAR, 0, 0 }, { flatbuffers::ET_CHAR, 0, 0 } }; @@ -800,10 +823,11 @@ inline const flatbuffers::TypeTable *OptionalByteTypeTable() { }; static const char * const names[] = { "None", - "One" + "One", + "Two" }; static const flatbuffers::TypeTable tt = { - flatbuffers::ST_ENUM, 2, type_codes, type_refs, nullptr, nullptr, names + flatbuffers::ST_ENUM, 3, type_codes, type_refs, nullptr, nullptr, names }; return &tt; } @@ -844,6 +868,7 @@ inline const flatbuffers::TypeTable *ScalarStuffTypeTable() { { flatbuffers::ET_BOOL, 0, -1 }, { flatbuffers::ET_BOOL, 0, -1 }, { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 }, { flatbuffers::ET_CHAR, 0, 0 } }; static const flatbuffers::TypeFunction type_refs[] = { @@ -884,10 +909,11 @@ inline const flatbuffers::TypeTable *ScalarStuffTypeTable() { "maybe_bool", "default_bool", "just_enum", + "maybe_enum", "default_enum" }; static const flatbuffers::TypeTable tt = { - flatbuffers::ST_TABLE, 35, type_codes, type_refs, nullptr, nullptr, names + flatbuffers::ST_TABLE, 36, type_codes, type_refs, nullptr, nullptr, names }; return &tt; } @@ -953,4 +979,4 @@ inline flatbuffers::unique_ptr UnPackSizePrefixe } // namespace optional_scalars -#endif // FLATBUFFERS_GENERATED_OPTIONALSCALARS_OPTIONAL_SCALARS_H_ +#endif // FLATBUFFERS_GENERATED_OPTIONALSCALARS2_OPTIONAL_SCALARS_H_ diff --git a/tests/optional_scalars_generated.lobster b/tests/optional_scalars_generated.lobster deleted file mode 100644 index 3a9f0c533..000000000 --- a/tests/optional_scalars_generated.lobster +++ /dev/null @@ -1,198 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify -import flatbuffers - -namespace optional_scalars - -enum OptionalByte: - OptionalByte_None = 0 - OptionalByte_One = 1 - -class ScalarStuff - -class ScalarStuff : flatbuffers_handle - def just_i8(): - return buf_.flatbuffers_field_int8(pos_, 4, 0) - def maybe_i8(): - return buf_.flatbuffers_field_int8(pos_, 6, 0), buf_.flatbuffers_field_present(pos_, 6) - def default_i8(): - return buf_.flatbuffers_field_int8(pos_, 8, 42) - def just_u8(): - return buf_.flatbuffers_field_int8(pos_, 10, 0) - def maybe_u8(): - return buf_.flatbuffers_field_int8(pos_, 12, 0), buf_.flatbuffers_field_present(pos_, 12) - def default_u8(): - return buf_.flatbuffers_field_int8(pos_, 14, 42) - def just_i16(): - return buf_.flatbuffers_field_int16(pos_, 16, 0) - def maybe_i16(): - return buf_.flatbuffers_field_int16(pos_, 18, 0), buf_.flatbuffers_field_present(pos_, 18) - def default_i16(): - return buf_.flatbuffers_field_int16(pos_, 20, 42) - def just_u16(): - return buf_.flatbuffers_field_int16(pos_, 22, 0) - def maybe_u16(): - return buf_.flatbuffers_field_int16(pos_, 24, 0), buf_.flatbuffers_field_present(pos_, 24) - def default_u16(): - return buf_.flatbuffers_field_int16(pos_, 26, 42) - def just_i32(): - return buf_.flatbuffers_field_int32(pos_, 28, 0) - def maybe_i32(): - return buf_.flatbuffers_field_int32(pos_, 30, 0), buf_.flatbuffers_field_present(pos_, 30) - def default_i32(): - return buf_.flatbuffers_field_int32(pos_, 32, 42) - def just_u32(): - return buf_.flatbuffers_field_int32(pos_, 34, 0) - def maybe_u32(): - return buf_.flatbuffers_field_int32(pos_, 36, 0), buf_.flatbuffers_field_present(pos_, 36) - def default_u32(): - return buf_.flatbuffers_field_int32(pos_, 38, 42) - def just_i64(): - return buf_.flatbuffers_field_int64(pos_, 40, 0) - def maybe_i64(): - return buf_.flatbuffers_field_int64(pos_, 42, 0), buf_.flatbuffers_field_present(pos_, 42) - def default_i64(): - return buf_.flatbuffers_field_int64(pos_, 44, 42) - def just_u64(): - return buf_.flatbuffers_field_int64(pos_, 46, 0) - def maybe_u64(): - return buf_.flatbuffers_field_int64(pos_, 48, 0), buf_.flatbuffers_field_present(pos_, 48) - def default_u64(): - return buf_.flatbuffers_field_int64(pos_, 50, 42) - def just_f32(): - return buf_.flatbuffers_field_float32(pos_, 52, 0.0) - def maybe_f32(): - return buf_.flatbuffers_field_float32(pos_, 54, 0), buf_.flatbuffers_field_present(pos_, 54) - def default_f32(): - return buf_.flatbuffers_field_float32(pos_, 56, 42.0) - def just_f64(): - return buf_.flatbuffers_field_float64(pos_, 58, 0.0) - def maybe_f64(): - return buf_.flatbuffers_field_float64(pos_, 60, 0), buf_.flatbuffers_field_present(pos_, 60) - def default_f64(): - return buf_.flatbuffers_field_float64(pos_, 62, 42.0) - def just_bool(): - return buf_.flatbuffers_field_int8(pos_, 64, 0) - def maybe_bool(): - return buf_.flatbuffers_field_int8(pos_, 66, 0), buf_.flatbuffers_field_present(pos_, 66) - def default_bool(): - return buf_.flatbuffers_field_int8(pos_, 68, 1) - def just_enum(): - return OptionalByte(buf_.flatbuffers_field_int8(pos_, 70, 0)) - def default_enum(): - return OptionalByte(buf_.flatbuffers_field_int8(pos_, 72, 1)) - -def GetRootAsScalarStuff(buf:string): return ScalarStuff { buf, buf.flatbuffers_indirect(0) } - -struct ScalarStuffBuilder: - b_:flatbuffers_builder - def start(): - b_.StartObject(35) - return this - def add_just_i8(just_i8:int): - b_.PrependInt8Slot(0, just_i8, 0) - return this - def add_maybe_i8(maybe_i8:int): - b_.PrependInt8Slot(1, maybe_i8) - return this - def add_default_i8(default_i8:int): - b_.PrependInt8Slot(2, default_i8, 42) - return this - def add_just_u8(just_u8:int): - b_.PrependUint8Slot(3, just_u8, 0) - return this - def add_maybe_u8(maybe_u8:int): - b_.PrependUint8Slot(4, maybe_u8) - return this - def add_default_u8(default_u8:int): - b_.PrependUint8Slot(5, default_u8, 42) - return this - def add_just_i16(just_i16:int): - b_.PrependInt16Slot(6, just_i16, 0) - return this - def add_maybe_i16(maybe_i16:int): - b_.PrependInt16Slot(7, maybe_i16) - return this - def add_default_i16(default_i16:int): - b_.PrependInt16Slot(8, default_i16, 42) - return this - def add_just_u16(just_u16:int): - b_.PrependUint16Slot(9, just_u16, 0) - return this - def add_maybe_u16(maybe_u16:int): - b_.PrependUint16Slot(10, maybe_u16) - return this - def add_default_u16(default_u16:int): - b_.PrependUint16Slot(11, default_u16, 42) - return this - def add_just_i32(just_i32:int): - b_.PrependInt32Slot(12, just_i32, 0) - return this - def add_maybe_i32(maybe_i32:int): - b_.PrependInt32Slot(13, maybe_i32) - return this - def add_default_i32(default_i32:int): - b_.PrependInt32Slot(14, default_i32, 42) - return this - def add_just_u32(just_u32:int): - b_.PrependUint32Slot(15, just_u32, 0) - return this - def add_maybe_u32(maybe_u32:int): - b_.PrependUint32Slot(16, maybe_u32) - return this - def add_default_u32(default_u32:int): - b_.PrependUint32Slot(17, default_u32, 42) - return this - def add_just_i64(just_i64:int): - b_.PrependInt64Slot(18, just_i64, 0) - return this - def add_maybe_i64(maybe_i64:int): - b_.PrependInt64Slot(19, maybe_i64) - return this - def add_default_i64(default_i64:int): - b_.PrependInt64Slot(20, default_i64, 42) - return this - def add_just_u64(just_u64:int): - b_.PrependUint64Slot(21, just_u64, 0) - return this - def add_maybe_u64(maybe_u64:int): - b_.PrependUint64Slot(22, maybe_u64) - return this - def add_default_u64(default_u64:int): - b_.PrependUint64Slot(23, default_u64, 42) - return this - def add_just_f32(just_f32:float): - b_.PrependFloat32Slot(24, just_f32, 0.0) - return this - def add_maybe_f32(maybe_f32:float): - b_.PrependFloat32Slot(25, maybe_f32) - return this - def add_default_f32(default_f32:float): - b_.PrependFloat32Slot(26, default_f32, 42.0) - return this - def add_just_f64(just_f64:float): - b_.PrependFloat64Slot(27, just_f64, 0.0) - return this - def add_maybe_f64(maybe_f64:float): - b_.PrependFloat64Slot(28, maybe_f64) - return this - def add_default_f64(default_f64:float): - b_.PrependFloat64Slot(29, default_f64, 42.0) - return this - def add_just_bool(just_bool:int): - b_.PrependBoolSlot(30, just_bool, 0) - return this - def add_maybe_bool(maybe_bool:int): - b_.PrependBoolSlot(31, maybe_bool) - return this - def add_default_bool(default_bool:int): - b_.PrependBoolSlot(32, default_bool, 1) - return this - def add_just_enum(just_enum:OptionalByte): - b_.PrependInt8Slot(33, just_enum, 0) - return this - def add_default_enum(default_enum:OptionalByte): - b_.PrependInt8Slot(34, default_enum, 1) - return this - def end(): - return b_.EndObject() - diff --git a/tests/test.cpp b/tests/test.cpp index 602438cb6..f8cb020d5 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -34,7 +34,7 @@ #include "namespace_test/namespace_test2_generated.h" #include "union_vector/union_vector_generated.h" #include "monster_extra_generated.h" -#include "optional_scalars_generated.h" +#include "optional_scalars2_generated.h" #if !defined(_MSC_VER) || _MSC_VER >= 1700 # include "arrays_test_generated.h" # include "evolution_test/evolution_v1_generated.h" @@ -3476,6 +3476,12 @@ void OptionalScalarsTest() { schemas.push_back("table Monster { mana : bool; }"); schemas.push_back("table Monster { mana : bool = 42; }"); schemas.push_back("table Monster { mana : bool = null; }"); + schemas.push_back("enum Enum: int {A=0, B=1} " + "table Monster { mana : Enum; }"); + schemas.push_back("enum Enum: int {A=0, B=1} " + "table Monster { mana : Enum = B; }"); + schemas.push_back("enum Enum: int {A=0, B=1} " + "table Monster { mana : Enum = null; }"); // Check the FieldDef is correctly set. for (auto schema = schemas.begin(); schema < schemas.end(); schema++) { @@ -3524,6 +3530,7 @@ void OptionalScalarsTest() { TEST_ASSERT(obj.maybe_i8.has_value() && obj.maybe_i8.value() == 3); TEST_ASSERT(obj.maybe_i8 && *obj.maybe_i8 == 3); obj.maybe_i32 = -1; + obj.maybe_enum = optional_scalars::OptionalByte_Two; fbb.Clear(); FinishScalarStuffBuffer(fbb, optional_scalars::ScalarStuff::Pack(fbb, &obj)); @@ -3532,6 +3539,7 @@ void OptionalScalarsTest() { TEST_EQ(opts->maybe_i8().value(), 3); TEST_ASSERT(opts->maybe_i32().has_value()); TEST_EQ(opts->maybe_i32().value(), -1); + TEST_EQ(opts->maybe_enum().value(), optional_scalars::OptionalByte_Two); TEST_ASSERT(opts->maybe_i32() == flatbuffers::Optional(-1)); }