sample_binary.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <contrib/libs/flatbuffers/samples/monster.fbs.h>
  17. using namespace MyGame::Sample;
  18. // Example how to use FlatBuffers to create and read binary buffers.
  19. int main(int /*argc*/, const char * /*argv*/[]) {
  20. // Build up a serialized buffer algorithmically:
  21. flatbuffers::FlatBufferBuilder builder;
  22. // First, lets serialize some weapons for the Monster: A 'sword' and an 'axe'.
  23. auto weapon_one_name = builder.CreateString("Sword");
  24. short weapon_one_damage = 3;
  25. auto weapon_two_name = builder.CreateString("Axe");
  26. short weapon_two_damage = 5;
  27. // Use the `CreateWeapon` shortcut to create Weapons with all fields set.
  28. auto sword = CreateWeapon(builder, weapon_one_name, weapon_one_damage);
  29. auto axe = CreateWeapon(builder, weapon_two_name, weapon_two_damage);
  30. // Create a FlatBuffer's `vector` from the `std::vector`.
  31. std::vector<flatbuffers::Offset<Weapon>> weapons_vector;
  32. weapons_vector.push_back(sword);
  33. weapons_vector.push_back(axe);
  34. auto weapons = builder.CreateVector(weapons_vector);
  35. // Second, serialize the rest of the objects needed by the Monster.
  36. auto position = Vec3(1.0f, 2.0f, 3.0f);
  37. auto name = builder.CreateString("MyMonster");
  38. unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  39. auto inventory = builder.CreateVector(inv_data, 10);
  40. // Shortcut for creating monster with all fields set:
  41. auto orc = CreateMonster(builder, &position, 150, 80, name, inventory,
  42. Color_Red, weapons, Equipment_Weapon, axe.Union());
  43. builder.Finish(orc); // Serialize the root of the object.
  44. // We now have a FlatBuffer we can store on disk or send over a network.
  45. // ** file/network code goes here :) **
  46. // access builder.GetBufferPointer() for builder.GetSize() bytes
  47. // Instead, we're going to access it right away (as if we just received it).
  48. // Get access to the root:
  49. auto monster = GetMonster(builder.GetBufferPointer());
  50. // Get and test some scalar types from the FlatBuffer.
  51. assert(monster->hp() == 80);
  52. assert(monster->mana() == 150); // default
  53. assert(monster->name()->str() == "MyMonster");
  54. // Get and test a field of the FlatBuffer's `struct`.
  55. auto pos = monster->pos();
  56. assert(pos);
  57. assert(pos->z() == 3.0f);
  58. (void)pos;
  59. // Get a test an element from the `inventory` FlatBuffer's `vector`.
  60. auto inv = monster->inventory();
  61. assert(inv);
  62. assert(inv->Get(9) == 9);
  63. (void)inv;
  64. // Get and test the `weapons` FlatBuffers's `vector`.
  65. std::string expected_weapon_names[] = { "Sword", "Axe" };
  66. short expected_weapon_damages[] = { 3, 5 };
  67. auto weps = monster->weapons();
  68. for (unsigned int i = 0; i < weps->size(); i++) {
  69. assert(weps->Get(i)->name()->str() == expected_weapon_names[i]);
  70. assert(weps->Get(i)->damage() == expected_weapon_damages[i]);
  71. }
  72. (void)expected_weapon_names;
  73. (void)expected_weapon_damages;
  74. // Get and test the `Equipment` union (`equipped` field).
  75. assert(monster->equipped_type() == Equipment_Weapon);
  76. auto equipped = static_cast<const Weapon *>(monster->equipped());
  77. assert(equipped->name()->str() == "Axe");
  78. assert(equipped->damage() == 5);
  79. (void)equipped;
  80. printf("The FlatBuffer was successfully created and verified!\n");
  81. }