Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h". |
| 18 | |
| 19 | using namespace MyGame::Sample; |
| 20 | |
| 21 | // Example how to use FlatBuffers to create and read binary buffers. |
| 22 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 23 | int main(int /*argc*/, const char * /*argv*/[]) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 24 | // Build up a serialized buffer algorithmically: |
| 25 | flatbuffers::FlatBufferBuilder builder; |
| 26 | |
| 27 | // First, lets serialize some weapons for the Monster: A 'sword' and an 'axe'. |
| 28 | auto weapon_one_name = builder.CreateString("Sword"); |
| 29 | short weapon_one_damage = 3; |
| 30 | |
| 31 | auto weapon_two_name = builder.CreateString("Axe"); |
| 32 | short weapon_two_damage = 5; |
| 33 | |
| 34 | // Use the `CreateWeapon` shortcut to create Weapons with all fields set. |
| 35 | auto sword = CreateWeapon(builder, weapon_one_name, weapon_one_damage); |
| 36 | auto axe = CreateWeapon(builder, weapon_two_name, weapon_two_damage); |
| 37 | |
| 38 | // Create a FlatBuffer's `vector` from the `std::vector`. |
| 39 | std::vector<flatbuffers::Offset<Weapon>> weapons_vector; |
| 40 | weapons_vector.push_back(sword); |
| 41 | weapons_vector.push_back(axe); |
| 42 | auto weapons = builder.CreateVector(weapons_vector); |
| 43 | |
| 44 | // Second, serialize the rest of the objects needed by the Monster. |
| 45 | auto position = Vec3(1.0f, 2.0f, 3.0f); |
| 46 | |
| 47 | auto name = builder.CreateString("MyMonster"); |
| 48 | |
| 49 | unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 50 | auto inventory = builder.CreateVector(inv_data, 10); |
| 51 | |
| 52 | // Shortcut for creating monster with all fields set: |
| 53 | auto orc = CreateMonster(builder, &position, 150, 80, name, inventory, |
| 54 | Color_Red, weapons, Equipment_Weapon, axe.Union()); |
| 55 | |
| 56 | builder.Finish(orc); // Serialize the root of the object. |
| 57 | |
| 58 | // We now have a FlatBuffer we can store on disk or send over a network. |
| 59 | |
| 60 | // ** file/network code goes here :) ** |
| 61 | // access builder.GetBufferPointer() for builder.GetSize() bytes |
| 62 | |
| 63 | // Instead, we're going to access it right away (as if we just received it). |
| 64 | |
| 65 | // Get access to the root: |
| 66 | auto monster = GetMonster(builder.GetBufferPointer()); |
| 67 | |
| 68 | // Get and test some scalar types from the FlatBuffer. |
| 69 | assert(monster->hp() == 80); |
| 70 | assert(monster->mana() == 150); // default |
| 71 | assert(monster->name()->str() == "MyMonster"); |
| 72 | |
| 73 | // Get and test a field of the FlatBuffer's `struct`. |
| 74 | auto pos = monster->pos(); |
| 75 | assert(pos); |
| 76 | assert(pos->z() == 3.0f); |
| 77 | (void)pos; |
| 78 | |
| 79 | // Get a test an element from the `inventory` FlatBuffer's `vector`. |
| 80 | auto inv = monster->inventory(); |
| 81 | assert(inv); |
| 82 | assert(inv->Get(9) == 9); |
| 83 | (void)inv; |
| 84 | |
| 85 | // Get and test the `weapons` FlatBuffers's `vector`. |
| 86 | std::string expected_weapon_names[] = { "Sword", "Axe" }; |
| 87 | short expected_weapon_damages[] = { 3, 5 }; |
| 88 | auto weps = monster->weapons(); |
| 89 | for (unsigned int i = 0; i < weps->size(); i++) { |
| 90 | assert(weps->Get(i)->name()->str() == expected_weapon_names[i]); |
| 91 | assert(weps->Get(i)->damage() == expected_weapon_damages[i]); |
| 92 | } |
| 93 | (void)expected_weapon_names; |
| 94 | (void)expected_weapon_damages; |
| 95 | |
| 96 | // Get and test the `Equipment` union (`equipped` field). |
| 97 | assert(monster->equipped_type() == Equipment_Weapon); |
| 98 | auto equipped = static_cast<const Weapon *>(monster->equipped()); |
| 99 | assert(equipped->name()->str() == "Axe"); |
| 100 | assert(equipped->damage() == 5); |
| 101 | (void)equipped; |
| 102 | |
| 103 | printf("The FlatBuffer was successfully created and verified!\n"); |
| 104 | } |