Add constant accessors to C++ unions (#4433)

* Add constant accessors to C++ unions

* Remove redundant const pointer return type

* Update generate_code.bat to reflect generate_code.sh

* Add updated generated files

* Remove extra space from generated code

* Update generated files

* Change directory back to tests after generating code
This commit is contained in:
endorph-soft 2017-09-22 06:36:20 +09:30 committed by Wouter van Oortmerssen
parent 28920aff8f
commit 21a8121982
5 changed files with 54 additions and 3 deletions

View File

@ -121,6 +121,10 @@ struct EquipmentUnion {
return type == Equipment_Weapon ?
reinterpret_cast<WeaponT *>(value) : nullptr;
}
const WeaponT *AsWeapon() const {
return type == Equipment_Weapon ?
reinterpret_cast<const WeaponT *>(value) : nullptr;
}
};
bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *obj, Equipment type);

View File

@ -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<const {{NATIVE_TYPE}} *>(value) : nullptr;";
code_ += " }";
}
code_ += "};";
code_ += "";

View File

@ -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

View File

@ -162,14 +162,26 @@ struct AnyUnion {
return type == Any_Monster ?
reinterpret_cast<MonsterT *>(value) : nullptr;
}
const MonsterT *AsMonster() const {
return type == Any_Monster ?
reinterpret_cast<const MonsterT *>(value) : nullptr;
}
TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() {
return type == Any_TestSimpleTableWithEnum ?
reinterpret_cast<TestSimpleTableWithEnumT *>(value) : nullptr;
}
const TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() const {
return type == Any_TestSimpleTableWithEnum ?
reinterpret_cast<const TestSimpleTableWithEnumT *>(value) : nullptr;
}
MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() {
return type == Any_MyGame_Example2_Monster ?
reinterpret_cast<MyGame::Example2::MonsterT *>(value) : nullptr;
}
const MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() const {
return type == Any_MyGame_Example2_Monster ?
reinterpret_cast<const MyGame::Example2::MonsterT *>(value) : nullptr;
}
};
bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type);

View File

@ -84,26 +84,50 @@ struct CharacterUnion {
return type == Character_MuLan ?
reinterpret_cast<AttackerT *>(value) : nullptr;
}
const AttackerT *AsMuLan() const {
return type == Character_MuLan ?
reinterpret_cast<const AttackerT *>(value) : nullptr;
}
Rapunzel *AsRapunzel() {
return type == Character_Rapunzel ?
reinterpret_cast<Rapunzel *>(value) : nullptr;
}
const Rapunzel *AsRapunzel() const {
return type == Character_Rapunzel ?
reinterpret_cast<const Rapunzel *>(value) : nullptr;
}
BookReader *AsBelle() {
return type == Character_Belle ?
reinterpret_cast<BookReader *>(value) : nullptr;
}
const BookReader *AsBelle() const {
return type == Character_Belle ?
reinterpret_cast<const BookReader *>(value) : nullptr;
}
BookReader *AsBookFan() {
return type == Character_BookFan ?
reinterpret_cast<BookReader *>(value) : nullptr;
}
const BookReader *AsBookFan() const {
return type == Character_BookFan ?
reinterpret_cast<const BookReader *>(value) : nullptr;
}
std::string *AsOther() {
return type == Character_Other ?
reinterpret_cast<std::string *>(value) : nullptr;
}
const std::string *AsOther() const {
return type == Character_Other ?
reinterpret_cast<const std::string *>(value) : nullptr;
}
std::string *AsUnused() {
return type == Character_Unused ?
reinterpret_cast<std::string *>(value) : nullptr;
}
const std::string *AsUnused() const {
return type == Character_Unused ?
reinterpret_cast<const std::string *>(value) : nullptr;
}
};
bool VerifyCharacter(flatbuffers::Verifier &verifier, const void *obj, Character type);