mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Compare commits
85 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75740c1374 | ||
|
|
e8598950fc | ||
|
|
ca5c9e7496 | ||
|
|
f7818d83d7 | ||
|
|
7ef2fc2517 | ||
|
|
6df9e1c537 | ||
|
|
6a0126340a | ||
|
|
4464405250 | ||
|
|
c3807fa39d | ||
|
|
a360958be3 | ||
|
|
432f3f26a4 | ||
|
|
6ccdfff0af | ||
|
|
71e97b7123 | ||
|
|
c243aa4e15 | ||
|
|
d575321eba | ||
|
|
f353fd8864 | ||
|
|
c9a840e935 | ||
|
|
620fe1c5cf | ||
|
|
69dae32776 | ||
|
|
da0f096ba2 | ||
|
|
36fe9d539f | ||
|
|
6180b5ac7a | ||
|
|
42bfe240e0 | ||
|
|
1fb0f1ef71 | ||
|
|
1c8c9438a2 | ||
|
|
85f64786da | ||
|
|
b0910e75e0 | ||
|
|
ff1ac8ab5a | ||
|
|
4d3db99283 | ||
|
|
72b9501e69 | ||
|
|
9c169083ad | ||
|
|
7bebaab69e | ||
|
|
d8117bb8a3 | ||
|
|
df4909e5f6 | ||
|
|
79c2d80ed4 | ||
|
|
3e1b789d21 | ||
|
|
cf7135ff58 | ||
|
|
f60276f54b | ||
|
|
2b01247b30 | ||
|
|
7cf74cb864 | ||
|
|
4fb5a764df | ||
|
|
6c2dc41e0d | ||
|
|
e568f17096 | ||
|
|
1263e9788e | ||
|
|
f5132b9ee1 | ||
|
|
c95ad9cc55 | ||
|
|
3550899987 | ||
|
|
73582b145c | ||
|
|
b929c62c71 | ||
|
|
3ec8d7f598 | ||
|
|
352b743c71 | ||
|
|
b7cb91c34e | ||
|
|
f79cc460f8 | ||
|
|
757854a6cd | ||
|
|
6f4b4c80a7 | ||
|
|
ae1763e226 | ||
|
|
aa46f0e4c2 | ||
|
|
89d2b0861b | ||
|
|
4390254e6a | ||
|
|
3a27013732 | ||
|
|
d72c478128 | ||
|
|
10f4ecac26 | ||
|
|
27823d5552 | ||
|
|
19361a58ec | ||
|
|
c6c8a9ba29 | ||
|
|
14eaddfdcc | ||
|
|
8833cff911 | ||
|
|
318668aed6 | ||
|
|
9566669245 | ||
|
|
354fd906a5 | ||
|
|
2d9b3ade18 | ||
|
|
ced2cb6ce9 | ||
|
|
285501f7be | ||
|
|
0952143971 | ||
|
|
0ce53c96c3 | ||
|
|
d6f70cdd7d | ||
|
|
07d5965c81 | ||
|
|
6ca102e413 | ||
|
|
8ef6ee2a3e | ||
|
|
118abc2871 | ||
|
|
fbcf063401 | ||
|
|
e97f38e53c | ||
|
|
4cdf3eb19b | ||
|
|
ea592296b8 | ||
|
|
d426890b92 |
8
.gitignore
vendored
8
.gitignore
vendored
@@ -29,12 +29,20 @@ proguard-project.txt
|
||||
linklint_results
|
||||
Makefile
|
||||
flatc
|
||||
flathash
|
||||
flattests
|
||||
flatsamplebinary
|
||||
flatsampletext
|
||||
snapshot.sh
|
||||
tests/go_gen
|
||||
tests/monsterdata_java_wire.mon
|
||||
tests/monsterdata_go_wire.mon
|
||||
CMakeLists.txt.user
|
||||
CMakeScripts/**
|
||||
CTestTestfile.cmake
|
||||
build/Xcode/FlatBuffers.xcodeproj/project.xcworkspace/**
|
||||
build/Xcode/FlatBuffers.xcodeproj/xcuserdata/**
|
||||
FlatBuffers.xcodeproj/
|
||||
java/.idea
|
||||
java/*.iml
|
||||
java/target
|
||||
|
||||
22
CMake/biicode.cmake
Normal file
22
CMake/biicode.cmake
Normal file
@@ -0,0 +1,22 @@
|
||||
# Initializes block variables
|
||||
INIT_BIICODE_BLOCK()
|
||||
|
||||
# Copying data files to project/bin folder
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/samples")
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples/monster.fbs"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/samples/monsterdata.json"
|
||||
DESTINATION
|
||||
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/samples")
|
||||
endif()
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests")
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests"
|
||||
DESTINATION
|
||||
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
|
||||
endif()
|
||||
|
||||
# Actually create targets: EXEcutables and libraries.
|
||||
ADD_BIICODE_TARGETS()
|
||||
|
||||
string(REPLACE " " ";" REPLACED_FLAGS ${CMAKE_CXX_FLAGS})
|
||||
target_compile_options(${BII_BLOCK_TARGET} INTERFACE ${REPLACED_FLAGS})
|
||||
target_include_directories(${BII_BLOCK_TARGET} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
@@ -6,9 +6,18 @@ project(FlatBuffers)
|
||||
option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
|
||||
option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON)
|
||||
option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON)
|
||||
option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" ON)
|
||||
option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
|
||||
|
||||
if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
|
||||
message(WARNING
|
||||
"Cannot build tests without building the compiler. Tests will be disabled.")
|
||||
set(FLATBUFFERS_BUILD_TESTS OFF)
|
||||
endif()
|
||||
|
||||
set(FlatBuffers_Compiler_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/hash.h
|
||||
include/flatbuffers/idl.h
|
||||
include/flatbuffers/util.h
|
||||
src/idl_parser.cpp
|
||||
@@ -20,8 +29,14 @@ set(FlatBuffers_Compiler_SRCS
|
||||
src/flatc.cpp
|
||||
)
|
||||
|
||||
set(FlatHash_SRCS
|
||||
include/flatbuffers/hash.h
|
||||
src/flathash.cpp
|
||||
)
|
||||
|
||||
set(FlatBuffers_Tests_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/hash.h
|
||||
include/flatbuffers/idl.h
|
||||
include/flatbuffers/util.h
|
||||
src/idl_parser.cpp
|
||||
@@ -41,6 +56,7 @@ set(FlatBuffers_Sample_Binary_SRCS
|
||||
|
||||
set(FlatBuffers_Sample_Text_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/hash.h
|
||||
include/flatbuffers/idl.h
|
||||
include/flatbuffers/util.h
|
||||
src/idl_parser.cpp
|
||||
@@ -67,12 +83,24 @@ if(FLATBUFFERS_CODE_COVERAGE)
|
||||
"${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||
endif()
|
||||
|
||||
if(BIICODE)
|
||||
# Execute biicode building
|
||||
include(CMake/biicode.cmake)
|
||||
return()
|
||||
endif(BIICODE)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
add_executable(flatc ${FlatBuffers_Compiler_SRCS})
|
||||
if(FLATBUFFERS_BUILD_FLATC)
|
||||
add_executable(flatc ${FlatBuffers_Compiler_SRCS})
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_FLATC)
|
||||
add_executable(flathash ${FlatHash_SRCS})
|
||||
endif()
|
||||
|
||||
function(compile_flatbuffers_schema_to_cpp SRC_FBS)
|
||||
get_filename_component(SRC_FBS_DIR ${SRC_FBS} DIRECTORY)
|
||||
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
|
||||
string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
|
||||
add_custom_command(
|
||||
OUTPUT ${GEN_HEADER}
|
||||
@@ -93,7 +121,9 @@ endif()
|
||||
|
||||
if(FLATBUFFERS_INSTALL)
|
||||
install(DIRECTORY include/flatbuffers DESTINATION include)
|
||||
install(TARGETS flatc DESTINATION bin)
|
||||
if(FLATBUFFERS_BUILD_FLATC)
|
||||
install(TARGETS flatc DESTINATION bin)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_TESTS)
|
||||
|
||||
42
CONTRIBUTING.md
Normal file
42
CONTRIBUTING.md
Normal file
@@ -0,0 +1,42 @@
|
||||
Contributing {#contributing}
|
||||
============
|
||||
|
||||
Want to contribute? Great! First, read this page (including the small print at
|
||||
the end).
|
||||
|
||||
# Before you contribute
|
||||
Before we can use your code, you must sign the
|
||||
[Google Individual Contributor License Agreement](https://developers.google.com/open-source/cla/individual?csw=1)
|
||||
(CLA), which you can do online. The CLA is necessary mainly because you own the
|
||||
copyright to your changes, even after your contribution becomes part of our
|
||||
codebase, so we need your permission to use and distribute your code. We also
|
||||
need to be sure of various other things—for instance that you'll tell us if you
|
||||
know that your code infringes on other people's patents. You don't have to sign
|
||||
the CLA until after you've submitted your code for review and a member has
|
||||
approved it, but you must do it before we can put your code into our codebase.
|
||||
Before you start working on a larger contribution, you should get in touch with
|
||||
us first through the issue tracker with your idea so that we can help out and
|
||||
possibly guide you. Coordinating up front makes it much easier to avoid
|
||||
frustration later on.
|
||||
|
||||
# Code reviews
|
||||
All submissions, including submissions by project members, require review. We
|
||||
use Github pull requests for this purpose.
|
||||
|
||||
Some tips for good pull requests:
|
||||
* Use our code
|
||||
[style guide](http://google-styleguide.googlecode.com/svn/trunk/cppguide.html).
|
||||
When in doubt, try to stay true to the existing code of the project.
|
||||
* Write a descriptive commit message. What problem are you solving and what
|
||||
are the consequences? Where and what did you test? Some good tips:
|
||||
[here](http://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message)
|
||||
and [here](https://www.kernel.org/doc/Documentation/SubmittingPatches).
|
||||
* If your PR consists of multiple commits which are successive improvements /
|
||||
fixes to your first commit, consider squashing them into a single commit
|
||||
(`git rebase -i`) such that your PR is a single commit on top of the current
|
||||
HEAD. This make reviewing the code so much easier, and our history more
|
||||
readable.
|
||||
|
||||
# The small print
|
||||
Contributions made by corporations are covered by a different agreement than
|
||||
the one above, the Software Grant and Corporate Contributor License Agreement.
|
||||
@@ -187,7 +187,7 @@
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
Copyright 2014 Google Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -235,10 +235,16 @@ select_android_build_target() {
|
||||
local android_build_target=
|
||||
for android_target in $(echo "${android_targets_installed}" | \
|
||||
awk -F- '{ print $2 }' | sort -n); do
|
||||
if [[ $((android_target)) -ge \
|
||||
local isNumber='^[0-9]+$'
|
||||
# skip preview API releases e.g. 'android-L'
|
||||
if [[ $android_target =~ $isNumber ]]; then
|
||||
if [[ $((android_target)) -ge \
|
||||
$((BUILDAPK_ANDROID_TARGET_MINVERSION)) ]]; then
|
||||
android_build_target="android-${android_target}"
|
||||
break
|
||||
android_build_target="android-${android_target}"
|
||||
break
|
||||
fi
|
||||
# else
|
||||
# The API version is a letter, so skip it.
|
||||
fi
|
||||
done
|
||||
if [[ "${android_build_target}" == "" ]]; then
|
||||
@@ -415,14 +421,18 @@ main() {
|
||||
local build_package=1
|
||||
for opt; do
|
||||
case ${opt} in
|
||||
# NDK_DEBUG=0 tells ndk-build to build this as debuggable but to not
|
||||
# modify the underlying code whereas NDK_DEBUG=1 also builds as debuggable
|
||||
# but does modify the code
|
||||
NDK_DEBUG=1) ant_target=debug ;;
|
||||
NDK_DEBUG=0) ant_target=debug ;;
|
||||
ADB_DEVICE*) adb_device="$(\
|
||||
echo "${opt}" | sed -E 's/^ADB_DEVICE=([^ ]+)$/-s \1/;t;s/.*//')" ;;
|
||||
BUILD=0) disable_build=1 ;;
|
||||
DEPLOY=0) disable_deploy=1 ;;
|
||||
RUN_DEBUGGER=1) run_debugger=1 ;;
|
||||
LAUNCH=0) launch=0 ;;
|
||||
clean) build_package=0 ;;
|
||||
clean) build_package=0 disable_deploy=1 launch=0 ;;
|
||||
-h|--help|help) usage ;;
|
||||
esac
|
||||
done
|
||||
|
||||
@@ -16,16 +16,25 @@
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
# Empty static library so that other projects can include FlatBuffers as a
|
||||
# module.
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := flatbuffers
|
||||
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include
|
||||
LOCAL_EXPORT_CPPFLAGS := -std=c++11 -fexceptions -Wall -Wno-literal-suffix
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
||||
LOCAL_MODULE := FlatBufferTest
|
||||
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../include
|
||||
LOCAL_SRC_FILES := main.cpp ../../tests/test.cpp ../../src/idl_parser.cpp ../../src/idl_gen_text.cpp
|
||||
LOCAL_LDLIBS := -llog -landroid
|
||||
LOCAL_STATIC_LIBRARIES := android_native_app_glue
|
||||
LOCAL_ARM_MODE:=arm
|
||||
LOCAL_CPPFLAGS += -std=c++11 -fexceptions -Wall -Wno-literal-suffix
|
||||
|
||||
# FlatBuffers test
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := FlatBufferTest
|
||||
LOCAL_SRC_FILES := main.cpp \
|
||||
../../tests/test.cpp \
|
||||
../../src/idl_parser.cpp \
|
||||
../../src/idl_gen_text.cpp \
|
||||
../../src/idl_gen_fbs.cpp
|
||||
LOCAL_LDLIBS := -llog -landroid
|
||||
LOCAL_STATIC_LIBRARIES := android_native_app_glue flatbuffers
|
||||
LOCAL_ARM_MODE := arm
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
$(call import-module,android/native_app_glue)
|
||||
|
||||
177
android/jni/include.mk
Normal file
177
android/jni/include.mk
Normal file
@@ -0,0 +1,177 @@
|
||||
# Copyright 2014 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.
|
||||
|
||||
# This file contains utility functions for Android projects using Flatbuffers.
|
||||
# To use this file, include it in your project's Android.mk by calling near the
|
||||
# top of your android makefile like so:
|
||||
#
|
||||
# include $(FLATBUFFERS_DIR)/android/jni/include.mk
|
||||
#
|
||||
# You will also need to import the flatbuffers module using the standard
|
||||
# import-module function.
|
||||
#
|
||||
# The main functionality this file provides are the following functions:
|
||||
# flatbuffers_fbs_to_h: Converts flatbuffer schema paths to header paths.
|
||||
# flatbuffers_header_build_rule:
|
||||
# Creates a build rule for a schema's generated header. This build rule
|
||||
# has a dependency on the flatc compiler which will be built if necessary.
|
||||
# flatbuffers_header_build_rules:
|
||||
# Creates build rules for generated headers for each schema listed and sets
|
||||
# up depenedendies.
|
||||
#
|
||||
# More information and example usage can be found in the comments preceeding
|
||||
# each function.
|
||||
|
||||
# Targets to build the Flatbuffers compiler as well as some utility definitions
|
||||
ifeq (,$(FLATBUFFERS_INCLUDE_MK_))
|
||||
FLATBUFFERS_INCLUDE_MK_ := 1
|
||||
|
||||
PROJECT_OS := $(OS)
|
||||
ifeq (,$(OS))
|
||||
PROJECT_OS := $(shell uname -s)
|
||||
else
|
||||
ifneq ($(findstring Windows,$(PROJECT_OS)),)
|
||||
PROJECT_OS := Windows
|
||||
endif
|
||||
endif
|
||||
|
||||
# The following block generates build rules which result in headers being
|
||||
# rebuilt from flatbuffers schemas.
|
||||
|
||||
FLATBUFFERS_CMAKELISTS_DIR := \
|
||||
$(realpath $(dir $(lastword $(MAKEFILE_LIST)))/../..)
|
||||
|
||||
# Directory that contains the FlatBuffers compiler.
|
||||
ifeq (Windows,$(PROJECT_OS))
|
||||
FLATBUFFERS_FLATC_PATH?=$(CURDIR)/bin
|
||||
FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/Debug/flatc.exe
|
||||
endif
|
||||
ifeq (Linux,$(PROJECT_OS))
|
||||
FLATBUFFERS_FLATC_PATH?=$(CURDIR)/bin
|
||||
FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/flatc
|
||||
endif
|
||||
ifeq (Darwin,$(PROJECT_OS))
|
||||
FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR)
|
||||
FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/Debug/flatc
|
||||
endif
|
||||
|
||||
# Search for cmake.
|
||||
CMAKE_ROOT := $(realpath $(LOCAL_PATH)/../../../../../../prebuilts/cmake)
|
||||
ifeq (,$(CMAKE))
|
||||
ifeq (Linux,$(PROJECT_OS))
|
||||
CMAKE := $(wildcard $(CMAKE_ROOT)/linux-x86/current/bin/cmake*)
|
||||
endif
|
||||
ifeq (Darwin,$(PROJECT_OS))
|
||||
CMAKE := \
|
||||
$(wildcard $(CMAKE_ROOT)/darwin-x86_64/current/*.app/Contents/bin/cmake)
|
||||
endif
|
||||
ifeq (Windows,$(PROJECT_OS))
|
||||
CMAKE := $(wildcard $(CMAKE_ROOT)/windows/current/bin/cmake*)
|
||||
endif
|
||||
endif
|
||||
ifeq (,$(CMAKE))
|
||||
CMAKE := cmake
|
||||
endif
|
||||
|
||||
# Generate a host build rule for the flatbuffers compiler.
|
||||
ifeq (Windows,$(PROJECT_OS))
|
||||
define build_flatc_recipe
|
||||
cd & jni\build_flatc.bat $(CMAKE)
|
||||
endef
|
||||
endif
|
||||
ifeq (Linux,$(PROJECT_OS))
|
||||
define build_flatc_recipe
|
||||
mkdir -p bin && cd bin && $(CMAKE) $(FLATBUFFERS_CMAKELISTS_DIR) \
|
||||
&& $(MAKE) flatc
|
||||
endef
|
||||
endif
|
||||
ifeq (Darwin,$(PROJECT_OS))
|
||||
define build_flatc_recipe
|
||||
cd $(FLATBUFFERS_CMAKELISTS_DIR) && "$(CMAKE)" -GXcode . && \
|
||||
xcodebuild -target flatc
|
||||
endef
|
||||
endif
|
||||
ifeq (,$(build_flatc_recipe))
|
||||
ifeq (,$(FLATBUFFERS_FLATC))
|
||||
$(error flatc binary not found!)
|
||||
endif
|
||||
endif
|
||||
|
||||
# Generate a build rule for flatc.
|
||||
ifeq ($(strip $(FLATBUFFERS_FLATC)),)
|
||||
flatc_target := build_flatc
|
||||
.PHONY: $(flatc_target)
|
||||
else
|
||||
flatc_target := $(FLATBUFFERS_FLATC)
|
||||
endif
|
||||
$(flatc_target):
|
||||
$(call build_flatc_recipe)
|
||||
|
||||
# $(flatbuffers_fbs_to_h schema_dir,output_dir,path)
|
||||
#
|
||||
# Convert the specified schema path to a Flatbuffers generated header path.
|
||||
# For example:
|
||||
#
|
||||
# $(call flatbuffers_fbs_to_h,$(MY_PROJ_DIR)/schemas,\
|
||||
# $(MY_PROJ_DIR)/gen/include,$(MY_PROJ_DIR)/schemas/example.fbs)
|
||||
#
|
||||
# This will convert the file path `$(MY_PROJ_DIR)/schemas/example.fbs)` to
|
||||
# `$(MY_PROJ_DIR)/gen/include/example_generated.h`
|
||||
define flatbuffers_fbs_to_h
|
||||
$(subst $(1),$(2),$(patsubst %.fbs,%_generated.h,$(3)))
|
||||
endef
|
||||
|
||||
# $(flatbuffers_header_build_rule schema_file,schema_dir,output_dir,\
|
||||
# schema_include_dirs)
|
||||
#
|
||||
# Generate a build rule that will convert a Flatbuffers schema to a generated
|
||||
# header derived from the schema filename using flatbuffers_fbs_to_h. For
|
||||
# example:
|
||||
#
|
||||
# $(call flatbuffers_header_build_rule,$(MY_PROJ_DIR)/schemas/example.fbs,\
|
||||
# $(MY_PROJ_DIR)/schemas,$(MY_PROJ_DIR)/gen/include)
|
||||
#
|
||||
# The final argument, schema_include_dirs, is optional and is only needed when
|
||||
# the schema files depend on other schema files outside their own directory.
|
||||
define flatbuffers_header_build_rule
|
||||
$(eval \
|
||||
$(call flatbuffers_fbs_to_h,$(2),$(3),$(1)): $(1) $(flatc_target)
|
||||
$(call host-echo-build-step,generic,Generate) \
|
||||
$(subst $(LOCAL_PATH)/,,$(call flatbuffers_fbs_to_h,$(2),$(3),$(1)))
|
||||
$(hide) $$(FLATBUFFERS_FLATC) --gen-includes \
|
||||
$(foreach include,$(4),-I $(include)) -o $$(dir $$@) -c $$<)
|
||||
endef
|
||||
|
||||
# $(flatbuffers_header_build_rules schema_files,schema_dir,output_dir,\
|
||||
# schema_include_dirs,src_files))
|
||||
#
|
||||
# Use this in your own Android.mk file to generate build rules that will
|
||||
# generate header files for your flatbuffer schemas as well as automatically
|
||||
# set your source files to be dependent on the generated headers. For example:
|
||||
#
|
||||
# $(call flatbuffers_header_build_rules,$(MY_PROJ_SCHEMA_FILES),\
|
||||
# $(MY_PROJ_SCHEMA_DIR),$(MY_PROJ_GENERATED_OUTPUT_DIR),
|
||||
# $(MY_PROJ_SCHEMA_INCLUDE_DIRS),$(LOCAL_SRC_FILES))
|
||||
define flatbuffers_header_build_rules
|
||||
$(foreach schema,$(1),\
|
||||
$(call flatbuffers_header_build_rule,\
|
||||
$(schema),$(strip $(2)),$(strip $(3)),$(strip $(4))))\
|
||||
$(foreach src,$(strip $(5)),\
|
||||
$(eval $(LOCAL_PATH)/$$(src): \
|
||||
$(foreach schema,$(strip $(1)),\
|
||||
$(call flatbuffers_fbs_to_h,$(strip $(2)),$(strip $(3)),$(schema)))))
|
||||
endef
|
||||
|
||||
endif # FLATBUFFERS_INCLUDE_MK_
|
||||
|
||||
5
biicode.conf
Normal file
5
biicode.conf
Normal file
@@ -0,0 +1,5 @@
|
||||
# Biicode configuration file
|
||||
|
||||
[paths]
|
||||
# Local directories to look for headers (within block)
|
||||
include
|
||||
@@ -11,14 +11,7 @@
|
||||
61823BBC53544106B6DBC38E /* idl_parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3709AC883348409592530AE6 /* idl_parser.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
61FF3C34FBEC4819A1C30F92 /* sample_text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECCEBFFA6977404F858F9739 /* sample_text.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
8C303C591975D6A700D7C1C5 /* idl_gen_go.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C303C581975D6A700D7C1C5 /* idl_gen_go.cpp */; };
|
||||
8C6905ED19F8357300CB8866 /* idl_gen_fbs.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905EC19F8357300CB8866 /* idl_gen_fbs.cpp */; };
|
||||
8C6905F619F835A900CB8866 /* flatc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905EF19F835A900CB8866 /* flatc.cpp */; };
|
||||
8C6905F719F835A900CB8866 /* idl_gen_cpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905F019F835A900CB8866 /* idl_gen_cpp.cpp */; };
|
||||
8C6905F819F835A900CB8866 /* idl_gen_fbs.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905F119F835A900CB8866 /* idl_gen_fbs.cpp */; };
|
||||
8C6905F919F835A900CB8866 /* idl_gen_general.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905F219F835A900CB8866 /* idl_gen_general.cpp */; };
|
||||
8C6905FA19F835A900CB8866 /* idl_gen_go.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905F319F835A900CB8866 /* idl_gen_go.cpp */; };
|
||||
8C6905FB19F835A900CB8866 /* idl_gen_text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905F419F835A900CB8866 /* idl_gen_text.cpp */; };
|
||||
8C6905FC19F835A900CB8866 /* idl_parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905F519F835A900CB8866 /* idl_parser.cpp */; };
|
||||
8C6905FD19F835B400CB8866 /* idl_gen_fbs.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C6905EC19F8357300CB8866 /* idl_gen_fbs.cpp */; };
|
||||
8CD8717B19CB937D0012A827 /* idl_gen_general.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8CD8717A19CB937D0012A827 /* idl_gen_general.cpp */; };
|
||||
A9C9A99F719A4ED58DC2D2FC /* idl_parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3709AC883348409592530AE6 /* idl_parser.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
@@ -337,18 +330,11 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
8C6905FB19F835A900CB8866 /* idl_gen_text.cpp in Sources */,
|
||||
8C303C591975D6A700D7C1C5 /* idl_gen_go.cpp in Sources */,
|
||||
AA9BACF55EB3456BA2F633BB /* flatc.cpp in Sources */,
|
||||
BE03D7B0C9584DD58B50ED34 /* idl_gen_cpp.cpp in Sources */,
|
||||
AD71FEBEE4E846529002C1F0 /* idl_gen_text.cpp in Sources */,
|
||||
8C6905F619F835A900CB8866 /* flatc.cpp in Sources */,
|
||||
8C6905FC19F835A900CB8866 /* idl_parser.cpp in Sources */,
|
||||
8C6905ED19F8357300CB8866 /* idl_gen_fbs.cpp in Sources */,
|
||||
8C6905FA19F835A900CB8866 /* idl_gen_go.cpp in Sources */,
|
||||
8C6905F919F835A900CB8866 /* idl_gen_general.cpp in Sources */,
|
||||
A9C9A99F719A4ED58DC2D2FC /* idl_parser.cpp in Sources */,
|
||||
8C6905F719F835A900CB8866 /* idl_gen_cpp.cpp in Sources */,
|
||||
8C6905F819F835A900CB8866 /* idl_gen_fbs.cpp in Sources */,
|
||||
8CD8717B19CB937D0012A827 /* idl_gen_general.cpp in Sources */,
|
||||
);
|
||||
|
||||
@@ -53,7 +53,7 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<div class="title">FlatBuffers Documentation</div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>FlatBuffers is an efficient cross platform serialization library for C++, with support for Java and Go. It was created at Google specifically for game development and other performance-critical applications.</p>
|
||||
<div class="textblock"><p>FlatBuffers is an efficient cross platform serialization library for C++, with support for Java, C# and Go. It was created at Google specifically for game development and other performance-critical applications.</p>
|
||||
<p>It is available as open source under the Apache license, v2 (see LICENSE.txt).</p>
|
||||
<h2>Why use FlatBuffers?</h2>
|
||||
<ul>
|
||||
@@ -65,7 +65,7 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<li><p class="startli"><b>Convenient to use</b> - Generated C++ code allows for terse access & construction code. Then there's optional functionality for parsing schemas and JSON-like text representations at runtime efficiently if needed (faster and more memory efficient than other JSON parsers).</p>
|
||||
<p class="startli">Java and Go code supports object-reuse.</p>
|
||||
</li>
|
||||
<li><b>Cross platform C++11/Java/Go code with no dependencies</b> - will work with any recent gcc/clang and VS2010. Comes with build files for the tests & samples (Android .mk files, and cmake for all other platforms).</li>
|
||||
<li><b>Cross platform C++11/Java/C#/Go code with no dependencies</b> - will work with any recent gcc/clang and VS2010. Comes with build files for the tests & samples (Android .mk files, and cmake for all other platforms).</li>
|
||||
</ul>
|
||||
<h3>Why not use Protocol Buffers, or .. ?</h3>
|
||||
<p>Protocol Buffers is indeed relatively similar to FlatBuffers, with the primary difference being that FlatBuffers does not need a parsing/ unpacking step to a secondary representation before you can access data, often coupled with per-object memory allocation. The code is an order of magnitude bigger, too. Protocol Buffers has neither optional text import/export nor schema language features like unions.</p>
|
||||
@@ -76,7 +76,7 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<p>This section is a quick rundown of how to use this system. Subsequent sections provide a more in-depth usage guide.</p>
|
||||
<ul>
|
||||
<li>Write a schema file that allows you to define the data structures you may want to serialize. Fields can have a scalar type (ints/floats of all sizes), or they can be a: string; array of any type; reference to yet another object; or, a set of possible objects (unions). Fields are optional and have defaults, so they don't need to be present for every object instance.</li>
|
||||
<li>Use <code>flatc</code> (the FlatBuffer compiler) to generate a C++ header (or Java/Go classes) with helper classes to access and construct serialized data. This header (say <code>mydata_generated.h</code>) only depends on <code>flatbuffers.h</code>, which defines the core functionality.</li>
|
||||
<li>Use <code>flatc</code> (the FlatBuffer compiler) to generate a C++ header (or Java/C#/Go classes) with helper classes to access and construct serialized data. This header (say <code>mydata_generated.h</code>) only depends on <code>flatbuffers.h</code>, which defines the core functionality.</li>
|
||||
<li>Use the <code>FlatBufferBuilder</code> class to construct a flat binary buffer. The generated functions allow you to add objects to this buffer recursively, often as simply as making a single function call.</li>
|
||||
<li>Store or send your buffer somewhere!</li>
|
||||
<li>When reading it back, you can obtain the pointer to the root object from the binary buffer, and from there traverse it conveniently in-place with <code>object->field()</code>.</li>
|
||||
@@ -87,7 +87,7 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<li>How to <a href="md__compiler.html">use the compiler</a>.</li>
|
||||
<li>How to <a href="md__schemas.html">write a schema</a>.</li>
|
||||
<li>How to <a href="md__cpp_usage.html">use the generated C++ code</a> in your own programs.</li>
|
||||
<li>How to <a href="md__java_usage.html">use the generated Java code</a> in your own programs.</li>
|
||||
<li>How to <a href="md__java_usage.html">use the generated Java/C# code</a> in your own programs.</li>
|
||||
<li>How to <a href="md__go_usage.html">use the generated Go code</a> in your own programs.</li>
|
||||
<li>Some <a href="md__benchmarks.html">benchmarks</a> showing the advantage of using FlatBuffers.</li>
|
||||
<li>A <a href="md__white_paper.html">white paper</a> explaining the "why" of FlatBuffers.</li>
|
||||
|
||||
@@ -54,29 +54,30 @@ $(document).ready(function(){initNavTree('md__benchmarks.html','');});
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>Comparing against other serialization solutions, running on Windows 7 64bit. We use the LITE runtime for Protocol Buffers (less code / lower overhead), Rapid JSON (one of the fastest C++ JSON parsers around), and pugixml, also one of the fastest XML parsers.</p>
|
||||
<p>We also compare against code that doesn't use a serialization library at all (the column "Raw structs"), which is what you get if you write hardcoded code that just writes structs. This is the fastest possible, but of course is not cross platform nor has any kind of forwards / backwards compatibility.</p>
|
||||
<p>We compare against Flatbuffers with the binary wire format (as intended), and also with JSON as the wire format with the optional JSON parser (which, using a schema, parses JSON into a binary buffer that can then be accessed as before).</p>
|
||||
<p>The benchmark object is a set of about 10 objects containing an array, 4 strings, and a large variety of int/float scalar values of all sizes, meant to be representative of game data, e.g. a scene format.</p>
|
||||
<table class="doxtable">
|
||||
<tr>
|
||||
<th></th><th>FlatBuffers (binary) </th><th>Protocol Buffers LITE </th><th>Rapid JSON </th><th>FlatBuffers (JSON) </th><th>pugixml </th></tr>
|
||||
<th></th><th>FlatBuffers (binary) </th><th>Protocol Buffers LITE </th><th>Rapid JSON </th><th>FlatBuffers (JSON) </th><th>pugixml </th><th>Raw structs </th></tr>
|
||||
<tr>
|
||||
<td>Decode + Traverse + Dealloc (1 million times, seconds) </td><td>0.08 </td><td>302 </td><td>583 </td><td>105 </td><td>196 </td></tr>
|
||||
<td>Decode + Traverse + Dealloc (1 million times, seconds) </td><td>0.08 </td><td>302 </td><td>583 </td><td>105 </td><td>196 </td><td>0.02 </td></tr>
|
||||
<tr>
|
||||
<td>Decode / Traverse / Dealloc (breakdown) </td><td>0 / 0.08 / 0 </td><td>220 / 0.15 / 81 </td><td>294 / 0.9 / 287 </td><td>70 / 0.08 / 35 </td><td>41 / 3.9 / 150 </td></tr>
|
||||
<td>Decode / Traverse / Dealloc (breakdown) </td><td>0 / 0.08 / 0 </td><td>220 / 0.15 / 81 </td><td>294 / 0.9 / 287 </td><td>70 / 0.08 / 35 </td><td>41 / 3.9 / 150 </td><td>0 / 0.02 / 0 </td></tr>
|
||||
<tr>
|
||||
<td>Encode (1 million times, seconds) </td><td>3.2 </td><td>185 </td><td>650 </td><td>169 </td><td>273 </td></tr>
|
||||
<td>Encode (1 million times, seconds) </td><td>3.2 </td><td>185 </td><td>650 </td><td>169 </td><td>273 </td><td>0.15 </td></tr>
|
||||
<tr>
|
||||
<td>Wire format size (normal / zlib, bytes) </td><td>344 / 220 </td><td>228 / 174 </td><td>1475 / 322 </td><td>1029 / 298 </td><td>1137 / 341 </td></tr>
|
||||
<td>Wire format size (normal / zlib, bytes) </td><td>344 / 220 </td><td>228 / 174 </td><td>1475 / 322 </td><td>1029 / 298 </td><td>1137 / 341 </td><td>312 / 187 </td></tr>
|
||||
<tr>
|
||||
<td>Memory needed to store decoded wire (bytes / blocks) </td><td>0 / 0 </td><td>760 / 20 </td><td>65689 / 4 </td><td>328 / 1 </td><td>34194 / 3 </td></tr>
|
||||
<td>Memory needed to store decoded wire (bytes / blocks) </td><td>0 / 0 </td><td>760 / 20 </td><td>65689 / 4 </td><td>328 / 1 </td><td>34194 / 3 </td><td>0 / 0 </td></tr>
|
||||
<tr>
|
||||
<td>Transient memory allocated during decode (KB) </td><td>0 </td><td>1 </td><td>131 </td><td>4 </td><td>34 </td></tr>
|
||||
<td>Transient memory allocated during decode (KB) </td><td>0 </td><td>1 </td><td>131 </td><td>4 </td><td>34 </td><td>0 </td></tr>
|
||||
<tr>
|
||||
<td>Generated source code size (KB) </td><td>4 </td><td>61 </td><td>0 </td><td>4 </td><td>0 </td></tr>
|
||||
<td>Generated source code size (KB) </td><td>4 </td><td>61 </td><td>0 </td><td>4 </td><td>0 </td><td>0 </td></tr>
|
||||
<tr>
|
||||
<td>Field access in handwritten traversal code </td><td>typed accessors </td><td>typed accessors </td><td>manual error checking </td><td>typed accessors </td><td>manual error checking </td></tr>
|
||||
<td>Field access in handwritten traversal code </td><td>typed accessors </td><td>typed accessors </td><td>manual error checking </td><td>typed accessors </td><td>manual error checking </td><td>typed but no safety </td></tr>
|
||||
<tr>
|
||||
<td>Library source code (KB) </td><td>15 </td><td>some subset of 3800 </td><td>87 </td><td>43 </td><td>327 </td></tr>
|
||||
<td>Library source code (KB) </td><td>15 </td><td>some subset of 3800 </td><td>87 </td><td>43 </td><td>327 </td><td>0 </td></tr>
|
||||
</table>
|
||||
<h3>Some other serialization systems we compared against but did not benchmark (yet), in rough order of applicability:</h3>
|
||||
<ul>
|
||||
|
||||
@@ -67,7 +67,7 @@ $(document).ready(function(){initNavTree('md__compiler.html','');});
|
||||
<li><code>-t</code> : If data is contained in this file, generate a <code>filename.json</code> representing the data in the flatbuffer.</li>
|
||||
<li><code>-o PATH</code> : Output all generated files to PATH (either absolute, or relative to the current directory). If omitted, PATH will be the current directory. PATH should end in your systems path separator, e.g. <code>/</code> or <code>\</code>.</li>
|
||||
<li><code>-I PATH</code> : when encountering <code>include</code> statements, attempt to load the files from this path. Paths will be tried in the order given, and if all fail (or none are specified) it will try to load relative to the path of the schema file being parsed.</li>
|
||||
<li><code>--strict-json</code> : Generate strict JSON (field names are enclosed in quotes). By default, no quotes are generated.</li>
|
||||
<li><code>--strict-json</code> : Require & generate strict JSON (field names are enclosed in quotes, no trailing commas in tables/vectors). By default, no quotes are required/generated, and trailing commas are allowed.</li>
|
||||
<li><code>--no-prefix</code> : Don't prefix enum values in generated C++ by their enum type.</li>
|
||||
<li><code>--gen-includes</code> : Generate include statements for included schemas the generated file depends on (C++).</li>
|
||||
<li><code>--proto</code>: Expect input files to be .proto files (protocol buffers). Output the corresponding .fbs file. Currently supports: <code>package</code>, <code>message</code>, <code>enum</code>. Does not support, but will skip without error: <code>import</code>, <code>option</code>. Does not support, will generate error: <code>service</code>, <code>extend</code>, <code>extensions</code>, <code>oneof</code>, <code>group</code>, custom options, nested declarations. </li>
|
||||
|
||||
@@ -65,6 +65,7 @@ $(document).ready(function(){initNavTree('md__cpp_usage.html','');});
|
||||
</div><!-- fragment --><p><code>CreateString</code> and <code>CreateVector</code> serialize these two built-in datatypes, and return offsets into the serialized data indicating where they are stored, such that <code>Monster</code> below can refer to them.</p>
|
||||
<p><code>CreateString</code> can also take an <code>std::string</code>, or a <code>const char *</code> with an explicit length, and is suitable for holding UTF-8 and binary data if needed.</p>
|
||||
<p><code>CreateVector</code> can also take an <code>std::vector</code>. The offset it returns is typed, i.e. can only be used to set fields of the correct type below. To create a vector of struct objects (which will be stored as contiguous memory in the buffer, use <code>CreateVectorOfStructs</code> instead.</p>
|
||||
<p>To create a vector of nested objects (e.g. tables, strings or other vectors) collect their offsets in a temporary array/vector, then call <code>CreateVector</code> on that (see e.g. the array of strings example in <code>test.cpp</code> <code>CreateFlatBufferTest</code>).</p>
|
||||
<div class="fragment"><div class="line">Vec3 vec(1, 2, 3);</div>
|
||||
</div><!-- fragment --><p><code>Vec3</code> is the first example of code from our generated header. Structs (unlike tables) translate to simple structs in C++, so we can construct them in a familiar way.</p>
|
||||
<p>We have now serialized the non-scalar components of of the monster example, so we could create the monster something like this:</p>
|
||||
@@ -82,11 +83,12 @@ $(document).ready(function(){initNavTree('md__cpp_usage.html','');});
|
||||
<p>Regardless of whether you used <code>CreateMonster</code> or <code>MonsterBuilder</code>, you now have an offset to the root of your data, and you can finish the buffer using:</p>
|
||||
<div class="fragment"><div class="line">FinishMonsterBuffer(fbb, mloc);</div>
|
||||
</div><!-- fragment --><p>The buffer is now ready to be stored somewhere, sent over the network, be compressed, or whatever you'd like to do with it. You can access the start of the buffer with <code>fbb.GetBufferPointer()</code>, and it's size from <code>fbb.GetSize()</code>.</p>
|
||||
<p>Calling code may take ownership of the buffer with <code>fbb.ReleaseBufferPointer()</code>. Should you do it, the <code>FlatBufferBuilder</code> will be in an invalid state, and <em>must</em> be cleared before it can be used again. However, it also means you are able to destroy the builder while keeping the buffer in your application.</p>
|
||||
<p><code>samples/sample_binary.cpp</code> is a complete code sample similar to the code above, that also includes the reading code below.</p>
|
||||
<h3>Reading in C++</h3>
|
||||
<p>If you've received a buffer from somewhere (disk, network, etc.) you can directly start traversing it using:</p>
|
||||
<div class="fragment"><div class="line"><span class="keyword">auto</span> monster = GetMonster(buffer_pointer);</div>
|
||||
</div><!-- fragment --><p><code>monster</code> is of type <code>Monster *</code>, and points to somewhere inside your buffer. If you look in your generated header, you'll see it has convenient accessors for all fields, e.g.</p>
|
||||
</div><!-- fragment --><p><code>monster</code> is of type <code>Monster *</code>, and points to somewhere <em>inside</em> your buffer (root object pointers are not the same as <code>buffer_pointer</code> !). If you look in your generated header, you'll see it has convenient accessors for all fields, e.g.</p>
|
||||
<div class="fragment"><div class="line">assert(monster->hp() == 80);</div>
|
||||
<div class="line">assert(monster->mana() == 150); <span class="comment">// default</span></div>
|
||||
<div class="line">assert(strcmp(monster->name()->c_str(), <span class="stringliteral">"MyMonster"</span>) == 0);</div>
|
||||
@@ -100,7 +102,15 @@ $(document).ready(function(){initNavTree('md__cpp_usage.html','');});
|
||||
<div class="fragment"><div class="line"><span class="keyword">auto</span> inv = monster->inventory();</div>
|
||||
<div class="line">assert(inv);</div>
|
||||
<div class="line">assert(inv->Get(9) == 9);</div>
|
||||
</div><!-- fragment --><h3>Direct memory access</h3>
|
||||
</div><!-- fragment --><h3>Storing maps / dictionaries in a FlatBuffer</h3>
|
||||
<p>FlatBuffers doesn't support maps natively, but there is support to emulate their behavior with vectors and binary search, which means you can have fast lookups directly from a FlatBuffer without having to unpack your data into a <code>std::map</code> or similar.</p>
|
||||
<p>To use it:</p><ul>
|
||||
<li>Designate one of the fields in a table as they "key" field. You do this by setting the <code>key</code> attribute on this field, e.g. <code>name:string (key)</code>. You may only have one key field, and it must be of string or scalar type.</li>
|
||||
<li>Write out tables of this type as usual, collect their offsets in an array or vector.</li>
|
||||
<li>Instead of <code>CreateVector</code>, call <code>CreateVectorOfSortedTables</code>, which will first sort all offsets such that the tables they refer to are sorted by the key field, then serialize it.</li>
|
||||
<li>Now when you're accessing the FlatBuffer, you can use <code>Vector::LookupByKey</code> instead of just <code>Vector::Get</code> to access elements of the vector, e.g.: <code>myvector->LookupByKey("Fred")</code>, which returns a pointer to the corresponding table type, or <code>nullptr</code> if not found. <code>LookupByKey</code> performs a binary search, so should have a similar speed to <code>std::map</code>, though may be faster because of better caching. <code>LookupByKey</code> only works if the vector has been sorted, it will likely not find elements if it hasn't been sorted.</li>
|
||||
</ul>
|
||||
<h3>Direct memory access</h3>
|
||||
<p>As you can see from the above examples, all elements in a buffer are accessed through generated accessors. This is because everything is stored in little endian format on all platforms (the accessor performs a swap operation on big endian machines), and also because the layout of things is generally not known to the user.</p>
|
||||
<p>For structs, layout is deterministic and guaranteed to be the same accross platforms (scalars are aligned to their own size, and structs themselves to their largest member), and you are allowed to access this memory directly by using <code>sizeof()</code> and <code>memcpy</code> on the pointer to a struct, or even an array of structs.</p>
|
||||
<p>To compute offsets to sub-elements of a struct, make sure they are a structs themselves, as then you can use the pointers to figure out the offset without having to hardcode it. This is handy for use of arrays of structs with calls like <code>glVertexAttribPointer</code> in OpenGL or similar APIs.</p>
|
||||
@@ -114,7 +124,7 @@ $(document).ready(function(){initNavTree('md__cpp_usage.html','');});
|
||||
</div><!-- fragment --><p>if <code>ok</code> is true, the buffer is safe to read.</p>
|
||||
<p>Besides untrusted data, this function may be useful to call in debug mode, as extra insurance against data being corrupted somewhere along the way.</p>
|
||||
<p>While verifying a buffer isn't "free", it is typically faster than a full traversal (since any scalar data is not actually touched), and since it may cause the buffer to be brought into cache before reading, the actual overhead may be even lower than expected.</p>
|
||||
<p>In specialized cases where a denial of service attack is possible, the verifier has two additional constructor arguments that allow you to limit the nesting depth and total amount of tables the verifier may encounter before declaring the buffer malformed.</p>
|
||||
<p>In specialized cases where a denial of service attack is possible, the verifier has two additional constructor arguments that allow you to limit the nesting depth and total amount of tables the verifier may encounter before declaring the buffer malformed. The default is <code>Verifier(buf, len, 64 /* max depth */, 1000000, /* max tables */)</code> which should be sufficient for most uses.</p>
|
||||
<h2>Text & schema parsing</h2>
|
||||
<p>Using binary buffers with the generated header provides a super low overhead use of FlatBuffer data. There are, however, times when you want to use text formats, for example because it interacts better with source control, or you want to give your users easy access to data.</p>
|
||||
<p>Another reason might be that you already have a lot of data in JSON format, or a tool that generates JSON, and if you can write a schema for it, this will provide you an easy way to use that data directly.</p>
|
||||
@@ -137,7 +147,8 @@ $(document).ready(function(){initNavTree('md__cpp_usage.html','');});
|
||||
<p>After each JSON file, the <code>Parser::fbb</code> member variable is the <code>FlatBufferBuilder</code> that contains the binary buffer version of that file, that you can access as described above.</p>
|
||||
<p><code>samples/sample_text.cpp</code> is a code sample showing the above operations.</p>
|
||||
<h3>Threading</h3>
|
||||
<p>None of the code is thread-safe, by design. That said, since currently a FlatBuffer is read-only and entirely <code>const</code>, reading by multiple threads is possible. </p>
|
||||
<p>Reading a FlatBuffer does not touch any memory outside the original buffer, and is entirely read-only (all const), so is safe to access from multiple threads even without synchronisation primitives.</p>
|
||||
<p>Creating a FlatBuffer is not thread safe. All state related to building a FlatBuffer is contained in a FlatBufferBuilder instance, and no memory outside of it is touched. To make this thread safe, either do not share instances of FlatBufferBuilder between threads (recommended), or manually wrap it in synchronisation primites. There's no automatic way to accomplish this, by design, as we feel multithreaded construction of a single buffer will be rare, and synchronisation overhead would be costly. </p>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
|
||||
@@ -87,15 +87,16 @@ $(document).ready(function(){initNavTree('md__go_usage.html','');});
|
||||
<div class="line">example.MonsterAddTest(builder, mon2)</div>
|
||||
<div class="line">example.MonsterAddTest4(builder, test4s)</div>
|
||||
<div class="line">mon := example.MonsterEnd(builder)</div>
|
||||
</div><!-- fragment --><p>Unlike C++, Go does not support table creation functions like 'createMonster()'. This is to create the buffer without using temporary object allocation (since the <code>Vec3</code> is an inline component of <code>Monster</code>, it has to be created right where it is added, whereas the name and the inventory are not inline). Structs do have convenient methods that allow you to construct them in one call. These also have arguments for nested structs, e.g. if a struct has a field <code>a</code> and a nested struct field <code>b</code> (which has fields <code>c</code> and <code>d</code>), then the arguments will be <code>a</code>, <code>c</code> and <code>d</code>.</p>
|
||||
</div><!-- fragment --><p>Unlike C++, Go does not support table creation functions like 'createMonster()'. This is to create the buffer without using temporary object allocation (since the <code>Vec3</code> is an inline component of <code>Monster</code>, it has to be created right where it is added, whereas the name and the inventory are not inline, and <b>must</b> be created outside of the table creation sequence). Structs do have convenient methods that allow you to construct them in one call. These also have arguments for nested structs, e.g. if a struct has a field <code>a</code> and a nested struct field <code>b</code> (which has fields <code>c</code> and <code>d</code>), then the arguments will be <code>a</code>, <code>c</code> and <code>d</code>.</p>
|
||||
<p>Vectors also use this start/end pattern to allow vectors of both scalar types and structs:</p>
|
||||
<div class="fragment"><div class="line">example.MonsterStartInventoryVector(builder, 5)</div>
|
||||
<div class="line"><span class="keywordflow">for</span> i := 4; i >= 0; i-- {</div>
|
||||
<div class="line"> builder.PrependByte(byte(i))</div>
|
||||
<div class="line">}</div>
|
||||
<div class="line">inv := builder.EndVector(5)</div>
|
||||
</div><!-- fragment --><p>The generated method 'StartInventoryVector' is provided as a convenience function which calls 'StartVector' with the correct element size of the vector type which in this case is 'ubyte' or 1 byte per vector element. You pass the number of elements you want to write. You write the elements backwards since the buffer is being constructed back to front.</p>
|
||||
</div><!-- fragment --><p>The generated method 'StartInventoryVector' is provided as a convenience function which calls 'StartVector' with the correct element size of the vector type which in this case is 'ubyte' or 1 byte per vector element. You pass the number of elements you want to write. You write the elements backwards since the buffer is being constructed back to front. Use the correct <code>Prepend</code> call for the type, or <code>PrependUOffsetT</code> for offsets. You then pass <code>inv</code> to the corresponding <code>Add</code> call when you construct the table containing it afterwards.</p>
|
||||
<p>There are <code>Prepend</code> functions for all the scalar types. You use <code>PrependUOffset</code> for any previously constructed objects (such as other tables, strings, vectors). For structs, you use the appropriate <code>create</code> function in-line, as shown above in the <code>Monster</code> example.</p>
|
||||
<p>Once you're done constructing a buffer, you call <code>Finish</code> with the root object offset (<code>mon</code> in the example above). Your data now resides in Builder.Bytes. Important to note is that the real data starts at the index indicated by Head(), for Offset() bytes (this is because the buffer is constructed backwards). If you wanted to read the buffer right after creating it (using <code>GetRootAsMonster</code> above), the second argument, instead of <code>0</code> would thus also be <code>Head()</code>.</p>
|
||||
<h2>Text Parsing</h2>
|
||||
<p>There currently is no support for parsing text (Schema's and JSON) directly from Go, though you could use the C++ parser through cgo. Please see the C++ documentation for more on text parsing. </p>
|
||||
</div></div><!-- contents -->
|
||||
|
||||
@@ -53,9 +53,10 @@ $(document).ready(function(){initNavTree('md__grammar.html','');});
|
||||
<div class="title">Formal Grammar of the schema language </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>schema = include* ( namespace_decl | type_decl | enum_decl | root_decl | object )*</p>
|
||||
<div class="textblock"><p>schema = include* ( namespace_decl | type_decl | enum_decl | root_decl | attribute_decl | object )*</p>
|
||||
<p>include = <code>include</code> string_constant <code>;</code></p>
|
||||
<p>namespace_decl = <code>namespace</code> ident ( <code>.</code> ident )* <code>;</code></p>
|
||||
<p>attribute_decl = <code>attribute</code> string_constant <code>;</code></p>
|
||||
<p>type_decl = ( <code>table</code> | <code>struct</code> ) ident metadata <code>{</code> field_decl+ <code>}</code></p>
|
||||
<p>enum_decl = ( <code>enum</code> | <code>union</code> ) ident [ <code>:</code> type ] metadata <code>{</code> commasep( enumval_decl ) <code>}</code></p>
|
||||
<p>root_decl = <code>root_type</code> ident <code>;</code></p>
|
||||
|
||||
@@ -67,9 +67,9 @@ $(document).ready(function(){initNavTree('md__internals.html','');});
|
||||
<h3>Structs</h3>
|
||||
<p>These are the simplest, and as mentioned, intended for simple data that benefits from being extra efficient and doesn't need versioning / extensibility. They are always stored inline in their parent (a struct, table, or vector) for maximum compactness. Structs define a consistent memory layout where all components are aligned to their size, and structs aligned to their largest scalar member. This is done independent of the alignment rules of the underlying compiler to guarantee a cross platform compatible layout. This layout is then enforced in the generated code.</p>
|
||||
<h3>Tables</h3>
|
||||
<p>These start with an <code>soffset_t</code> to a vtable (signed version of <code>uoffset_t</code>, since vtables may be stored anywhere), followed by all the fields as aligned scalars (or offsets). Unlike structs, not all fields need to be present. There is no set order and layout.</p>
|
||||
<p>These start with an <code>soffset_t</code> to a vtable. This is a signed version of <code>uoffset_t</code>, since vtables may be stored anywhere relative to the object. This offset is substracted (not added) from the object start to arrive at the vtable start. This offset is followed by all the fields as aligned scalars (or offsets). Unlike structs, not all fields need to be present. There is no set order and layout.</p>
|
||||
<p>To be able to access fields regardless of these uncertainties, we go through a vtable of offsets. Vtables are shared between any objects that happen to have the same vtable values.</p>
|
||||
<p>The elements of a vtable are all of type <code>voffset_t</code>, which is a <code>uint16_t</code>. The first element is the number of elements of the vtable, including this one. The second one is the size of the object, in bytes (including the vtable offset). This size is used for streaming, to know how many bytes to read to be able to access all fields of the object. The remaining elements are the N offsets, where N is the amount of fields declared in the schema when the code that constructed this buffer was compiled (thus, the size of the table is N + 2).</p>
|
||||
<p>The elements of a vtable are all of type <code>voffset_t</code>, which is a <code>uint16_t</code>. The first element is the size of the vtable in bytes, including the size element. The second one is the size of the object, in bytes (including the vtable offset). This size could be used for streaming, to know how many bytes to read to be able to access all fields of the object. The remaining elements are the N offsets, where N is the amount of fields declared in the schema when the code that constructed this buffer was compiled (thus, the size of the table is N + 2).</p>
|
||||
<p>All accessor functions in the generated code for tables contain the offset into this table as a constant. This offset is checked against the first field (the number of elements), to protect against newer code reading older data. If this offset is out of range, or the vtable entry is 0, that means the field is not present in this object, and the default value is return. Otherwise, the entry is used as offset to the field to be read.</p>
|
||||
<h3>Strings and Vectors</h3>
|
||||
<p>Strings are simply a vector of bytes, and are always null-terminated. Vectors are stored as contiguous aligned scalar elements prefixed by a 32bit element count (not including any null termination).</p>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Use in Java</title>
|
||||
<title>FlatBuffers: Use in Java/C-sharp</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
<script type="text/javascript" src="dynsections.js"></script>
|
||||
@@ -50,10 +50,11 @@ $(document).ready(function(){initNavTree('md__java_usage.html','');});
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Use in Java </div> </div>
|
||||
<div class="title">Use in Java/C-sharp </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>FlatBuffers supports reading and writing binary FlatBuffers in Java. Generate code for Java with the <code>-j</code> option to <code>flatc</code>.</p>
|
||||
<div class="textblock"><p>FlatBuffers supports reading and writing binary FlatBuffers in Java and C#. Generate code for Java with the <code>-j</code> option to <code>flatc</code>, or for C# with <code>-n</code> (think .Net).</p>
|
||||
<p>Note that this document is from the perspective of Java. Code for both languages is generated in the same way, with only very subtle differences, for example any <code>camelCase</code> Java call will be <code>CamelCase</code> in C#.</p>
|
||||
<p>See <code>javaTest.java</code> for an example. Essentially, you read a FlatBuffer binary file into a <code>byte[]</code>, which you then turn into a <code>ByteBuffer</code>, which you pass to the <code>getRootAsMyRootType</code> function:</p>
|
||||
<div class="fragment"><div class="line">ByteBuffer bb = ByteBuffer.wrap(data);</div>
|
||||
<div class="line">Monster monster = Monster.getRootAsMonster(bb);</div>
|
||||
@@ -94,7 +95,7 @@ $(document).ready(function(){initNavTree('md__java_usage.html','');});
|
||||
<div class="fragment"><div class="line">Monster.startInventoryVector(fbb, 5);</div>
|
||||
<div class="line"><span class="keywordflow">for</span> (byte i = 4; i >=0; i--) fbb.addByte(i);</div>
|
||||
<div class="line"><span class="keywordtype">int</span> inv = fbb.endVector();</div>
|
||||
</div><!-- fragment --><p>You can use the generated method <code>startInventoryVector</code> to conveniently call <code>startVector</code> with the right element size. You pass the number of elements you want to write. Note how you write the elements backwards since the buffer is being constructed back to front.</p>
|
||||
</div><!-- fragment --><p>You can use the generated method <code>startInventoryVector</code> to conveniently call <code>startVector</code> with the right element size. You pass the number of elements you want to write. Note how you write the elements backwards since the buffer is being constructed back to front. You then pass <code>inv</code> to the corresponding <code>Add</code> call when you construct the table containing it afterwards.</p>
|
||||
<p>There are <code>add</code> functions for all the scalar types. You use <code>addOffset</code> for any previously constructed objects (such as other tables, strings, vectors). For structs, you use the appropriate <code>create</code> function in-line, as shown above in the <code>Monster</code> example.</p>
|
||||
<p>To finish the buffer, call:</p>
|
||||
<div class="fragment"><div class="line">Monster.finishMonsterBuffer(fbb, mon);</div>
|
||||
|
||||
@@ -57,6 +57,8 @@ $(document).ready(function(){initNavTree('md__schemas.html','');});
|
||||
|
||||
namespace MyGame;
|
||||
|
||||
attribute "priority";
|
||||
|
||||
enum Color : byte { Red = 1, Green, Blue }
|
||||
|
||||
union Any { Monster, Weapon, Pickup }
|
||||
@@ -91,12 +93,15 @@ root_type Monster;
|
||||
<h3>Structs</h3>
|
||||
<p>Similar to a table, only now none of the fields are optional (so no defaults either), and fields may not be added or be deprecated. Structs may only contain scalars or other structs. Use this for simple objects where you are very sure no changes will ever be made (as quite clear in the example <code>Vec3</code>). Structs use less memory than tables and are even faster to access (they are always stored in-line in their parent object, and use no virtual table).</p>
|
||||
<h3>Types</h3>
|
||||
<p>Builtin scalar types are:</p>
|
||||
<p>Built-in scalar types are:</p>
|
||||
<ul>
|
||||
<li>8 bit: <code>byte ubyte bool</code></li>
|
||||
<li>16 bit: <code>short ushort</code></li>
|
||||
<li>32 bit: <code>int uint float</code></li>
|
||||
<li>64 bit: <code>long ulong double</code></li>
|
||||
</ul>
|
||||
<p>Built-in non-scalar types:</p>
|
||||
<ul>
|
||||
<li>Vector of any other type (denoted with <code>[type]</code>). Nesting vectors is not supported, instead you can wrap the inner vector in a table.</li>
|
||||
<li><code>string</code>, which may only hold UTF-8 or 7-bit ASCII. For other text encodings or general binary data use vectors (<code>[byte]</code> or <code>[ubyte]</code>) instead.</li>
|
||||
<li>References to other tables or structs, enums or unions (see below).</li>
|
||||
@@ -109,10 +114,12 @@ root_type Monster;
|
||||
<p>Define a sequence of named constants, each with a given value, or increasing by one from the previous one. The default first value is <code>0</code>. As you can see in the enum declaration, you specify the underlying integral type of the enum with <code>:</code> (in this case <code>byte</code>), which then determines the type of any fields declared with this enum type.</p>
|
||||
<h3>Unions</h3>
|
||||
<p>Unions share a lot of properties with enums, but instead of new names for constants, you use names of tables. You can then declare a union field which can hold a reference to any of those types, and additionally a hidden field with the suffix <code>_type</code> is generated that holds the corresponding enum value, allowing you to know which type to cast to at runtime.</p>
|
||||
<p>Unions are a good way to be able to send multiple message types as a FlatBuffer. Note that because a union field is really two fields, it must always be part of a table, it cannot be the root of a FlatBuffer by itself.</p>
|
||||
<p>If you have a need to distinguish between different FlatBuffers in a more open-ended way, for example for use as files, see the file identification feature below.</p>
|
||||
<h3>Namespaces</h3>
|
||||
<p>These will generate the corresponding namespace in C++ for all helper code, and packages in Java. You can use <code>.</code> to specify nested namespaces / packages.</p>
|
||||
<h3>Includes</h3>
|
||||
<p>You can include other schemas files in your current one, e.g.: </p><pre class="fragment">include "mydefinitions.fbs"
|
||||
<p>You can include other schemas files in your current one, e.g.: </p><pre class="fragment">include "mydefinitions.fbs";
|
||||
</pre><p>This makes it easier to refer to types defined elsewhere. <code>include</code> automatically ensures each file is parsed just once, even when referred to more than once.</p>
|
||||
<p>When using the <code>flatc</code> compiler to generate code for schema definitions, only definitions in the current file will be generated, not those from the included files (those you still generate separately).</p>
|
||||
<h3>Root type</h3>
|
||||
@@ -124,11 +131,12 @@ root_type Monster;
|
||||
</pre><p>Identifiers must always be exactly 4 characters long. These 4 characters will end up as bytes at offsets 4-7 (inclusive) in the buffer.</p>
|
||||
<p>For any schema that has such an identifier, <code>flatc</code> will automatically add the identifier to any binaries it generates (with <code>-b</code>), and generated calls like <code>FinishMonsterBuffer</code> also add the identifier. If you have specified an identifier and wish to generate a buffer without one, you can always still do so by calling <code>FlatBufferBuilder::Finish</code> explicitly.</p>
|
||||
<p>After loading a buffer, you can use a call like <code>MonsterBufferHasIdentifier</code> to check if the identifier is present.</p>
|
||||
<p>Note that this is best for open-ended uses such as files. If you simply wanted to send one of a set of possible messages over a network for example, you'd be better off with a union.</p>
|
||||
<p>Additionally, by default <code>flatc</code> will output binary files as <code>.bin</code>. This declaration in the schema will change that to whatever you want: </p><pre class="fragment">file_extension "ext";
|
||||
</pre><h3>Comments & documentation</h3>
|
||||
<p>May be written as in most C-based languages. Additionally, a triple comment (<code>///</code>) on a line by itself signals that a comment is documentation for whatever is declared on the line after it (table/struct/field/enum/union/element), and the comment is output in the corresponding C++ code. Multiple such lines per item are allowed.</p>
|
||||
<h3>Attributes</h3>
|
||||
<p>Attributes may be attached to a declaration, behind a field, or after the name of a table/struct/enum/union. These may either have a value or not. Some attributes like <code>deprecated</code> are understood by the compiler, others are simply ignored (like <code>priority</code>), but are available to query if you parse the schema at runtime. This is useful if you write your own code generators/editors etc., and you wish to add additional information specific to your tool (such as a help text).</p>
|
||||
<p>Attributes may be attached to a declaration, behind a field, or after the name of a table/struct/enum/union. These may either have a value or not. Some attributes like <code>deprecated</code> are understood by the compiler, user defined ones need to be declared with the attribute declaration (like <code>priority</code> in the example above), and are available to query if you parse the schema at runtime. This is useful if you write your own code generators/editors etc., and you wish to add additional information specific to your tool (such as a help text).</p>
|
||||
<p>Current understood attributes:</p>
|
||||
<ul>
|
||||
<li><code>id: n</code> (on a table field): manually set the field identifier to <code>n</code>. If you use this attribute, you must use it on ALL fields of this table, and the numbers must be a contiguous range from 0 onwards. Additionally, since a union type effectively adds two fields, its id must be that of the second field (the first field is the type field and not explicitly declared in the schema). For example, if the last field before the union field had id 6, the union field should have id 8, and the unions type field will implicitly be 7. IDs allow the fields to be placed in any order in the schema. When a new field is added to the schema is must use the next available ID.</li>
|
||||
@@ -137,6 +145,8 @@ root_type Monster;
|
||||
<li><code>original_order</code> (on a table): since elements in a table do not need to be stored in any particular order, they are often optimized for space by sorting them to size. This attribute stops that from happening.</li>
|
||||
<li><code>force_align: size</code> (on a struct): force the alignment of this struct to be something higher than what it is naturally aligned to. Causes these structs to be aligned to that amount inside a buffer, IF that buffer is allocated with that alignment (which is not necessarily the case for buffers accessed directly inside a <code>FlatBufferBuilder</code>).</li>
|
||||
<li><code>bit_flags</code> (on an enum): the values of this field indicate bits, meaning that any value N specified in the schema will end up representing 1<<N, or if you don't specify values at all, you'll get the sequence 1, 2, 4, 8, ...</li>
|
||||
<li><code>nested_flatbuffer: "table_name"</code> (on a field): this indicates that the field (which must be a vector of ubyte) contains flatbuffer data, for which the root type is given by <code>table_name</code>. The generated code will then produce a convenient accessor for the nested FlatBuffer.</li>
|
||||
<li><code>key</code> (on a field): this field is meant to be used as a key when sorting a vector of the type of table it sits in. Can be used for in-place binary search.</li>
|
||||
</ul>
|
||||
<h2>JSON Parsing</h2>
|
||||
<p>The same parser that parses the schema declarations above is also able to parse JSON objects that conform to this schema. So, unlike other JSON parsers, this parser is strongly typed, and parses directly into a FlatBuffer (see the compiler documentation on how to do this from the command line, or the C++ documentation on how to do this at runtime).</p>
|
||||
@@ -144,6 +154,7 @@ root_type Monster;
|
||||
<ul>
|
||||
<li>It accepts field names with and without quotes, like many JSON parsers already do. It outputs them without quotes as well, though can be made to output them using the <code>strict_json</code> flag.</li>
|
||||
<li>If a field has an enum type, the parser will recognize symbolic enum values (with or without quotes) instead of numbers, e.g. <code>field: EnumVal</code>. If a field is of integral type, you can still use symbolic names, but values need to be prefixed with their type and need to be quoted, e.g. <code>field: "Enum.EnumVal"</code>. For enums representing flags, you may place multiple inside a string separated by spaces to OR them, e.g. <code>field: "EnumVal1 EnumVal2"</code> or <code>field: "Enum.EnumVal1 Enum.EnumVal2"</code>.</li>
|
||||
<li>Similarly, for unions, these need to specified with two fields much like you do when serializing from code. E.g. for a field <code>foo</code>, you must add a field <code>foo_type: FooOne</code> right before the <code>foo</code> field, where <code>FooOne</code> would be the table out of the union you want to use.</li>
|
||||
</ul>
|
||||
<p>When parsing JSON, it recognizes the following escape codes in strings:</p>
|
||||
<ul>
|
||||
|
||||
@@ -6,7 +6,7 @@ var NAVTREE =
|
||||
[ "Writing a schema", "md__schemas.html", null ],
|
||||
[ "Use in C++", "md__cpp_usage.html", null ],
|
||||
[ "Use in Go", "md__go_usage.html", null ],
|
||||
[ "Use in Java", "md__java_usage.html", null ],
|
||||
[ "Use in Java/C-sharp", "md__java_usage.html", null ],
|
||||
[ "Benchmarks", "md__benchmarks.html", null ],
|
||||
[ "FlatBuffers white paper", "md__white_paper.html", null ],
|
||||
[ "FlatBuffer Internals", "md__internals.html", null ],
|
||||
|
||||
@@ -60,7 +60,7 @@ $(document).ready(function(){initNavTree('pages.html','');});
|
||||
<tr id="row_2_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__schemas.html" target="_self">Writing a schema</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_3_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__cpp_usage.html" target="_self">Use in C++</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_4_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__go_usage.html" target="_self">Use in Go</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_5_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__java_usage.html" target="_self">Use in Java</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_5_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__java_usage.html" target="_self">Use in Java/C-sharp</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_6_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__benchmarks.html" target="_self">Benchmarks</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_7_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__white_paper.html" target="_self">FlatBuffers white paper</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_8_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__internals.html" target="_self">FlatBuffer Internals</a></td><td class="desc"></td></tr>
|
||||
|
||||
@@ -5,6 +5,12 @@ Comparing against other serialization solutions, running on Windows 7
|
||||
overhead), Rapid JSON (one of the fastest C++ JSON parsers around),
|
||||
and pugixml, also one of the fastest XML parsers.
|
||||
|
||||
We also compare against code that doesn't use a serialization library
|
||||
at all (the column "Raw structs"), which is what you get if you write
|
||||
hardcoded code that just writes structs. This is the fastest possible,
|
||||
but of course is not cross platform nor has any kind of forwards /
|
||||
backwards compatibility.
|
||||
|
||||
We compare against Flatbuffers with the binary wire format (as
|
||||
intended), and also with JSON as the wire format with the optional JSON
|
||||
parser (which, using a schema, parses JSON into a binary buffer that can
|
||||
@@ -14,17 +20,17 @@ The benchmark object is a set of about 10 objects containing an array, 4
|
||||
strings, and a large variety of int/float scalar values of all sizes,
|
||||
meant to be representative of game data, e.g. a scene format.
|
||||
|
||||
| | FlatBuffers (binary) | Protocol Buffers LITE | Rapid JSON | FlatBuffers (JSON) | pugixml |
|
||||
|--------------------------------------------------------|-----------------------|-----------------------|-----------------------|-----------------------| ----------------------|
|
||||
| Decode + Traverse + Dealloc (1 million times, seconds) | 0.08 | 302 | 583 | 105 | 196 |
|
||||
| Decode / Traverse / Dealloc (breakdown) | 0 / 0.08 / 0 | 220 / 0.15 / 81 | 294 / 0.9 / 287 | 70 / 0.08 / 35 | 41 / 3.9 / 150 |
|
||||
| Encode (1 million times, seconds) | 3.2 | 185 | 650 | 169 | 273 |
|
||||
| Wire format size (normal / zlib, bytes) | 344 / 220 | 228 / 174 | 1475 / 322 | 1029 / 298 | 1137 / 341 |
|
||||
| Memory needed to store decoded wire (bytes / blocks) | 0 / 0 | 760 / 20 | 65689 / 4 | 328 / 1 | 34194 / 3 |
|
||||
| Transient memory allocated during decode (KB) | 0 | 1 | 131 | 4 | 34 |
|
||||
| Generated source code size (KB) | 4 | 61 | 0 | 4 | 0 |
|
||||
| Field access in handwritten traversal code | typed accessors | typed accessors | manual error checking | typed accessors | manual error checking |
|
||||
| Library source code (KB) | 15 | some subset of 3800 | 87 | 43 | 327 |
|
||||
| | FlatBuffers (binary) | Protocol Buffers LITE | Rapid JSON | FlatBuffers (JSON) | pugixml | Raw structs |
|
||||
|--------------------------------------------------------|-----------------------|-----------------------|-----------------------|-----------------------| ----------------------| ----------------------|
|
||||
| Decode + Traverse + Dealloc (1 million times, seconds) | 0.08 | 302 | 583 | 105 | 196 | 0.02 |
|
||||
| Decode / Traverse / Dealloc (breakdown) | 0 / 0.08 / 0 | 220 / 0.15 / 81 | 294 / 0.9 / 287 | 70 / 0.08 / 35 | 41 / 3.9 / 150 | 0 / 0.02 / 0 |
|
||||
| Encode (1 million times, seconds) | 3.2 | 185 | 650 | 169 | 273 | 0.15 |
|
||||
| Wire format size (normal / zlib, bytes) | 344 / 220 | 228 / 174 | 1475 / 322 | 1029 / 298 | 1137 / 341 | 312 / 187 |
|
||||
| Memory needed to store decoded wire (bytes / blocks) | 0 / 0 | 760 / 20 | 65689 / 4 | 328 / 1 | 34194 / 3 | 0 / 0 |
|
||||
| Transient memory allocated during decode (KB) | 0 | 1 | 131 | 4 | 34 | 0 |
|
||||
| Generated source code size (KB) | 4 | 61 | 0 | 4 | 0 | 0 |
|
||||
| Field access in handwritten traversal code | typed accessors | typed accessors | manual error checking | typed accessors | manual error checking | typed but no safety |
|
||||
| Library source code (KB) | 15 | some subset of 3800 | 87 | 43 | 327 | 0 |
|
||||
|
||||
### Some other serialization systems we compared against but did not benchmark (yet), in rough order of applicability:
|
||||
|
||||
|
||||
@@ -41,8 +41,9 @@ be generated for each file processed:
|
||||
fail (or none are specified) it will try to load relative to the path of
|
||||
the schema file being parsed.
|
||||
|
||||
- `--strict-json` : Generate strict JSON (field names are enclosed in quotes).
|
||||
By default, no quotes are generated.
|
||||
- `--strict-json` : Require & generate strict JSON (field names are enclosed
|
||||
in quotes, no trailing commas in tables/vectors). By default, no quotes are
|
||||
required/generated, and trailing commas are allowed.
|
||||
|
||||
- `--no-prefix` : Don't prefix enum values in generated C++ by their enum
|
||||
type.
|
||||
|
||||
@@ -42,6 +42,11 @@ correct type below. To create a vector of struct objects (which will
|
||||
be stored as contiguous memory in the buffer, use `CreateVectorOfStructs`
|
||||
instead.
|
||||
|
||||
To create a vector of nested objects (e.g. tables, strings or other vectors)
|
||||
collect their offsets in a temporary array/vector, then call `CreateVector`
|
||||
on that (see e.g. the array of strings example in `test.cpp`
|
||||
`CreateFlatBufferTest`).
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
Vec3 vec(1, 2, 3);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
@@ -107,6 +112,12 @@ be compressed, or whatever you'd like to do with it. You can access the
|
||||
start of the buffer with `fbb.GetBufferPointer()`, and it's size from
|
||||
`fbb.GetSize()`.
|
||||
|
||||
Calling code may take ownership of the buffer with `fbb.ReleaseBufferPointer()`.
|
||||
Should you do it, the `FlatBufferBuilder` will be in an invalid state,
|
||||
and *must* be cleared before it can be used again.
|
||||
However, it also means you are able to destroy the builder while keeping
|
||||
the buffer in your application.
|
||||
|
||||
`samples/sample_binary.cpp` is a complete code sample similar to
|
||||
the code above, that also includes the reading code below.
|
||||
|
||||
@@ -119,8 +130,9 @@ directly start traversing it using:
|
||||
auto monster = GetMonster(buffer_pointer);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
`monster` is of type `Monster *`, and points to somewhere inside your
|
||||
buffer. If you look in your generated header, you'll see it has
|
||||
`monster` is of type `Monster *`, and points to somewhere *inside* your
|
||||
buffer (root object pointers are not the same as `buffer_pointer` !).
|
||||
If you look in your generated header, you'll see it has
|
||||
convenient accessors for all fields, e.g.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
@@ -151,6 +163,32 @@ Similarly, we can access elements of the inventory array:
|
||||
assert(inv->Get(9) == 9);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
### Storing maps / dictionaries in a FlatBuffer
|
||||
|
||||
FlatBuffers doesn't support maps natively, but there is support to
|
||||
emulate their behavior with vectors and binary search, which means you
|
||||
can have fast lookups directly from a FlatBuffer without having to unpack
|
||||
your data into a `std::map` or similar.
|
||||
|
||||
To use it:
|
||||
- Designate one of the fields in a table as they "key" field. You do this
|
||||
by setting the `key` attribute on this field, e.g.
|
||||
`name:string (key)`.
|
||||
You may only have one key field, and it must be of string or scalar type.
|
||||
- Write out tables of this type as usual, collect their offsets in an
|
||||
array or vector.
|
||||
- Instead of `CreateVector`, call `CreateVectorOfSortedTables`,
|
||||
which will first sort all offsets such that the tables they refer to
|
||||
are sorted by the key field, then serialize it.
|
||||
- Now when you're accessing the FlatBuffer, you can use `Vector::LookupByKey`
|
||||
instead of just `Vector::Get` to access elements of the vector, e.g.:
|
||||
`myvector->LookupByKey("Fred")`, which returns a pointer to the
|
||||
corresponding table type, or `nullptr` if not found.
|
||||
`LookupByKey` performs a binary search, so should have a similar speed to
|
||||
`std::map`, though may be faster because of better caching. `LookupByKey`
|
||||
only works if the vector has been sorted, it will likely not find elements
|
||||
if it hasn't been sorted.
|
||||
|
||||
### Direct memory access
|
||||
|
||||
As you can see from the above examples, all elements in a buffer are
|
||||
@@ -213,7 +251,9 @@ reading, the actual overhead may be even lower than expected.
|
||||
In specialized cases where a denial of service attack is possible,
|
||||
the verifier has two additional constructor arguments that allow
|
||||
you to limit the nesting depth and total amount of tables the
|
||||
verifier may encounter before declaring the buffer malformed.
|
||||
verifier may encounter before declaring the buffer malformed. The default is
|
||||
`Verifier(buf, len, 64 /* max depth */, 1000000, /* max tables */)` which
|
||||
should be sufficient for most uses.
|
||||
|
||||
## Text & schema parsing
|
||||
|
||||
@@ -291,7 +331,14 @@ file, that you can access as described above.
|
||||
|
||||
### Threading
|
||||
|
||||
None of the code is thread-safe, by design. That said, since currently a
|
||||
FlatBuffer is read-only and entirely `const`, reading by multiple threads
|
||||
is possible.
|
||||
Reading a FlatBuffer does not touch any memory outside the original buffer,
|
||||
and is entirely read-only (all const), so is safe to access from multiple
|
||||
threads even without synchronisation primitives.
|
||||
|
||||
Creating a FlatBuffer is not thread safe. All state related to building
|
||||
a FlatBuffer is contained in a FlatBufferBuilder instance, and no memory
|
||||
outside of it is touched. To make this thread safe, either do not
|
||||
share instances of FlatBufferBuilder between threads (recommended), or
|
||||
manually wrap it in synchronisation primites. There's no automatic way to
|
||||
accomplish this, by design, as we feel multithreaded construction
|
||||
of a single buffer will be rare, and synchronisation overhead would be costly.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# FlatBuffers
|
||||
|
||||
FlatBuffers is an efficient cross platform serialization library for C++,
|
||||
with support for Java and Go. It was created at Google specifically for game
|
||||
with support for Java, C# and Go. It was created at Google specifically for game
|
||||
development and other performance-critical applications.
|
||||
|
||||
It is available as open source under the Apache license, v2 (see LICENSE.txt).
|
||||
@@ -48,8 +48,8 @@ It is available as open source under the Apache license, v2 (see LICENSE.txt).
|
||||
|
||||
Java and Go code supports object-reuse.
|
||||
|
||||
- **Cross platform C++11/Java/Go code with no dependencies** - will work with
|
||||
any recent gcc/clang and VS2010. Comes with build files for the tests &
|
||||
- **Cross platform C++11/Java/C#/Go code with no dependencies** - will work
|
||||
with any recent gcc/clang and VS2010. Comes with build files for the tests &
|
||||
samples (Android .mk files, and cmake for all other platforms).
|
||||
|
||||
### Why not use Protocol Buffers, or .. ?
|
||||
@@ -87,10 +87,10 @@ sections provide a more in-depth usage guide.
|
||||
Fields are optional and have defaults, so they don't need to be
|
||||
present for every object instance.
|
||||
|
||||
- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or Java/Go
|
||||
classes) with helper classes to access and construct serialized data. This
|
||||
header (say `mydata_generated.h`) only depends on `flatbuffers.h`, which
|
||||
defines the core functionality.
|
||||
- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or
|
||||
Java/C#/Go classes) with helper classes to access and construct serialized
|
||||
data. This header (say `mydata_generated.h`) only depends on
|
||||
`flatbuffers.h`, which defines the core functionality.
|
||||
|
||||
- Use the `FlatBufferBuilder` class to construct a flat binary buffer.
|
||||
The generated functions allow you to add objects to this
|
||||
@@ -110,7 +110,7 @@ sections provide a more in-depth usage guide.
|
||||
- How to [write a schema](md__schemas.html).
|
||||
- How to [use the generated C++ code](md__cpp_usage.html) in your own
|
||||
programs.
|
||||
- How to [use the generated Java code](md__java_usage.html) in your own
|
||||
- How to [use the generated Java/C# code](md__java_usage.html) in your own
|
||||
programs.
|
||||
- How to [use the generated Go code](md__go_usage.html) in your own
|
||||
programs.
|
||||
|
||||
@@ -75,7 +75,8 @@ Unlike C++, Go does not support table creation functions like 'createMonster()'.
|
||||
This is to create the buffer without
|
||||
using temporary object allocation (since the `Vec3` is an inline component of
|
||||
`Monster`, it has to be created right where it is added, whereas the name and
|
||||
the inventory are not inline).
|
||||
the inventory are not inline, and **must** be created outside of the table
|
||||
creation sequence).
|
||||
Structs do have convenient methods that allow you to construct them in one call.
|
||||
These also have arguments for nested structs, e.g. if a struct has a field `a`
|
||||
and a nested struct field `b` (which has fields `c` and `d`), then the arguments
|
||||
@@ -97,13 +98,23 @@ function which calls 'StartVector' with the correct element size of the vector
|
||||
type which in this case is 'ubyte' or 1 byte per vector element.
|
||||
You pass the number of elements you want to write.
|
||||
You write the elements backwards since the buffer
|
||||
is being constructed back to front.
|
||||
is being constructed back to front. Use the correct `Prepend` call for the type,
|
||||
or `PrependUOffsetT` for offsets. You then pass `inv` to the corresponding
|
||||
`Add` call when you construct the table containing it afterwards.
|
||||
|
||||
There are `Prepend` functions for all the scalar types. You use
|
||||
`PrependUOffset` for any previously constructed objects (such as other tables,
|
||||
strings, vectors). For structs, you use the appropriate `create` function
|
||||
in-line, as shown above in the `Monster` example.
|
||||
|
||||
Once you're done constructing a buffer, you call `Finish` with the root object
|
||||
offset (`mon` in the example above). Your data now resides in Builder.Bytes.
|
||||
Important to note is that the real data starts at the index indicated by Head(),
|
||||
for Offset() bytes (this is because the buffer is constructed backwards).
|
||||
If you wanted to read the buffer right after creating it (using
|
||||
`GetRootAsMonster` above), the second argument, instead of `0` would thus
|
||||
also be `Head()`.
|
||||
|
||||
## Text Parsing
|
||||
|
||||
There currently is no support for parsing text (Schema's and JSON) directly
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
# Formal Grammar of the schema language
|
||||
|
||||
schema = include*
|
||||
( namespace\_decl | type\_decl | enum\_decl | root\_decl | object )*
|
||||
( namespace\_decl | type\_decl | enum\_decl | root\_decl |
|
||||
attribute\_decl | object )*
|
||||
|
||||
include = `include` string\_constant `;`
|
||||
|
||||
namespace\_decl = `namespace` ident ( `.` ident )* `;`
|
||||
|
||||
attribute\_decl = `attribute` string\_constant `;`
|
||||
|
||||
type\_decl = ( `table` | `struct` ) ident metadata `{` field\_decl+ `}`
|
||||
|
||||
enum\_decl = ( `enum` | `union` ) ident [ `:` type ] metadata `{` commasep(
|
||||
|
||||
@@ -73,8 +73,10 @@ code.
|
||||
|
||||
### Tables
|
||||
|
||||
These start with an `soffset_t` to a vtable (signed version of
|
||||
`uoffset_t`, since vtables may be stored anywhere), followed by all the
|
||||
These start with an `soffset_t` to a vtable. This is a signed version of
|
||||
`uoffset_t`, since vtables may be stored anywhere relative to the object.
|
||||
This offset is substracted (not added) from the object start to arrive at
|
||||
the vtable start. This offset is followed by all the
|
||||
fields as aligned scalars (or offsets). Unlike structs, not all fields
|
||||
need to be present. There is no set order and layout.
|
||||
|
||||
@@ -83,9 +85,9 @@ through a vtable of offsets. Vtables are shared between any objects that
|
||||
happen to have the same vtable values.
|
||||
|
||||
The elements of a vtable are all of type `voffset_t`, which is
|
||||
a `uint16_t`. The first element is the number of elements of the vtable,
|
||||
including this one. The second one is the size of the object, in bytes
|
||||
(including the vtable offset). This size is used for streaming, to know
|
||||
a `uint16_t`. The first element is the size of the vtable in bytes,
|
||||
including the size element. The second one is the size of the object, in bytes
|
||||
(including the vtable offset). This size could be used for streaming, to know
|
||||
how many bytes to read to be able to access all fields of the object.
|
||||
The remaining elements are the N offsets, where N is the amount of fields
|
||||
declared in the schema when the code that constructed this buffer was
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
# Use in Java
|
||||
# Use in Java/C-sharp
|
||||
|
||||
FlatBuffers supports reading and writing binary FlatBuffers in Java. Generate
|
||||
code for Java with the `-j` option to `flatc`.
|
||||
FlatBuffers supports reading and writing binary FlatBuffers in Java and C#.
|
||||
Generate code for Java with the `-j` option to `flatc`, or for C# with `-n`
|
||||
(think .Net).
|
||||
|
||||
Note that this document is from the perspective of Java. Code for both languages
|
||||
is generated in the same way, with only very subtle differences, for example
|
||||
any `camelCase` Java call will be `CamelCase` in C#.
|
||||
|
||||
See `javaTest.java` for an example. Essentially, you read a FlatBuffer binary
|
||||
file into a `byte[]`, which you then turn into a `ByteBuffer`, which you pass to
|
||||
@@ -125,7 +130,8 @@ does not sit in an array, you can also use the start/end pattern:
|
||||
You can use the generated method `startInventoryVector` to conveniently call
|
||||
`startVector` with the right element size. You pass the number of
|
||||
elements you want to write. Note how you write the elements backwards since
|
||||
the buffer is being constructed back to front.
|
||||
the buffer is being constructed back to front. You then pass `inv` to the
|
||||
corresponding `Add` call when you construct the table containing it afterwards.
|
||||
|
||||
There are `add` functions for all the scalar types. You use `addOffset` for
|
||||
any previously constructed objects (such as other tables, strings, vectors).
|
||||
|
||||
@@ -9,6 +9,8 @@ first:
|
||||
|
||||
namespace MyGame;
|
||||
|
||||
attribute "priority";
|
||||
|
||||
enum Color : byte { Red = 1, Green, Blue }
|
||||
|
||||
union Any { Monster, Weapon, Pickup }
|
||||
@@ -80,7 +82,7 @@ parent object, and use no virtual table).
|
||||
|
||||
### Types
|
||||
|
||||
Builtin scalar types are:
|
||||
Built-in scalar types are:
|
||||
|
||||
- 8 bit: `byte ubyte bool`
|
||||
|
||||
@@ -90,6 +92,8 @@ Builtin scalar types are:
|
||||
|
||||
- 64 bit: `long ulong double`
|
||||
|
||||
Built-in non-scalar types:
|
||||
|
||||
- Vector of any other type (denoted with `[type]`). Nesting vectors
|
||||
is not supported, instead you can wrap the inner vector in a table.
|
||||
|
||||
@@ -135,6 +139,14 @@ additionally a hidden field with the suffix `_type` is generated that
|
||||
holds the corresponding enum value, allowing you to know which type to
|
||||
cast to at runtime.
|
||||
|
||||
Unions are a good way to be able to send multiple message types as a FlatBuffer.
|
||||
Note that because a union field is really two fields, it must always be
|
||||
part of a table, it cannot be the root of a FlatBuffer by itself.
|
||||
|
||||
If you have a need to distinguish between different FlatBuffers in a more
|
||||
open-ended way, for example for use as files, see the file identification
|
||||
feature below.
|
||||
|
||||
### Namespaces
|
||||
|
||||
These will generate the corresponding namespace in C++ for all helper
|
||||
@@ -145,7 +157,7 @@ packages.
|
||||
|
||||
You can include other schemas files in your current one, e.g.:
|
||||
|
||||
include "mydefinitions.fbs"
|
||||
include "mydefinitions.fbs";
|
||||
|
||||
This makes it easier to refer to types defined elsewhere. `include`
|
||||
automatically ensures each file is parsed just once, even when referred to
|
||||
@@ -193,6 +205,10 @@ without one, you can always still do so by calling
|
||||
After loading a buffer, you can use a call like
|
||||
`MonsterBufferHasIdentifier` to check if the identifier is present.
|
||||
|
||||
Note that this is best for open-ended uses such as files. If you simply wanted
|
||||
to send one of a set of possible messages over a network for example, you'd
|
||||
be better off with a union.
|
||||
|
||||
Additionally, by default `flatc` will output binary files as `.bin`.
|
||||
This declaration in the schema will change that to whatever you want:
|
||||
|
||||
@@ -211,8 +227,9 @@ in the corresponding C++ code. Multiple such lines per item are allowed.
|
||||
Attributes may be attached to a declaration, behind a field, or after
|
||||
the name of a table/struct/enum/union. These may either have a value or
|
||||
not. Some attributes like `deprecated` are understood by the compiler,
|
||||
others are simply ignored (like `priority`), but are available to query
|
||||
if you parse the schema at runtime.
|
||||
user defined ones need to be declared with the attribute declaration
|
||||
(like `priority` in the example above), and are
|
||||
available to query if you parse the schema at runtime.
|
||||
This is useful if you write your own code generators/editors etc., and
|
||||
you wish to add additional information specific to your tool (such as a
|
||||
help text).
|
||||
@@ -254,6 +271,13 @@ Current understood attributes:
|
||||
meaning that any value N specified in the schema will end up
|
||||
representing 1<<N, or if you don't specify values at all, you'll get
|
||||
the sequence 1, 2, 4, 8, ...
|
||||
- `nested_flatbuffer: "table_name"` (on a field): this indicates that the field
|
||||
(which must be a vector of ubyte) contains flatbuffer data, for which the
|
||||
root type is given by `table_name`. The generated code will then produce
|
||||
a convenient accessor for the nested FlatBuffer.
|
||||
- `key` (on a field): this field is meant to be used as a key when sorting
|
||||
a vector of the type of table it sits in. Can be used for in-place
|
||||
binary search.
|
||||
|
||||
## JSON Parsing
|
||||
|
||||
@@ -277,6 +301,10 @@ JSON:
|
||||
representing flags, you may place multiple inside a string
|
||||
separated by spaces to OR them, e.g.
|
||||
`field: "EnumVal1 EnumVal2"` or `field: "Enum.EnumVal1 Enum.EnumVal2"`.
|
||||
- Similarly, for unions, these need to specified with two fields much like
|
||||
you do when serializing from code. E.g. for a field `foo`, you must
|
||||
add a field `foo_type: FooOne` right before the `foo` field, where
|
||||
`FooOne` would be the table out of the union you want to use.
|
||||
|
||||
When parsing JSON, it recognizes the following escape codes in strings:
|
||||
|
||||
|
||||
@@ -152,7 +152,7 @@ func (b *Builder) EndObject() UOffsetT {
|
||||
// Doubles the size of the byteslice, and copies the old data towards the
|
||||
// end of the new byteslice (since we build the buffer backwards).
|
||||
func (b *Builder) growByteBuffer() {
|
||||
if (len(b.Bytes) & 0xC0000000) != 0 {
|
||||
if (int64(len(b.Bytes)) & int64(0xC0000000)) != 0 {
|
||||
panic("cannot grow buffer beyond 2 gigabytes")
|
||||
}
|
||||
newSize := len(b.Bytes) * 2
|
||||
@@ -259,6 +259,18 @@ func (b *Builder) CreateString(s string) UOffsetT {
|
||||
return b.EndVector(len(x))
|
||||
}
|
||||
|
||||
// CreateByteVector writes a ubyte vector
|
||||
func (b *Builder) CreateByteVector(v []byte) UOffsetT {
|
||||
b.Prep(int(SizeUOffsetT), len(v)*SizeByte)
|
||||
|
||||
l := UOffsetT(len(v))
|
||||
|
||||
b.head -= l
|
||||
copy(b.Bytes[b.head:b.head+l], v)
|
||||
|
||||
return b.EndVector(len(v))
|
||||
}
|
||||
|
||||
func (b *Builder) notNested() {
|
||||
// Check that no other objects are being built while making this
|
||||
// object. If not, panic:
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#if __cplusplus <= 199711L && \
|
||||
(!defined(_MSC_VER) || _MSC_VER < 1600) && \
|
||||
@@ -61,6 +63,13 @@
|
||||
#define FLATBUFFERS_STRING_EXPAND(X) #X
|
||||
#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
|
||||
|
||||
#if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
|
||||
(!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407))
|
||||
#define FLATBUFFERS_FINAL_CLASS final
|
||||
#else
|
||||
#define FLATBUFFERS_FINAL_CLASS
|
||||
#endif
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// Our default offset / size type, 32bit on purpose on 64bit systems.
|
||||
@@ -77,6 +86,10 @@ typedef uint16_t voffset_t;
|
||||
|
||||
typedef uintmax_t largest_scalar_t;
|
||||
|
||||
// Pointer to relinquished memory.
|
||||
typedef std::unique_ptr<uint8_t, std::function<void(uint8_t * /* unused */)>>
|
||||
unique_ptr_t;
|
||||
|
||||
// Wrapper for uoffset_t to allow safe template specialization.
|
||||
template<typename T> struct Offset {
|
||||
uoffset_t o;
|
||||
@@ -281,6 +294,31 @@ public:
|
||||
return reinterpret_cast<const uint8_t *>(&length_ + 1);
|
||||
}
|
||||
|
||||
template<typename K> return_type LookupByKey(K key) const {
|
||||
auto span = size();
|
||||
uoffset_t start = 0;
|
||||
// Perform binary search for key.
|
||||
while (span) {
|
||||
// Compare against middle element of current span.
|
||||
auto middle = span / 2;
|
||||
auto table = Get(start + middle);
|
||||
auto comp = table->KeyCompareWithValue(key);
|
||||
if (comp > 0) {
|
||||
// Greater than. Adjust span and try again.
|
||||
span = middle;
|
||||
} else if (comp < 0) {
|
||||
// Less than. Adjust span and try again.
|
||||
middle++;
|
||||
start += middle;
|
||||
span -= middle;
|
||||
} else {
|
||||
// Found element.
|
||||
return table;
|
||||
}
|
||||
}
|
||||
return nullptr; // Key not found.
|
||||
}
|
||||
|
||||
protected:
|
||||
// This class is only used to access pre-existing data. Don't ever
|
||||
// try to construct these manually.
|
||||
@@ -289,8 +327,27 @@ protected:
|
||||
uoffset_t length_;
|
||||
};
|
||||
|
||||
// Convenient helper function to get the length of any vector, regardless
|
||||
// of wether it is null or not (the field is not set).
|
||||
template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
|
||||
return v ? v->Length() : 0;
|
||||
}
|
||||
|
||||
struct String : public Vector<char> {
|
||||
const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
|
||||
|
||||
bool operator <(const String &o) const {
|
||||
return strcmp(c_str(), o.c_str()) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Simple indirection for buffer allocation, to allow this to be overridden
|
||||
// with custom allocation (see the FlatBufferBuilder constructor).
|
||||
class simple_allocator {
|
||||
public:
|
||||
virtual ~simple_allocator() {}
|
||||
virtual uint8_t *allocate(size_t size) const { return new uint8_t[size]; }
|
||||
virtual void deallocate(uint8_t *p) const { delete[] p; }
|
||||
};
|
||||
|
||||
// This is a minimal replication of std::vector<uint8_t> functionality,
|
||||
@@ -298,30 +355,56 @@ struct String : public Vector<char> {
|
||||
// in the lowest address in the vector.
|
||||
class vector_downward {
|
||||
public:
|
||||
explicit vector_downward(size_t initial_size)
|
||||
explicit vector_downward(size_t initial_size,
|
||||
const simple_allocator &allocator)
|
||||
: reserved_(initial_size),
|
||||
buf_(new uint8_t[reserved_]),
|
||||
cur_(buf_ + reserved_) {
|
||||
buf_(allocator.allocate(reserved_)),
|
||||
cur_(buf_ + reserved_),
|
||||
allocator_(allocator) {
|
||||
assert((initial_size & (sizeof(largest_scalar_t) - 1)) == 0);
|
||||
}
|
||||
|
||||
~vector_downward() { delete[] buf_; }
|
||||
~vector_downward() {
|
||||
if (buf_)
|
||||
allocator_.deallocate(buf_);
|
||||
}
|
||||
|
||||
void clear() { cur_ = buf_ + reserved_; }
|
||||
void clear() {
|
||||
if (buf_ == nullptr)
|
||||
buf_ = allocator_.allocate(reserved_);
|
||||
|
||||
cur_ = buf_ + reserved_;
|
||||
}
|
||||
|
||||
// Relinquish the pointer to the caller.
|
||||
unique_ptr_t release() {
|
||||
// Actually deallocate from the start of the allocated memory.
|
||||
std::function<void(uint8_t *)> deleter(
|
||||
std::bind(&simple_allocator::deallocate, allocator_, buf_));
|
||||
|
||||
// Point to the desired offset.
|
||||
unique_ptr_t retval(data(), deleter);
|
||||
|
||||
// Don't deallocate when this instance is destroyed.
|
||||
buf_ = nullptr;
|
||||
cur_ = nullptr;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
size_t growth_policy(size_t bytes) {
|
||||
return (bytes / 2) & ~(sizeof(largest_scalar_t) - 1);
|
||||
}
|
||||
|
||||
uint8_t *make_space(size_t len) {
|
||||
if (buf_ > cur_ - len) {
|
||||
if (len > static_cast<size_t>(cur_ - buf_)) {
|
||||
auto old_size = size();
|
||||
reserved_ += std::max(len, growth_policy(reserved_));
|
||||
auto new_buf = new uint8_t[reserved_];
|
||||
auto new_buf = allocator_.allocate(reserved_);
|
||||
auto new_cur = new_buf + reserved_ - old_size;
|
||||
memcpy(new_cur, cur_, old_size);
|
||||
cur_ = new_cur;
|
||||
delete[] buf_;
|
||||
allocator_.deallocate(buf_);
|
||||
buf_ = new_buf;
|
||||
}
|
||||
cur_ -= len;
|
||||
@@ -332,10 +415,14 @@ class vector_downward {
|
||||
}
|
||||
|
||||
uoffset_t size() const {
|
||||
assert(cur_ != nullptr && buf_ != nullptr);
|
||||
return static_cast<uoffset_t>(reserved_ - (cur_ - buf_));
|
||||
}
|
||||
|
||||
uint8_t *data() const { return cur_; }
|
||||
uint8_t *data() const {
|
||||
assert(cur_ != nullptr);
|
||||
return cur_;
|
||||
}
|
||||
|
||||
uint8_t *data_at(size_t offset) { return buf_ + reserved_ - offset; }
|
||||
|
||||
@@ -361,6 +448,7 @@ class vector_downward {
|
||||
size_t reserved_;
|
||||
uint8_t *buf_;
|
||||
uint8_t *cur_; // Points at location between empty (below) and used (above).
|
||||
const simple_allocator &allocator_;
|
||||
};
|
||||
|
||||
// Converts a Field ID to a virtual table offset.
|
||||
@@ -383,10 +471,12 @@ inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
|
||||
// AddElement/EndTable, or the builtin CreateString/CreateVector functions.
|
||||
// Do this is depth-first order to build up a tree to the root.
|
||||
// Finish() wraps up the buffer ready for transport.
|
||||
class FlatBufferBuilder {
|
||||
class FlatBufferBuilder FLATBUFFERS_FINAL_CLASS {
|
||||
public:
|
||||
explicit FlatBufferBuilder(uoffset_t initial_size = 1024)
|
||||
: buf_(initial_size), minalign_(1), force_defaults_(false) {
|
||||
explicit FlatBufferBuilder(uoffset_t initial_size = 1024,
|
||||
const simple_allocator *allocator = nullptr)
|
||||
: buf_(initial_size, allocator ? *allocator : default_allocator),
|
||||
minalign_(1), force_defaults_(false) {
|
||||
offsetbuf_.reserve(16); // Avoid first few reallocs.
|
||||
vtables_.reserve(16);
|
||||
EndianCheck();
|
||||
@@ -398,6 +488,7 @@ class FlatBufferBuilder {
|
||||
buf_.clear();
|
||||
offsetbuf_.clear();
|
||||
vtables_.clear();
|
||||
minalign_ = 1;
|
||||
}
|
||||
|
||||
// The current size of the serialized buffer, counting from the end.
|
||||
@@ -406,6 +497,10 @@ class FlatBufferBuilder {
|
||||
// Get the serialized buffer (after you call Finish()).
|
||||
uint8_t *GetBufferPointer() const { return buf_.data(); }
|
||||
|
||||
// Get the released pointer to the serialized buffer.
|
||||
// Don't attempt to use this FlatBufferBuilder afterwards!
|
||||
unique_ptr_t ReleaseBufferPointer() { return buf_.release(); }
|
||||
|
||||
void ForceDefaults(bool fd) { force_defaults_ = fd; }
|
||||
|
||||
void Pad(size_t num_bytes) { buf_.fill(num_bytes); }
|
||||
@@ -502,7 +597,7 @@ class FlatBufferBuilder {
|
||||
uoffset_t EndTable(uoffset_t start, voffset_t numfields) {
|
||||
// Write the vtable offset, which is the start of any Table.
|
||||
// We fill it's value later.
|
||||
auto vtableoffsetloc = PushElement<uoffset_t>(0);
|
||||
auto vtableoffsetloc = PushElement<soffset_t>(0);
|
||||
// Write a vtable, which consists entirely of voffset_t elements.
|
||||
// It starts with the number of offsets, followed by a type id, followed
|
||||
// by the offsets themselves. In reverse:
|
||||
@@ -522,12 +617,14 @@ class FlatBufferBuilder {
|
||||
}
|
||||
offsetbuf_.clear();
|
||||
auto vt1 = reinterpret_cast<voffset_t *>(buf_.data());
|
||||
auto vt1_size = *vt1;
|
||||
auto vt1_size = ReadScalar<voffset_t>(vt1);
|
||||
auto vt_use = GetSize();
|
||||
// See if we already have generated a vtable with this exact same
|
||||
// layout before. If so, make it point to the old one, remove this one.
|
||||
for (auto it = vtables_.begin(); it != vtables_.end(); ++it) {
|
||||
if (memcmp(buf_.data_at(*it), vt1, vt1_size)) continue;
|
||||
auto vt2 = reinterpret_cast<voffset_t *>(buf_.data_at(*it));
|
||||
auto vt2_size = *vt2;
|
||||
if (vt1_size != vt2_size || memcmp(vt2, vt1, vt1_size)) continue;
|
||||
vt_use = *it;
|
||||
buf_.pop(GetSize() - vtableoffsetloc);
|
||||
break;
|
||||
@@ -551,7 +648,7 @@ class FlatBufferBuilder {
|
||||
// just been constructed.
|
||||
template<typename T> void Required(Offset<T> table, voffset_t field) {
|
||||
auto table_ptr = buf_.data_at(table.o);
|
||||
auto vtable_ptr = table_ptr - ReadScalar<uoffset_t>(table_ptr);
|
||||
auto vtable_ptr = table_ptr - ReadScalar<soffset_t>(table_ptr);
|
||||
bool ok = ReadScalar<voffset_t>(vtable_ptr + field) != 0;
|
||||
// If this fails, the caller will show what field needs to be set.
|
||||
assert(ok);
|
||||
@@ -617,12 +714,12 @@ class FlatBufferBuilder {
|
||||
return Offset<Vector<T>>(EndVector(len));
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<T>> CreateVector(const std::vector<T> &v){
|
||||
template<typename T> Offset<Vector<T>> CreateVector(const std::vector<T> &v) {
|
||||
return CreateVector(v.data(), v.size());
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||
const T *v, size_t len) {
|
||||
const T *v, size_t len) {
|
||||
NotNested();
|
||||
StartVector(len * sizeof(T) / AlignOf<T>(), AlignOf<T>());
|
||||
PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len);
|
||||
@@ -630,10 +727,43 @@ class FlatBufferBuilder {
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||
const std::vector<T> &v) {
|
||||
const std::vector<T> &v) {
|
||||
return CreateVectorOfStructs(v.data(), v.size());
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(
|
||||
Offset<T> *v, size_t len) {
|
||||
std::sort(v, v + len,
|
||||
[this](const Offset<T> &a, const Offset<T> &b) -> bool {
|
||||
auto table_a = reinterpret_cast<T *>(buf_.data_at(a.o));
|
||||
auto table_b = reinterpret_cast<T *>(buf_.data_at(b.o));
|
||||
return table_a->KeyCompareLessThan(table_b);
|
||||
}
|
||||
);
|
||||
return CreateVector(v, len);
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(
|
||||
std::vector<T> *v) {
|
||||
return CreateVectorOfSortedTables(v->data(), v->size());
|
||||
}
|
||||
|
||||
// Specialized version for non-copying use cases. Write the data any time
|
||||
// later to the returned buffer pointer `buf`.
|
||||
uoffset_t CreateUninitializedVector(size_t len, size_t elemsize,
|
||||
uint8_t **buf) {
|
||||
NotNested();
|
||||
StartVector(len, elemsize);
|
||||
*buf = buf_.make_space(len * elemsize);
|
||||
return EndVector(len);
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<T>> CreateUninitializedVector(
|
||||
size_t len, T **buf) {
|
||||
return CreateUninitializedVector(len, sizeof(T),
|
||||
reinterpret_cast<uint8_t **>(buf));
|
||||
}
|
||||
|
||||
static const size_t kFileIdentifierLength = 4;
|
||||
|
||||
// Finish serializing a buffer by writing the root offset.
|
||||
@@ -662,6 +792,8 @@ class FlatBufferBuilder {
|
||||
voffset_t id;
|
||||
};
|
||||
|
||||
simple_allocator default_allocator;
|
||||
|
||||
vector_downward buf_;
|
||||
|
||||
// Accumulating offsets of table members while it is being built.
|
||||
@@ -688,7 +820,7 @@ inline bool BufferHasIdentifier(const void *buf, const char *identifier) {
|
||||
}
|
||||
|
||||
// Helper class to verify the integrity of a FlatBuffer
|
||||
class Verifier {
|
||||
class Verifier FLATBUFFERS_FINAL_CLASS {
|
||||
public:
|
||||
Verifier(const uint8_t *buf, size_t buf_len, size_t _max_depth = 64,
|
||||
size_t _max_tables = 1000000)
|
||||
@@ -806,7 +938,7 @@ class Verifier {
|
||||
// always have all members present and do not support forwards/backwards
|
||||
// compatible extensions.
|
||||
|
||||
class Struct {
|
||||
class Struct FLATBUFFERS_FINAL_CLASS {
|
||||
public:
|
||||
template<typename T> T GetField(uoffset_t o) const {
|
||||
return ReadScalar<T>(&data_[o]);
|
||||
|
||||
105
include/flatbuffers/hash.h
Normal file
105
include/flatbuffers/hash.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FLATBUFFERS_HASH_H_
|
||||
#define FLATBUFFERS_HASH_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
template <typename T>
|
||||
struct FnvTraits {
|
||||
static const T kFnvPrime;
|
||||
static const T kOffsetBasis;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct FnvTraits<uint32_t> {
|
||||
static const uint32_t kFnvPrime = 0x01000193;
|
||||
static const uint32_t kOffsetBasis = 0x811C9DC5;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct FnvTraits<uint64_t> {
|
||||
static const uint64_t kFnvPrime = 0x00000100000001b3;
|
||||
static const uint64_t kOffsetBasis = 0xcbf29ce484222645;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
T HashFnv1(const char *input) {
|
||||
T hash = FnvTraits<T>::kOffsetBasis;
|
||||
for (const char *c = input; *c; ++c) {
|
||||
hash *= FnvTraits<T>::kFnvPrime;
|
||||
hash ^= static_cast<unsigned char>(*c);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T HashFnv1a(const char *input) {
|
||||
T hash = FnvTraits<T>::kOffsetBasis;
|
||||
for (const char *c = input; *c; ++c) {
|
||||
hash ^= static_cast<unsigned char>(*c);
|
||||
hash *= FnvTraits<T>::kFnvPrime;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct NamedHashFunction {
|
||||
const char *name;
|
||||
|
||||
typedef T (*HashFunction)(const char*);
|
||||
HashFunction function;
|
||||
};
|
||||
|
||||
const NamedHashFunction<uint32_t> kHashFunctions32[] = {
|
||||
{ "fnv1_32", HashFnv1<uint32_t> },
|
||||
{ "fnv1a_32", HashFnv1a<uint32_t> },
|
||||
};
|
||||
|
||||
const NamedHashFunction<uint64_t> kHashFunctions64[] = {
|
||||
{ "fnv1_64", HashFnv1<uint64_t> },
|
||||
{ "fnv1a_64", HashFnv1a<uint64_t> },
|
||||
};
|
||||
|
||||
inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
|
||||
const char *name) {
|
||||
std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
if (std::strcmp(name, kHashFunctions32[i].name) == 0) {
|
||||
return kHashFunctions32[i].function;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(
|
||||
const char *name) {
|
||||
std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
if (std::strcmp(name, kHashFunctions64[i].name) == 0) {
|
||||
return kHashFunctions64[i].function;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_HASH_H_
|
||||
@@ -18,10 +18,13 @@
|
||||
#define FLATBUFFERS_IDL_H_
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/hash.h"
|
||||
|
||||
// This file defines the data types representing a parsed IDL (Interface
|
||||
// Definition Language) / schema file.
|
||||
@@ -34,7 +37,7 @@ namespace flatbuffers {
|
||||
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
|
||||
TD(NONE, "", uint8_t, byte, byte, byte) \
|
||||
TD(UTYPE, "", uint8_t, byte, byte, byte) /* begin scalar/int */ \
|
||||
TD(BOOL, "bool", uint8_t, byte, byte, byte) \
|
||||
TD(BOOL, "bool", uint8_t, boolean,byte, bool) \
|
||||
TD(CHAR, "byte", int8_t, byte, int8, sbyte) \
|
||||
TD(UCHAR, "ubyte", uint8_t, byte, byte, byte) \
|
||||
TD(SHORT, "short", int16_t, short, int16, short) \
|
||||
@@ -178,6 +181,7 @@ struct Definition {
|
||||
Definition() : generated(false), defined_namespace(nullptr) {}
|
||||
|
||||
std::string name;
|
||||
std::string file;
|
||||
std::vector<std::string> doc_comment;
|
||||
SymbolTable<Value> attributes;
|
||||
bool generated; // did we already output code for this definition?
|
||||
@@ -185,11 +189,14 @@ struct Definition {
|
||||
};
|
||||
|
||||
struct FieldDef : public Definition {
|
||||
FieldDef() : deprecated(false), required(false), padding(0), used(false) {}
|
||||
FieldDef() : deprecated(false), required(false), key(false), padding(0),
|
||||
used(false) {}
|
||||
|
||||
Value value;
|
||||
bool deprecated;
|
||||
bool required;
|
||||
bool deprecated; // Field is allowed to be present in old data, but can't be
|
||||
// written in new data nor accessed in new code.
|
||||
bool required; // Field must always be present.
|
||||
bool key; // Field functions as a key for creating sorted vectors.
|
||||
size_t padding; // Bytes to always pad after this field.
|
||||
bool used; // Used during JSON parsing to check for repeated fields.
|
||||
};
|
||||
@@ -199,6 +206,7 @@ struct StructDef : public Definition {
|
||||
: fixed(false),
|
||||
predecl(true),
|
||||
sortbysize(true),
|
||||
has_key(false),
|
||||
minalign(1),
|
||||
bytesize(0)
|
||||
{}
|
||||
@@ -213,6 +221,7 @@ struct StructDef : public Definition {
|
||||
bool fixed; // If it's struct, not a table.
|
||||
bool predecl; // If it's used before it was defined.
|
||||
bool sortbysize; // Whether fields come in the declaration or size order.
|
||||
bool has_key; // It has a key field.
|
||||
size_t minalign; // What the whole object needs to be aligned to.
|
||||
size_t bytesize; // Size if fixed.
|
||||
};
|
||||
@@ -260,14 +269,24 @@ struct EnumDef : public Definition {
|
||||
|
||||
class Parser {
|
||||
public:
|
||||
Parser(bool proto_mode = false) :
|
||||
root_struct_def(nullptr),
|
||||
source_(nullptr),
|
||||
cursor_(nullptr),
|
||||
line_(1),
|
||||
proto_mode_(proto_mode) {
|
||||
// Just in case none are declared:
|
||||
namespaces_.push_back(new Namespace());
|
||||
Parser(bool strict_json = false, bool proto_mode = false)
|
||||
: root_struct_def(nullptr),
|
||||
source_(nullptr),
|
||||
cursor_(nullptr),
|
||||
line_(1),
|
||||
proto_mode_(proto_mode),
|
||||
strict_json_(strict_json) {
|
||||
// Just in case none are declared:
|
||||
namespaces_.push_back(new Namespace());
|
||||
known_attributes_.insert("deprecated");
|
||||
known_attributes_.insert("required");
|
||||
known_attributes_.insert("key");
|
||||
known_attributes_.insert("hash");
|
||||
known_attributes_.insert("id");
|
||||
known_attributes_.insert("force_align");
|
||||
known_attributes_.insert("bit_flags");
|
||||
known_attributes_.insert("original_order");
|
||||
known_attributes_.insert("nested_flatbuffer");
|
||||
}
|
||||
|
||||
~Parser() {
|
||||
@@ -294,6 +313,11 @@ class Parser {
|
||||
// Mark all definitions as already having code generated.
|
||||
void MarkGenerated();
|
||||
|
||||
// Get the files recursively included by the given file. The returned
|
||||
// container will have at least the given file.
|
||||
std::set<std::string> GetIncludedFilesRecursive(
|
||||
const std::string &file_name) const;
|
||||
|
||||
private:
|
||||
int64_t ParseHexNum(int nibbles);
|
||||
void Next();
|
||||
@@ -312,6 +336,7 @@ class Parser {
|
||||
uoffset_t ParseVector(const Type &type);
|
||||
void ParseMetaData(Definition &def);
|
||||
bool TryTypedValue(int dtoken, bool check, Value &e, BaseType req);
|
||||
void ParseHash(Value &e, FieldDef* field);
|
||||
void ParseSingleValue(Value &e);
|
||||
int64_t ParseIntegerFromString(Type &type);
|
||||
StructDef *LookupCreateStruct(const std::string &name);
|
||||
@@ -334,17 +359,22 @@ class Parser {
|
||||
std::string file_extension_;
|
||||
|
||||
std::map<std::string, bool> included_files_;
|
||||
std::map<std::string, std::set<std::string>> files_included_per_file_;
|
||||
|
||||
private:
|
||||
const char *source_, *cursor_;
|
||||
int line_; // the current line being parsed
|
||||
int token_;
|
||||
std::stack<std::string> files_being_parsed_;
|
||||
bool proto_mode_;
|
||||
bool strict_json_;
|
||||
std::string attribute_;
|
||||
std::vector<std::string> doc_comment_;
|
||||
|
||||
std::vector<std::pair<Value, FieldDef *>> field_stack_;
|
||||
std::vector<uint8_t> struct_stack_;
|
||||
|
||||
std::set<std::string> known_attributes_;
|
||||
};
|
||||
|
||||
// Utility functions for multiple generators:
|
||||
@@ -363,7 +393,7 @@ struct GeneratorOptions {
|
||||
bool include_dependence_headers;
|
||||
|
||||
// Possible options for the more general generator below.
|
||||
enum Language { kJava, kCSharp, kMAX };
|
||||
enum Language { kJava, kCSharp, kGo, kMAX };
|
||||
|
||||
Language lang;
|
||||
|
||||
@@ -383,6 +413,18 @@ extern void GenerateText(const Parser &parser,
|
||||
const void *flatbuffer,
|
||||
const GeneratorOptions &opts,
|
||||
std::string *text);
|
||||
extern bool GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// Generate binary files from a given FlatBuffer, and a given Parser
|
||||
// object that has been populated with the corresponding schema.
|
||||
// See idl_gen_general.cpp.
|
||||
extern bool GenerateBinary(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// Generate a C++ header from the definitions in the Parser object.
|
||||
// See idl_gen_cpp.
|
||||
@@ -432,6 +474,34 @@ extern bool GenerateFBS(const Parser &parser,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// Generate a make rule for the generated C++ header.
|
||||
// See idl_gen_cpp.cpp.
|
||||
extern std::string CPPMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// Generate a make rule for the generated Java/C#/... files.
|
||||
// See idl_gen_general.cpp.
|
||||
extern std::string GeneralMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// Generate a make rule for the generated text (JSON) files.
|
||||
// See idl_gen_text.cpp.
|
||||
extern std::string TextMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// Generate a make rule for the generated binary files.
|
||||
// See idl_gen_general.cpp.
|
||||
extern std::string BinaryMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_IDL_H_
|
||||
|
||||
@@ -80,6 +80,12 @@ inline int64_t StringToInt(const char *str, int base = 10) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Check if file "name" exists.
|
||||
inline bool FileExists(const char *name) {
|
||||
std::ifstream ifs(name);
|
||||
return ifs.good();
|
||||
}
|
||||
|
||||
// Load file "name" into "buf" returning true if successful
|
||||
// false otherwise. If "binary" is false data is read
|
||||
// using ifstream's text mode, otherwise data is read with
|
||||
@@ -234,6 +240,33 @@ inline int FromUTF8(const char **in) {
|
||||
return ucc;
|
||||
}
|
||||
|
||||
// Wraps a string to a maximum length, inserting new lines where necessary. Any
|
||||
// existing whitespace will be collapsed down to a single space. A prefix or
|
||||
// suffix can be provided, which will be inserted before or after a wrapped
|
||||
// line, respectively.
|
||||
inline std::string WordWrap(const std::string in, size_t max_length,
|
||||
const std::string wrapped_line_prefix,
|
||||
const std::string wrapped_line_suffix) {
|
||||
std::istringstream in_stream(in);
|
||||
std::string wrapped, line, word;
|
||||
|
||||
in_stream >> word;
|
||||
line = word;
|
||||
|
||||
while (in_stream >> word) {
|
||||
if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) <
|
||||
max_length) {
|
||||
line += " " + word;
|
||||
} else {
|
||||
wrapped += line + wrapped_line_suffix + "\n";
|
||||
line = wrapped_line_prefix + word;
|
||||
}
|
||||
}
|
||||
wrapped += line;
|
||||
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_UTIL_H_
|
||||
|
||||
@@ -22,33 +22,67 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
// Class that helps you build a FlatBuffer.
|
||||
// See the section "Use in Java" in the main FlatBuffers documentation.
|
||||
|
||||
/**
|
||||
* Class that helps you build a FlatBuffer. See the section
|
||||
* <a href="http://google.github.io/flatbuffers/md__java_usage.html">"Use in Java"</a> in the
|
||||
* main FlatBuffers documentation.
|
||||
*/
|
||||
public class FlatBufferBuilder {
|
||||
ByteBuffer bb; // Where we construct the FlatBuffer.
|
||||
int space; // Remaining space in the ByteBuffer.
|
||||
ByteBuffer bb; // Where we construct the FlatBuffer.
|
||||
int space; // Remaining space in the ByteBuffer.
|
||||
static final Charset utf8charset = Charset.forName("UTF-8");
|
||||
int minalign = 1; // Minimum alignment encountered so far.
|
||||
int[] vtable = null; // The vtable for the current table, null otherwise.
|
||||
int object_start; // Starting offset of the current struct/table.
|
||||
int minalign = 1; // Minimum alignment encountered so far.
|
||||
int[] vtable = null; // The vtable for the current table.
|
||||
int vtable_in_use = 0; // The amount of fields we're actually using.
|
||||
boolean nested = false; // Whether we are currently serializing a table.
|
||||
int object_start; // Starting offset of the current struct/table.
|
||||
int[] vtables = new int[16]; // List of offsets of all vtables.
|
||||
int num_vtables = 0; // Number of entries in `vtables` in use.
|
||||
int vector_num_elems = 0; // For the current vector being built.
|
||||
boolean force_defaults = false; // False omits default values from the serialized data
|
||||
|
||||
// Start with a buffer of size `initial_size`, then grow as required.
|
||||
/**
|
||||
* Start with a buffer of size {@code initial_size}, then grow as required.
|
||||
*
|
||||
* @param initial_size The initial size of the internal buffer to use
|
||||
*/
|
||||
public FlatBufferBuilder(int initial_size) {
|
||||
if (initial_size <= 0) initial_size = 1;
|
||||
space = initial_size;
|
||||
bb = newByteBuffer(initial_size);
|
||||
}
|
||||
|
||||
// Alternative constructor allowing reuse of ByteBuffers
|
||||
/**
|
||||
* Alternative constructor allowing reuse of {@link ByteBuffer}s. The builder
|
||||
* can still grow the buffer as necessary. User classes should make sure
|
||||
* to call {@link #dataBuffer()} to obtain the resulting encoded message
|
||||
*
|
||||
* @param existing_bb The byte buffer to reuse
|
||||
*/
|
||||
public FlatBufferBuilder(ByteBuffer existing_bb) {
|
||||
init(existing_bb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative initializer that allows reusing this object on an existing
|
||||
* ByteBuffer. This method resets the builder's internal state, but keeps
|
||||
* objects that have been allocated for temporary storage.
|
||||
*
|
||||
* @param existing_bb The byte buffer to reuse
|
||||
* @return this
|
||||
*/
|
||||
public FlatBufferBuilder init(ByteBuffer existing_bb){
|
||||
bb = existing_bb;
|
||||
bb.clear();
|
||||
bb.order(ByteOrder.LITTLE_ENDIAN);
|
||||
minalign = 1;
|
||||
space = bb.capacity();
|
||||
vtable_in_use = 0;
|
||||
nested = false;
|
||||
object_start = 0;
|
||||
num_vtables = 0;
|
||||
vector_num_elems = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
static ByteBuffer newByteBuffer(int capacity) {
|
||||
@@ -57,8 +91,14 @@ public class FlatBufferBuilder {
|
||||
return newbb;
|
||||
}
|
||||
|
||||
// Doubles the size of the ByteBuffer, and copies the old data towards the
|
||||
// end of the new buffer (since we build the buffer backwards).
|
||||
/**
|
||||
* Doubles the size of the backing {link ByteBuffer} and copies the old data towards the
|
||||
* end of the new buffer (since we build the buffer backwards).
|
||||
*
|
||||
* @param bb The current buffer with the existing data
|
||||
* @return A new byte buffer with the old data copied copied to it. The data is
|
||||
* located at the end of the buffer.
|
||||
*/
|
||||
static ByteBuffer growByteBuffer(ByteBuffer bb) {
|
||||
int old_buf_size = bb.capacity();
|
||||
if ((old_buf_size & 0xC0000000) != 0) // Ensure we don't grow beyond what fits in an int.
|
||||
@@ -71,20 +111,34 @@ public class FlatBufferBuilder {
|
||||
return nbb;
|
||||
}
|
||||
|
||||
// Offset relative to the end of the buffer.
|
||||
/**
|
||||
* Offset relative to the end of the buffer.
|
||||
*
|
||||
* @return Offset relative to the end of the buffer.
|
||||
*/
|
||||
public int offset() {
|
||||
return bb.capacity() - space;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add zero valued bytes to prepare a new entry to be added
|
||||
*
|
||||
* @param byte_size Number of bytes to add.
|
||||
*/
|
||||
public void pad(int byte_size) {
|
||||
for (int i = 0; i < byte_size; i++) bb.put(--space, (byte)0);
|
||||
}
|
||||
|
||||
// Prepare to write an element of `size` after `additional_bytes`
|
||||
// have been written, e.g. if you write a string, you need to align such
|
||||
// the int length field is aligned to SIZEOF_INT, and the string data follows it
|
||||
// directly.
|
||||
// If all you need to do is align, `additional_bytes` will be 0.
|
||||
/**
|
||||
* Prepare to write an element of {@code size} after {@code additional_bytes}
|
||||
* have been written, e.g. if you write a string, you need to align such
|
||||
* the int length field is aligned to {@link com.google.flatbuffers.Constants#SIZEOF_INT}, and
|
||||
* the string data follows it directly. If all you need to do is alignment, {@code additional_bytes}
|
||||
* will be 0.
|
||||
*
|
||||
* @param size This is the of the new element to write
|
||||
* @param additional_bytes The padding size
|
||||
*/
|
||||
public void prep(int size, int additional_bytes) {
|
||||
// Track the biggest thing we've ever aligned to.
|
||||
if (size > minalign) minalign = size;
|
||||
@@ -102,23 +156,29 @@ public class FlatBufferBuilder {
|
||||
|
||||
// Add a scalar to the buffer, backwards from the current location.
|
||||
// Doesn't align nor check for space.
|
||||
public void putByte (byte x) { bb.put (space -= 1, x); }
|
||||
public void putShort (short x) { bb.putShort (space -= 2, x); }
|
||||
public void putInt (int x) { bb.putInt (space -= 4, x); }
|
||||
public void putLong (long x) { bb.putLong (space -= 8, x); }
|
||||
public void putFloat (float x) { bb.putFloat (space -= 4, x); }
|
||||
public void putDouble(double x) { bb.putDouble(space -= 8, x); }
|
||||
public void putBoolean(boolean x) { bb.put (space -= 1, (byte)(x ? 1 : 0)); }
|
||||
public void putByte (byte x) { bb.put (space -= 1, x); }
|
||||
public void putShort (short x) { bb.putShort (space -= 2, x); }
|
||||
public void putInt (int x) { bb.putInt (space -= 4, x); }
|
||||
public void putLong (long x) { bb.putLong (space -= 8, x); }
|
||||
public void putFloat (float x) { bb.putFloat (space -= 4, x); }
|
||||
public void putDouble (double x) { bb.putDouble(space -= 8, x); }
|
||||
|
||||
// Adds a scalar to the buffer, properly aligned, and the buffer grown
|
||||
// if needed.
|
||||
public void addByte (byte x) { prep(1, 0); putByte (x); }
|
||||
public void addShort (short x) { prep(2, 0); putShort (x); }
|
||||
public void addInt (int x) { prep(4, 0); putInt (x); }
|
||||
public void addLong (long x) { prep(8, 0); putLong (x); }
|
||||
public void addFloat (float x) { prep(4, 0); putFloat (x); }
|
||||
public void addDouble(double x) { prep(8, 0); putDouble(x); }
|
||||
public void addBoolean(boolean x) { prep(1, 0); putBoolean(x); }
|
||||
public void addByte (byte x) { prep(1, 0); putByte (x); }
|
||||
public void addShort (short x) { prep(2, 0); putShort (x); }
|
||||
public void addInt (int x) { prep(4, 0); putInt (x); }
|
||||
public void addLong (long x) { prep(8, 0); putLong (x); }
|
||||
public void addFloat (float x) { prep(4, 0); putFloat (x); }
|
||||
public void addDouble (double x) { prep(8, 0); putDouble (x); }
|
||||
|
||||
// Adds on offset, relative to where it will be written.
|
||||
/**
|
||||
* Adds on offset, relative to where it will be written.
|
||||
*
|
||||
* @param off The offset to add
|
||||
*/
|
||||
public void addOffset(int off) {
|
||||
prep(SIZEOF_INT, 0); // Ensure alignment is already done.
|
||||
assert off <= offset();
|
||||
@@ -126,6 +186,48 @@ public class FlatBufferBuilder {
|
||||
putInt(off);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a new array/vector of objects. Users usually will not call
|
||||
* this directly. The {@code FlatBuffers} compiler will create a start/end
|
||||
* method for vector types in generated code.
|
||||
* <p>
|
||||
* The expected sequence of calls is:
|
||||
* <ol>
|
||||
* <li>Start the array using this method.</li>
|
||||
* <li>Call {@link #addOffset(int)} {@code num_elems} number of times to set
|
||||
* the offset of each element in the array.</li>
|
||||
* <li>Call {@link #endVector()} to retrieve the offset of the array.</li>
|
||||
* </ol>
|
||||
* <p>
|
||||
* For example, to create an array of strings, do:
|
||||
* <pre>{@code
|
||||
* // Need 10 strings
|
||||
* FlatBufferBuilder builder = new FlatBufferBuilder(existingBuffer);
|
||||
* int[] offsets = new int[10];
|
||||
*
|
||||
* for (int i = 0; i < 10; i++) {
|
||||
* offsets[i] = fbb.createString(" " + i);
|
||||
* }
|
||||
*
|
||||
* // Have the strings in the buffer, but don't have a vector.
|
||||
* // Add a vector that references the newly created strings:
|
||||
* builder.startVector(4, offsets.length, 4);
|
||||
*
|
||||
* // Add each string to the newly created vector
|
||||
* // The strings are added in reverse order since the buffer
|
||||
* // is filled in back to front
|
||||
* for (int i = offsets.length - 1; i >= 0; i--) {
|
||||
* builder.addOffset(offsets[i]);
|
||||
* }
|
||||
*
|
||||
* // Finish off the vector
|
||||
* int offsetOfTheVector = fbb.endVector();
|
||||
* }</pre>
|
||||
*
|
||||
* @param elem_size The size of each element in the array
|
||||
* @param num_elems The number of elements in the array
|
||||
* @param alignment The alignment of the array
|
||||
*/
|
||||
public void startVector(int elem_size, int num_elems, int alignment) {
|
||||
notNested();
|
||||
vector_num_elems = num_elems;
|
||||
@@ -133,11 +235,24 @@ public class FlatBufferBuilder {
|
||||
prep(alignment, elem_size * num_elems); // Just in case alignment > int.
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish off the creation of an array and all its elements. The array
|
||||
* must be created with {@link #startVector(int, int, int)}.
|
||||
*
|
||||
* @return The offset at which the newly created array starts.
|
||||
* @see #startVector(int, int, int)
|
||||
*/
|
||||
public int endVector() {
|
||||
putInt(vector_num_elems);
|
||||
return offset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the string {@code s} in the buffer using UTF-8.
|
||||
*
|
||||
* @param s The string to encode
|
||||
* @return The offset in the buffer where the encoded string starts
|
||||
*/
|
||||
public int createString(String s) {
|
||||
byte[] utf8 = s.getBytes(utf8charset);
|
||||
addByte((byte)0);
|
||||
@@ -147,35 +262,86 @@ public class FlatBufferBuilder {
|
||||
return endVector();
|
||||
}
|
||||
|
||||
/**
|
||||
* Should not be creating any other object, string or vector
|
||||
* while an object is being constructed
|
||||
*/
|
||||
public void notNested() {
|
||||
// You should not be creating any other objects or strings/vectors
|
||||
// while an object is being constructed
|
||||
if (vtable != null)
|
||||
if (nested)
|
||||
throw new AssertionError("FlatBuffers: object serialization must not be nested.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Structures are always stored inline, they need to be created right
|
||||
* where they're used. You'll get this assertion failure if you
|
||||
* created it elsewhere.
|
||||
*
|
||||
* @param obj The offset of the created object
|
||||
*/
|
||||
public void Nested(int obj) {
|
||||
// Structs are always stored inline, so need to be created right
|
||||
// where they are used. You'll get this assert if you created it
|
||||
// elsewhere.
|
||||
if (obj != offset())
|
||||
throw new AssertionError("FlatBuffers: struct must be serialized inline.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Start encoding a new object in the buffer. Users will not usually need to
|
||||
* call this directly. The {@code FlatBuffers} compiler will generate helper methods
|
||||
* that call this method internally.
|
||||
* <p>
|
||||
* For example, using the "Monster" code found on the
|
||||
* <a href="http://google.github.io/flatbuffers/md__java_usage.html">landing page</a>. An
|
||||
* object of type {@code Monster} can be created using the following code:
|
||||
*
|
||||
* <pre>{@code
|
||||
* int testArrayOfString = Monster.createTestarrayofstringVector(fbb, new int[] {
|
||||
* fbb.createString("test1"),
|
||||
* fbb.createString("test2")
|
||||
* });
|
||||
*
|
||||
* Monster.startMonster(fbb);
|
||||
* Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
|
||||
* Color.Green, (short)5, (byte)6));
|
||||
* Monster.addHp(fbb, (short)80);
|
||||
* Monster.addName(fbb, str);
|
||||
* Monster.addInventory(fbb, inv);
|
||||
* Monster.addTestType(fbb, (byte)Any.Monster);
|
||||
* Monster.addTest(fbb, mon2);
|
||||
* Monster.addTest4(fbb, test4);
|
||||
* Monster.addTestarrayofstring(fbb, testArrayOfString);
|
||||
* int mon = Monster.endMonster(fbb);
|
||||
* }</pre>
|
||||
* <p>
|
||||
* Here:
|
||||
* <ul>
|
||||
* <li>The call to {@code Monster#startMonster(FlatBufferBuilder)} will call this
|
||||
* method with the right number of fields set.</li>
|
||||
* <li>{@code Monster#endMonster(FlatBufferBuilder)} will ensure {@link #endObject()} is called.</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* It's not recommended to call this method directly. If it's called manually, you must ensure
|
||||
* to audit all calls to it whenever fields are added or removed from your schema. This is
|
||||
* automatically done by the code generated by the {@code FlatBuffers} compiler.
|
||||
*
|
||||
* @param numfields The number of fields found in this object.
|
||||
*/
|
||||
public void startObject(int numfields) {
|
||||
notNested();
|
||||
vtable = new int[numfields];
|
||||
if (vtable == null || vtable.length < numfields) vtable = new int[numfields];
|
||||
vtable_in_use = numfields;
|
||||
Arrays.fill(vtable, 0, vtable_in_use, 0);
|
||||
nested = true;
|
||||
object_start = offset();
|
||||
}
|
||||
|
||||
// Add a scalar to a table at `o` into its vtable, with value `x` and default `d`
|
||||
public void addByte (int o, byte x, int d) { if(x != d) { addByte (x); slot(o); } }
|
||||
public void addShort (int o, short x, int d) { if(x != d) { addShort (x); slot(o); } }
|
||||
public void addInt (int o, int x, int d) { if(x != d) { addInt (x); slot(o); } }
|
||||
public void addLong (int o, long x, long d) { if(x != d) { addLong (x); slot(o); } }
|
||||
public void addFloat (int o, float x, double d) { if(x != d) { addFloat (x); slot(o); } }
|
||||
public void addDouble(int o, double x, double d) { if(x != d) { addDouble(x); slot(o); } }
|
||||
public void addOffset(int o, int x, int d) { if(x != d) { addOffset(x); slot(o); } }
|
||||
public void addBoolean(int o, boolean x, boolean d) { if(force_defaults || x != d) { addBoolean(x); slot(o); } }
|
||||
public void addByte (int o, byte x, int d) { if(force_defaults || x != d) { addByte (x); slot(o); } }
|
||||
public void addShort (int o, short x, int d) { if(force_defaults || x != d) { addShort (x); slot(o); } }
|
||||
public void addInt (int o, int x, int d) { if(force_defaults || x != d) { addInt (x); slot(o); } }
|
||||
public void addLong (int o, long x, long d) { if(force_defaults || x != d) { addLong (x); slot(o); } }
|
||||
public void addFloat (int o, float x, double d) { if(force_defaults || x != d) { addFloat (x); slot(o); } }
|
||||
public void addDouble (int o, double x, double d) { if(force_defaults || x != d) { addDouble (x); slot(o); } }
|
||||
public void addOffset (int o, int x, int d) { if(force_defaults || x != d) { addOffset (x); slot(o); } }
|
||||
|
||||
// Structs are stored inline, so nothing additional is being added. `d` is always 0.
|
||||
public void addStruct(int voffset, int x, int d) {
|
||||
@@ -190,12 +356,19 @@ public class FlatBufferBuilder {
|
||||
vtable[voffset] = offset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish off writing the object that is under construction.
|
||||
*
|
||||
* @return The offset to the object inside {@link #dataBuffer()}
|
||||
* @see #startObject(int)
|
||||
*/
|
||||
public int endObject() {
|
||||
assert vtable != null; // calling endObject without a startObject
|
||||
if (vtable == null || !nested)
|
||||
throw new AssertionError("FlatBuffers: endObject called without startObject");
|
||||
addInt(0);
|
||||
int vtableloc = offset();
|
||||
// Write out the current vtable.
|
||||
for (int i = vtable.length - 1; i >= 0 ; i--) {
|
||||
for (int i = vtable_in_use - 1; i >= 0 ; i--) {
|
||||
// Offset relative to the start of the table.
|
||||
short off = (short)(vtable[i] != 0 ? vtableloc - vtable[i] : 0);
|
||||
addShort(off);
|
||||
@@ -203,7 +376,7 @@ public class FlatBufferBuilder {
|
||||
|
||||
final int standard_fields = 2; // The fields below:
|
||||
addShort((short)(vtableloc - object_start));
|
||||
addShort((short)((vtable.length + standard_fields) * SIZEOF_SHORT));
|
||||
addShort((short)((vtable_in_use + standard_fields) * SIZEOF_SHORT));
|
||||
|
||||
// Search for an existing vtable that matches the current one.
|
||||
int existing_vtable = 0;
|
||||
@@ -238,7 +411,7 @@ public class FlatBufferBuilder {
|
||||
bb.putInt(bb.capacity() - vtableloc, offset() - vtableloc);
|
||||
}
|
||||
|
||||
vtable = null;
|
||||
nested = false;
|
||||
return vtableloc;
|
||||
}
|
||||
|
||||
@@ -270,20 +443,46 @@ public class FlatBufferBuilder {
|
||||
finish(root_table);
|
||||
}
|
||||
|
||||
/**
|
||||
* In order to save space, fields that are set to their default value
|
||||
* don't get serialized into the buffer. Forcing defaults provides a
|
||||
* way to manually disable this optimization.
|
||||
*
|
||||
* @param forceDefaults true always serializes default values
|
||||
* @return this
|
||||
*/
|
||||
public FlatBufferBuilder forceDefaults(boolean forceDefaults){
|
||||
this.force_defaults = forceDefaults;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Get the ByteBuffer representing the FlatBuffer. Only call this after you've
|
||||
// called finish(). The actual data starts at the ByteBuffer's current position,
|
||||
// not necessarily at 0.
|
||||
public ByteBuffer dataBuffer() { return bb; }
|
||||
|
||||
// The FlatBuffer data doesn't start at offset 0 in the ByteBuffer,
|
||||
// but now the ByteBuffer's position is set to that location upon
|
||||
// finish(). This method should not be needed anymore, but is left
|
||||
// here as private for the moment to document this API change.
|
||||
// It will be removed in the future.
|
||||
/**
|
||||
* The FlatBuffer data doesn't start at offset 0 in the {@link ByteBuffer}, but
|
||||
* now the {@code ByteBuffer}'s position is set to that location upon {@link #finish(int)}.
|
||||
*
|
||||
* @return The {@link ByteBuffer#position() position} the data starts in {@link #dataBuffer()}
|
||||
* @deprecated This method should not be needed anymore, but is left
|
||||
* here for the moment to document this API change. It will be removed in the future.
|
||||
*/
|
||||
@Deprecated
|
||||
private int dataStart() {
|
||||
return space;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function for copying a byte array from {@code start} to
|
||||
* {@code start} + {@code length}
|
||||
*
|
||||
* @param start Start copying at this offset
|
||||
* @param length How many bytes to copy
|
||||
* @return A range copy of the {@link #dataBuffer() data buffer}
|
||||
* @throws IndexOutOfBoundsException If the range of bytes is ouf of bound
|
||||
*/
|
||||
public byte[] sizedByteArray(int start, int length){
|
||||
byte[] array = new byte[length];
|
||||
bb.position(start);
|
||||
@@ -291,7 +490,11 @@ public class FlatBufferBuilder {
|
||||
return array;
|
||||
}
|
||||
|
||||
// Utility function for copying a byte array that starts at 0.
|
||||
/**
|
||||
* Utility function for copying a byte array that starts at 0.
|
||||
*
|
||||
* @return A full copy of the {@link #dataBuffer() data buffer}
|
||||
*/
|
||||
public byte[] sizedByteArray() {
|
||||
return sizedByteArray(space, bb.capacity() - space);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
// All tables in the generated code derive from this class, and add their own accessors.
|
||||
public class Table {
|
||||
@@ -45,16 +45,16 @@ public class Table {
|
||||
protected String __string(int offset) {
|
||||
offset += bb.getInt(offset);
|
||||
if (bb.hasArray()) {
|
||||
return new String(bb.array(), offset + SIZEOF_INT, bb.getInt(offset), Charset.forName("UTF-8"));
|
||||
return new String(bb.array(), offset + SIZEOF_INT, bb.getInt(offset), FlatBufferBuilder.utf8charset);
|
||||
} else {
|
||||
// We can't access .array(), since the ByteBuffer is read-only.
|
||||
// We can't access .array(), since the ByteBuffer is read-only,
|
||||
// off-heap or a memory map
|
||||
ByteBuffer bb = this.bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);
|
||||
// We're forced to make an extra copy:
|
||||
byte[] copy = new byte[bb.getInt(offset)];
|
||||
int old_pos = bb.position();
|
||||
bb.position(offset + SIZEOF_INT);
|
||||
bb.get(copy);
|
||||
bb.position(old_pos);
|
||||
return new String(copy, 0, copy.length, Charset.forName("UTF-8"));
|
||||
return new String(copy, 0, copy.length, FlatBufferBuilder.utf8charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,12 +78,11 @@ public class Table {
|
||||
protected ByteBuffer __vector_as_bytebuffer(int vector_offset, int elem_size) {
|
||||
int o = __offset(vector_offset);
|
||||
if (o == 0) return null;
|
||||
int old_pos = bb.position();
|
||||
bb.position(__vector(o));
|
||||
ByteBuffer nbb = bb.slice();
|
||||
bb.position(old_pos);
|
||||
nbb.limit(__vector_len(o) * elem_size);
|
||||
return nbb;
|
||||
ByteBuffer bb = this.bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);
|
||||
int vectorstart = __vector(o);
|
||||
bb.position(vectorstart);
|
||||
bb.limit(vectorstart + __vector_len(o) * elem_size);
|
||||
return bb;
|
||||
}
|
||||
|
||||
// Initialize any Table-derived type to point to the union at the given offset.
|
||||
|
||||
84
java/pom.xml
Normal file
84
java/pom.xml
Normal file
@@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.google.flatbuffers</groupId>
|
||||
<artifactId>flatbuffers-java</artifactId>
|
||||
<version>0.0.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>FlatBuffers Java API</name>
|
||||
<description>
|
||||
Memory Efficient Serialization Library
|
||||
</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<url>https://github.com/google/flatbuffers</url>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache License V2.0</name>
|
||||
<url>https://raw.githubusercontent.com/google/flatbuffers/master/LICENSE.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
<scm>
|
||||
<url>https://github.com/google/flatbuffers</url>
|
||||
<connection>
|
||||
scm:git:https://github.com/google/flatbuffers.git
|
||||
</connection>
|
||||
</scm>
|
||||
<dependencies>
|
||||
</dependencies>
|
||||
<build>
|
||||
<sourceDirectory>./</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
<version>3.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Test.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
<version>2.18.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.9.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
318
net/FlatBuffers/ByteBuffer.cs
Normal file → Executable file
318
net/FlatBuffers/ByteBuffer.cs
Normal file → Executable file
@@ -14,13 +14,18 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//#define UNSAFE_BYTEBUFFER // uncomment this line to use faster ByteBuffer
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace FlatBuffers
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to mimick Java's ByteBuffer which is used heavily in Flatbuffers
|
||||
/// Class to mimic Java's ByteBuffer which is used heavily in Flatbuffers.
|
||||
/// If your execution environment allows unsafe code, you should enable
|
||||
/// unsafe code in your project and #define UNSAFE_BYTEBUFFER to use a
|
||||
/// MUCH faster version of ByteBuffer.
|
||||
/// </summary>
|
||||
public class ByteBuffer
|
||||
{
|
||||
@@ -39,25 +44,79 @@ namespace FlatBuffers
|
||||
|
||||
public int position() { return _pos; }
|
||||
|
||||
protected void WriteLittleEndian(int offset, byte[] data)
|
||||
// Pre-allocated helper arrays for convertion.
|
||||
private float[] floathelper = new[] { 0.0f };
|
||||
private int[] inthelper = new[] { 0 };
|
||||
private double[] doublehelper = new[] { 0.0 };
|
||||
private ulong[] ulonghelper = new[] { 0UL };
|
||||
|
||||
// Helper functions for the unsafe version.
|
||||
static public ushort ReverseBytes(ushort input)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
return (ushort)(((input & 0x00FFU) << 8) |
|
||||
((input & 0xFF00U) >> 8));
|
||||
}
|
||||
static public uint ReverseBytes(uint input)
|
||||
{
|
||||
return ((input & 0x000000FFU) << 24) |
|
||||
((input & 0x0000FF00U) << 8) |
|
||||
((input & 0x00FF0000U) >> 8) |
|
||||
((input & 0xFF000000U) >> 24);
|
||||
}
|
||||
static public ulong ReverseBytes(ulong input)
|
||||
{
|
||||
return (((input & 0x00000000000000FFUL) << 56) |
|
||||
((input & 0x000000000000FF00UL) << 40) |
|
||||
((input & 0x0000000000FF0000UL) << 24) |
|
||||
((input & 0x00000000FF000000UL) << 8) |
|
||||
((input & 0x000000FF00000000UL) >> 8) |
|
||||
((input & 0x0000FF0000000000UL) >> 24) |
|
||||
((input & 0x00FF000000000000UL) >> 40) |
|
||||
((input & 0xFF00000000000000UL) >> 56));
|
||||
}
|
||||
|
||||
#if !UNSAFE_BYTEBUFFER
|
||||
// Helper functions for the safe (but slower) version.
|
||||
protected void WriteLittleEndian(int offset, int count, ulong data)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
data = data.Reverse().ToArray();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
_buffer[offset + i] = (byte)(data >> i * 8);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
_buffer[offset + count - 1 - i] = (byte)(data >> i * 8);
|
||||
}
|
||||
}
|
||||
Buffer.BlockCopy(data, 0, _buffer, offset, data.Length);
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
protected byte[] ReadLittleEndian(int offset, int count)
|
||||
protected ulong ReadLittleEndian(int offset, int count)
|
||||
{
|
||||
AssertOffsetAndLength(offset, count);
|
||||
var tmp = new byte[count];
|
||||
Buffer.BlockCopy(_buffer, offset, tmp, 0, count);
|
||||
return (BitConverter.IsLittleEndian)
|
||||
? tmp
|
||||
: tmp.Reverse().ToArray();
|
||||
ulong r = 0;
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
r |= (ulong)_buffer[offset + i] << i * 8;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
r |= (ulong)_buffer[offset + count - 1 - i] << i * 8;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
#endif // !UNSAFE_BYTEBUFFER
|
||||
|
||||
private void AssertOffsetAndLength(int offset, int length)
|
||||
{
|
||||
@@ -81,54 +140,150 @@ namespace FlatBuffers
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
#if UNSAFE_BYTEBUFFER
|
||||
// Unsafe but more efficient versions of Put*.
|
||||
public void PutShort(int offset, short value)
|
||||
{
|
||||
PutUshort(offset, (ushort)value);
|
||||
}
|
||||
|
||||
public unsafe void PutUshort(int offset, ushort value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ushort));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
*(ushort*)(ptr + offset) = BitConverter.IsLittleEndian
|
||||
? value
|
||||
: ReverseBytes(value);
|
||||
}
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
public void PutInt(int offset, int value)
|
||||
{
|
||||
PutUint(offset, (uint)value);
|
||||
}
|
||||
|
||||
public unsafe void PutUint(int offset, uint value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(uint));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
*(uint*)(ptr + offset) = BitConverter.IsLittleEndian
|
||||
? value
|
||||
: ReverseBytes(value);
|
||||
}
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
public unsafe void PutLong(int offset, long value)
|
||||
{
|
||||
PutUlong(offset, (ulong)value);
|
||||
}
|
||||
|
||||
public unsafe void PutUlong(int offset, ulong value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ulong));
|
||||
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
*(ulong*)(ptr + offset) = BitConverter.IsLittleEndian
|
||||
? value
|
||||
: ReverseBytes(value);
|
||||
}
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
public unsafe void PutFloat(int offset, float value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(float));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
*(float*)(ptr + offset) = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
*(uint*)(ptr + offset) = ReverseBytes(*(uint*)(&value));
|
||||
}
|
||||
}
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
public unsafe void PutDouble(int offset, double value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(double));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
*(double*)(ptr + offset) = value;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
*(ulong*)(ptr + offset) = ReverseBytes(*(ulong*)(ptr + offset));
|
||||
}
|
||||
}
|
||||
_pos = offset;
|
||||
}
|
||||
#else // !UNSAFE_BYTEBUFFER
|
||||
// Slower versions of Put* for when unsafe code is not allowed.
|
||||
public void PutShort(int offset, short value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(short));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
WriteLittleEndian(offset, sizeof(short), (ulong)value);
|
||||
}
|
||||
|
||||
public void PutUshort(int offset, ushort value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ushort));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
WriteLittleEndian(offset, sizeof(ushort), (ulong)value);
|
||||
}
|
||||
|
||||
public void PutInt(int offset, int value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(int));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
WriteLittleEndian(offset, sizeof(int), (ulong)value);
|
||||
}
|
||||
|
||||
public void PutUint(int offset, uint value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(uint));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
WriteLittleEndian(offset, sizeof(uint), (ulong)value);
|
||||
}
|
||||
|
||||
public void PutLong(int offset, long value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(long));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
WriteLittleEndian(offset, sizeof(long), (ulong)value);
|
||||
}
|
||||
|
||||
public void PutUlong(int offset, ulong value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ulong));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
WriteLittleEndian(offset, sizeof(ulong), value);
|
||||
}
|
||||
|
||||
public void PutFloat(int offset, float value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(float));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
floathelper[0] = value;
|
||||
Buffer.BlockCopy(floathelper, 0, inthelper, 0, sizeof(float));
|
||||
WriteLittleEndian(offset, sizeof(float), (ulong)inthelper[0]);
|
||||
}
|
||||
|
||||
public void PutDouble(int offset, double value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(double));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
doublehelper[0] = value;
|
||||
Buffer.BlockCopy(doublehelper, 0, ulonghelper, 0, sizeof(double));
|
||||
WriteLittleEndian(offset, sizeof(double), ulonghelper[0]);
|
||||
}
|
||||
|
||||
#endif // UNSAFE_BYTEBUFFER
|
||||
|
||||
public sbyte GetSbyte(int index)
|
||||
{
|
||||
AssertOffsetAndLength(index, sizeof(sbyte));
|
||||
@@ -141,60 +296,137 @@ namespace FlatBuffers
|
||||
return _buffer[index];
|
||||
}
|
||||
|
||||
#if UNSAFE_BYTEBUFFER
|
||||
// Unsafe but more efficient versions of Get*.
|
||||
public short GetShort(int offset)
|
||||
{
|
||||
return (short)GetUshort(offset);
|
||||
}
|
||||
|
||||
public unsafe ushort GetUshort(int offset)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ushort));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
return BitConverter.IsLittleEndian
|
||||
? *(ushort*)(ptr + offset)
|
||||
: ReverseBytes(*(ushort*)(ptr + offset));
|
||||
}
|
||||
}
|
||||
|
||||
public int GetInt(int offset)
|
||||
{
|
||||
return (int)GetUint(offset);
|
||||
}
|
||||
|
||||
public unsafe uint GetUint(int offset)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(uint));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
return BitConverter.IsLittleEndian
|
||||
? *(uint*)(ptr + offset)
|
||||
: ReverseBytes(*(uint*)(ptr + offset));
|
||||
}
|
||||
}
|
||||
|
||||
public long GetLong(int offset)
|
||||
{
|
||||
return (long)GetUlong(offset);
|
||||
}
|
||||
|
||||
public unsafe ulong GetUlong(int offset)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ulong));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
return BitConverter.IsLittleEndian
|
||||
? *(ulong*)(ptr + offset)
|
||||
: ReverseBytes(*(ulong*)(ptr + offset));
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe float GetFloat(int offset)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(float));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
return *(float*)(ptr + offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint uvalue = ReverseBytes(*(uint*)(ptr + offset));
|
||||
return *(float*)(&uvalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe double GetDouble(int offset)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(double));
|
||||
fixed (byte* ptr = _buffer)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
return *(double*)(ptr + offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong uvalue = ReverseBytes(*(ulong*)(ptr + offset));
|
||||
return *(double*)(&uvalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
#else // !UNSAFE_BYTEBUFFER
|
||||
// Slower versions of Get* for when unsafe code is not allowed.
|
||||
public short GetShort(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(short));
|
||||
var value = BitConverter.ToInt16(tmp, 0);
|
||||
return value;
|
||||
return (short)ReadLittleEndian(index, sizeof(short));
|
||||
}
|
||||
|
||||
public ushort GetUshort(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(ushort));
|
||||
var value = BitConverter.ToUInt16(tmp, 0);
|
||||
return value;
|
||||
return (ushort)ReadLittleEndian(index, sizeof(ushort));
|
||||
}
|
||||
|
||||
public int GetInt(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(int));
|
||||
var value = BitConverter.ToInt32(tmp, 0);
|
||||
return value;
|
||||
return (int)ReadLittleEndian(index, sizeof(int));
|
||||
}
|
||||
|
||||
public uint GetUint(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(uint));
|
||||
var value = BitConverter.ToUInt32(tmp, 0);
|
||||
return value;
|
||||
return (uint)ReadLittleEndian(index, sizeof(uint));
|
||||
}
|
||||
|
||||
public long GetLong(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(long));
|
||||
var value = BitConverter.ToInt64(tmp, 0);
|
||||
return value;
|
||||
return (long)ReadLittleEndian(index, sizeof(long));
|
||||
}
|
||||
|
||||
public ulong GetUlong(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(ulong));
|
||||
var value = BitConverter.ToUInt64(tmp, 0);
|
||||
return value;
|
||||
return ReadLittleEndian(index, sizeof(ulong));
|
||||
}
|
||||
|
||||
public float GetFloat(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(float));
|
||||
var value = BitConverter.ToSingle(tmp, 0);
|
||||
return value;
|
||||
int i = (int)ReadLittleEndian(index, sizeof(float));
|
||||
inthelper[0] = i;
|
||||
Buffer.BlockCopy(inthelper, 0, floathelper, 0, sizeof(float));
|
||||
return floathelper[0];
|
||||
}
|
||||
|
||||
public double GetDouble(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(double));
|
||||
var value = BitConverter.ToDouble(tmp, 0);
|
||||
return value;
|
||||
ulong i = ReadLittleEndian(index, sizeof(double));
|
||||
// There's Int64BitsToDouble but it uses unsafe code internally.
|
||||
ulonghelper[0] = i;
|
||||
Buffer.BlockCopy(ulonghelper, 0, doublehelper, 0, sizeof(double));
|
||||
return doublehelper[0];
|
||||
}
|
||||
#endif // UNSAFE_BYTEBUFFER
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +105,11 @@ namespace FlatBuffers
|
||||
Pad(alignSize);
|
||||
}
|
||||
|
||||
public void PutBool(bool x)
|
||||
{
|
||||
_bb.PutByte(_space -= sizeof(byte), (byte)(x ? 1 : 0));
|
||||
}
|
||||
|
||||
public void PutSbyte(sbyte x)
|
||||
{
|
||||
_bb.PutSbyte(_space -= sizeof(sbyte), x);
|
||||
@@ -157,6 +162,7 @@ namespace FlatBuffers
|
||||
|
||||
// Adds a scalar to the buffer, properly aligned, and the buffer grown
|
||||
// if needed.
|
||||
public void AddBool(bool x) { Prep(sizeof(byte), 0); PutBool(x); }
|
||||
public void AddSbyte(sbyte x) { Prep(sizeof(sbyte), 0); PutSbyte(x); }
|
||||
public void AddByte(byte x) { Prep(sizeof(byte), 0); PutByte(x); }
|
||||
public void AddShort(short x) { Prep(sizeof(short), 0); PutShort(x); }
|
||||
@@ -164,7 +170,7 @@ namespace FlatBuffers
|
||||
public void AddInt(int x) { Prep(sizeof(int), 0); PutInt(x); }
|
||||
public void AddUint(uint x) { Prep(sizeof(uint), 0); PutUint(x); }
|
||||
public void AddLong(long x) { Prep(sizeof(long), 0); PutLong(x); }
|
||||
public void AddULong(ulong x) { Prep(sizeof(ulong), 0); PutUlong(x); }
|
||||
public void AddUlong(ulong x) { Prep(sizeof(ulong), 0); PutUlong(x); }
|
||||
public void AddFloat(float x) { Prep(sizeof(float), 0); PutFloat(x); }
|
||||
public void AddDouble(double x) { Prep(sizeof(double), 0);
|
||||
PutDouble(x); }
|
||||
@@ -231,6 +237,7 @@ namespace FlatBuffers
|
||||
}
|
||||
|
||||
// Add a scalar to a table at `o` into its vtable, with value `x` and default `d`
|
||||
public void AddBool(int o, bool x, bool d) { if (x != d) { AddBool(x); Slot(o); } }
|
||||
public void AddSbyte(int o, sbyte x, sbyte d) { if (x != d) { AddSbyte(x); Slot(o); } }
|
||||
public void AddByte(int o, byte x, byte d) { if (x != d) { AddByte(x); Slot(o); } }
|
||||
public void AddShort(int o, short x, int d) { if (x != d) { AddShort(x); Slot(o); } }
|
||||
@@ -238,7 +245,7 @@ namespace FlatBuffers
|
||||
public void AddInt(int o, int x, int d) { if (x != d) { AddInt(x); Slot(o); } }
|
||||
public void AddUint(int o, uint x, uint d) { if (x != d) { AddUint(x); Slot(o); } }
|
||||
public void AddLong(int o, long x, long d) { if (x != d) { AddLong(x); Slot(o); } }
|
||||
public void AddULong(int o, ulong x, ulong d) { if (x != d) { AddULong(x); Slot(o); } }
|
||||
public void AddUlong(int o, ulong x, ulong d) { if (x != d) { AddUlong(x); Slot(o); } }
|
||||
public void AddFloat(int o, float x, double d) { if (x != d) { AddFloat(x); Slot(o); } }
|
||||
public void AddDouble(int o, double x, double d) { if (x != d) { AddDouble(x); Slot(o); } }
|
||||
public void AddOffset(int o, int x, int d) { if (x != d) { AddOffset(x); Slot(o); } }
|
||||
@@ -359,9 +366,9 @@ namespace FlatBuffers
|
||||
// Utility function for copying a byte array that starts at 0.
|
||||
public byte[] SizedByteArray()
|
||||
{
|
||||
var newArray = new byte[_bb.Data.Length];
|
||||
var newArray = new byte[_bb.Data.Length - _bb.position()];
|
||||
Buffer.BlockCopy(_bb.Data, _bb.position(), newArray, 0,
|
||||
_bb.Data.Length);
|
||||
_bb.Data.Length - _bb.position());
|
||||
return newArray;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
using System;
|
||||
/*
|
||||
* Copyright 2014 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.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
@@ -41,9 +41,6 @@
|
||||
<Compile Include="Struct.cs" />
|
||||
<Compile Include="Table.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="FlatBuffers.1.0.0.nuspec" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
using System.Reflection;
|
||||
/*
|
||||
* Copyright 2014 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.
|
||||
*/
|
||||
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
@@ -10,7 +26,7 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("FlatBuffers")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2014 Google Inc")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2015 Google Inc")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace FlatBuffers
|
||||
protected int __offset(int vtableOffset)
|
||||
{
|
||||
int vtable = bb_pos - bb.GetInt(bb_pos);
|
||||
return vtableOffset < bb.GetShort(vtable) ? bb.GetShort(vtable + vtableOffset) : 0;
|
||||
return vtableOffset < bb.GetShort(vtable) ? (int)bb.GetShort(vtable + vtableOffset) : 0;
|
||||
}
|
||||
|
||||
// Retrieve the relative offset stored at "offset"
|
||||
|
||||
56
samples/android/jni/Android.mk
Executable file
56
samples/android/jni/Android.mk
Executable file
@@ -0,0 +1,56 @@
|
||||
# Copyright (c) 2013 Google, Inc.
|
||||
#
|
||||
# This software is provided 'as-is', without any express or implied
|
||||
# warranty. In no event will the authors be held liable for any damages
|
||||
# arising from the use of this software.
|
||||
# Permission is granted to anyone to use this software for any purpose,
|
||||
# including commercial applications, and to alter it and redistribute it
|
||||
# freely, subject to the following restrictions:
|
||||
# 1. The origin of this software must not be misrepresented; you must not
|
||||
# claim that you wrote the original software. If you use this software
|
||||
# in a product, an acknowledgment in the product documentation would be
|
||||
# appreciated but is not required.
|
||||
# 2. Altered source versions must be plainly marked as such, and must not be
|
||||
# misrepresented as being the original software.
|
||||
# 3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
FLATBUFFERS_ROOT_DIR := $(LOCAL_PATH)/../../..
|
||||
|
||||
# FlatBuffers test
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
# Include the FlatBuffer utility function to generate header files from schemas.
|
||||
include $(FLATBUFFERS_ROOT_DIR)/android/jni/include.mk
|
||||
|
||||
LOCAL_MODULE := sample_android_project
|
||||
|
||||
# Set up some useful variables to identify schema and output directories and
|
||||
# schema files.
|
||||
ANDROID_SAMPLE_GENERATED_OUTPUT_DIR := $(LOCAL_PATH)/gen/include
|
||||
ANDROID_SAMPLE_SCHEMA_DIR := $(LOCAL_PATH)/schemas
|
||||
ANDROID_SAMPLE_SCHEMA_FILES := $(ANDROID_SAMPLE_SCHEMA_DIR)/animal.fbs
|
||||
|
||||
LOCAL_C_INCLUDES := $(ANDROID_SAMPLE_GENERATED_OUTPUT_DIR)
|
||||
|
||||
$(info $(LOCAL_C_INCLUDES))
|
||||
|
||||
LOCAL_SRC_FILES := main.cpp
|
||||
|
||||
LOCAL_CPPFLAGS := -std=c++11 -fexceptions -Wall -Wno-literal-suffix
|
||||
LOCAL_LDLIBS := -llog -landroid
|
||||
LOCAL_ARM_MODE := arm
|
||||
LOCAL_STATIC_LIBRARIES := android_native_app_glue flatbuffers
|
||||
|
||||
ifeq (,$(ANDROID_SAMPLE_RUN_ONCE))
|
||||
ANDROID_SAMPLE_RUN_ONCE := 1
|
||||
$(call flatbuffers_header_build_rules,$(ANDROID_SAMPLE_SCHEMA_FILES),$(ANDROID_SAMPLE_SCHEMA_DIR),$(ANDROID_SAMPLE_GENERATED_OUTPUT_DIR),,$(LOCAL_SRC_FILES))
|
||||
endif
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
# Path to Flatbuffers root directory.
|
||||
$(call import-add-path,$(FLATBUFFERS_ROOT_DIR)/..)
|
||||
|
||||
$(call import-module,flatbuffers/android/jni)
|
||||
$(call import-module,android/native_app_glue)
|
||||
22
samples/android/jni/Application.mk
Executable file
22
samples/android/jni/Application.mk
Executable file
@@ -0,0 +1,22 @@
|
||||
# Copyright (c) 2014 Google, Inc.
|
||||
#
|
||||
# This software is provided 'as-is', without any express or implied
|
||||
# warranty. In no event will the authors be held liable for any damages
|
||||
# arising from the use of this software.
|
||||
# Permission is granted to anyone to use this software for any purpose,
|
||||
# including commercial applications, and to alter it and redistribute it
|
||||
# freely, subject to the following restrictions:
|
||||
# 1. The origin of this software must not be misrepresented; you must not
|
||||
# claim that you wrote the original software. If you use this software
|
||||
# in a product, an acknowledgment in the product documentation would be
|
||||
# appreciated but is not required.
|
||||
# 2. Altered source versions must be plainly marked as such, and must not be
|
||||
# misrepresented as being the original software.
|
||||
# 3. This notice may not be removed or altered from any source distribution.
|
||||
APP_PLATFORM := android-10
|
||||
APP_PROJECT_PATH := $(call my-dir)/..
|
||||
APP_STL := gnustl_static
|
||||
|
||||
APP_ABI := armeabi-v7a
|
||||
NDK_TOOLCHAIN_VERSION := 4.8
|
||||
APP_CPPFLAGS += -std=c++11
|
||||
28
samples/android/jni/main.cpp
Normal file
28
samples/android/jni/main.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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 "android_native_app_glue.h"
|
||||
#include "animal_generated.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
void android_main(android_app *app) {
|
||||
app_dummy();
|
||||
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
auto name = builder.CreateString("Dog");
|
||||
auto sound = builder.CreateString("Bark");
|
||||
auto animal_buffer = sample::CreateAnimal(builder, name, sound);
|
||||
builder.Finish(animal_buffer);
|
||||
}
|
||||
|
||||
22
samples/android/jni/schemas/animal.fbs
Normal file
22
samples/android/jni/schemas/animal.fbs
Normal file
@@ -0,0 +1,22 @@
|
||||
// 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.
|
||||
|
||||
namespace sample;
|
||||
|
||||
table Animal {
|
||||
name:string;
|
||||
sound:string;
|
||||
}
|
||||
|
||||
root_type Animal;
|
||||
@@ -37,9 +37,9 @@ inline const char **EnumNamesAny() {
|
||||
|
||||
inline const char *EnumNameAny(Any e) { return EnumNamesAny()[e]; }
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type);
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type);
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(4) Vec3 {
|
||||
MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
|
||||
private:
|
||||
float x_;
|
||||
float y_;
|
||||
@@ -55,7 +55,7 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 {
|
||||
};
|
||||
STRUCT_END(Vec3, 12);
|
||||
|
||||
struct Monster : private flatbuffers::Table {
|
||||
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
const Vec3 *pos() const { return GetStruct<const Vec3 *>(4); }
|
||||
int16_t mana() const { return GetField<int16_t>(6, 150); }
|
||||
int16_t hp() const { return GetField<int16_t>(8, 100); }
|
||||
@@ -110,7 +110,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type) {
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type) {
|
||||
switch (type) {
|
||||
case Any_NONE: return true;
|
||||
case Any_Monster: return verifier.VerifyTable(reinterpret_cast<const Monster *>(union_obj));
|
||||
|
||||
119
src/flatc.cpp
119
src/flatc.cpp
@@ -21,38 +21,6 @@
|
||||
static void Error(const char *err, const char *obj = nullptr,
|
||||
bool usage = false, bool show_exe_name = true);
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
bool GenerateBinary(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin";
|
||||
return !parser.builder_.GetSize() ||
|
||||
flatbuffers::SaveFile(
|
||||
(path + file_name + "." + ext).c_str(),
|
||||
reinterpret_cast<char *>(parser.builder_.GetBufferPointer()),
|
||||
parser.builder_.GetSize(),
|
||||
true);
|
||||
}
|
||||
|
||||
bool GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts) {
|
||||
if (!parser.builder_.GetSize()) return true;
|
||||
if (!parser.root_struct_def) Error("root_type not set");
|
||||
std::string text;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), opts,
|
||||
&text);
|
||||
return flatbuffers::SaveFile((path + file_name + ".json").c_str(),
|
||||
text,
|
||||
false);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// This struct allows us to create a table of all possible output generators
|
||||
// for the various programming languages and formats we support.
|
||||
struct Generator {
|
||||
@@ -60,31 +28,42 @@ struct Generator {
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const flatbuffers::GeneratorOptions &opts);
|
||||
const char *opt;
|
||||
const char *name;
|
||||
const char *generator_opt;
|
||||
const char *lang_name;
|
||||
flatbuffers::GeneratorOptions::Language lang;
|
||||
const char *help;
|
||||
const char *generator_help;
|
||||
|
||||
std::string (*make_rule)(const flatbuffers::Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const flatbuffers::GeneratorOptions &opts);
|
||||
};
|
||||
|
||||
const Generator generators[] = {
|
||||
{ flatbuffers::GenerateBinary, "-b", "binary",
|
||||
flatbuffers::GeneratorOptions::kMAX,
|
||||
"Generate wire format binaries for any data definitions" },
|
||||
"Generate wire format binaries for any data definitions",
|
||||
flatbuffers::BinaryMakeRule },
|
||||
{ flatbuffers::GenerateTextFile, "-t", "text",
|
||||
flatbuffers::GeneratorOptions::kMAX,
|
||||
"Generate text output for any data definitions" },
|
||||
"Generate text output for any data definitions",
|
||||
flatbuffers::TextMakeRule },
|
||||
{ flatbuffers::GenerateCPP, "-c", "C++",
|
||||
flatbuffers::GeneratorOptions::kMAX,
|
||||
"Generate C++ headers for tables/structs" },
|
||||
"Generate C++ headers for tables/structs",
|
||||
flatbuffers::CPPMakeRule },
|
||||
{ flatbuffers::GenerateGo, "-g", "Go",
|
||||
flatbuffers::GeneratorOptions::kMAX,
|
||||
"Generate Go files for tables/structs" },
|
||||
flatbuffers::GeneratorOptions::kGo,
|
||||
"Generate Go files for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GenerateGeneral, "-j", "Java",
|
||||
flatbuffers::GeneratorOptions::kJava,
|
||||
"Generate Java classes for tables/structs" },
|
||||
"Generate Java classes for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GenerateGeneral, "-n", "C#",
|
||||
flatbuffers::GeneratorOptions::kCSharp,
|
||||
"Generate C# classes for tables/structs" }
|
||||
"Generate C# classes for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
};
|
||||
|
||||
const char *program_name = NULL;
|
||||
@@ -98,18 +77,22 @@ static void Error(const char *err, const char *obj, bool usage,
|
||||
if (usage) {
|
||||
printf("usage: %s [OPTION]... FILE... [-- FILE...]\n", program_name);
|
||||
for (size_t i = 0; i < sizeof(generators) / sizeof(generators[0]); ++i)
|
||||
printf(" %s %s.\n", generators[i].opt, generators[i].help);
|
||||
printf(" %s %s.\n",
|
||||
generators[i].generator_opt,
|
||||
generators[i].generator_help);
|
||||
printf(
|
||||
" -o PATH Prefix PATH to all generated files.\n"
|
||||
" -I PATH Search for includes in the specified path.\n"
|
||||
" --strict-json Strict JSON: add quotes to field names.\n"
|
||||
" -M Print make rules for generated files.\n"
|
||||
" --strict-json Strict JSON: field names must be / will be quoted,\n"
|
||||
" no trailing commas in tables/vectors.\n"
|
||||
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
|
||||
" --gen-includes Generate include statements for included schemas the\n"
|
||||
" generated file depends on (C++).\n"
|
||||
" --proto Input is a .proto, translate to .fbs.\n"
|
||||
"FILEs may depend on declarations in earlier files.\n"
|
||||
"FILEs after the -- must be binary flatbuffer format files.\n"
|
||||
"Output files are named using the base file name of the input,"
|
||||
"Output files are named using the base file name of the input,\n"
|
||||
"and written to the current directory or the path given by -o.\n"
|
||||
"example: %s -c -b schema1.fbs schema2.fbs data.json\n",
|
||||
program_name);
|
||||
@@ -124,22 +107,23 @@ int main(int argc, const char *argv[]) {
|
||||
const size_t num_generators = sizeof(generators) / sizeof(generators[0]);
|
||||
bool generator_enabled[num_generators] = { false };
|
||||
bool any_generator = false;
|
||||
bool print_make_rules = false;
|
||||
bool proto_mode = false;
|
||||
std::vector<std::string> filenames;
|
||||
std::vector<const char *> include_directories;
|
||||
size_t binary_files_from = std::numeric_limits<size_t>::max();
|
||||
for (int i = 1; i < argc; i++) {
|
||||
const char *arg = argv[i];
|
||||
for (int argi = 1; argi < argc; argi++) {
|
||||
const char *arg = argv[argi];
|
||||
if (arg[0] == '-') {
|
||||
if (filenames.size() && arg[1] != '-')
|
||||
Error("invalid option location", arg, true);
|
||||
std::string opt = arg;
|
||||
if (opt == "-o") {
|
||||
if (++i >= argc) Error("missing path following", arg, true);
|
||||
output_path = flatbuffers::ConCatPathFileName(argv[i], "");
|
||||
if (++argi >= argc) Error("missing path following", arg, true);
|
||||
output_path = flatbuffers::ConCatPathFileName(argv[argi], "");
|
||||
} else if(opt == "-I") {
|
||||
if (++i >= argc) Error("missing path following", arg, true);
|
||||
include_directories.push_back(argv[i]);
|
||||
if (++argi >= argc) Error("missing path following", arg, true);
|
||||
include_directories.push_back(argv[argi]);
|
||||
} else if(opt == "--strict-json") {
|
||||
opts.strict_json = true;
|
||||
} else if(opt == "--no-prefix") {
|
||||
@@ -151,9 +135,11 @@ int main(int argc, const char *argv[]) {
|
||||
} else if(opt == "--proto") {
|
||||
proto_mode = true;
|
||||
any_generator = true;
|
||||
} else if(opt == "-M") {
|
||||
print_make_rules = true;
|
||||
} else {
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
if(opt == generators[i].opt) {
|
||||
if (opt == generators[i].generator_opt) {
|
||||
generator_enabled[i] = true;
|
||||
any_generator = true;
|
||||
goto found;
|
||||
@@ -163,18 +149,17 @@ int main(int argc, const char *argv[]) {
|
||||
found:;
|
||||
}
|
||||
} else {
|
||||
filenames.push_back(argv[i]);
|
||||
filenames.push_back(argv[argi]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!filenames.size()) Error("missing input files", nullptr, true);
|
||||
|
||||
if (!any_generator)
|
||||
Error("no options: no output files generated.",
|
||||
"specify one of -c -g -j -t -b etc.", true);
|
||||
Error("no options", "specify one of -c -g -j -t -b etc.", true);
|
||||
|
||||
// Now process the files:
|
||||
flatbuffers::Parser parser(proto_mode);
|
||||
flatbuffers::Parser parser(opts.strict_json, proto_mode);
|
||||
for (auto file_it = filenames.begin();
|
||||
file_it != filenames.end();
|
||||
++file_it) {
|
||||
@@ -204,14 +189,22 @@ int main(int argc, const char *argv[]) {
|
||||
flatbuffers::StripExtension(*file_it));
|
||||
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
opts.lang = generators[i].lang;
|
||||
if (generator_enabled[i]) {
|
||||
flatbuffers::EnsureDirExists(output_path);
|
||||
opts.lang = generators[i].lang;
|
||||
if (!generators[i].generate(parser, output_path, filebase, opts)) {
|
||||
Error((std::string("Unable to generate ") +
|
||||
generators[i].name +
|
||||
" for " +
|
||||
filebase).c_str());
|
||||
if (!print_make_rules) {
|
||||
flatbuffers::EnsureDirExists(output_path);
|
||||
if (!generators[i].generate(parser, output_path, filebase, opts)) {
|
||||
Error((std::string("Unable to generate ") +
|
||||
generators[i].lang_name +
|
||||
" for " +
|
||||
filebase).c_str());
|
||||
}
|
||||
} else {
|
||||
std::string make_rule = generators[i].make_rule(
|
||||
parser, output_path, *file_it, opts);
|
||||
if (!make_rule.empty())
|
||||
printf("%s\n", flatbuffers::WordWrap(
|
||||
make_rule, 80, " ", " \\").c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
102
src/flathash.cpp
Normal file
102
src/flathash.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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 <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "flatbuffers/hash.h"
|
||||
|
||||
enum OutputFormat {
|
||||
kDecimal,
|
||||
kHexadecimal,
|
||||
kHexadecimal0x,
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
const char* name = argv[0];
|
||||
if (argc <= 1) {
|
||||
printf("%s HASH [OPTION]... STRING... [-- STRING...]\n", name);
|
||||
printf("Available hashing algorithms:\n 32 bit:\n");
|
||||
size_t size = sizeof(flatbuffers::kHashFunctions32) /
|
||||
sizeof(flatbuffers::kHashFunctions32[0]);
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
printf(" * %s\n", flatbuffers::kHashFunctions32[i].name);
|
||||
}
|
||||
printf(" 64 bit:\n");
|
||||
size = sizeof(flatbuffers::kHashFunctions64) /
|
||||
sizeof(flatbuffers::kHashFunctions64[0]);
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
printf(" * %s\n", flatbuffers::kHashFunctions64[i].name);
|
||||
}
|
||||
printf(
|
||||
" -d Output hash in decimal.\n"
|
||||
" -x Output hash in hexadecimal.\n"
|
||||
" -0x Output hash in hexadecimal and prefix with 0x.\n"
|
||||
" -c Append the string to the output in a c-style comment.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* hash_algorithm = argv[1];
|
||||
|
||||
flatbuffers::NamedHashFunction<uint32_t>::HashFunction hash_function32 =
|
||||
flatbuffers::FindHashFunction32(hash_algorithm);
|
||||
flatbuffers::NamedHashFunction<uint64_t>::HashFunction hash_function64 =
|
||||
flatbuffers::FindHashFunction64(hash_algorithm);
|
||||
|
||||
if (!hash_function32 && !hash_function64) {
|
||||
printf("\"%s\" is not a known hash algorithm.\n", hash_algorithm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
OutputFormat output_format = kHexadecimal;
|
||||
bool annotate = false;
|
||||
bool escape_dash = false;
|
||||
for (int i = 2; i < argc; i++) {
|
||||
const char* arg = argv[i];
|
||||
if (!escape_dash && arg[0] == '-') {
|
||||
std::string opt = arg;
|
||||
if (opt == "-d") output_format = kDecimal;
|
||||
else if (opt == "-x") output_format = kHexadecimal;
|
||||
else if (opt == "-0x") output_format = kHexadecimal0x;
|
||||
else if (opt == "-c") annotate = true;
|
||||
else if (opt == "--") escape_dash = true;
|
||||
else printf("Unrecognized argument: \"%s\"\n", arg);
|
||||
} else {
|
||||
std::stringstream ss;
|
||||
if (output_format == kDecimal) {
|
||||
ss << std::dec;
|
||||
} else if (output_format == kHexadecimal) {
|
||||
ss << std::hex;
|
||||
} else if (output_format == kHexadecimal0x) {
|
||||
ss << std::hex;
|
||||
ss << "0x";
|
||||
}
|
||||
if (hash_function32)
|
||||
ss << hash_function32(arg);
|
||||
else if (hash_function64)
|
||||
ss << hash_function64(arg);
|
||||
|
||||
if (annotate)
|
||||
ss << " /* \"" << arg << "\" */";
|
||||
|
||||
ss << "\n";
|
||||
|
||||
std::cout << ss.str();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,12 @@ static std::string WrapInNameSpace(const Parser &parser, const Namespace *ns,
|
||||
}
|
||||
}
|
||||
|
||||
static std::string WrapInNameSpace(const Parser &parser,
|
||||
const Definition &def) {
|
||||
return WrapInNameSpace(parser, def.defined_namespace, def.name);
|
||||
}
|
||||
|
||||
|
||||
// Return a C++ type from the table in idl.h
|
||||
static std::string GenTypeBasic(const Parser &parser, const Type &type,
|
||||
bool real_enum) {
|
||||
@@ -48,8 +54,7 @@ static std::string GenTypeBasic(const Parser &parser, const Type &type,
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
return real_enum && type.enum_def
|
||||
? WrapInNameSpace(parser, type.enum_def->defined_namespace,
|
||||
type.enum_def->name)
|
||||
? WrapInNameSpace(parser, *type.enum_def)
|
||||
: ctypename[type.base_type];
|
||||
}
|
||||
|
||||
@@ -66,8 +71,7 @@ static std::string GenTypePointer(const Parser &parser, const Type &type) {
|
||||
return "flatbuffers::Vector<" +
|
||||
GenTypeWire(parser, type.VectorType(), "", false) + ">";
|
||||
case BASE_TYPE_STRUCT: {
|
||||
return WrapInNameSpace(parser, type.struct_def->defined_namespace,
|
||||
type.struct_def->name);
|
||||
return WrapInNameSpace(parser, *type.struct_def);
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
@@ -114,8 +118,8 @@ static std::string GenEnumVal(const EnumDef &enum_def, const EnumVal &enum_val,
|
||||
}
|
||||
|
||||
// Generate an enum declaration and an enum string lookup table.
|
||||
static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
|
||||
std::string *code_ptr_post,
|
||||
static void GenEnum(const Parser &parser, EnumDef &enum_def,
|
||||
std::string *code_ptr, std::string *code_ptr_post,
|
||||
const GeneratorOptions &opts) {
|
||||
if (enum_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
@@ -180,7 +184,8 @@ static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
|
||||
code_post += ": return true;\n"; // "NONE" enum value.
|
||||
} else {
|
||||
code_post += ": return verifier.VerifyTable(reinterpret_cast<const ";
|
||||
code_post += ev.struct_def->name + " *>(union_obj));\n";
|
||||
code_post += WrapInNameSpace(parser, *ev.struct_def);
|
||||
code_post += " *>(union_obj));\n";
|
||||
}
|
||||
}
|
||||
code_post += " default: return false;\n }\n}\n\n";
|
||||
@@ -208,7 +213,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
|
||||
// Generate an accessor struct, with methods of the form:
|
||||
// type name() const { return GetField<type>(offset, defaultval); }
|
||||
GenComment(struct_def.doc_comment, code_ptr);
|
||||
code += "struct " + struct_def.name + " : private flatbuffers::Table";
|
||||
code += "struct " + struct_def.name;
|
||||
code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table";
|
||||
code += " {\n";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
@@ -236,9 +242,27 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
|
||||
auto nested_root = parser.structs_.Lookup(nested->constant);
|
||||
assert(nested_root); // Guaranteed to exist by parser.
|
||||
code += " const " + nested_root->name + " *" + field.name;
|
||||
code += "_nested_root() { return flatbuffers::GetRoot<";
|
||||
code += "_nested_root() const { return flatbuffers::GetRoot<";
|
||||
code += nested_root->name + ">(" + field.name + "()->Data()); }\n";
|
||||
}
|
||||
// Generate a comparison function for this field if it is a key.
|
||||
if (field.key) {
|
||||
code += " bool KeyCompareLessThan(const " + struct_def.name;
|
||||
code += " *o) const { return ";
|
||||
if (field.value.type.base_type == BASE_TYPE_STRING) code += "*";
|
||||
code += field.name + "() < ";
|
||||
if (field.value.type.base_type == BASE_TYPE_STRING) code += "*";
|
||||
code += "o->" + field.name + "(); }\n";
|
||||
code += " int KeyCompareWithValue(";
|
||||
if (field.value.type.base_type == BASE_TYPE_STRING) {
|
||||
code += "const char *val) const { return strcmp(" + field.name;
|
||||
code += "()->c_str(), val); }\n";
|
||||
} else {
|
||||
code += GenTypeBasic(parser, field.value.type, false);
|
||||
code += " val) const { return " + field.name + "() < val ? -1 : ";
|
||||
code += field.name + "() > val; }\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Generate a verifier function that can check a buffer from an untrusted
|
||||
@@ -414,7 +438,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
|
||||
// platforms.
|
||||
GenComment(struct_def.doc_comment, code_ptr);
|
||||
code += "MANUALLY_ALIGNED_STRUCT(" + NumToString(struct_def.minalign) + ") ";
|
||||
code += struct_def.name + " {\n private:\n";
|
||||
code += struct_def.name + " FLATBUFFERS_FINAL_CLASS {\n private:\n";
|
||||
int padding_id = 0;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
@@ -516,7 +540,7 @@ std::string GenerateCPP(const Parser &parser,
|
||||
std::string enum_code, enum_code_post;
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
GenEnum(**it, &enum_code, &enum_code_post, opts);
|
||||
GenEnum(parser, **it, &enum_code, &enum_code_post, opts);
|
||||
}
|
||||
|
||||
// Generate forward declarations for all structs/tables, since they may
|
||||
@@ -624,34 +648,41 @@ std::string GenerateCPP(const Parser &parser,
|
||||
|
||||
// Generate convenient global helper functions:
|
||||
if (parser.root_struct_def) {
|
||||
auto &name = parser.root_struct_def->name;
|
||||
// The root datatype accessor:
|
||||
code += "inline const " + parser.root_struct_def->name + " *Get";
|
||||
code += parser.root_struct_def->name;
|
||||
code += "inline const " + name + " *Get";
|
||||
code += name;
|
||||
code += "(const void *buf) { return flatbuffers::GetRoot<";
|
||||
code += parser.root_struct_def->name + ">(buf); }\n\n";
|
||||
code += name + ">(buf); }\n\n";
|
||||
|
||||
// The root verifier:
|
||||
code += "inline bool Verify";
|
||||
code += parser.root_struct_def->name;
|
||||
code += name;
|
||||
code += "Buffer(flatbuffers::Verifier &verifier) { "
|
||||
"return verifier.VerifyBuffer<";
|
||||
code += parser.root_struct_def->name + ">(); }\n\n";
|
||||
|
||||
// Finish a buffer with a given root object:
|
||||
code += "inline void Finish" + parser.root_struct_def->name;
|
||||
code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<";
|
||||
code += parser.root_struct_def->name + "> root) { fbb.Finish(root";
|
||||
if (parser.file_identifier_.length())
|
||||
code += ", \"" + parser.file_identifier_ + "\"";
|
||||
code += "); }\n\n";
|
||||
code += name + ">(); }\n\n";
|
||||
|
||||
if (parser.file_identifier_.length()) {
|
||||
// Return the identifier
|
||||
code += "inline const char *" + name;
|
||||
code += "Identifier() { return \"" + parser.file_identifier_;
|
||||
code += "\"; }\n\n";
|
||||
|
||||
// Check if a buffer has the identifier.
|
||||
code += "inline bool " + parser.root_struct_def->name;
|
||||
code += "inline bool " + name;
|
||||
code += "BufferHasIdentifier(const void *buf) { return flatbuffers::";
|
||||
code += "BufferHasIdentifier(buf, \"" + parser.file_identifier_;
|
||||
code += "\"); }\n\n";
|
||||
code += "BufferHasIdentifier(buf, ";
|
||||
code += name + "Identifier()); }\n\n";
|
||||
}
|
||||
|
||||
// Finish a buffer with a given root object:
|
||||
code += "inline void Finish" + name;
|
||||
code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<";
|
||||
code += name + "> root) { fbb.Finish(root";
|
||||
if (parser.file_identifier_.length())
|
||||
code += ", " + name + "Identifier()";
|
||||
code += "); }\n\n";
|
||||
|
||||
}
|
||||
|
||||
CloseNestedNameSpaces(name_space, &code);
|
||||
@@ -665,13 +696,33 @@ std::string GenerateCPP(const Parser &parser,
|
||||
return std::string();
|
||||
}
|
||||
|
||||
static std::string GeneratedFileName(const std::string &path,
|
||||
const std::string &file_name) {
|
||||
return path + file_name + "_generated.h";
|
||||
}
|
||||
|
||||
bool GenerateCPP(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts) {
|
||||
auto code = GenerateCPP(parser, file_name, opts);
|
||||
return !code.length() ||
|
||||
SaveFile((path + file_name + "_generated.h").c_str(), code, false);
|
||||
SaveFile(GeneratedFileName(path, file_name).c_str(), code, false);
|
||||
}
|
||||
|
||||
std::string CPPMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
std::string filebase = flatbuffers::StripPath(
|
||||
flatbuffers::StripExtension(file_name));
|
||||
std::string make_rule = GeneratedFileName(path, filebase) + ": ";
|
||||
auto included_files = parser.GetIncludedFilesRecursive(file_name);
|
||||
for (auto it = included_files.begin();
|
||||
it != included_files.end(); ++it) {
|
||||
make_rule += " " + *it;
|
||||
}
|
||||
return make_rule;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -58,9 +58,9 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name,
|
||||
}
|
||||
schema += ";\n\n";
|
||||
// Generate code for all the enum declarations.
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
EnumDef &enum_def = **it;
|
||||
for (auto enum_def_it = parser.enums_.vec.begin();
|
||||
enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
|
||||
EnumDef &enum_def = **enum_def_it;
|
||||
schema += "enum " + enum_def.name + " : ";
|
||||
schema += GenType(enum_def.underlying_type) + " {\n";
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
@@ -75,9 +75,9 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name,
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
StructDef &struct_def = **it;
|
||||
schema += "table " + struct_def.name + " {\n";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
for (auto field_it = struct_def.fields.vec.begin();
|
||||
field_it != struct_def.fields.vec.end(); ++field_it) {
|
||||
auto &field = **field_it;
|
||||
schema += " " + field.name + ":" + GenType(field.value.type);
|
||||
if (field.value.constant != "0") schema += " = " + field.value.constant;
|
||||
if (field.required) schema += " (required)";
|
||||
|
||||
@@ -98,6 +98,23 @@ LanguageParameters language_parameters[] = {
|
||||
"\n}\n",
|
||||
"",
|
||||
"using FlatBuffers;\n\n",
|
||||
},
|
||||
// TODO: add Go support to the general generator.
|
||||
// WARNING: this is currently only used for generating make rules for Go.
|
||||
{
|
||||
GeneratorOptions::kGo,
|
||||
true,
|
||||
".go",
|
||||
"string",
|
||||
"bool ",
|
||||
"\n{\n",
|
||||
"const ",
|
||||
"",
|
||||
"package ",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"import (\n\tflatbuffers \"github.com/google/flatbuffers/go\"\n)",
|
||||
}
|
||||
};
|
||||
|
||||
@@ -116,10 +133,11 @@ static std::string GenTypeBasic(const LanguageParameters &lang,
|
||||
const Type &type) {
|
||||
static const char *gtypename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE) \
|
||||
#JTYPE, #NTYPE,
|
||||
#JTYPE, #NTYPE, #GTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
|
||||
return gtypename[type.base_type * GeneratorOptions::kMAX + lang.language];
|
||||
}
|
||||
|
||||
@@ -149,6 +167,57 @@ static std::string GenTypeGet(const LanguageParameters &lang,
|
||||
: GenTypePointer(lang, type);
|
||||
}
|
||||
|
||||
// Find the destination type the user wants to receive the value in (e.g.
|
||||
// one size higher signed types for unsigned serialized values in Java).
|
||||
static Type DestinationType(const LanguageParameters &lang, const Type &type,
|
||||
bool vectorelem) {
|
||||
if (lang.language != GeneratorOptions::kJava) return type;
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_UCHAR: return Type(BASE_TYPE_INT);
|
||||
case BASE_TYPE_USHORT: return Type(BASE_TYPE_INT);
|
||||
case BASE_TYPE_UINT: return Type(BASE_TYPE_LONG);
|
||||
case BASE_TYPE_VECTOR:
|
||||
if (vectorelem)
|
||||
return DestinationType(lang, type.VectorType(), vectorelem);
|
||||
// else fall thru:
|
||||
default: return type;
|
||||
}
|
||||
}
|
||||
|
||||
// Mask to turn serialized value into destination type value.
|
||||
static std::string DestinationMask(const LanguageParameters &lang,
|
||||
const Type &type, bool vectorelem) {
|
||||
if (lang.language != GeneratorOptions::kJava) return "";
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_UCHAR: return " & 0xFF";
|
||||
case BASE_TYPE_USHORT: return " & 0xFFFF";
|
||||
case BASE_TYPE_UINT: return " & 0xFFFFFFFFL";
|
||||
case BASE_TYPE_VECTOR:
|
||||
if (vectorelem)
|
||||
return DestinationMask(lang, type.VectorType(), vectorelem);
|
||||
// else fall thru:
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
// Cast necessary to correctly read serialized unsigned values.
|
||||
static std::string DestinationCast(const LanguageParameters &lang,
|
||||
const Type &type) {
|
||||
if (lang.language == GeneratorOptions::kJava &&
|
||||
(type.base_type == BASE_TYPE_UINT ||
|
||||
(type.base_type == BASE_TYPE_VECTOR &&
|
||||
type.element == BASE_TYPE_UINT))) return "(long)";
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
static std::string GenDefaultValue(const Value &value) {
|
||||
return value.type.base_type == BASE_TYPE_BOOL
|
||||
? (value.constant == "0" ? "false" : "true")
|
||||
: value.constant;
|
||||
}
|
||||
|
||||
static void GenEnum(const LanguageParameters &lang, EnumDef &enum_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
@@ -214,13 +283,17 @@ static std::string GenGetter(const LanguageParameters &lang,
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING: return "__string";
|
||||
case BASE_TYPE_STRUCT: return "__struct";
|
||||
case BASE_TYPE_UNION: return "__union";
|
||||
case BASE_TYPE_UNION: return "__union";
|
||||
case BASE_TYPE_VECTOR: return GenGetter(lang, type.VectorType());
|
||||
default:
|
||||
return "bb." + FunctionStart(lang, 'G') + "et" +
|
||||
(GenTypeBasic(lang, type) != "byte"
|
||||
? MakeCamel(GenTypeGet(lang, type))
|
||||
: "");
|
||||
default: {
|
||||
std::string getter = "bb." + FunctionStart(lang, 'G') + "et";
|
||||
if (type.base_type == BASE_TYPE_BOOL) {
|
||||
getter = "0!=" + getter;
|
||||
} else if (GenTypeBasic(lang, type) != "byte") {
|
||||
getter += MakeCamel(GenTypeGet(lang, type));
|
||||
}
|
||||
return getter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,7 +321,11 @@ static void GenStructArgs(const LanguageParameters &lang,
|
||||
GenStructArgs(lang, *field.value.type.struct_def, code_ptr,
|
||||
(field.value.type.struct_def->name + "_").c_str());
|
||||
} else {
|
||||
code += ", " + GenTypeBasic(lang, field.value.type) + " " + nameprefix;
|
||||
code += ", ";
|
||||
code += GenTypeBasic(lang,
|
||||
DestinationType(lang, field.value.type, false));
|
||||
code += " ";
|
||||
code += nameprefix;
|
||||
code += MakeCamel(field.name, lang.first_camel_upper);
|
||||
}
|
||||
}
|
||||
@@ -276,8 +353,16 @@ static void GenStructBody(const LanguageParameters &lang,
|
||||
(field.value.type.struct_def->name + "_").c_str());
|
||||
} else {
|
||||
code += " builder." + FunctionStart(lang, 'P') + "ut";
|
||||
code += GenMethod(lang, field.value.type) + "(" += nameprefix;
|
||||
code += MakeCamel(field.name, lang.first_camel_upper) + ");\n";
|
||||
code += GenMethod(lang, field.value.type) + "(";
|
||||
auto argname = nameprefix + MakeCamel(field.name, lang.first_camel_upper);
|
||||
std::string type_mask = DestinationMask(lang, field.value.type, false);
|
||||
if (type_mask.length()) {
|
||||
code += "(" + GenTypeBasic(lang, field.value.type) + ")";
|
||||
code += "(" + argname + type_mask + ")";
|
||||
} else {
|
||||
code += argname;
|
||||
}
|
||||
code += ");\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -300,13 +385,18 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
if (!struct_def.fixed) {
|
||||
// Generate a special accessor for the table that when used as the root
|
||||
// of a FlatBuffer
|
||||
code += " public static " + struct_def.name + " ";
|
||||
code += FunctionStart(lang, 'G') + "etRootAs" + struct_def.name;
|
||||
code += "(ByteBuffer _bb) { ";
|
||||
std::string method_name = FunctionStart(lang, 'G') + "etRootAs" + struct_def.name;
|
||||
std::string method_signature = " public static " + struct_def.name + " " + method_name;
|
||||
|
||||
// create convenience method that doesn't require an existing object
|
||||
code += method_signature + "(ByteBuffer _bb) ";
|
||||
code += "{ return " + method_name + "(_bb, new " + struct_def.name+ "()); }\n";
|
||||
|
||||
// create method that allows object reuse
|
||||
code += method_signature + "(ByteBuffer _bb, " + struct_def.name + " obj) { ";
|
||||
code += lang.set_bb_byteorder;
|
||||
code += "return (new " + struct_def.name;
|
||||
code += "()).__init(_bb." + FunctionStart(lang, 'G');
|
||||
code += "etInt(_bb.position()) + _bb.position(), _bb); }\n";
|
||||
code += "return (obj.__init(_bb." + FunctionStart(lang, 'G');
|
||||
code += "etInt(_bb.position()) + _bb.position(), _bb)); }\n";
|
||||
if (parser.root_struct_def == &struct_def) {
|
||||
if (parser.file_identifier_.length()) {
|
||||
// Check if a buffer has the identifier.
|
||||
@@ -330,7 +420,11 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
if (field.deprecated) continue;
|
||||
GenComment(field.doc_comment, code_ptr, " ");
|
||||
std::string type_name = GenTypeGet(lang, field.value.type);
|
||||
std::string method_start = " public " + type_name + " " +
|
||||
std::string type_name_dest =
|
||||
GenTypeGet(lang, DestinationType(lang, field.value.type, true));
|
||||
std::string dest_mask = DestinationMask(lang, field.value.type, true);
|
||||
std::string dest_cast = DestinationCast(lang, field.value.type);
|
||||
std::string method_start = " public " + type_name_dest + " " +
|
||||
MakeCamel(field.name, lang.first_camel_upper);
|
||||
// Generate the accessors that don't do object reuse.
|
||||
if (field.value.type.base_type == BASE_TYPE_STRUCT) {
|
||||
@@ -348,7 +442,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
code += "(new ";
|
||||
code += type_name + "(), j); }\n";
|
||||
}
|
||||
std::string getter = GenGetter(lang, field.value.type);
|
||||
std::string getter = dest_cast + GenGetter(lang, field.value.type);
|
||||
code += method_start + "(";
|
||||
// Most field accessors need to retrieve and test the field offset first,
|
||||
// this is the prefix code for that:
|
||||
@@ -357,14 +451,16 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
"); return o != 0 ? ";
|
||||
std::string default_cast = "";
|
||||
if (lang.language == GeneratorOptions::kCSharp)
|
||||
default_cast = "(" + type_name + ")";
|
||||
default_cast = "(" + type_name_dest + ")";
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
code += ") { return " + getter;
|
||||
code += "(bb_pos + " + NumToString(field.value.offset) + ")";
|
||||
code += dest_mask;
|
||||
} else {
|
||||
code += offset_prefix + getter;
|
||||
code += "(o + bb_pos) : " + default_cast + field.value.constant;
|
||||
code += "(o + bb_pos)" + dest_mask + " : " + default_cast;
|
||||
code += GenDefaultValue(field.value);
|
||||
}
|
||||
} else {
|
||||
switch (field.value.type.base_type) {
|
||||
@@ -402,7 +498,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
} else {
|
||||
code += index;
|
||||
}
|
||||
code += ") : ";
|
||||
code += ")" + dest_mask + " : ";
|
||||
code += IsScalar(field.value.type.element)
|
||||
? default_cast + "0"
|
||||
: "null";
|
||||
@@ -422,7 +518,9 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
code += "Length(" + offset_prefix;
|
||||
code += "__vector_len(o) : 0; }\n";
|
||||
}
|
||||
if ((field.value.type.base_type == BASE_TYPE_VECTOR ||
|
||||
// Generate a ByteBuffer accessor for strings & vectors of scalars.
|
||||
if (((field.value.type.base_type == BASE_TYPE_VECTOR &&
|
||||
IsScalar(field.value.type.VectorType().base_type)) ||
|
||||
field.value.type.base_type == BASE_TYPE_STRING) &&
|
||||
lang.language == GeneratorOptions::kJava) {
|
||||
code += " public ByteBuffer ";
|
||||
@@ -470,12 +568,16 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
code += ",\n " + GenTypeBasic(lang, field.value.type) + " ";
|
||||
code += ",\n ";
|
||||
code += GenTypeBasic(lang,
|
||||
DestinationType(lang, field.value.type, false));
|
||||
code += " ";
|
||||
code += field.name;
|
||||
// Java doesn't have defaults, which means this method must always
|
||||
// supply all arguments, and thus won't compile when fields are added.
|
||||
if (lang.language != GeneratorOptions::kJava)
|
||||
code += " = " + field.value.constant;
|
||||
if (lang.language != GeneratorOptions::kJava) {
|
||||
code += " = " + GenDefaultValue(field.value);
|
||||
}
|
||||
}
|
||||
code += ") {\n builder.";
|
||||
code += FunctionStart(lang, 'S') + "tartObject(";
|
||||
@@ -516,13 +618,21 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
code += " public static void " + FunctionStart(lang, 'A') + "dd";
|
||||
code += MakeCamel(field.name);
|
||||
code += "(FlatBufferBuilder builder, ";
|
||||
code += GenTypeBasic(lang, field.value.type);
|
||||
code += GenTypeBasic(lang,
|
||||
DestinationType(lang, field.value.type, false));
|
||||
auto argname = MakeCamel(field.name, false);
|
||||
if (!IsScalar(field.value.type.base_type)) argname += "Offset";
|
||||
code += " " + argname + ") { builder." + FunctionStart(lang, 'A') + "dd";
|
||||
code += GenMethod(lang, field.value.type) + "(";
|
||||
code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
|
||||
code += argname + ", " + field.value.constant;
|
||||
std::string type_mask = DestinationMask(lang, field.value.type, false);
|
||||
if (type_mask.length()) {
|
||||
code += "(" + GenTypeBasic(lang, field.value.type) + ")";
|
||||
code += "(" + argname + type_mask + ")";
|
||||
} else {
|
||||
code += argname;
|
||||
}
|
||||
code += ", " + GenDefaultValue(field.value);
|
||||
code += "); }\n";
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
auto vector_type = field.value.type.VectorType();
|
||||
@@ -591,15 +701,14 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string namespace_general;
|
||||
std::string namespace_dir = path;
|
||||
std::string namespace_dir = path; // Either empty or ends in separator.
|
||||
auto &namespaces = parser.namespaces_.back()->components;
|
||||
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
|
||||
if (namespace_general.length()) {
|
||||
namespace_general += ".";
|
||||
namespace_dir += kPathSeparator;
|
||||
}
|
||||
namespace_general += *it;
|
||||
namespace_dir += *it;
|
||||
namespace_dir += *it + kPathSeparator;
|
||||
}
|
||||
EnsureDirExists(namespace_dir);
|
||||
|
||||
@@ -609,8 +718,7 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
|
||||
if (needs_includes) code += lang.includes;
|
||||
code += classcode;
|
||||
code += lang.namespace_end;
|
||||
auto filename = namespace_dir + kPathSeparator + def.name +
|
||||
lang.file_extension;
|
||||
auto filename = namespace_dir + def.name + lang.file_extension;
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
@@ -641,4 +749,91 @@ bool GenerateGeneral(const Parser &parser,
|
||||
return true;
|
||||
}
|
||||
|
||||
static std::string ClassFileName(const LanguageParameters &lang,
|
||||
const Parser &parser, const Definition &def,
|
||||
const std::string &path) {
|
||||
std::string namespace_general;
|
||||
std::string namespace_dir = path;
|
||||
auto &namespaces = parser.namespaces_.back()->components;
|
||||
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
|
||||
if (namespace_general.length()) {
|
||||
namespace_general += ".";
|
||||
namespace_dir += kPathSeparator;
|
||||
}
|
||||
namespace_general += *it;
|
||||
namespace_dir += *it;
|
||||
}
|
||||
|
||||
return namespace_dir + kPathSeparator + def.name + lang.file_extension;
|
||||
}
|
||||
|
||||
std::string GeneralMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts) {
|
||||
assert(opts.lang <= GeneratorOptions::kMAX);
|
||||
auto lang = language_parameters[opts.lang];
|
||||
|
||||
std::string make_rule;
|
||||
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
if (make_rule != "")
|
||||
make_rule += " ";
|
||||
make_rule += ClassFileName(lang, parser, **it, path);
|
||||
}
|
||||
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
if (make_rule != "")
|
||||
make_rule += " ";
|
||||
make_rule += ClassFileName(lang, parser, **it, path);
|
||||
}
|
||||
|
||||
make_rule += ": ";
|
||||
auto included_files = parser.GetIncludedFilesRecursive(file_name);
|
||||
for (auto it = included_files.begin();
|
||||
it != included_files.end(); ++it) {
|
||||
make_rule += " " + *it;
|
||||
}
|
||||
return make_rule;
|
||||
}
|
||||
|
||||
std::string BinaryFileName(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin";
|
||||
return path + file_name + "." + ext;
|
||||
}
|
||||
|
||||
bool GenerateBinary(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
return !parser.builder_.GetSize() ||
|
||||
flatbuffers::SaveFile(
|
||||
BinaryFileName(parser, path, file_name).c_str(),
|
||||
reinterpret_cast<char *>(parser.builder_.GetBufferPointer()),
|
||||
parser.builder_.GetSize(),
|
||||
true);
|
||||
}
|
||||
|
||||
std::string BinaryMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
if (!parser.builder_.GetSize()) return "";
|
||||
std::string filebase = flatbuffers::StripPath(
|
||||
flatbuffers::StripExtension(file_name));
|
||||
std::string make_rule = BinaryFileName(parser, path, filebase) + ": " +
|
||||
file_name;
|
||||
auto included_files = parser.GetIncludedFilesRecursive(
|
||||
parser.root_struct_def->file);
|
||||
for (auto it = included_files.begin();
|
||||
it != included_files.end(); ++it) {
|
||||
make_rule += " " + *it;
|
||||
}
|
||||
return make_rule;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -580,23 +580,21 @@ static bool SaveType(const Parser &parser, const Definition &def,
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string namespace_name;
|
||||
std::string namespace_dir = path;
|
||||
std::string namespace_dir = path; // Either empty or ends in separator.
|
||||
auto &namespaces = parser.namespaces_.back()->components;
|
||||
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
|
||||
if (namespace_name.length()) {
|
||||
namespace_name += ".";
|
||||
namespace_dir += PATH_SEPARATOR;
|
||||
}
|
||||
namespace_name = *it;
|
||||
namespace_dir += *it;
|
||||
mkdir(namespace_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
|
||||
namespace_dir += *it + kPathSeparator;
|
||||
}
|
||||
|
||||
EnsureDirExists(namespace_dir);
|
||||
|
||||
std::string code = "";
|
||||
BeginFile(namespace_name, needs_imports, &code);
|
||||
code += classcode;
|
||||
std::string filename = namespace_dir + PATH_SEPARATOR + def.name + ".go";
|
||||
std::string filename = namespace_dir + def.name + ".go";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -268,5 +268,40 @@ void GenerateText(const Parser &parser, const void *flatbuffer,
|
||||
text += NewLine(opts);
|
||||
}
|
||||
|
||||
std::string TextFileName(const std::string &path,
|
||||
const std::string &file_name) {
|
||||
return path + file_name + ".json";
|
||||
}
|
||||
|
||||
bool GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts) {
|
||||
if (!parser.builder_.GetSize() || !parser.root_struct_def) return true;
|
||||
std::string text;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), opts,
|
||||
&text);
|
||||
return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(),
|
||||
text,
|
||||
false);
|
||||
}
|
||||
|
||||
std::string TextMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
if (!parser.builder_.GetSize() || !parser.root_struct_def) return "";
|
||||
std::string filebase = flatbuffers::StripPath(
|
||||
flatbuffers::StripExtension(file_name));
|
||||
std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
|
||||
auto included_files = parser.GetIncludedFilesRecursive(
|
||||
parser.root_struct_def->file);
|
||||
for (auto it = included_files.begin();
|
||||
it != included_files.end(); ++it) {
|
||||
make_rule += " " + *it;
|
||||
}
|
||||
return make_rule;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
||||
@@ -15,8 +15,10 @@
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/hash.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
@@ -85,7 +87,8 @@ template<> inline Offset<void> atot<Offset<void>>(const char *s) {
|
||||
TD(RootType, 266, "root_type") \
|
||||
TD(FileIdentifier, 267, "file_identifier") \
|
||||
TD(FileExtension, 268, "file_extension") \
|
||||
TD(Include, 269, "include")
|
||||
TD(Include, 269, "include") \
|
||||
TD(Attribute, 270, "attribute")
|
||||
#ifdef __GNUC__
|
||||
__extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
|
||||
#endif
|
||||
@@ -123,7 +126,8 @@ int64_t Parser::ParseHexNum(int nibbles) {
|
||||
if (!isxdigit(cursor_[i]))
|
||||
Error("escape code must be followed by " + NumToString(nibbles) +
|
||||
" hex digits");
|
||||
auto val = StringToInt(cursor_, 16);
|
||||
std::string target(cursor_, cursor_ + nibbles);
|
||||
auto val = StringToInt(target.c_str(), 16);
|
||||
cursor_ += nibbles;
|
||||
return val;
|
||||
}
|
||||
@@ -184,7 +188,7 @@ void Parser::Next() {
|
||||
const char *start = ++cursor_;
|
||||
while (*cursor_ && *cursor_ != '\n') cursor_++;
|
||||
if (*start == '/') { // documentation comment
|
||||
if (!seen_newline)
|
||||
if (cursor_ != source_ && !seen_newline)
|
||||
Error("a documentation comment should be on a line on its own");
|
||||
doc_comment_.push_back(std::string(start + 1, cursor_));
|
||||
}
|
||||
@@ -222,7 +226,8 @@ void Parser::Next() {
|
||||
if (attribute_ == "union") { token_ = kTokenUnion; return; }
|
||||
if (attribute_ == "namespace") { token_ = kTokenNameSpace; return; }
|
||||
if (attribute_ == "root_type") { token_ = kTokenRootType; return; }
|
||||
if (attribute_ == "include") { token_ = kTokenInclude; return; }
|
||||
if (attribute_ == "include") { token_ = kTokenInclude; return; }
|
||||
if (attribute_ == "attribute") { token_ = kTokenAttribute; return; }
|
||||
if (attribute_ == "file_identifier") {
|
||||
token_ = kTokenFileIdentifier;
|
||||
return;
|
||||
@@ -330,6 +335,7 @@ FieldDef &Parser::AddField(StructDef &struct_def,
|
||||
field.value.offset =
|
||||
FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
|
||||
field.name = name;
|
||||
field.file = struct_def.file;
|
||||
field.value.type = type;
|
||||
if (struct_def.fixed) { // statically compute the field offset
|
||||
auto size = InlineSize(type);
|
||||
@@ -387,12 +393,44 @@ void Parser::ParseField(StructDef &struct_def) {
|
||||
field.doc_comment = dc;
|
||||
ParseMetaData(field);
|
||||
field.deprecated = field.attributes.Lookup("deprecated") != nullptr;
|
||||
auto hash_name = field.attributes.Lookup("hash");
|
||||
if (hash_name) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_INT:
|
||||
case BASE_TYPE_UINT: {
|
||||
if (FindHashFunction32(hash_name->constant.c_str()) == nullptr)
|
||||
Error("Unknown hashing algorithm for 32 bit types: " +
|
||||
hash_name->constant);
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_LONG:
|
||||
case BASE_TYPE_ULONG: {
|
||||
if (FindHashFunction64(hash_name->constant.c_str()) == nullptr)
|
||||
Error("Unknown hashing algorithm for 64 bit types: " +
|
||||
hash_name->constant);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Error("only int, uint, long and ulong data types support hashing.");
|
||||
}
|
||||
}
|
||||
if (field.deprecated && struct_def.fixed)
|
||||
Error("can't deprecate fields in a struct");
|
||||
field.required = field.attributes.Lookup("required") != nullptr;
|
||||
if (field.required && (struct_def.fixed ||
|
||||
IsScalar(field.value.type.base_type)))
|
||||
Error("only non-scalar fields in tables may be 'required'");
|
||||
field.key = field.attributes.Lookup("key") != nullptr;
|
||||
if (field.key) {
|
||||
if (struct_def.has_key)
|
||||
Error("only one field may be set as 'key'");
|
||||
struct_def.has_key = true;
|
||||
if (!IsScalar(field.value.type.base_type)) {
|
||||
field.required = true;
|
||||
if (field.value.type.base_type != BASE_TYPE_STRING)
|
||||
Error("'key' field must be string or scalar type");
|
||||
}
|
||||
}
|
||||
auto nested = field.attributes.Lookup("nested_flatbuffer");
|
||||
if (nested) {
|
||||
if (nested->type.base_type != BASE_TYPE_STRING)
|
||||
@@ -449,6 +487,18 @@ void Parser::ParseAnyValue(Value &val, FieldDef *field) {
|
||||
val.constant = NumToString(ParseVector(val.type.VectorType()));
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_INT:
|
||||
case BASE_TYPE_UINT:
|
||||
case BASE_TYPE_LONG:
|
||||
case BASE_TYPE_ULONG: {
|
||||
if (field && field->attributes.Lookup("hash") &&
|
||||
(token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
|
||||
ParseHash(val, field);
|
||||
} else {
|
||||
ParseSingleValue(val);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ParseSingleValue(val);
|
||||
break;
|
||||
@@ -467,9 +517,11 @@ void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
|
||||
uoffset_t Parser::ParseTable(const StructDef &struct_def) {
|
||||
Expect('{');
|
||||
size_t fieldn = 0;
|
||||
if (!IsNext('}')) for (;;) {
|
||||
for (;;) {
|
||||
if ((!strict_json_ || !fieldn) && IsNext('}')) break;
|
||||
std::string name = attribute_;
|
||||
if (!IsNext(kTokenStringConstant)) Expect(kTokenIdentifier);
|
||||
if (!IsNext(kTokenStringConstant))
|
||||
Expect(strict_json_ ? kTokenStringConstant : kTokenIdentifier);
|
||||
auto field = struct_def.fields.Lookup(name);
|
||||
if (!field) Error("unknown field: " + name);
|
||||
if (struct_def.fixed && (fieldn >= struct_def.fields.vec.size()
|
||||
@@ -561,16 +613,16 @@ uoffset_t Parser::ParseTable(const StructDef &struct_def) {
|
||||
|
||||
uoffset_t Parser::ParseVector(const Type &type) {
|
||||
int count = 0;
|
||||
if (token_ != ']') for (;;) {
|
||||
for (;;) {
|
||||
if ((!strict_json_ || !count) && IsNext(']')) break;
|
||||
Value val;
|
||||
val.type = type;
|
||||
ParseAnyValue(val, NULL);
|
||||
ParseAnyValue(val, nullptr);
|
||||
field_stack_.push_back(std::make_pair(val, nullptr));
|
||||
count++;
|
||||
if (token_ == ']') break;
|
||||
if (IsNext(']')) break;
|
||||
Expect(',');
|
||||
}
|
||||
Next();
|
||||
|
||||
builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
|
||||
InlineAlignment(type));
|
||||
@@ -598,6 +650,8 @@ void Parser::ParseMetaData(Definition &def) {
|
||||
for (;;) {
|
||||
auto name = attribute_;
|
||||
Expect(kTokenIdentifier);
|
||||
if (known_attributes_.find(name) == known_attributes_.end())
|
||||
Error("user define attributes must be declared before use: " + name);
|
||||
auto e = new Value();
|
||||
def.attributes.Add(name, e);
|
||||
if (IsNext(':')) {
|
||||
@@ -669,6 +723,31 @@ int64_t Parser::ParseIntegerFromString(Type &type) {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void Parser::ParseHash(Value &e, FieldDef* field) {
|
||||
assert(field);
|
||||
Value *hash_name = field->attributes.Lookup("hash");
|
||||
switch (e.type.base_type) {
|
||||
case BASE_TYPE_INT:
|
||||
case BASE_TYPE_UINT: {
|
||||
auto hash = FindHashFunction32(hash_name->constant.c_str());
|
||||
uint32_t hashed_value = hash(attribute_.c_str());
|
||||
e.constant = NumToString(hashed_value);
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_LONG:
|
||||
case BASE_TYPE_ULONG: {
|
||||
auto hash = FindHashFunction64(hash_name->constant.c_str());
|
||||
uint64_t hashed_value = hash(attribute_.c_str());
|
||||
e.constant = NumToString(hashed_value);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
Next();
|
||||
}
|
||||
|
||||
void Parser::ParseSingleValue(Value &e) {
|
||||
// First check if this could be a string/identifier enum value:
|
||||
if (e.type.base_type != BASE_TYPE_STRING &&
|
||||
@@ -708,16 +787,17 @@ StructDef *Parser::LookupCreateStruct(const std::string &name) {
|
||||
}
|
||||
|
||||
void Parser::ParseEnum(bool is_union) {
|
||||
std::vector<std::string> dc = doc_comment_;
|
||||
std::vector<std::string> enum_comment = doc_comment_;
|
||||
Next();
|
||||
std::string name = attribute_;
|
||||
std::string enum_name = attribute_;
|
||||
Expect(kTokenIdentifier);
|
||||
auto &enum_def = *new EnumDef();
|
||||
enum_def.name = name;
|
||||
enum_def.doc_comment = dc;
|
||||
enum_def.name = enum_name;
|
||||
if (!files_being_parsed_.empty()) enum_def.file = files_being_parsed_.top();
|
||||
enum_def.doc_comment = enum_comment;
|
||||
enum_def.is_union = is_union;
|
||||
enum_def.defined_namespace = namespaces_.back();
|
||||
if (enums_.Add(name, &enum_def)) Error("enum already exists: " + name);
|
||||
if (enums_.Add(enum_name, &enum_def)) Error("enum already exists: " + enum_name);
|
||||
if (is_union) {
|
||||
enum_def.underlying_type.base_type = BASE_TYPE_UTYPE;
|
||||
enum_def.underlying_type.enum_def = &enum_def;
|
||||
@@ -741,19 +821,19 @@ void Parser::ParseEnum(bool is_union) {
|
||||
Expect('{');
|
||||
if (is_union) enum_def.vals.Add("NONE", new EnumVal("NONE", 0));
|
||||
do {
|
||||
std::string name = attribute_;
|
||||
std::vector<std::string> dc = doc_comment_;
|
||||
std::string value_name = attribute_;
|
||||
std::vector<std::string> value_comment = doc_comment_;
|
||||
Expect(kTokenIdentifier);
|
||||
auto prevsize = enum_def.vals.vec.size();
|
||||
auto value = enum_def.vals.vec.size()
|
||||
? enum_def.vals.vec.back()->value + 1
|
||||
: 0;
|
||||
auto &ev = *new EnumVal(name, value);
|
||||
if (enum_def.vals.Add(name, &ev))
|
||||
Error("enum value already exists: " + name);
|
||||
ev.doc_comment = dc;
|
||||
auto &ev = *new EnumVal(value_name, value);
|
||||
if (enum_def.vals.Add(value_name, &ev))
|
||||
Error("enum value already exists: " + value_name);
|
||||
ev.doc_comment = value_comment;
|
||||
if (is_union) {
|
||||
ev.struct_def = LookupCreateStruct(name);
|
||||
ev.struct_def = LookupCreateStruct(value_name);
|
||||
}
|
||||
if (IsNext('=')) {
|
||||
ev.value = atoi(attribute_.c_str());
|
||||
@@ -781,6 +861,7 @@ StructDef &Parser::StartStruct() {
|
||||
if (!struct_def.predecl) Error("datatype already exists: " + name);
|
||||
struct_def.predecl = false;
|
||||
struct_def.name = name;
|
||||
if (!files_being_parsed_.empty()) struct_def.file = files_being_parsed_.top();
|
||||
// Move this struct to the back of the vector just in case it was predeclared,
|
||||
// to preserve declaration order.
|
||||
remove(structs_.vec.begin(), structs_.vec.end(), &struct_def);
|
||||
@@ -1004,7 +1085,16 @@ Type Parser::ParseTypeFromProtoType() {
|
||||
|
||||
bool Parser::Parse(const char *source, const char **include_paths,
|
||||
const char *source_filename) {
|
||||
if (source_filename) included_files_[source_filename] = true;
|
||||
if (source_filename &&
|
||||
included_files_.find(source_filename) == included_files_.end()) {
|
||||
included_files_[source_filename] = true;
|
||||
files_included_per_file_[source_filename] = std::set<std::string>();
|
||||
files_being_parsed_.push(source_filename);
|
||||
}
|
||||
if (!include_paths) {
|
||||
const char *current_directory[] = { "", nullptr };
|
||||
include_paths = current_directory;
|
||||
}
|
||||
source_ = cursor_ = source;
|
||||
line_ = 1;
|
||||
error_.clear();
|
||||
@@ -1015,22 +1105,23 @@ bool Parser::Parse(const char *source, const char **include_paths,
|
||||
while (IsNext(kTokenInclude)) {
|
||||
auto name = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
if (included_files_.find(name) == included_files_.end()) {
|
||||
// Look for the file in include_paths.
|
||||
std::string filepath;
|
||||
for (auto paths = include_paths; paths && *paths; paths++) {
|
||||
filepath = flatbuffers::ConCatPathFileName(*paths, name);
|
||||
if(FileExists(filepath.c_str())) break;
|
||||
}
|
||||
if (filepath.empty())
|
||||
Error("unable to locate include file: " + name);
|
||||
if (source_filename)
|
||||
files_included_per_file_[source_filename].insert(filepath);
|
||||
if (included_files_.find(filepath) == included_files_.end()) {
|
||||
// We found an include file that we have not parsed yet.
|
||||
// Load it and parse it.
|
||||
std::string contents;
|
||||
if (!include_paths) {
|
||||
const char *current_directory[] = { "", nullptr };
|
||||
include_paths = current_directory;
|
||||
}
|
||||
for (auto paths = include_paths; paths && *paths; paths++) {
|
||||
auto filepath = flatbuffers::ConCatPathFileName(*paths, name);
|
||||
if(LoadFile(filepath.c_str(), true, &contents)) break;
|
||||
}
|
||||
if (contents.empty())
|
||||
if (!LoadFile(filepath.c_str(), true, &contents))
|
||||
Error("unable to load include file: " + name);
|
||||
included_files_[name] = true;
|
||||
if (!Parse(contents.c_str(), include_paths)) {
|
||||
if (!Parse(contents.c_str(), include_paths, filepath.c_str())) {
|
||||
// Any errors, we're done.
|
||||
return false;
|
||||
}
|
||||
@@ -1090,6 +1181,12 @@ bool Parser::Parse(const char *source, const char **include_paths,
|
||||
Expect(';');
|
||||
} else if(token_ == kTokenInclude) {
|
||||
Error("includes must come before declarations");
|
||||
} else if(token_ == kTokenAttribute) {
|
||||
Next();
|
||||
auto name = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
Expect(';');
|
||||
known_attributes_.insert(name);
|
||||
} else {
|
||||
ParseDecl();
|
||||
}
|
||||
@@ -1101,10 +1198,10 @@ bool Parser::Parse(const char *source, const char **include_paths,
|
||||
for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
|
||||
auto &enum_def = **it;
|
||||
if (enum_def.is_union) {
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &val = **it;
|
||||
for (auto val_it = enum_def.vals.vec.begin();
|
||||
val_it != enum_def.vals.vec.end();
|
||||
++val_it) {
|
||||
auto &val = **val_it;
|
||||
if (val.struct_def && val.struct_def->fixed)
|
||||
Error("only tables can be union elements: " + val.name);
|
||||
}
|
||||
@@ -1119,10 +1216,35 @@ bool Parser::Parse(const char *source, const char **include_paths,
|
||||
error_ += NumToString(line_) + ":0"; // gcc alike
|
||||
#endif
|
||||
error_ += ": error: " + msg;
|
||||
if (source_filename) files_being_parsed_.pop();
|
||||
return false;
|
||||
}
|
||||
if (source_filename) files_being_parsed_.pop();
|
||||
assert(!struct_stack_.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
std::set<std::string> Parser::GetIncludedFilesRecursive(
|
||||
const std::string &file_name) const {
|
||||
std::set<std::string> included_files;
|
||||
std::list<std::string> to_process;
|
||||
|
||||
if (file_name.empty()) return included_files;
|
||||
to_process.push_back(file_name);
|
||||
|
||||
while (!to_process.empty()) {
|
||||
std::string current = to_process.front();
|
||||
to_process.pop_front();
|
||||
included_files.insert(current);
|
||||
|
||||
auto new_files = files_included_per_file_.at(current);
|
||||
for (auto it = new_files.begin(); it != new_files.end(); ++it) {
|
||||
if (included_files.find(*it) == included_files.end())
|
||||
to_process.push_back(*it);
|
||||
}
|
||||
}
|
||||
|
||||
return included_files;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
using System;
|
||||
/*
|
||||
* Copyright 2014 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.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
@@ -239,6 +239,34 @@ namespace FlatBuffers.Test
|
||||
var uut = new ByteBuffer(buffer);
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0));
|
||||
}
|
||||
public void ByteBuffer_ReverseBytesUshort()
|
||||
{
|
||||
ushort original = (ushort)0x1234U;
|
||||
ushort reverse = ByteBuffer.ReverseBytes(original);
|
||||
Assert.AreEqual(0x3412U, reverse);
|
||||
|
||||
ushort rereverse = ByteBuffer.ReverseBytes(reverse);
|
||||
Assert.AreEqual(original, rereverse);
|
||||
}
|
||||
|
||||
public void ByteBuffer_ReverseBytesUint()
|
||||
{
|
||||
uint original = 0x12345678;
|
||||
uint reverse = ByteBuffer.ReverseBytes(original);
|
||||
Assert.AreEqual(0x78563412U, reverse);
|
||||
|
||||
uint rereverse = ByteBuffer.ReverseBytes(reverse);
|
||||
Assert.AreEqual(original, rereverse);
|
||||
}
|
||||
|
||||
public void ByteBuffer_ReverseBytesUlong()
|
||||
{
|
||||
ulong original = 0x1234567890ABCDEFUL;
|
||||
ulong reverse = ByteBuffer.ReverseBytes(original);
|
||||
Assert.AreEqual(0xEFCDAB9078563412UL, reverse);
|
||||
|
||||
ulong rereverse = ByteBuffer.ReverseBytes(reverse);
|
||||
Assert.AreEqual(original, rereverse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,21 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\net\FlatBuffers\ByteBuffer.cs">
|
||||
<Link>FlatBuffers\ByteBuffer.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\net\FlatBuffers\FlatBufferBuilder.cs">
|
||||
<Link>FlatBuffers\FlatBufferBuilder.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\net\FlatBuffers\FlatBufferConstants.cs">
|
||||
<Link>FlatBuffers\FlatBufferConstants.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\net\FlatBuffers\Struct.cs">
|
||||
<Link>FlatBuffers\Struct.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\net\FlatBuffers\Table.cs">
|
||||
<Link>FlatBuffers\Table.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\MyGame\Example\Any.cs">
|
||||
<Link>MyGame\Example\Any.cs</Link>
|
||||
</Compile>
|
||||
@@ -47,6 +62,9 @@
|
||||
<Compile Include="..\MyGame\Example\Monster.cs">
|
||||
<Link>MyGame\Example\Monster.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\MyGame\Example\Stat.cs">
|
||||
<Link>MyGame\Example\Stat.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\MyGame\Example\Test.cs">
|
||||
<Link>MyGame\Example\Test.cs</Link>
|
||||
</Compile>
|
||||
@@ -55,17 +73,10 @@
|
||||
</Compile>
|
||||
<Compile Include="Assert.cs" />
|
||||
<Compile Include="ByteBufferTests.cs" />
|
||||
<Compile Include="MyGame\Example\Stat.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="FlatBuffersExampleTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\net\FlatBuffers\FlatBuffers.csproj">
|
||||
<Project>{28C00774-1E73-4A75-AD8F-844CD21A064D}</Project>
|
||||
<Name>FlatBuffers</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="..\monsterdata_test.mon">
|
||||
<Link>Resources\monsterdata_test.mon</Link>
|
||||
|
||||
@@ -76,6 +76,7 @@ namespace FlatBuffers.Test
|
||||
Monster.AddTest(fbb, mon2);
|
||||
Monster.AddTest4(fbb, test4);
|
||||
Monster.AddTestarrayofstring(fbb, testArrayOfString);
|
||||
Monster.AddTestbool(fbb, false);
|
||||
var mon = Monster.EndMonster(fbb);
|
||||
|
||||
fbb.Finish(mon);
|
||||
@@ -134,6 +135,8 @@ namespace FlatBuffers.Test
|
||||
Assert.AreEqual(2, monster.TestarrayofstringLength());
|
||||
Assert.AreEqual("test1", monster.Testarrayofstring(0));
|
||||
Assert.AreEqual("test2", monster.Testarrayofstring(1));
|
||||
|
||||
Assert.AreEqual(false, monster.Testbool());
|
||||
}
|
||||
|
||||
public void CanReadCppGeneratedWireFile()
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
using System;
|
||||
/*
|
||||
* Copyright 2014 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.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
|
||||
@@ -1,8 +1,24 @@
|
||||
using System.Reflection;
|
||||
/*
|
||||
* Copyright 2014 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.
|
||||
*/
|
||||
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("FlatBuffers.Test")]
|
||||
@@ -14,8 +30,8 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
@@ -25,11 +41,11 @@ using System.Runtime.InteropServices;
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
|
||||
@@ -80,6 +80,7 @@ class JavaTest {
|
||||
Monster.addTest(fbb, mon2);
|
||||
Monster.addTest4(fbb, test4);
|
||||
Monster.addTestarrayofstring(fbb, testArrayOfString);
|
||||
Monster.addTestbool(fbb, false);
|
||||
int mon = Monster.endMonster(fbb);
|
||||
|
||||
Monster.finishMonsterBuffer(fbb, mon);
|
||||
@@ -166,6 +167,8 @@ class JavaTest {
|
||||
TestEq(monster.testarrayofstringLength(), 2);
|
||||
TestEq(monster.testarrayofstring(0),"test1");
|
||||
TestEq(monster.testarrayofstring(1),"test2");
|
||||
|
||||
TestEq(monster.testbool(), false);
|
||||
}
|
||||
|
||||
static <T> void TestEq(T a, T b) {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Copyright 2014 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.
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace MyGame.Example
|
||||
using FlatBuffers;
|
||||
|
||||
public class Monster : Table {
|
||||
public static Monster GetRootAsMonster(ByteBuffer _bb) { return (new Monster()).__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb); }
|
||||
public static Monster GetRootAsMonster(ByteBuffer _bb) { return GetRootAsMonster(_bb, new Monster()); }
|
||||
public static Monster GetRootAsMonster(ByteBuffer _bb, Monster obj) { return (obj.__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public static bool MonsterBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONS"); }
|
||||
public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
|
||||
@@ -36,8 +37,17 @@ public class Monster : Table {
|
||||
public int TestnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
|
||||
public Stat Testempty() { return Testempty(new Stat()); }
|
||||
public Stat Testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
public bool Testbool() { int o = __offset(34); return o != 0 ? 0!=bb.Get(o + bb_pos) : (bool)false; }
|
||||
public int Testhashs32Fnv1() { int o = __offset(36); return o != 0 ? bb.GetInt(o + bb_pos) : (int)0; }
|
||||
public uint Testhashu32Fnv1() { int o = __offset(38); return o != 0 ? bb.GetUint(o + bb_pos) : (uint)0; }
|
||||
public long Testhashs64Fnv1() { int o = __offset(40); return o != 0 ? bb.GetLong(o + bb_pos) : (long)0; }
|
||||
public ulong Testhashu64Fnv1() { int o = __offset(42); return o != 0 ? bb.GetUlong(o + bb_pos) : (ulong)0; }
|
||||
public int Testhashs32Fnv1a() { int o = __offset(44); return o != 0 ? bb.GetInt(o + bb_pos) : (int)0; }
|
||||
public uint Testhashu32Fnv1a() { int o = __offset(46); return o != 0 ? bb.GetUint(o + bb_pos) : (uint)0; }
|
||||
public long Testhashs64Fnv1a() { int o = __offset(48); return o != 0 ? bb.GetLong(o + bb_pos) : (long)0; }
|
||||
public ulong Testhashu64Fnv1a() { int o = __offset(50); return o != 0 ? bb.GetUlong(o + bb_pos) : (ulong)0; }
|
||||
|
||||
public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(15); }
|
||||
public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(24); }
|
||||
public static void AddPos(FlatBufferBuilder builder, int posOffset) { builder.AddStruct(0, posOffset, 0); }
|
||||
public static void AddMana(FlatBufferBuilder builder, short mana) { builder.AddShort(1, mana, 150); }
|
||||
public static void AddHp(FlatBufferBuilder builder, short hp) { builder.AddShort(2, hp, 100); }
|
||||
@@ -61,6 +71,15 @@ public class Monster : Table {
|
||||
public static int CreateTestnestedflatbufferVector(FlatBufferBuilder builder, byte[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddByte(data[i]); return builder.EndVector(); }
|
||||
public static void StartTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); }
|
||||
public static void AddTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.AddOffset(14, testemptyOffset, 0); }
|
||||
public static void AddTestbool(FlatBufferBuilder builder, bool testbool) { builder.AddBool(15, testbool, false); }
|
||||
public static void AddTesthashs32Fnv1(FlatBufferBuilder builder, int testhashs32Fnv1) { builder.AddInt(16, testhashs32Fnv1, 0); }
|
||||
public static void AddTesthashu32Fnv1(FlatBufferBuilder builder, uint testhashu32Fnv1) { builder.AddUint(17, testhashu32Fnv1, 0); }
|
||||
public static void AddTesthashs64Fnv1(FlatBufferBuilder builder, long testhashs64Fnv1) { builder.AddLong(18, testhashs64Fnv1, 0); }
|
||||
public static void AddTesthashu64Fnv1(FlatBufferBuilder builder, ulong testhashu64Fnv1) { builder.AddUlong(19, testhashu64Fnv1, 0); }
|
||||
public static void AddTesthashs32Fnv1a(FlatBufferBuilder builder, int testhashs32Fnv1a) { builder.AddInt(20, testhashs32Fnv1a, 0); }
|
||||
public static void AddTesthashu32Fnv1a(FlatBufferBuilder builder, uint testhashu32Fnv1a) { builder.AddUint(21, testhashu32Fnv1a, 0); }
|
||||
public static void AddTesthashs64Fnv1a(FlatBufferBuilder builder, long testhashs64Fnv1a) { builder.AddLong(22, testhashs64Fnv1a, 0); }
|
||||
public static void AddTesthashu64Fnv1a(FlatBufferBuilder builder, ulong testhashu64Fnv1a) { builder.AddUlong(23, testhashu64Fnv1a, 0); }
|
||||
public static int EndMonster(FlatBufferBuilder builder) {
|
||||
int o = builder.EndObject();
|
||||
builder.Required(o, 10); // name
|
||||
|
||||
@@ -207,7 +207,79 @@ func (rcv *Monster) Testempty(obj *Stat) *Stat {
|
||||
return nil
|
||||
}
|
||||
|
||||
func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(15) }
|
||||
func (rcv *Monster) Testbool() byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(34))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetByte(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs32Fnv1() int32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(36))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetInt32(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu32Fnv1() uint32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(38))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetUint32(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs64Fnv1() int64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(40))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetInt64(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu64Fnv1() uint64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(42))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetUint64(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs32Fnv1a() int32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(44))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetInt32(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu32Fnv1a() uint32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(46))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetUint32(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs64Fnv1a() int64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(48))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetInt64(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu64Fnv1a() uint64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(50))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetUint64(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(24) }
|
||||
func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) }
|
||||
func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { builder.PrependInt16Slot(1, mana, 150) }
|
||||
func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { builder.PrependInt16Slot(2, hp, 100) }
|
||||
@@ -232,4 +304,13 @@ func MonsterAddTestnestedflatbuffer(builder *flatbuffers.Builder, testnestedflat
|
||||
func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems, 1)
|
||||
}
|
||||
func MonsterAddTestempty(builder *flatbuffers.Builder, testempty flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(14, flatbuffers.UOffsetT(testempty), 0) }
|
||||
func MonsterAddTestbool(builder *flatbuffers.Builder, testbool byte) { builder.PrependByteSlot(15, testbool, 0) }
|
||||
func MonsterAddTesthashs32Fnv1(builder *flatbuffers.Builder, testhashs32Fnv1 int32) { builder.PrependInt32Slot(16, testhashs32Fnv1, 0) }
|
||||
func MonsterAddTesthashu32Fnv1(builder *flatbuffers.Builder, testhashu32Fnv1 uint32) { builder.PrependUint32Slot(17, testhashu32Fnv1, 0) }
|
||||
func MonsterAddTesthashs64Fnv1(builder *flatbuffers.Builder, testhashs64Fnv1 int64) { builder.PrependInt64Slot(18, testhashs64Fnv1, 0) }
|
||||
func MonsterAddTesthashu64Fnv1(builder *flatbuffers.Builder, testhashu64Fnv1 uint64) { builder.PrependUint64Slot(19, testhashu64Fnv1, 0) }
|
||||
func MonsterAddTesthashs32Fnv1a(builder *flatbuffers.Builder, testhashs32Fnv1a int32) { builder.PrependInt32Slot(20, testhashs32Fnv1a, 0) }
|
||||
func MonsterAddTesthashu32Fnv1a(builder *flatbuffers.Builder, testhashu32Fnv1a uint32) { builder.PrependUint32Slot(21, testhashu32Fnv1a, 0) }
|
||||
func MonsterAddTesthashs64Fnv1a(builder *flatbuffers.Builder, testhashs64Fnv1a int64) { builder.PrependInt64Slot(22, testhashs64Fnv1a, 0) }
|
||||
func MonsterAddTesthashu64Fnv1a(builder *flatbuffers.Builder, testhashu64Fnv1a uint64) { builder.PrependUint64Slot(23, testhashu64Fnv1a, 0) }
|
||||
func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() }
|
||||
|
||||
@@ -8,7 +8,8 @@ import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
public class Monster extends Table {
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Monster()).__init(_bb.getInt(_bb.position()) + _bb.position(), _bb); }
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb) { return getRootAsMonster(_bb, new Monster()); }
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb, Monster obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__init(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public static boolean MonsterBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONS"); }
|
||||
public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
|
||||
@@ -18,7 +19,7 @@ public class Monster extends Table {
|
||||
public short hp() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) : 100; }
|
||||
public String name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
|
||||
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
|
||||
public byte inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
|
||||
public int inventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer inventoryAsByteBuffer() { return __vector_as_bytebuffer(14, 1); }
|
||||
public byte color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 8; }
|
||||
@@ -27,25 +28,31 @@ public class Monster extends Table {
|
||||
public Test test4(int j) { return test4(new Test(), j); }
|
||||
public Test test4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__init(__vector(o) + j * 4, bb) : null; }
|
||||
public int test4Length() { int o = __offset(22); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer test4AsByteBuffer() { return __vector_as_bytebuffer(22, 4); }
|
||||
public String testarrayofstring(int j) { int o = __offset(24); return o != 0 ? __string(__vector(o) + j * 4) : null; }
|
||||
public int testarrayofstringLength() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer testarrayofstringAsByteBuffer() { return __vector_as_bytebuffer(24, 4); }
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
public Monster testarrayoftables(int j) { return testarrayoftables(new Monster(), j); }
|
||||
public Monster testarrayoftables(Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__init(__indirect(__vector(o) + j * 4), bb) : null; }
|
||||
public int testarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer testarrayoftablesAsByteBuffer() { return __vector_as_bytebuffer(26, 4); }
|
||||
public Monster enemy() { return enemy(new Monster()); }
|
||||
public Monster enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
public byte testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
|
||||
public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); }
|
||||
public Stat testempty() { return testempty(new Stat()); }
|
||||
public Stat testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
public boolean testbool() { int o = __offset(34); return o != 0 ? 0!=bb.get(o + bb_pos) : false; }
|
||||
public int testhashs32Fnv1() { int o = __offset(36); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
|
||||
public long testhashu32Fnv1() { int o = __offset(38); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; }
|
||||
public long testhashs64Fnv1() { int o = __offset(40); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
|
||||
public long testhashu64Fnv1() { int o = __offset(42); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
|
||||
public int testhashs32Fnv1a() { int o = __offset(44); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
|
||||
public long testhashu32Fnv1a() { int o = __offset(46); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; }
|
||||
public long testhashs64Fnv1a() { int o = __offset(48); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
|
||||
public long testhashu64Fnv1a() { int o = __offset(50); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
|
||||
|
||||
public static void startMonster(FlatBufferBuilder builder) { builder.startObject(15); }
|
||||
public static void startMonster(FlatBufferBuilder builder) { builder.startObject(24); }
|
||||
public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); }
|
||||
public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); }
|
||||
public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); }
|
||||
@@ -69,6 +76,15 @@ public class Monster extends Table {
|
||||
public static int createTestnestedflatbufferVector(FlatBufferBuilder builder, byte[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addByte(data[i]); return builder.endVector(); }
|
||||
public static void startTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
|
||||
public static void addTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.addOffset(14, testemptyOffset, 0); }
|
||||
public static void addTestbool(FlatBufferBuilder builder, boolean testbool) { builder.addBoolean(15, testbool, false); }
|
||||
public static void addTesthashs32Fnv1(FlatBufferBuilder builder, int testhashs32Fnv1) { builder.addInt(16, testhashs32Fnv1, 0); }
|
||||
public static void addTesthashu32Fnv1(FlatBufferBuilder builder, long testhashu32Fnv1) { builder.addInt(17, (int)(testhashu32Fnv1 & 0xFFFFFFFFL), 0); }
|
||||
public static void addTesthashs64Fnv1(FlatBufferBuilder builder, long testhashs64Fnv1) { builder.addLong(18, testhashs64Fnv1, 0); }
|
||||
public static void addTesthashu64Fnv1(FlatBufferBuilder builder, long testhashu64Fnv1) { builder.addLong(19, testhashu64Fnv1, 0); }
|
||||
public static void addTesthashs32Fnv1a(FlatBufferBuilder builder, int testhashs32Fnv1a) { builder.addInt(20, testhashs32Fnv1a, 0); }
|
||||
public static void addTesthashu32Fnv1a(FlatBufferBuilder builder, long testhashu32Fnv1a) { builder.addInt(21, (int)(testhashu32Fnv1a & 0xFFFFFFFFL), 0); }
|
||||
public static void addTesthashs64Fnv1a(FlatBufferBuilder builder, long testhashs64Fnv1a) { builder.addLong(22, testhashs64Fnv1a, 0); }
|
||||
public static void addTesthashu64Fnv1a(FlatBufferBuilder builder, long testhashu64Fnv1a) { builder.addLong(23, testhashu64Fnv1a, 0); }
|
||||
public static int endMonster(FlatBufferBuilder builder) {
|
||||
int o = builder.endObject();
|
||||
builder.required(o, 10); // name
|
||||
|
||||
@@ -6,24 +6,29 @@ namespace MyGame.Example
|
||||
using FlatBuffers;
|
||||
|
||||
public class Stat : Table {
|
||||
public static Stat GetRootAsStat(ByteBuffer _bb) { return (new Stat()).__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb); }
|
||||
public static Stat GetRootAsStat(ByteBuffer _bb) { return GetRootAsStat(_bb, new Stat()); }
|
||||
public static Stat GetRootAsStat(ByteBuffer _bb, Stat obj) { return (obj.__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public Stat __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
|
||||
public string Id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
|
||||
public long Val() { int o = __offset(6); return o != 0 ? bb.GetLong(o + bb_pos) : (long)0; }
|
||||
public ushort Count() { int o = __offset(8); return o != 0 ? bb.GetUshort(o + bb_pos) : (ushort)0; }
|
||||
|
||||
public static int CreateStat(FlatBufferBuilder builder,
|
||||
int id = 0,
|
||||
long val = 0) {
|
||||
builder.StartObject(2);
|
||||
long val = 0,
|
||||
ushort count = 0) {
|
||||
builder.StartObject(3);
|
||||
Stat.AddVal(builder, val);
|
||||
Stat.AddId(builder, id);
|
||||
Stat.AddCount(builder, count);
|
||||
return Stat.EndStat(builder);
|
||||
}
|
||||
|
||||
public static void StartStat(FlatBufferBuilder builder) { builder.StartObject(2); }
|
||||
public static void StartStat(FlatBufferBuilder builder) { builder.StartObject(3); }
|
||||
public static void AddId(FlatBufferBuilder builder, int idOffset) { builder.AddOffset(0, idOffset, 0); }
|
||||
public static void AddVal(FlatBufferBuilder builder, long val) { builder.AddLong(1, val, 0); }
|
||||
public static void AddCount(FlatBufferBuilder builder, ushort count) { builder.AddUshort(2, count, 0); }
|
||||
public static int EndStat(FlatBufferBuilder builder) {
|
||||
int o = builder.EndObject();
|
||||
return o;
|
||||
|
||||
@@ -30,7 +30,16 @@ func (rcv *Stat) Val() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func StatStart(builder *flatbuffers.Builder) { builder.StartObject(2) }
|
||||
func (rcv *Stat) Count() uint16 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(8))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetUint16(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func StatStart(builder *flatbuffers.Builder) { builder.StartObject(3) }
|
||||
func StatAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(id), 0) }
|
||||
func StatAddVal(builder *flatbuffers.Builder, val int64) { builder.PrependInt64Slot(1, val, 0) }
|
||||
func StatAddCount(builder *flatbuffers.Builder, count uint16) { builder.PrependUint16Slot(2, count, 0) }
|
||||
func StatEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() }
|
||||
|
||||
@@ -8,25 +8,30 @@ import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
public class Stat extends Table {
|
||||
public static Stat getRootAsStat(ByteBuffer _bb) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Stat()).__init(_bb.getInt(_bb.position()) + _bb.position(), _bb); }
|
||||
public static Stat getRootAsStat(ByteBuffer _bb) { return getRootAsStat(_bb, new Stat()); }
|
||||
public static Stat getRootAsStat(ByteBuffer _bb, Stat obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__init(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public Stat __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
|
||||
public String id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
|
||||
public ByteBuffer idAsByteBuffer() { return __vector_as_bytebuffer(4, 1); }
|
||||
public long val() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
|
||||
public int count() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; }
|
||||
|
||||
public static int createStat(FlatBufferBuilder builder,
|
||||
int id,
|
||||
long val) {
|
||||
builder.startObject(2);
|
||||
long val,
|
||||
int count) {
|
||||
builder.startObject(3);
|
||||
Stat.addVal(builder, val);
|
||||
Stat.addId(builder, id);
|
||||
Stat.addCount(builder, count);
|
||||
return Stat.endStat(builder);
|
||||
}
|
||||
|
||||
public static void startStat(FlatBufferBuilder builder) { builder.startObject(2); }
|
||||
public static void startStat(FlatBufferBuilder builder) { builder.startObject(3); }
|
||||
public static void addId(FlatBufferBuilder builder, int idOffset) { builder.addOffset(0, idOffset, 0); }
|
||||
public static void addVal(FlatBufferBuilder builder, long val) { builder.addLong(1, val, 0); }
|
||||
public static void addCount(FlatBufferBuilder builder, int count) { builder.addShort(2, (short)(count & 0xFFFF), 0); }
|
||||
public static int endStat(FlatBufferBuilder builder) {
|
||||
int o = builder.endObject();
|
||||
return o;
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2014 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.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -74,6 +90,9 @@ func TestAll(t *testing.T) {
|
||||
// some sanity checks:
|
||||
CheckDocExample(generated, off, t.Fatalf)
|
||||
|
||||
// Check Builder.CreateByteVector
|
||||
CheckCreateByteVector(t.Fatalf)
|
||||
|
||||
// If the filename of the FlatBuffers file generated by the Java test
|
||||
// is given, check that Go code can read it, and that Go code
|
||||
// generates an identical buffer when used to create the example data:
|
||||
@@ -1064,6 +1083,25 @@ func CheckDocExample(buf []byte, off flatbuffers.UOffsetT, fail func(string, ...
|
||||
_ = example.MonsterEnd(builder)
|
||||
}
|
||||
|
||||
func CheckCreateByteVector(fail func(string, ...interface{})) {
|
||||
raw := [30]byte{}
|
||||
for i := 0; i < len(raw); i++ {
|
||||
raw[i] = byte(i)
|
||||
}
|
||||
|
||||
for size := 0; size < len(raw); size++ {
|
||||
b1 := flatbuffers.NewBuilder(0)
|
||||
b2 := flatbuffers.NewBuilder(0)
|
||||
b1.StartVector(1, size, 1)
|
||||
for i := size - 1; i >= 0; i-- {
|
||||
b1.PrependByte(raw[i])
|
||||
}
|
||||
b1.EndVector(size)
|
||||
b2.CreateByteVector(raw[:size])
|
||||
CheckByteEquality(b1.Bytes, b2.Bytes, fail)
|
||||
}
|
||||
}
|
||||
|
||||
// Include simple random number generator to ensure results will be the
|
||||
// same cross platform.
|
||||
// http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator
|
||||
|
||||
@@ -4,6 +4,8 @@ include "include_test1.fbs";
|
||||
|
||||
namespace MyGame.Example;
|
||||
|
||||
attribute "priority";
|
||||
|
||||
enum Color:byte (bit_flags) { Red = 0, Green, Blue = 3, }
|
||||
|
||||
union Any { Monster } // TODO: add more elements
|
||||
@@ -22,13 +24,14 @@ struct Vec3 (force_align: 16) {
|
||||
table Stat {
|
||||
id:string;
|
||||
val:long;
|
||||
count:ushort;
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3 (id: 0);
|
||||
hp:short = 100 (id: 2);
|
||||
mana:short = 150 (id: 1);
|
||||
name:string (id: 3, required);
|
||||
name:string (id: 3, required, key);
|
||||
color:Color = Blue (id: 6);
|
||||
inventory:[ubyte] (id: 5);
|
||||
friendly:bool = false (deprecated, priority: 1, id: 4);
|
||||
@@ -41,6 +44,15 @@ table Monster {
|
||||
test4:[Test] (id: 9);
|
||||
testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster");
|
||||
testempty:Stat (id:14);
|
||||
testbool:bool (id:15);
|
||||
testhashs32_fnv1:int (id:16, hash:"fnv1_32");
|
||||
testhashu32_fnv1:uint (id:17, hash:"fnv1_32");
|
||||
testhashs64_fnv1:long (id:18, hash:"fnv1_64");
|
||||
testhashu64_fnv1:ulong (id:19, hash:"fnv1_64");
|
||||
testhashs32_fnv1a:int (id:20, hash:"fnv1a_32");
|
||||
testhashu32_fnv1a:uint (id:21, hash:"fnv1a_32");
|
||||
testhashs64_fnv1a:long (id:22, hash:"fnv1a_64");
|
||||
testhashu64_fnv1a:ulong (id:23, hash:"fnv1a_64");
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
|
||||
@@ -46,7 +46,7 @@ inline const char *EnumNameAny(Any e) { return EnumNamesAny()[e]; }
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type);
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(2) Test {
|
||||
MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
|
||||
private:
|
||||
int16_t a_;
|
||||
int8_t b_;
|
||||
@@ -61,7 +61,7 @@ MANUALLY_ALIGNED_STRUCT(2) Test {
|
||||
};
|
||||
STRUCT_END(Test, 4);
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(16) Vec3 {
|
||||
MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS {
|
||||
private:
|
||||
float x_;
|
||||
float y_;
|
||||
@@ -86,14 +86,16 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 {
|
||||
};
|
||||
STRUCT_END(Vec3, 32);
|
||||
|
||||
struct Stat : private flatbuffers::Table {
|
||||
struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
const flatbuffers::String *id() const { return GetPointer<const flatbuffers::String *>(4); }
|
||||
int64_t val() const { return GetField<int64_t>(6, 0); }
|
||||
uint16_t count() const { return GetField<uint16_t>(8, 0); }
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 4 /* id */) &&
|
||||
verifier.Verify(id()) &&
|
||||
VerifyField<int64_t>(verifier, 6 /* val */) &&
|
||||
VerifyField<uint16_t>(verifier, 8 /* count */) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
@@ -103,28 +105,33 @@ struct StatBuilder {
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_id(flatbuffers::Offset<flatbuffers::String> id) { fbb_.AddOffset(4, id); }
|
||||
void add_val(int64_t val) { fbb_.AddElement<int64_t>(6, val, 0); }
|
||||
void add_count(uint16_t count) { fbb_.AddElement<uint16_t>(8, count, 0); }
|
||||
StatBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
StatBuilder &operator=(const StatBuilder &);
|
||||
flatbuffers::Offset<Stat> Finish() {
|
||||
auto o = flatbuffers::Offset<Stat>(fbb_.EndTable(start_, 2));
|
||||
auto o = flatbuffers::Offset<Stat>(fbb_.EndTable(start_, 3));
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<Stat> CreateStat(flatbuffers::FlatBufferBuilder &_fbb,
|
||||
flatbuffers::Offset<flatbuffers::String> id = 0,
|
||||
int64_t val = 0) {
|
||||
int64_t val = 0,
|
||||
uint16_t count = 0) {
|
||||
StatBuilder builder_(_fbb);
|
||||
builder_.add_val(val);
|
||||
builder_.add_id(id);
|
||||
builder_.add_count(count);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct Monster : private flatbuffers::Table {
|
||||
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
const Vec3 *pos() const { return GetStruct<const Vec3 *>(4); }
|
||||
int16_t mana() const { return GetField<int16_t>(6, 150); }
|
||||
int16_t hp() const { return GetField<int16_t>(8, 100); }
|
||||
const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(10); }
|
||||
bool KeyCompareLessThan(const Monster *o) const { return *name() < *o->name(); }
|
||||
int KeyCompareWithValue(const char *val) const { return strcmp(name()->c_str(), val); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
Color color() const { return static_cast<Color>(GetField<int8_t>(16, 8)); }
|
||||
Any test_type() const { return static_cast<Any>(GetField<uint8_t>(18, 0)); }
|
||||
@@ -136,8 +143,17 @@ struct Monster : private flatbuffers::Table {
|
||||
const flatbuffers::Vector<flatbuffers::Offset<Monster>> *testarrayoftables() const { return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<Monster>> *>(26); }
|
||||
const Monster *enemy() const { return GetPointer<const Monster *>(28); }
|
||||
const flatbuffers::Vector<uint8_t> *testnestedflatbuffer() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(30); }
|
||||
const Monster *testnestedflatbuffer_nested_root() { return flatbuffers::GetRoot<Monster>(testnestedflatbuffer()->Data()); }
|
||||
const Monster *testnestedflatbuffer_nested_root() const { return flatbuffers::GetRoot<Monster>(testnestedflatbuffer()->Data()); }
|
||||
const Stat *testempty() const { return GetPointer<const Stat *>(32); }
|
||||
uint8_t testbool() const { return GetField<uint8_t>(34, 0); }
|
||||
int32_t testhashs32_fnv1() const { return GetField<int32_t>(36, 0); }
|
||||
uint32_t testhashu32_fnv1() const { return GetField<uint32_t>(38, 0); }
|
||||
int64_t testhashs64_fnv1() const { return GetField<int64_t>(40, 0); }
|
||||
uint64_t testhashu64_fnv1() const { return GetField<uint64_t>(42, 0); }
|
||||
int32_t testhashs32_fnv1a() const { return GetField<int32_t>(44, 0); }
|
||||
uint32_t testhashu32_fnv1a() const { return GetField<uint32_t>(46, 0); }
|
||||
int64_t testhashs64_fnv1a() const { return GetField<int64_t>(48, 0); }
|
||||
uint64_t testhashu64_fnv1a() const { return GetField<uint64_t>(50, 0); }
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<Vec3>(verifier, 4 /* pos */) &&
|
||||
@@ -165,6 +181,15 @@ struct Monster : private flatbuffers::Table {
|
||||
verifier.Verify(testnestedflatbuffer()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 32 /* testempty */) &&
|
||||
verifier.VerifyTable(testempty()) &&
|
||||
VerifyField<uint8_t>(verifier, 34 /* testbool */) &&
|
||||
VerifyField<int32_t>(verifier, 36 /* testhashs32_fnv1 */) &&
|
||||
VerifyField<uint32_t>(verifier, 38 /* testhashu32_fnv1 */) &&
|
||||
VerifyField<int64_t>(verifier, 40 /* testhashs64_fnv1 */) &&
|
||||
VerifyField<uint64_t>(verifier, 42 /* testhashu64_fnv1 */) &&
|
||||
VerifyField<int32_t>(verifier, 44 /* testhashs32_fnv1a */) &&
|
||||
VerifyField<uint32_t>(verifier, 46 /* testhashu32_fnv1a */) &&
|
||||
VerifyField<int64_t>(verifier, 48 /* testhashs64_fnv1a */) &&
|
||||
VerifyField<uint64_t>(verifier, 50 /* testhashu64_fnv1a */) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
@@ -186,10 +211,19 @@ struct MonsterBuilder {
|
||||
void add_enemy(flatbuffers::Offset<Monster> enemy) { fbb_.AddOffset(28, enemy); }
|
||||
void add_testnestedflatbuffer(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer) { fbb_.AddOffset(30, testnestedflatbuffer); }
|
||||
void add_testempty(flatbuffers::Offset<Stat> testempty) { fbb_.AddOffset(32, testempty); }
|
||||
void add_testbool(uint8_t testbool) { fbb_.AddElement<uint8_t>(34, testbool, 0); }
|
||||
void add_testhashs32_fnv1(int32_t testhashs32_fnv1) { fbb_.AddElement<int32_t>(36, testhashs32_fnv1, 0); }
|
||||
void add_testhashu32_fnv1(uint32_t testhashu32_fnv1) { fbb_.AddElement<uint32_t>(38, testhashu32_fnv1, 0); }
|
||||
void add_testhashs64_fnv1(int64_t testhashs64_fnv1) { fbb_.AddElement<int64_t>(40, testhashs64_fnv1, 0); }
|
||||
void add_testhashu64_fnv1(uint64_t testhashu64_fnv1) { fbb_.AddElement<uint64_t>(42, testhashu64_fnv1, 0); }
|
||||
void add_testhashs32_fnv1a(int32_t testhashs32_fnv1a) { fbb_.AddElement<int32_t>(44, testhashs32_fnv1a, 0); }
|
||||
void add_testhashu32_fnv1a(uint32_t testhashu32_fnv1a) { fbb_.AddElement<uint32_t>(46, testhashu32_fnv1a, 0); }
|
||||
void add_testhashs64_fnv1a(int64_t testhashs64_fnv1a) { fbb_.AddElement<int64_t>(48, testhashs64_fnv1a, 0); }
|
||||
void add_testhashu64_fnv1a(uint64_t testhashu64_fnv1a) { fbb_.AddElement<uint64_t>(50, testhashu64_fnv1a, 0); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
MonsterBuilder &operator=(const MonsterBuilder &);
|
||||
flatbuffers::Offset<Monster> Finish() {
|
||||
auto o = flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 15));
|
||||
auto o = flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 24));
|
||||
fbb_.Required(o, 10); // name
|
||||
return o;
|
||||
}
|
||||
@@ -209,8 +243,25 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Monster>>> testarrayoftables = 0,
|
||||
flatbuffers::Offset<Monster> enemy = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer = 0,
|
||||
flatbuffers::Offset<Stat> testempty = 0) {
|
||||
flatbuffers::Offset<Stat> testempty = 0,
|
||||
uint8_t testbool = 0,
|
||||
int32_t testhashs32_fnv1 = 0,
|
||||
uint32_t testhashu32_fnv1 = 0,
|
||||
int64_t testhashs64_fnv1 = 0,
|
||||
uint64_t testhashu64_fnv1 = 0,
|
||||
int32_t testhashs32_fnv1a = 0,
|
||||
uint32_t testhashu32_fnv1a = 0,
|
||||
int64_t testhashs64_fnv1a = 0,
|
||||
uint64_t testhashu64_fnv1a = 0) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_testhashu64_fnv1a(testhashu64_fnv1a);
|
||||
builder_.add_testhashs64_fnv1a(testhashs64_fnv1a);
|
||||
builder_.add_testhashu64_fnv1(testhashu64_fnv1);
|
||||
builder_.add_testhashs64_fnv1(testhashs64_fnv1);
|
||||
builder_.add_testhashu32_fnv1a(testhashu32_fnv1a);
|
||||
builder_.add_testhashs32_fnv1a(testhashs32_fnv1a);
|
||||
builder_.add_testhashu32_fnv1(testhashu32_fnv1);
|
||||
builder_.add_testhashs32_fnv1(testhashs32_fnv1);
|
||||
builder_.add_testempty(testempty);
|
||||
builder_.add_testnestedflatbuffer(testnestedflatbuffer);
|
||||
builder_.add_enemy(enemy);
|
||||
@@ -223,6 +274,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
builder_.add_pos(pos);
|
||||
builder_.add_hp(hp);
|
||||
builder_.add_mana(mana);
|
||||
builder_.add_testbool(testbool);
|
||||
builder_.add_test_type(test_type);
|
||||
builder_.add_color(color);
|
||||
return builder_.Finish();
|
||||
@@ -240,9 +292,11 @@ inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<
|
||||
|
||||
inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer<Monster>(); }
|
||||
|
||||
inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<Monster> root) { fbb.Finish(root, "MONS"); }
|
||||
inline const char *MonsterIdentifier() { return "MONS"; }
|
||||
|
||||
inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, "MONS"); }
|
||||
inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, MonsterIdentifier()); }
|
||||
|
||||
inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<Monster> root) { fbb.Finish(root, MonsterIdentifier()); }
|
||||
|
||||
} // namespace Example
|
||||
} // namespace MyGame
|
||||
|
||||
@@ -36,5 +36,13 @@
|
||||
testarrayofstring: [
|
||||
"test1",
|
||||
"test2"
|
||||
]
|
||||
],
|
||||
testhashs32_fnv1: -579221183,
|
||||
testhashu32_fnv1: 3715746113,
|
||||
testhashs64_fnv1: 7930699090847568257,
|
||||
testhashu64_fnv1: 7930699090847568257,
|
||||
testhashs32_fnv1a: -1904106383,
|
||||
testhashu32_fnv1a: 2390860913,
|
||||
testhashs64_fnv1a: 4898026182817603057,
|
||||
testhashu64_fnv1a: 4898026182817603057
|
||||
}
|
||||
|
||||
@@ -36,5 +36,13 @@
|
||||
testarrayofstring: [
|
||||
"test1",
|
||||
"test2"
|
||||
]
|
||||
],
|
||||
testhashs32_fnv1: "This string is being hashed!",
|
||||
testhashu32_fnv1: "This string is being hashed!",
|
||||
testhashs64_fnv1: "This string is being hashed!",
|
||||
testhashu64_fnv1: "This string is being hashed!",
|
||||
testhashs32_fnv1a: "This string is being hashed!",
|
||||
testhashu32_fnv1a: "This string is being hashed!",
|
||||
testhashs64_fnv1a: "This string is being hashed!",
|
||||
testhashu64_fnv1a: "This string is being hashed!",
|
||||
}
|
||||
|
||||
Binary file not shown.
121
tests/test.cpp
121
tests/test.cpp
@@ -60,7 +60,7 @@ uint32_t lcg_rand() {
|
||||
void lcg_reset() { lcg_seed = 48271; }
|
||||
|
||||
// example of how to build up a serialized buffer algorithmically:
|
||||
std::string CreateFlatBufferTest() {
|
||||
flatbuffers::unique_ptr_t CreateFlatBufferTest(std::string &buffer) {
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
|
||||
@@ -70,15 +70,30 @@ std::string CreateFlatBufferTest() {
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
auto inventory = builder.CreateVector(inv_data, 10);
|
||||
|
||||
// Alternatively, create the vector first, and fill in data later:
|
||||
// unsigned char *inv_buf = nullptr;
|
||||
// auto inventory = builder.CreateUninitializedVector<unsigned char>(
|
||||
// 10, &inv_buf);
|
||||
// memcpy(inv_buf, inv_data, 10);
|
||||
|
||||
Test tests[] = { Test(10, 20), Test(30, 40) };
|
||||
auto testv = builder.CreateVectorOfStructs(tests, 2);
|
||||
|
||||
// create monster with very few fields set:
|
||||
// (same functionality as CreateMonster below, but sets fields manually)
|
||||
flatbuffers::Offset<Monster> mlocs[3];
|
||||
auto fred = builder.CreateString("Fred");
|
||||
MonsterBuilder mb(builder);
|
||||
mb.add_name(fred);
|
||||
auto mloc2 = mb.Finish();
|
||||
auto barney = builder.CreateString("Barney");
|
||||
auto wilma = builder.CreateString("Wilma");
|
||||
MonsterBuilder mb1(builder);
|
||||
mb1.add_name(fred);
|
||||
mlocs[0] = mb1.Finish();
|
||||
MonsterBuilder mb2(builder);
|
||||
mb2.add_name(barney);
|
||||
mlocs[1] = mb2.Finish();
|
||||
MonsterBuilder mb3(builder);
|
||||
mb3.add_name(wilma);
|
||||
mlocs[2] = mb3.Finish();
|
||||
|
||||
// Create an array of strings:
|
||||
flatbuffers::Offset<flatbuffers::String> strings[2];
|
||||
@@ -86,12 +101,12 @@ std::string CreateFlatBufferTest() {
|
||||
strings[1] = builder.CreateString("fred");
|
||||
auto vecofstrings = builder.CreateVector(strings, 2);
|
||||
|
||||
// Create an array of tables:
|
||||
auto vecoftables = builder.CreateVector(&mloc2, 1);
|
||||
// Create an array of sorted tables, can be used with binary search when read:
|
||||
auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
|
||||
|
||||
// shortcut for creating monster with all fields set:
|
||||
auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
|
||||
Any_Monster, mloc2.Union(), // Store a union.
|
||||
Any_Monster, mlocs[1].Union(), // Store a union.
|
||||
testv, vecofstrings, vecoftables, 0);
|
||||
|
||||
FinishMonsterBuffer(builder, mloc);
|
||||
@@ -104,23 +119,25 @@ std::string CreateFlatBufferTest() {
|
||||
#endif
|
||||
|
||||
// return the buffer for the caller to use.
|
||||
return std::string(reinterpret_cast<const char *>(builder.GetBufferPointer()),
|
||||
builder.GetSize());
|
||||
auto bufferpointer =
|
||||
reinterpret_cast<const char *>(builder.GetBufferPointer());
|
||||
buffer.assign(bufferpointer, bufferpointer + builder.GetSize());
|
||||
|
||||
return builder.ReleaseBufferPointer();
|
||||
}
|
||||
|
||||
// example of accessing a buffer loaded in memory:
|
||||
void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
void AccessFlatBufferTest(const uint8_t *flatbuf, const std::size_t length) {
|
||||
|
||||
// First, verify the buffers integrity (optional)
|
||||
flatbuffers::Verifier verifier(
|
||||
reinterpret_cast<const uint8_t *>(flatbuf.c_str()),
|
||||
flatbuf.length());
|
||||
flatbuffers::Verifier verifier(flatbuf, length);
|
||||
TEST_EQ(VerifyMonsterBuffer(verifier), true);
|
||||
|
||||
TEST_EQ(MonsterBufferHasIdentifier(flatbuf.c_str()), true);
|
||||
TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
|
||||
TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
|
||||
|
||||
// Access the buffer from the root.
|
||||
auto monster = GetMonster(flatbuf.c_str());
|
||||
auto monster = GetMonster(flatbuf);
|
||||
|
||||
TEST_EQ(monster->hp(), 80);
|
||||
TEST_EQ(monster->mana(), 150); // default
|
||||
@@ -136,6 +153,7 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
TEST_EQ(pos->test3().b(), 20);
|
||||
|
||||
auto inventory = monster->inventory();
|
||||
TEST_EQ(VectorLength(inventory), 10UL); // Works even if inventory is null.
|
||||
TEST_NOTNULL(inventory);
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
for (auto it = inventory->begin(); it != inventory->end(); ++it)
|
||||
@@ -155,9 +173,15 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
|
||||
// Example of accessing a vector of tables:
|
||||
auto vecoftables = monster->testarrayoftables();
|
||||
TEST_EQ(vecoftables->Length(), 1U);
|
||||
TEST_EQ(vecoftables->Length(), 3U);
|
||||
for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
|
||||
TEST_EQ(strcmp(it->name()->c_str(), "Fred"), 0);
|
||||
TEST_EQ(strlen(it->name()->c_str()) >= 4, true);
|
||||
TEST_EQ(strcmp(vecoftables->Get(0)->name()->c_str(), "Barney"), 0);
|
||||
TEST_EQ(strcmp(vecoftables->Get(1)->name()->c_str(), "Fred"), 0);
|
||||
TEST_EQ(strcmp(vecoftables->Get(2)->name()->c_str(), "Wilma"), 0);
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Barney"));
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
|
||||
|
||||
// Since Flatbuffers uses explicit mechanisms to override the default
|
||||
// compiler alignment, double check that the compiler indeed obeys them:
|
||||
@@ -226,7 +250,7 @@ void ParseProtoTest() {
|
||||
"tests/prototest/test.golden", false, &goldenfile), true);
|
||||
|
||||
// Parse proto.
|
||||
flatbuffers::Parser parser(true);
|
||||
flatbuffers::Parser parser(false, true);
|
||||
TEST_EQ(parser.Parse(protofile.c_str(), nullptr), true);
|
||||
|
||||
// Generate fbs.
|
||||
@@ -342,6 +366,8 @@ void FuzzTest2() {
|
||||
const int num_struct_definitions = 5; // Subset of num_definitions.
|
||||
const int fields_per_definition = 15;
|
||||
const int instances_per_definition = 5;
|
||||
const int deprecation_rate = 10; // 1 in deprecation_rate fields will
|
||||
// be deprecated.
|
||||
|
||||
std::string schema = "namespace test;\n\n";
|
||||
|
||||
@@ -380,24 +406,41 @@ void FuzzTest2() {
|
||||
"{\n");
|
||||
|
||||
for (int field = 0; field < fields_per_definition; field++) {
|
||||
const bool is_last_field = field == fields_per_definition - 1;
|
||||
|
||||
// Deprecate 1 in deprecation_rate fields. Only table fields can be
|
||||
// deprecated.
|
||||
// Don't deprecate the last field to avoid dangling commas in JSON.
|
||||
const bool deprecated = !is_struct &&
|
||||
!is_last_field &&
|
||||
(lcg_rand() % deprecation_rate == 0);
|
||||
|
||||
std::string field_name = "f" + flatbuffers::NumToString(field);
|
||||
AddToSchemaAndInstances((" " + field_name + ":").c_str(),
|
||||
(field_name + ": ").c_str());
|
||||
deprecated ? "" : (field_name + ": ").c_str());
|
||||
// Pick random type:
|
||||
int base_type = lcg_rand() % (flatbuffers::BASE_TYPE_UNION + 1);
|
||||
switch (base_type) {
|
||||
case flatbuffers::BASE_TYPE_STRING:
|
||||
if (is_struct) {
|
||||
Dummy(); // No strings in structs,
|
||||
Dummy(); // No strings in structs.
|
||||
} else {
|
||||
AddToSchemaAndInstances("string", "\"hi\"");
|
||||
AddToSchemaAndInstances("string", deprecated ? "" : "\"hi\"");
|
||||
}
|
||||
break;
|
||||
case flatbuffers::BASE_TYPE_VECTOR:
|
||||
if (is_struct) {
|
||||
Dummy(); // No vectors in structs.
|
||||
}
|
||||
else {
|
||||
AddToSchemaAndInstances("[ubyte]",
|
||||
deprecated ? "" : "[\n0,\n1,\n255\n]");
|
||||
}
|
||||
break;
|
||||
case flatbuffers::BASE_TYPE_NONE:
|
||||
case flatbuffers::BASE_TYPE_UTYPE:
|
||||
case flatbuffers::BASE_TYPE_STRUCT:
|
||||
case flatbuffers::BASE_TYPE_UNION:
|
||||
case flatbuffers::BASE_TYPE_VECTOR:
|
||||
if (definition) {
|
||||
// Pick a random previous definition and random data instance of
|
||||
// that definition.
|
||||
@@ -405,7 +448,9 @@ void FuzzTest2() {
|
||||
int instance = lcg_rand() % instances_per_definition;
|
||||
AddToSchemaAndInstances(
|
||||
("D" + flatbuffers::NumToString(defref)).c_str(),
|
||||
definitions[defref].instances[instance].c_str());
|
||||
deprecated
|
||||
? ""
|
||||
: definitions[defref].instances[instance].c_str());
|
||||
} else {
|
||||
// If this is the first definition, we have no definition we can
|
||||
// refer to.
|
||||
@@ -414,13 +459,18 @@ void FuzzTest2() {
|
||||
break;
|
||||
default:
|
||||
// All the scalar types.
|
||||
AddToSchemaAndInstances(
|
||||
flatbuffers::kTypeNames[base_type],
|
||||
flatbuffers::NumToString(lcg_rand() % 128).c_str());
|
||||
schema += flatbuffers::kTypeNames[base_type];
|
||||
|
||||
if (!deprecated) {
|
||||
// We want each instance to use its own random value.
|
||||
for (int inst = 0; inst < instances_per_definition; inst++)
|
||||
definitions[definition].instances[inst] +=
|
||||
flatbuffers::NumToString(lcg_rand() % 128).c_str();
|
||||
}
|
||||
}
|
||||
AddToSchemaAndInstances(
|
||||
";\n",
|
||||
field == fields_per_definition - 1 ? "\n" : ",\n");
|
||||
deprecated ? "(deprecated);\n" : ";\n",
|
||||
deprecated ? "" : is_last_field ? "\n" : ",\n");
|
||||
}
|
||||
AddToSchemaAndInstances("}\n\n", "}");
|
||||
}
|
||||
@@ -470,8 +520,9 @@ void FuzzTest2() {
|
||||
}
|
||||
|
||||
// Test that parser errors are actually generated.
|
||||
void TestError(const char *src, const char *error_substr) {
|
||||
flatbuffers::Parser parser;
|
||||
void TestError(const char *src, const char *error_substr,
|
||||
bool strict_json = false) {
|
||||
flatbuffers::Parser parser(strict_json);
|
||||
TEST_EQ(parser.Parse(src), false); // Must signal error
|
||||
// Must be the error we're expecting
|
||||
TEST_NOTNULL(strstr(parser.error_.c_str(), error_substr));
|
||||
@@ -499,6 +550,9 @@ void ErrorTest() {
|
||||
TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {",
|
||||
"type id");
|
||||
TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
|
||||
TestError("table X { Y:int; } root_type X; { Y:", "string constant", true);
|
||||
TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant",
|
||||
true);
|
||||
TestError("struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
|
||||
"{ V:{ Y:1 } }", "incomplete");
|
||||
TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
|
||||
@@ -566,8 +620,11 @@ void UnicodeTest() {
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
// Run our various test suites:
|
||||
|
||||
auto flatbuf = CreateFlatBufferTest();
|
||||
AccessFlatBufferTest(flatbuf);
|
||||
std::string rawbuf;
|
||||
auto flatbuf = CreateFlatBufferTest(rawbuf);
|
||||
AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
|
||||
rawbuf.length());
|
||||
AccessFlatBufferTest(flatbuf.get(), rawbuf.length());
|
||||
|
||||
#ifndef __ANDROID__ // requires file access
|
||||
ParseAndGenerateTextTest();
|
||||
|
||||
Reference in New Issue
Block a user