Initial commit
This commit is contained in:
31
flatbuffers/all/cmake/FlatcTargets.cmake
Normal file
31
flatbuffers/all/cmake/FlatcTargets.cmake
Normal file
@@ -0,0 +1,31 @@
|
||||
if(NOT TARGET flatbuffers::flatc)
|
||||
if(CMAKE_CROSSCOMPILING)
|
||||
find_program(FLATBUFFERS_FLATC_EXECUTABLE
|
||||
NAMES flatc
|
||||
PATHS ENV PATH
|
||||
NO_DEFAULT_PATH
|
||||
)
|
||||
else()
|
||||
find_program(FLATBUFFERS_FLATC_EXECUTABLE
|
||||
NAMES flatc
|
||||
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../bin/"
|
||||
NO_DEFAULT_PATH
|
||||
)
|
||||
endif()
|
||||
# TODO: In conan v2 with CMakeToolchain, can be replaced by:
|
||||
# find_program(FLATBUFFERS_FLATC_EXECUTABLE NAMES flatc)
|
||||
# # Nice enough to handle flatbuffers not in build_requires for native build
|
||||
# if(NOT FLATBUFFERS_FLATC_EXECUTABLE AND NOT CMAKE_CROSSCOMPILING)
|
||||
# find_program(FLATBUFFERS_FLATC_EXECUTABLE
|
||||
# NAMES flatc
|
||||
# PATHS "${CMAKE_CURRENT_LIST_DIR}/../../bin/"
|
||||
# NO_DEFAULT_PATH
|
||||
# )
|
||||
# endif()
|
||||
|
||||
if(FLATBUFFERS_FLATC_EXECUTABLE)
|
||||
get_filename_component(FLATBUFFERS_FLATC_EXECUTABLE "${FLATBUFFERS_FLATC_EXECUTABLE}" ABSOLUTE)
|
||||
add_executable(flatbuffers::flatc IMPORTED)
|
||||
set_property(TARGET flatbuffers::flatc PROPERTY IMPORTED_LOCATION ${FLATBUFFERS_FLATC_EXECUTABLE})
|
||||
endif()
|
||||
endif()
|
||||
62
flatbuffers/all/conandata.yml
Normal file
62
flatbuffers/all/conandata.yml
Normal file
@@ -0,0 +1,62 @@
|
||||
sources:
|
||||
"25.12.19":
|
||||
url: "https://github.com/google/flatbuffers/archive/v25.12.19.tar.gz"
|
||||
sha256: "f81c3162b1046fe8b84b9a0dbdd383e24fdbcf88583b9cb6028f90d04d90696a"
|
||||
"25.9.23":
|
||||
url: "https://github.com/google/flatbuffers/archive/v25.9.23.tar.gz"
|
||||
sha256: "9102253214dea6ae10c2ac966ea1ed2155d22202390b532d1dea64935c518ada"
|
||||
"25.2.10":
|
||||
url: "https://github.com/google/flatbuffers/archive/v25.2.10.tar.gz"
|
||||
sha256: "b9c2df49707c57a48fc0923d52b8c73beb72d675f9d44b2211e4569be40a7421"
|
||||
"24.12.23":
|
||||
url: "https://github.com/google/flatbuffers/archive/v24.12.23.tar.gz"
|
||||
sha256: "7e2ef35f1af9e2aa0c6a7d0a09298c2cb86caf3d4f58c0658b306256e5bcab10"
|
||||
"24.3.25":
|
||||
url: "https://github.com/google/flatbuffers/archive/v24.3.25.tar.gz"
|
||||
sha256: "4157c5cacdb59737c5d627e47ac26b140e9ee28b1102f812b36068aab728c1ed"
|
||||
"24.3.7":
|
||||
url: "https://github.com/google/flatbuffers/archive/v24.3.7.tar.gz"
|
||||
sha256: "bfff9d2150fcff88f844e8c608b02b2a0e94c92aea39b04c0624783464304784"
|
||||
"23.5.26":
|
||||
url: "https://github.com/google/flatbuffers/archive/v23.5.26.tar.gz"
|
||||
sha256: "1cce06b17cddd896b6d73cc047e36a254fb8df4d7ea18a46acf16c4c0cd3f3f3"
|
||||
"23.3.3":
|
||||
url: "https://github.com/google/flatbuffers/archive/v23.3.3.tar.gz"
|
||||
sha256: "8aff985da30aaab37edf8e5b02fda33ed4cbdd962699a8e2af98fdef306f4e4d"
|
||||
"23.1.21":
|
||||
url: "https://github.com/google/flatbuffers/archive/v23.1.21.tar.gz"
|
||||
sha256: "d84cb25686514348e615163b458ae0767001b24b42325f426fd56406fd384238"
|
||||
"22.12.06":
|
||||
url: "https://github.com/google/flatbuffers/archive/v22.12.06.tar.gz"
|
||||
sha256: "209823306f2cbedab6ff70997e0d236fcfd1864ca9ad082cbfdb196e7386daed"
|
||||
"22.11.23":
|
||||
url: "https://github.com/google/flatbuffers/archive/v22.11.23.tar.gz"
|
||||
sha256: "8e9bacc942db59ca89a383dd7923f3e69a377d6e579d1ba13557de1fdfddf56a"
|
||||
"22.10.26":
|
||||
url: "https://github.com/google/flatbuffers/archive/v22.10.26.tar.gz"
|
||||
sha256: "34f1820cfd78a3d92abc880fbb1a644c7fb31a71238995f4ed6b5915a1ad4e79"
|
||||
"22.9.29":
|
||||
url: "https://github.com/google/flatbuffers/archive/refs/tags/v22.9.29.tar.gz"
|
||||
sha256: "372df01795c670f6538055a7932fc7eb3e81b3653be4a216c081e9c3c26b1b6d"
|
||||
"2.0.8":
|
||||
url: "https://github.com/google/flatbuffers/archive/refs/tags/v2.0.8.tar.gz"
|
||||
sha256: "f97965a727d26386afaefff950badef2db3ab6af9afe23ed6d94bfb65f95f37e"
|
||||
"2.0.5":
|
||||
url: "https://github.com/google/flatbuffers/archive/refs/tags/v2.0.5.tar.gz"
|
||||
sha256: "b01e97c988c429e164c5c7df9e87c80007ca87f593c0d73733ba536ddcbc8f98"
|
||||
"1.12.0":
|
||||
url: "https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz"
|
||||
sha256: "62f2223fb9181d1d6338451375628975775f7522185266cd5296571ac152bc45"
|
||||
patches:
|
||||
"2.0.5":
|
||||
- patch_file: "patches/0002-apple-no-universal-build.patch"
|
||||
patch_description: "Don't use universal2 architecture"
|
||||
patch_type: "bugfix"
|
||||
- patch_file: "patches/0003-no-flatc-execution-build-time.patch"
|
||||
patch_description: "No flatc execution during build time"
|
||||
patch_type: "conan"
|
||||
"1.12.0":
|
||||
- patch_file: "patches/0005-arm-cxx-compiler-build.patch"
|
||||
patch_description: "IAR ARM C++ Compiler build bugfix"
|
||||
patch_type: "backport"
|
||||
patch_source: "https://github.com/google/flatbuffers/commit/82fac326c5981a85233b8ae7e0d7ec8fda432963"
|
||||
158
flatbuffers/all/conanfile.py
Normal file
158
flatbuffers/all/conanfile.py
Normal file
@@ -0,0 +1,158 @@
|
||||
from conan import ConanFile
|
||||
from conan.tools.apple import is_apple_os
|
||||
from conan.tools.build import check_min_cppstd, valid_min_cppstd
|
||||
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
|
||||
from conan.tools.files import export_conandata_patches, apply_conandata_patches, collect_libs, copy, get, replace_in_file, rmdir
|
||||
from conan.tools.scm import Version
|
||||
import os
|
||||
|
||||
required_conan_version = ">=2.1"
|
||||
|
||||
|
||||
class FlatbuffersConan(ConanFile):
|
||||
name = "flatbuffers"
|
||||
license = "Apache-2.0"
|
||||
url = "https://github.com/conan-io/conan-center-index"
|
||||
homepage = "http://google.github.io/flatbuffers"
|
||||
topics = ("serialization", "rpc", "json-parser")
|
||||
description = "Memory Efficient Serialization Library"
|
||||
|
||||
settings = "os", "arch", "compiler", "build_type"
|
||||
package_type = "library"
|
||||
options = {
|
||||
"shared": [True, False],
|
||||
"fPIC": [True, False],
|
||||
"header_only": [True, False],
|
||||
}
|
||||
default_options = {
|
||||
"shared": False,
|
||||
"fPIC": True,
|
||||
"header_only": False,
|
||||
}
|
||||
|
||||
def _has_flatc(self, info=False):
|
||||
# don't build flatc when it makes little sense or not supported
|
||||
host_os = self.info.settings.os if info else self.settings.os
|
||||
return host_os not in ["Android", "iOS", "watchOS", "tvOS", "Neutrino"]
|
||||
|
||||
def export_sources(self):
|
||||
copy(self, os.path.join("cmake", "FlatcTargets.cmake"), self.recipe_folder, self.export_sources_folder)
|
||||
export_conandata_patches(self)
|
||||
|
||||
def config_options(self):
|
||||
if self.settings.os == "Windows":
|
||||
del self.options.fPIC
|
||||
|
||||
def configure(self):
|
||||
if self.options.shared or self.options.header_only:
|
||||
self.options.rm_safe("fPIC")
|
||||
if self.options.header_only:
|
||||
del self.options.shared
|
||||
|
||||
def layout(self):
|
||||
cmake_layout(self, src_folder="src")
|
||||
|
||||
def package_id(self):
|
||||
if self.info.options.header_only and not self._has_flatc(info=True):
|
||||
self.info.clear()
|
||||
|
||||
def validate(self):
|
||||
check_min_cppstd(self, 11)
|
||||
|
||||
def build_requirements(self):
|
||||
# since 23.3.3 version, flatbuffers cmake scripts were refactored to use cmake 3.8 version
|
||||
# see https://github.com/google/flatbuffers/pull/7801
|
||||
if Version(self.version) >= "2.0.8" and Version(self.version) < "23.3.3":
|
||||
self.tool_requires("cmake/[>=3.16 <4]")
|
||||
|
||||
def source(self):
|
||||
get(self, **self.conan_data["sources"][self.version], strip_root=True)
|
||||
self._patch_sources()
|
||||
|
||||
def generate(self):
|
||||
tc = CMakeToolchain(self)
|
||||
tc.variables["FLATBUFFERS_BUILD_TESTS"] = False
|
||||
tc.variables["FLATBUFFERS_INSTALL"] = True
|
||||
tc.variables["FLATBUFFERS_BUILD_FLATLIB"] = not self.options.header_only and not self.options.shared
|
||||
tc.variables["FLATBUFFERS_BUILD_FLATC"] = self._has_flatc()
|
||||
tc.variables["FLATBUFFERS_STATIC_FLATC"] = False
|
||||
tc.variables["FLATBUFFERS_BUILD_FLATHASH"] = False
|
||||
tc.variables["FLATBUFFERS_BUILD_SHAREDLIB"] = not self.options.header_only and self.options.shared
|
||||
# Honor conan profile
|
||||
tc.variables["FLATBUFFERS_LIBCXX_WITH_CLANG"] = False
|
||||
# Mimic upstream CMake/Version.cmake removed in _patch_sources()
|
||||
version = Version(self.version)
|
||||
tc.cache_variables["VERSION_MAJOR"] = str(version.major)
|
||||
tc.cache_variables["VERSION_MINOR"] = str(version.minor or "0")
|
||||
tc.cache_variables["VERSION_PATCH"] = str(version.patch or "0")
|
||||
tc.cache_variables["VERSION_COMMIT"] = str(version.pre or "0")
|
||||
# For msvc shared
|
||||
tc.variables["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = True
|
||||
# Relocatable shared libs on Macos
|
||||
tc.cache_variables["CMAKE_POLICY_DEFAULT_CMP0042"] = "NEW"
|
||||
# Fix iOS/tvOS/watchOS
|
||||
if is_apple_os(self):
|
||||
tc.variables["CMAKE_MACOSX_BUNDLE"] = False
|
||||
tc.generate()
|
||||
|
||||
def _patch_sources(self):
|
||||
apply_conandata_patches(self)
|
||||
|
||||
cmakelists = os.path.join(self.source_folder, "CMakeLists.txt")
|
||||
# Prefer manual injection of current version in build(), otherwise it tries to call git
|
||||
replace_in_file(self, cmakelists, "include(CMake/Version.cmake)", "")
|
||||
# No warnings as errors
|
||||
replace_in_file(self, cmakelists, "/WX", "")
|
||||
replace_in_file(self, cmakelists, "-Werror ", "")
|
||||
# Install dll to bin folder
|
||||
replace_in_file(self, cmakelists,
|
||||
"RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}",
|
||||
"RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
def build(self):
|
||||
cmake = CMake(self)
|
||||
cmake.configure()
|
||||
cmake.build()
|
||||
|
||||
def package(self):
|
||||
copy(self, "LICENSE*", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
|
||||
cmake = CMake(self)
|
||||
cmake.install()
|
||||
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
|
||||
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
|
||||
copy(self, "FlatcTargets.cmake",
|
||||
src=os.path.join(self.source_folder, os.pardir, "cmake"),
|
||||
dst=os.path.join(self.package_folder, self._module_path))
|
||||
copy(self, "BuildFlatBuffers.cmake",
|
||||
src=os.path.join(self.source_folder, "CMake"),
|
||||
dst=os.path.join(self.package_folder, self._module_path))
|
||||
|
||||
@property
|
||||
def _module_path(self):
|
||||
return os.path.join("lib", "cmake")
|
||||
|
||||
def package_info(self):
|
||||
self.cpp_info.set_property("cmake_find_mode", "both")
|
||||
self.cpp_info.set_property("cmake_module_file_name", "flatbuffers")
|
||||
self.cpp_info.set_property("cmake_file_name", "flatbuffers")
|
||||
cmake_target = "flatbuffers"
|
||||
if not self.options.header_only and self.options.shared:
|
||||
cmake_target += "_shared"
|
||||
self.cpp_info.set_property("cmake_target_name", f"flatbuffers::{cmake_target}")
|
||||
self.cpp_info.set_property("pkg_config_name", "flatbuffers")
|
||||
|
||||
# TODO: back to global scope in conan v2 once cmake_find_package* generators removed
|
||||
if not self.options.header_only:
|
||||
self.cpp_info.components["libflatbuffers"].libs = collect_libs(self)
|
||||
if self.settings.os in ["Linux", "FreeBSD"]:
|
||||
self.cpp_info.components["libflatbuffers"].system_libs.append("m")
|
||||
|
||||
# Provide flatbuffers::flatc target and CMake functions from BuildFlatBuffers.cmake
|
||||
build_modules = [
|
||||
os.path.join(self._module_path, "FlatcTargets.cmake"),
|
||||
os.path.join(self._module_path, "BuildFlatBuffers.cmake"),
|
||||
]
|
||||
self.cpp_info.set_property("cmake_build_modules", build_modules)
|
||||
|
||||
if self._has_flatc():
|
||||
self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))
|
||||
10
flatbuffers/all/patches/0002-apple-no-universal-build.patch
Normal file
10
flatbuffers/all/patches/0002-apple-no-universal-build.patch
Normal file
@@ -0,0 +1,10 @@
|
||||
--- a/CMakeLists.txt
|
||||
+++ b/CMakeLists.txt
|
||||
@@ -282,7 +282,6 @@ elseif(CMAKE_COMPILER_IS_GNUCXX)
|
||||
|
||||
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
|
||||
if(APPLE)
|
||||
- set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
|
||||
@@ -0,0 +1,18 @@
|
||||
--- a/CMakeLists.txt
|
||||
+++ b/CMakeLists.txt
|
||||
@@ -514,6 +514,7 @@ function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
|
||||
register_generated_output(${GEN_BFBS_HEADER})
|
||||
endfunction()
|
||||
|
||||
+if(0)
|
||||
# Look if we have python 3 installed so that we can run the generate code python
|
||||
# script after flatc is built.
|
||||
find_package(PythonInterp 3)
|
||||
@@ -544,6 +545,7 @@ if(PYTHONINTERP_FOUND AND
|
||||
else()
|
||||
message("No Python3 interpreter found! Unable to generate files automatically.")
|
||||
endif()
|
||||
+endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_TESTS)
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
@@ -0,0 +1,20 @@
|
||||
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
||||
index 21e90151..2ce3eb48 100644
|
||||
--- a/CMakeLists.txt
|
||||
+++ b/CMakeLists.txt
|
||||
@@ -536,6 +536,7 @@ function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
|
||||
register_generated_output(${GEN_BFBS_HEADER})
|
||||
endfunction()
|
||||
|
||||
+if(0)
|
||||
# Look if we have python 3.5 installed so that we can run the generate code
|
||||
# python script after flatc is built.
|
||||
find_package(Python3 3.5 COMPONENTS Interpreter)
|
||||
@@ -559,6 +560,7 @@ if(Python3_Interpreter_FOUND)
|
||||
else()
|
||||
message("No Python3 interpreter found! Unable to generate files automatically.")
|
||||
endif()
|
||||
+endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_TESTS)
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
16
flatbuffers/all/patches/0005-arm-cxx-compiler-build.patch
Normal file
16
flatbuffers/all/patches/0005-arm-cxx-compiler-build.patch
Normal file
@@ -0,0 +1,16 @@
|
||||
diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h
|
||||
index c4dc5bcd..0f3a7dfd 100644
|
||||
--- a/include/flatbuffers/flatbuffers.h
|
||||
+++ b/include/flatbuffers/flatbuffers.h
|
||||
@@ -1871,10 +1871,7 @@ class FlatBufferBuilder {
|
||||
vector_downward &buf_;
|
||||
|
||||
private:
|
||||
- TableKeyComparator &operator=(const TableKeyComparator &other) {
|
||||
- buf_ = other.buf_;
|
||||
- return *this;
|
||||
- }
|
||||
+ FLATBUFFERS_DELETE_FUNC(TableKeyComparator &operator=(const TableKeyComparator &other))
|
||||
};
|
||||
/// @endcond
|
||||
|
||||
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