Initial commit
This commit is contained in:
38
flatbuffers/all/test_package/CMakeLists.txt
Normal file
38
flatbuffers/all/test_package/CMakeLists.txt
Normal file
@@ -0,0 +1,38 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
project(test_package LANGUAGES CXX)
|
||||
|
||||
find_package(FlatBuffers REQUIRED CONFIG)
|
||||
if(TARGET flatbuffers::flatbuffers_shared)
|
||||
set(FLATBUFFERS_TARGET flatbuffers::flatbuffers_shared)
|
||||
else()
|
||||
set(FLATBUFFERS_TARGET flatbuffers::flatbuffers)
|
||||
endif()
|
||||
|
||||
add_executable(${PROJECT_NAME} test_package.cpp)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE ${FLATBUFFERS_TARGET})
|
||||
if(FLATBUFFERS_HEADER_ONLY)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE FLATBUFFERS_HEADER_ONLY)
|
||||
endif()
|
||||
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
|
||||
|
||||
# Testing this block in case of cross-build would require to add flatbuffers to build requirements of test package
|
||||
# But due to c3i limitations, package id of build requirement if header_only is not available while building in c3i
|
||||
if(NOT CMAKE_CROSSCOMPILING)
|
||||
add_executable(sample_binary sample_binary.cpp)
|
||||
target_link_libraries(sample_binary PRIVATE ${FLATBUFFERS_TARGET})
|
||||
target_compile_features(sample_binary PRIVATE cxx_std_11)
|
||||
|
||||
set(MONSTER_GENERATED_HEADER ${CMAKE_CURRENT_BINARY_DIR}/monster_generated.h)
|
||||
set(MONSTER_FBS ${CMAKE_CURRENT_SOURCE_DIR}/monster.fbs)
|
||||
add_custom_command(
|
||||
OUTPUT ${MONSTER_GENERATED_HEADER}
|
||||
COMMAND $<TARGET_FILE:flatbuffers::flatc>
|
||||
--cpp
|
||||
-o ${CMAKE_CURRENT_BINARY_DIR}
|
||||
${MONSTER_FBS}
|
||||
DEPENDS ${MONSTER_FBS}
|
||||
)
|
||||
add_custom_target(generate_monster_header DEPENDS ${MONSTER_GENERATED_HEADER})
|
||||
add_dependencies(sample_binary generate_monster_header)
|
||||
target_include_directories(sample_binary PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
|
||||
endif()
|
||||
39
flatbuffers/all/test_package/conanfile.py
Normal file
39
flatbuffers/all/test_package/conanfile.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from conan import ConanFile
|
||||
from conan.tools.build import can_run
|
||||
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
|
||||
from conan.tools.env import VirtualRunEnv
|
||||
import os
|
||||
|
||||
|
||||
class TestPackageConan(ConanFile):
|
||||
settings = "os", "arch", "compiler", "build_type"
|
||||
generators = "CMakeDeps"
|
||||
test_type = "explicit"
|
||||
|
||||
def layout(self):
|
||||
cmake_layout(self)
|
||||
|
||||
def requirements(self):
|
||||
self.requires(self.tested_reference_str, run=can_run(self))
|
||||
|
||||
def generate(self):
|
||||
env = VirtualRunEnv(self)
|
||||
env.generate()
|
||||
if can_run(self):
|
||||
env = VirtualRunEnv(self)
|
||||
env.generate(scope="build")
|
||||
tc = CMakeToolchain(self)
|
||||
tc.variables["FLATBUFFERS_HEADER_ONLY"] = self.dependencies["flatbuffers"].options.header_only
|
||||
tc.generate()
|
||||
|
||||
def build(self):
|
||||
cmake = CMake(self)
|
||||
cmake.configure()
|
||||
cmake.build()
|
||||
|
||||
def test(self):
|
||||
if can_run(self):
|
||||
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
|
||||
self.run(bin_path, env="conanrun")
|
||||
sample_binary = os.path.join(self.cpp.build.bindirs[0], "sample_binary")
|
||||
self.run(sample_binary, env="conanrun")
|
||||
33
flatbuffers/all/test_package/monster.fbs
Normal file
33
flatbuffers/all/test_package/monster.fbs
Normal file
@@ -0,0 +1,33 @@
|
||||
// Example IDL file for our monster's schema.
|
||||
|
||||
namespace MyGame.Sample;
|
||||
|
||||
enum Color:byte { Red = 0, Green, Blue = 2 }
|
||||
|
||||
union Equipment { Weapon } // Optionally add more tables.
|
||||
|
||||
struct Vec3 {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3;
|
||||
mana:short = 150;
|
||||
hp:short = 100;
|
||||
name:string;
|
||||
friendly:bool = false (deprecated);
|
||||
inventory:[ubyte];
|
||||
color:Color = Blue;
|
||||
weapons:[Weapon];
|
||||
equipped:Equipment;
|
||||
path:[Vec3];
|
||||
}
|
||||
|
||||
table Weapon {
|
||||
name:string;
|
||||
damage:short;
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
104
flatbuffers/all/test_package/sample_binary.cpp
Normal file
104
flatbuffers/all/test_package/sample_binary.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2015 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
|
||||
|
||||
using namespace MyGame::Sample;
|
||||
|
||||
// Example how to use FlatBuffers to create and read binary buffers.
|
||||
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
// Build up a serialized buffer algorithmically:
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
// First, lets serialize some weapons for the Monster: A 'sword' and an 'axe'.
|
||||
auto weapon_one_name = builder.CreateString("Sword");
|
||||
short weapon_one_damage = 3;
|
||||
|
||||
auto weapon_two_name = builder.CreateString("Axe");
|
||||
short weapon_two_damage = 5;
|
||||
|
||||
// Use the `CreateWeapon` shortcut to create Weapons with all fields set.
|
||||
auto sword = CreateWeapon(builder, weapon_one_name, weapon_one_damage);
|
||||
auto axe = CreateWeapon(builder, weapon_two_name, weapon_two_damage);
|
||||
|
||||
// Create a FlatBuffer's `vector` from the `std::vector`.
|
||||
std::vector<flatbuffers::Offset<Weapon>> weapons_vector;
|
||||
weapons_vector.push_back(sword);
|
||||
weapons_vector.push_back(axe);
|
||||
auto weapons = builder.CreateVector(weapons_vector);
|
||||
|
||||
// Second, serialize the rest of the objects needed by the Monster.
|
||||
auto position = Vec3(1.0f, 2.0f, 3.0f);
|
||||
|
||||
auto name = builder.CreateString("MyMonster");
|
||||
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
auto inventory = builder.CreateVector(inv_data, 10);
|
||||
|
||||
// Shortcut for creating monster with all fields set:
|
||||
auto orc = CreateMonster(builder, &position, 150, 80, name, inventory,
|
||||
Color_Red, weapons, Equipment_Weapon, axe.Union());
|
||||
|
||||
builder.Finish(orc); // Serialize the root of the object.
|
||||
|
||||
// We now have a FlatBuffer we can store on disk or send over a network.
|
||||
|
||||
// ** file/network code goes here :) **
|
||||
// access builder.GetBufferPointer() for builder.GetSize() bytes
|
||||
|
||||
// Instead, we're going to access it right away (as if we just received it).
|
||||
|
||||
// Get access to the root:
|
||||
auto monster = GetMonster(builder.GetBufferPointer());
|
||||
|
||||
// Get and test some scalar types from the FlatBuffer.
|
||||
assert(monster->hp() == 80);
|
||||
assert(monster->mana() == 150); // default
|
||||
assert(monster->name()->str() == "MyMonster");
|
||||
|
||||
// Get and test a field of the FlatBuffer's `struct`.
|
||||
auto pos = monster->pos();
|
||||
assert(pos);
|
||||
assert(pos->z() == 3.0f);
|
||||
(void)pos;
|
||||
|
||||
// Get a test an element from the `inventory` FlatBuffer's `vector`.
|
||||
auto inv = monster->inventory();
|
||||
assert(inv);
|
||||
assert(inv->Get(9) == 9);
|
||||
(void)inv;
|
||||
|
||||
// Get and test the `weapons` FlatBuffers's `vector`.
|
||||
std::string expected_weapon_names[] = { "Sword", "Axe" };
|
||||
short expected_weapon_damages[] = { 3, 5 };
|
||||
auto weps = monster->weapons();
|
||||
for (unsigned int i = 0; i < weps->size(); i++) {
|
||||
assert(weps->Get(i)->name()->str() == expected_weapon_names[i]);
|
||||
assert(weps->Get(i)->damage() == expected_weapon_damages[i]);
|
||||
}
|
||||
(void)expected_weapon_names;
|
||||
(void)expected_weapon_damages;
|
||||
|
||||
// Get and test the `Equipment` union (`equipped` field).
|
||||
assert(monster->equipped_type() == Equipment_Weapon);
|
||||
auto equipped = static_cast<const Weapon *>(monster->equipped());
|
||||
assert(equipped->name()->str() == "Axe");
|
||||
assert(equipped->damage() == 5);
|
||||
(void)equipped;
|
||||
|
||||
printf("The FlatBuffer was successfully created and verified!\n");
|
||||
}
|
||||
48
flatbuffers/all/test_package/test_package.cpp
Normal file
48
flatbuffers/all/test_package/test_package.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2018 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
#ifndef FLATBUFFERS_HEADER_ONLY
|
||||
#include "flatbuffers/util.h"
|
||||
#endif
|
||||
// Test to validate Conan package generated
|
||||
|
||||
int main(int /*argc*/, const char * /*argv*/ []) {
|
||||
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
const auto offset = builder.CreateString("test");
|
||||
if (!offset.IsNull()) {
|
||||
std::cout << "Offset is " << builder.CreateString("test").o << ".\n";
|
||||
} else {
|
||||
std::cout << "Offset is null.\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#ifndef FLATBUFFERS_HEADER_ONLY
|
||||
const std::string filename("conanbuildinfo.txt");
|
||||
if (flatbuffers::FileExists(filename.c_str())) {
|
||||
std::cout << "File " << filename << " exists.\n";
|
||||
} else {
|
||||
std::cout << "File " << filename << " does not exist.\n";
|
||||
}
|
||||
#endif
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user