diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 2f3141bb1..523a136b8 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -121,6 +121,10 @@ struct EquipmentUnion { return type == Equipment_Weapon ? reinterpret_cast(value) : nullptr; } + const WeaponT *AsWeapon() const { + return type == Equipment_Weapon ? + reinterpret_cast(value) : nullptr; + } }; bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *obj, Equipment type); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 4cdcff5b0..29f93d3ce 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -759,6 +759,11 @@ class CppGenerator : public BaseGenerator { code_ += " return type == {{NATIVE_ID}} ?"; code_ += " reinterpret_cast<{{NATIVE_TYPE}} *>(value) : nullptr;"; code_ += " }"; + + code_ += " const {{NATIVE_TYPE}} *As{{NATIVE_NAME}}() const {"; + code_ += " return type == {{NATIVE_ID}} ?"; + code_ += " reinterpret_cast(value) : nullptr;"; + code_ += " }"; } code_ += "};"; code_ += ""; diff --git a/tests/generate_code.bat b/tests/generate_code.bat index ca97b1b67..f594542b1 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -15,7 +15,13 @@ set buildtype=Release if "%1"=="-b" set buildtype=%2 -..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --php --grpc --gen-mutable --gen-object-api --no-includes -I include_test monster_test.fbs monsterdata_test.json -..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --php --gen-mutable -o namespace_test namespace_test\namespace_test1.fbs namespace_test\namespace_test2.fbs -..\%buildtype%\flatc.exe --binary --schema -I include_test monster_test.fbs +..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --ts --php --grpc --gen-mutable --gen-object-api --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json +..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --ts --php --gen-mutable --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs +..\%buildtype%\flatc.exe --cpp --js --ts --php --gen-mutable --gen-object-api --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs +..\%buildtype%\flatc.exe -b --schema --bfbs-comments -I include_test monster_test.fbs ..\%buildtype%\flatc.exe --jsonschema --schema -I include_test monster_test.fbs +cd ../samples +..\%buildtype%\flatc.exe --cpp --gen-mutable --gen-object-api --cpp-ptr-type flatbuffers::unique_ptr monster.fbs +cd ../reflection + +cd ../tests \ No newline at end of file diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 3580cca14..3423a1089 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -162,14 +162,26 @@ struct AnyUnion { return type == Any_Monster ? reinterpret_cast(value) : nullptr; } + const MonsterT *AsMonster() const { + return type == Any_Monster ? + reinterpret_cast(value) : nullptr; + } TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() { return type == Any_TestSimpleTableWithEnum ? reinterpret_cast(value) : nullptr; } + const TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() const { + return type == Any_TestSimpleTableWithEnum ? + reinterpret_cast(value) : nullptr; + } MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() { return type == Any_MyGame_Example2_Monster ? reinterpret_cast(value) : nullptr; } + const MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() const { + return type == Any_MyGame_Example2_Monster ? + reinterpret_cast(value) : nullptr; + } }; bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type); diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index 213053e1c..a21cb0009 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -84,26 +84,50 @@ struct CharacterUnion { return type == Character_MuLan ? reinterpret_cast(value) : nullptr; } + const AttackerT *AsMuLan() const { + return type == Character_MuLan ? + reinterpret_cast(value) : nullptr; + } Rapunzel *AsRapunzel() { return type == Character_Rapunzel ? reinterpret_cast(value) : nullptr; } + const Rapunzel *AsRapunzel() const { + return type == Character_Rapunzel ? + reinterpret_cast(value) : nullptr; + } BookReader *AsBelle() { return type == Character_Belle ? reinterpret_cast(value) : nullptr; } + const BookReader *AsBelle() const { + return type == Character_Belle ? + reinterpret_cast(value) : nullptr; + } BookReader *AsBookFan() { return type == Character_BookFan ? reinterpret_cast(value) : nullptr; } + const BookReader *AsBookFan() const { + return type == Character_BookFan ? + reinterpret_cast(value) : nullptr; + } std::string *AsOther() { return type == Character_Other ? reinterpret_cast(value) : nullptr; } + const std::string *AsOther() const { + return type == Character_Other ? + reinterpret_cast(value) : nullptr; + } std::string *AsUnused() { return type == Character_Unused ? reinterpret_cast(value) : nullptr; } + const std::string *AsUnused() const { + return type == Character_Unused ? + reinterpret_cast(value) : nullptr; + } }; bool VerifyCharacter(flatbuffers::Verifier &verifier, const void *obj, Character type);