Initial commit of the FlatBuffers code.
Change-Id: I4c9f0f722490b374257adb3fec63e44ae93da920 Tested: using VS2010 / Xcode / gcc on Linux.
1
.gitattributes
vendored
Executable file
@@ -0,0 +1 @@
|
||||
* text=auto
|
||||
36
.gitignore
vendored
Executable file
@@ -0,0 +1,36 @@
|
||||
*_wire.txt
|
||||
*_wire.bin
|
||||
.DS_Store
|
||||
*.o
|
||||
*.o.d
|
||||
*.class
|
||||
*.a
|
||||
*~
|
||||
*.vcxproj
|
||||
*.vcxproj.filters
|
||||
*.vcxproj.user
|
||||
*.sln
|
||||
*.suo
|
||||
*.keystore
|
||||
**/bin/**
|
||||
**/gen/**
|
||||
**/libs/**
|
||||
**/obj/**
|
||||
**/*.dir/**
|
||||
**/CMakeFiles/**
|
||||
**/cmake_install.cmake
|
||||
**/CMakeCache.txt
|
||||
**/Debug/**
|
||||
**/Release/**
|
||||
build.xml
|
||||
local.properties
|
||||
project.properties
|
||||
proguard-project.txt
|
||||
linklint_results
|
||||
Makefile
|
||||
flatc
|
||||
flattests
|
||||
flatsamplebinary
|
||||
flatsampletext
|
||||
snapshot.sh
|
||||
|
||||
76
CMakeLists.txt
Normal file
@@ -0,0 +1,76 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
project(FlatBuffers)
|
||||
|
||||
# NOTE: Code coverage only works on Linux & OSX.
|
||||
option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
|
||||
|
||||
set(FlatBuffers_Compiler_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/idl.h
|
||||
include/flatbuffers/util.h
|
||||
src/idl_parser.cpp
|
||||
src/idl_gen_cpp.cpp
|
||||
src/idl_gen_java.cpp
|
||||
src/idl_gen_text.cpp
|
||||
src/flatc.cpp
|
||||
)
|
||||
|
||||
set(FlatBuffers_Tests_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/idl.h
|
||||
include/flatbuffers/util.h
|
||||
src/idl_parser.cpp
|
||||
src/idl_gen_text.cpp
|
||||
tests/test.cpp
|
||||
# file generate by running compiler on tests/monster_test.fbs
|
||||
tests/monster_test_generated.h
|
||||
)
|
||||
|
||||
set(FlatBuffers_Sample_Binary_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
samples/sample_binary.cpp
|
||||
# file generate by running compiler on samples/monster.fbs
|
||||
samples/monster_generated.h
|
||||
)
|
||||
|
||||
set(FlatBuffers_Sample_Text_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/idl.h
|
||||
include/flatbuffers/util.h
|
||||
src/idl_parser.cpp
|
||||
src/idl_gen_text.cpp
|
||||
samples/sample_text.cpp
|
||||
# file generate by running compiler on samples/monster.fbs
|
||||
samples/monster_generated.h
|
||||
)
|
||||
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
|
||||
# source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
|
||||
# source_group(Tests FILES ${FlatBuffers_Tests_SRCS})
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
add_definitions("-std=c++0x")
|
||||
add_definitions("-Wall")
|
||||
endif()
|
||||
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
||||
add_definitions("-std=c++0x")
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_CODE_COVERAGE)
|
||||
add_definitions("-g -fprofile-arcs -ftest-coverage")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||
endif()
|
||||
|
||||
include_directories(include)
|
||||
|
||||
add_executable(flatc ${FlatBuffers_Compiler_SRCS})
|
||||
add_executable(flattests ${FlatBuffers_Tests_SRCS})
|
||||
add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
|
||||
add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
|
||||
|
||||
add_test(NAME flattest
|
||||
CONFIGURATIONS Debug
|
||||
WORKING_DIRECTORY tests
|
||||
COMMAND flattests)
|
||||
202
LICENSE.txt
Executable file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
20
android/.project
Executable file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- 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.
|
||||
-->
|
||||
<projectDescription>
|
||||
<name>FlatBufferTest</name>
|
||||
</projectDescription>
|
||||
48
android/AndroidManifest.xml
Executable file
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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.
|
||||
-->
|
||||
<!-- BEGIN_INCLUDE(manifest) -->
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.FlatBufferTest"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
|
||||
<uses-feature android:glEsVersion="0x00020000"></uses-feature>
|
||||
<!-- This is the platform API where NativeActivity was introduced. -->
|
||||
<uses-sdk android:minSdkVersion="9" />
|
||||
|
||||
<!-- This .apk has no Java code itself, so set hasCode to false. -->
|
||||
<application android:label="@string/app_name" android:hasCode="false">
|
||||
|
||||
<!-- Our activity is the built-in NativeActivity framework class.
|
||||
This will take care of integrating with our NDK code. -->
|
||||
<activity android:name="android.app.NativeActivity"
|
||||
android:label="@string/app_name"
|
||||
android:configChanges="orientation|keyboardHidden"
|
||||
android:screenOrientation="landscape">
|
||||
<!-- Tell NativeActivity the name of or .so -->
|
||||
<meta-data android:name="android.app.lib_name"
|
||||
android:value="FlatBufferTest" />
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
<!-- END_INCLUDE(manifest) -->
|
||||
500
android/build_apk.sh
Executable file
@@ -0,0 +1,500 @@
|
||||
#!/bin/bash -eu
|
||||
# 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.
|
||||
#
|
||||
# Build, deploy, debug / execute a native Android package based upon
|
||||
# NativeActivity.
|
||||
|
||||
declare -r script_directory=$(dirname $0)
|
||||
declare -r android_root=${script_directory}/../../../../../../
|
||||
declare -r script_name=$(basename $0)
|
||||
declare -r android_manifest=AndroidManifest.xml
|
||||
declare -r os_name=$(uname -s)
|
||||
|
||||
# Minimum Android target version supported by this project.
|
||||
: ${BUILDAPK_ANDROID_TARGET_MINVERSION:=10}
|
||||
# Directory containing the Android SDK
|
||||
# (http://developer.android.com/sdk/index.html).
|
||||
: ${ANDROID_SDK_HOME:=}
|
||||
# Directory containing the Android NDK
|
||||
# (http://developer.android.com/tools/sdk/ndk/index.html).
|
||||
: ${NDK_HOME:=}
|
||||
|
||||
# Display script help and exit.
|
||||
usage() {
|
||||
echo "
|
||||
Build the Android package in the current directory and deploy it to a
|
||||
connected device.
|
||||
|
||||
Usage: ${script_name} \\
|
||||
[ADB_DEVICE=serial_number] [BUILD=0] [DEPLOY=0] [RUN_DEBUGGER=1] \
|
||||
[LAUNCH=0] [SWIG_BIN=swig_binary_directory] [SWIG_LIB=swig_include_directory] [ndk-build arguments ...]
|
||||
|
||||
ADB_DEVICE=serial_number:
|
||||
serial_number specifies the device to deploy the built apk to if multiple
|
||||
Android devices are connected to the host.
|
||||
BUILD=0:
|
||||
Disables the build of the package.
|
||||
DEPLOY=0:
|
||||
Disables the deployment of the built apk to the Android device.
|
||||
RUN_DEBUGGER=1:
|
||||
Launches the application in gdb after it has been deployed. To debug in
|
||||
gdb, NDK_DEBUG=1 must also be specified on the command line to build a
|
||||
debug apk.
|
||||
LAUNCH=0:
|
||||
Disable the launch of the apk on the Android device.
|
||||
SWIG_BIN=swig_binary_directory:
|
||||
The directory where the SWIG binary lives. No need to set this if SWIG is
|
||||
installed and point to from your PATH variable.
|
||||
SWIG_LIB=swig_include_directory:
|
||||
The directory where SWIG shared include files are, usually obtainable from
|
||||
commandline with \"swig -swiglib\". No need to set this if SWIG is installed
|
||||
and point to from your PATH variable.
|
||||
ndk-build arguments...:
|
||||
Additional arguments for ndk-build. See ndk-build -h for more information.
|
||||
" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Get the number of CPU cores present on the host.
|
||||
get_number_of_cores() {
|
||||
case ${os_name} in
|
||||
Darwin)
|
||||
sysctl hw.ncpu | awk '{ print $2 }'
|
||||
;;
|
||||
CYGWIN*|Linux)
|
||||
awk '/^processor/ { n=$3 } END { print n + 1 }' /proc/cpuinfo
|
||||
;;
|
||||
*)
|
||||
echo 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Get the package name from an AndroidManifest.xml file.
|
||||
get_package_name_from_manifest() {
|
||||
xmllint --xpath 'string(/manifest/@package)' "${1}"
|
||||
}
|
||||
|
||||
# Get the library name from an AndroidManifest.xml file.
|
||||
get_library_name_from_manifest() {
|
||||
echo "\
|
||||
setns android=http://schemas.android.com/apk/res/android
|
||||
xpath string(/manifest/application/activity\
|
||||
[@android:name=\"android.app.NativeActivity\"]/meta-data\
|
||||
[@android:name=\"android.app.lib_name\"]/@android:value)" |
|
||||
xmllint --shell "${1}" | awk '/Object is a string/ { print $NF }'
|
||||
}
|
||||
|
||||
# Get the number of Android devices connected to the system.
|
||||
get_number_of_devices_connected() {
|
||||
adb devices -l | \
|
||||
awk '/^..*$/ { if (p) { print $0 } }
|
||||
/List of devices attached/ { p = 1 }' | \
|
||||
wc -l
|
||||
return ${PIPESTATUS[0]}
|
||||
}
|
||||
|
||||
# Kill a process and its' children. This is provided for cygwin which
|
||||
# doesn't ship with pkill.
|
||||
kill_process_group() {
|
||||
local parent_pid="${1}"
|
||||
local child_pid=
|
||||
for child_pid in $(ps -f | \
|
||||
awk '{ if ($3 == '"${parent_pid}"') { print $2 } }'); do
|
||||
kill_process_group "${child_pid}"
|
||||
done
|
||||
kill "${parent_pid}" 2>/dev/null
|
||||
}
|
||||
|
||||
# Find and run "adb".
|
||||
adb() {
|
||||
local adb_path=
|
||||
for path in "$(which adb 2>/dev/null)" \
|
||||
"${ANDROID_SDK_HOME}/sdk/platform-tools/adb" \
|
||||
"${android_root}/prebuilts/sdk/platform-tools/adb"; do
|
||||
if [[ -e "${path}" ]]; then
|
||||
adb_path="${path}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ "${adb_path}" == "" ]]; then
|
||||
echo -e "Unable to find adb." \
|
||||
"\nAdd the Android ADT sdk/platform-tools directory to the" \
|
||||
"PATH." >&2
|
||||
exit 1
|
||||
fi
|
||||
"${adb_path}" "$@"
|
||||
}
|
||||
|
||||
# Find and run "android".
|
||||
android() {
|
||||
local android_executable=android
|
||||
if echo "${os_name}" | grep -q CYGWIN; then
|
||||
android_executable=android.bat
|
||||
fi
|
||||
local android_path=
|
||||
for path in "$(which ${android_executable})" \
|
||||
"${ANDROID_SDK_HOME}/sdk/tools/${android_executable}" \
|
||||
"${android_root}/prebuilts/sdk/tools/${android_executable}"; do
|
||||
if [[ -e "${path}" ]]; then
|
||||
android_path="${path}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ "${android_path}" == "" ]]; then
|
||||
echo -e "Unable to find android tool." \
|
||||
"\nAdd the Android ADT sdk/tools directory to the PATH." >&2
|
||||
exit 1
|
||||
fi
|
||||
# Make sure ant is installed.
|
||||
if [[ "$(which ant)" == "" ]]; then
|
||||
echo -e "Unable to find ant." \
|
||||
"\nPlease install ant and add to the PATH." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
"${android_path}" "$@"
|
||||
}
|
||||
|
||||
# Find and run "ndk-build"
|
||||
ndkbuild() {
|
||||
local ndkbuild_path=
|
||||
for path in "$(which ndk-build 2>/dev/null)" \
|
||||
"${NDK_HOME}/ndk-build" \
|
||||
"${android_root}/prebuilts/ndk/current/ndk-build"; do
|
||||
if [[ -e "${path}" ]]; then
|
||||
ndkbuild_path="${path}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ "${ndkbuild_path}" == "" ]]; then
|
||||
echo -e "Unable to find ndk-build." \
|
||||
"\nAdd the Android NDK directory to the PATH." >&2
|
||||
exit 1
|
||||
fi
|
||||
"${ndkbuild_path}" "$@"
|
||||
}
|
||||
|
||||
# Get file modification time of $1 in seconds since the epoch.
|
||||
stat_mtime() {
|
||||
local filename="${1}"
|
||||
case ${os_name} in
|
||||
Darwin) stat -f%m "${filename}" 2>/dev/null || echo 0 ;;
|
||||
*) stat -c%Y "${filename}" 2>/dev/null || echo 0 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Build the native (C/C++) build targets in the current directory.
|
||||
build_native_targets() {
|
||||
# Save the list of output modules in the install directory so that it's
|
||||
# possible to restore their timestamps after the build is complete. This
|
||||
# works around a bug in ndk/build/core/setup-app.mk which results in the
|
||||
# unconditional execution of the clean-installed-binaries rule.
|
||||
restore_libraries="$(find libs -type f 2>/dev/null | \
|
||||
sed -E 's@^libs/(.*)@\1@')"
|
||||
|
||||
# Build native code.
|
||||
ndkbuild -j$(get_number_of_cores) "$@"
|
||||
|
||||
# Restore installed libraries.
|
||||
# Obviously this is a nasty hack (along with ${restore_libraries} above) as
|
||||
# it assumes it knows where the NDK will be placing output files.
|
||||
(
|
||||
IFS=$'\n'
|
||||
for libpath in ${restore_libraries}; do
|
||||
source_library="obj/local/${libpath}"
|
||||
target_library="libs/${libpath}"
|
||||
if [[ -e "${source_library}" ]]; then
|
||||
cp -a "${source_library}" "${target_library}"
|
||||
fi
|
||||
done
|
||||
)
|
||||
}
|
||||
|
||||
# Select the oldest installed android build target that is at least as new as
|
||||
# BUILDAPK_ANDROID_TARGET_MINVERSION. If a suitable build target isn't found,
|
||||
# this function prints an error message and exits with an error.
|
||||
select_android_build_target() {
|
||||
local -r android_targets_installed=$( \
|
||||
android list targets | \
|
||||
awk -F'"' '/^id:.*android/ { print $2 }')
|
||||
local android_build_target=
|
||||
for android_target in $(echo "${android_targets_installed}" | \
|
||||
awk -F- '{ print $2 }' | sort -n); do
|
||||
if [[ $((android_target)) -ge \
|
||||
$((BUILDAPK_ANDROID_TARGET_MINVERSION)) ]]; then
|
||||
android_build_target="android-${android_target}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ "${android_build_target}" == "" ]]; then
|
||||
echo -e \
|
||||
"Found installed Android targets:" \
|
||||
"$(echo ${android_targets_installed} | sed 's/ /\n /g;s/^/\n /;')" \
|
||||
"\nAndroid SDK platform" \
|
||||
"android-$((BUILDAPK_ANDROID_TARGET_MINVERSION))" \
|
||||
"must be installed to build this project." \
|
||||
"\nUse the \"android\" application to install API" \
|
||||
"$((BUILDAPK_ANDROID_TARGET_MINVERSION)) or newer." >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "${android_build_target}"
|
||||
}
|
||||
|
||||
# Sign unsigned apk $1 and write the result to $2 with key store file $3 and
|
||||
# password $4.
|
||||
# If a key store file $3 and password $4 aren't specified, a temporary
|
||||
# (60 day) key is generated and used to sign the package.
|
||||
sign_apk() {
|
||||
local unsigned_apk="${1}"
|
||||
local signed_apk="${2}"
|
||||
if [[ $(stat_mtime "${unsigned_apk}") -gt \
|
||||
$(stat_mtime "${signed_apk}") ]]; then
|
||||
local -r key_alias=$(basename ${signed_apk} .apk)
|
||||
local keystore="${3}"
|
||||
local key_password="${4}"
|
||||
[[ "${keystore}" == "" ]] && keystore="${unsigned_apk}.keystore"
|
||||
[[ "${key_password}" == "" ]] && \
|
||||
key_password="${key_alias}123456"
|
||||
if [[ ! -e ${keystore} ]]; then
|
||||
keytool -genkey -v -dname "cn=, ou=${key_alias}, o=fpl" \
|
||||
-storepass ${key_password} \
|
||||
-keypass ${key_password} -keystore ${keystore} \
|
||||
-alias ${key_alias} -keyalg RSA -keysize 2048 -validity 60
|
||||
fi
|
||||
cp "${unsigned_apk}" "${signed_apk}"
|
||||
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 \
|
||||
-keystore ${keystore} -storepass ${key_password} \
|
||||
-keypass ${key_password} "${signed_apk}" ${key_alias}
|
||||
fi
|
||||
}
|
||||
|
||||
# Build the apk $1 for package filename $2 in the current directory using the
|
||||
# ant build target $3.
|
||||
build_apk() {
|
||||
local -r output_apk="${1}"
|
||||
local -r package_filename="${2}"
|
||||
local -r ant_target="${3}"
|
||||
# Get the list of installed android targets and select the oldest target
|
||||
# that is at least as new as BUILDAPK_ANDROID_TARGET_MINVERSION.
|
||||
local -r android_build_target=$(select_android_build_target)
|
||||
[[ "${android_build_target}" == "" ]] && exit 1
|
||||
echo "Building ${output_apk} for target ${android_build_target}" >&2
|
||||
|
||||
# Create / update build.xml and local.properties files.
|
||||
if [[ $(stat_mtime "${android_manifest}") -gt \
|
||||
$(stat_mtime build.xml) ]]; then
|
||||
android update project --target "${android_build_target}" \
|
||||
-n ${package_filename} --path .
|
||||
fi
|
||||
|
||||
# Use ant to build the apk.
|
||||
ant -quiet ${ant_target}
|
||||
|
||||
# Sign release apks with a temporary key as these packages will not be
|
||||
# redistributed.
|
||||
local unsigned_apk="bin/${package_filename}-${ant_target}-unsigned.apk"
|
||||
if [[ "${ant_target}" == "release" ]]; then
|
||||
sign_apk "${unsigned_apk}" "${output_apk}" "" ""
|
||||
fi
|
||||
}
|
||||
|
||||
# Uninstall package $1 and install apk $2 on device $3 where $3 is "-s device"
|
||||
# or an empty string. If $3 is an empty string adb will fail when multiple
|
||||
# devices are connected to the host system.
|
||||
install_apk() {
|
||||
local -r uninstall_package_name="${1}"
|
||||
local -r install_apk="${2}"
|
||||
local -r adb_device="${3}"
|
||||
# Uninstall the package if it's already installed.
|
||||
adb ${adb_device} uninstall "${uninstall_package_name}" 1>&2 > /dev/null || \
|
||||
true # no error check
|
||||
|
||||
# Install the apk.
|
||||
# NOTE: The following works around adb not returning an error code when
|
||||
# it fails to install an apk.
|
||||
echo "Install ${install_apk}" >&2
|
||||
local -r adb_install_result=$(adb ${adb_device} install "${install_apk}")
|
||||
echo "${adb_install_result}"
|
||||
if echo "${adb_install_result}" | grep -qF 'Failure ['; then
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Launch previously installed package $1 on device $2.
|
||||
# If $2 is an empty string adb will fail when multiple devices are connected
|
||||
# to the host system.
|
||||
launch_package() {
|
||||
(
|
||||
# Determine the SDK version of Android on the device.
|
||||
local -r android_sdk_version=$(
|
||||
adb ${adb_device} shell cat system/build.prop | \
|
||||
awk -F= '/ro.build.version.sdk/ {
|
||||
v=$2; sub(/[ \r\n]/, "", v); print v
|
||||
}')
|
||||
|
||||
# Clear logs from previous runs.
|
||||
# Note that logcat does not just 'tail' the logs, it dumps the entire log
|
||||
# history.
|
||||
adb ${adb_device} logcat -c
|
||||
|
||||
local finished_msg='Displayed '"${package_name}"
|
||||
local timeout_msg='Activity destroy timeout.*'"${package_name}"
|
||||
# Maximum time to wait before stopping log monitoring. 0 = infinity.
|
||||
local launch_timeout=0
|
||||
# If this is a Gingerbread device, kill log monitoring after 10 seconds.
|
||||
if [[ $((android_sdk_version)) -le 10 ]]; then
|
||||
launch_timeout=10
|
||||
fi
|
||||
# Display logcat in the background.
|
||||
# Stop displaying the log when the app launch / execution completes or the
|
||||
# logcat
|
||||
(
|
||||
adb ${adb_device} logcat | \
|
||||
awk "
|
||||
{
|
||||
print \$0
|
||||
}
|
||||
|
||||
/ActivityManager.*: ${finished_msg}/ {
|
||||
exit 0
|
||||
}
|
||||
|
||||
/ActivityManager.*: ${timeout_msg}/ {
|
||||
exit 0
|
||||
}" &
|
||||
adb_logcat_pid=$!;
|
||||
if [[ $((launch_timeout)) -gt 0 ]]; then
|
||||
sleep $((launch_timeout));
|
||||
kill ${adb_logcat_pid};
|
||||
else
|
||||
wait ${adb_logcat_pid};
|
||||
fi
|
||||
) &
|
||||
logcat_pid=$!
|
||||
# Kill adb logcat if this shell exits.
|
||||
trap "kill_process_group ${logcat_pid}" SIGINT SIGTERM EXIT
|
||||
|
||||
# If the SDK is newer than 10, "am" supports stopping an activity.
|
||||
adb_stop_activity=
|
||||
if [[ $((android_sdk_version)) -gt 10 ]]; then
|
||||
adb_stop_activity=-S
|
||||
fi
|
||||
|
||||
# Launch the activity and wait for it to complete.
|
||||
adb ${adb_device} shell am start ${adb_stop_activity} -n \
|
||||
${package_name}/android.app.NativeActivity
|
||||
|
||||
wait "${logcat_pid}"
|
||||
)
|
||||
}
|
||||
|
||||
# See usage().
|
||||
main() {
|
||||
# Parse arguments for this script.
|
||||
local adb_device=
|
||||
local ant_target=release
|
||||
local disable_deploy=0
|
||||
local disable_build=0
|
||||
local run_debugger=0
|
||||
local launch=1
|
||||
local build_package=1
|
||||
for opt; do
|
||||
case ${opt} in
|
||||
NDK_DEBUG=1) 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 ;;
|
||||
-h|--help|help) usage ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# If a target device hasn't been specified and multiple devices are connected
|
||||
# to the host machine, display an error.
|
||||
local -r devices_connected=$(get_number_of_devices_connected)
|
||||
if [[ "${adb_device}" == "" && $((devices_connected)) -gt 1 && \
|
||||
($((disable_deploy)) -eq 0 || $((launch)) -ne 0 || \
|
||||
$((run_debugger)) -ne 0) ]]; then
|
||||
if [[ $((disable_deploy)) -ne 0 ]]; then
|
||||
echo "Deployment enabled, disable using DEPLOY=0" >&2
|
||||
fi
|
||||
if [[ $((launch)) -ne 0 ]]; then
|
||||
echo "Launch enabled." >&2
|
||||
fi
|
||||
if [[ $((disable_deploy)) -eq 0 ]]; then
|
||||
echo "Deployment enabled." >&2
|
||||
fi
|
||||
if [[ $((run_debugger)) -ne 0 ]]; then
|
||||
echo "Debugger launch enabled." >&2
|
||||
fi
|
||||
echo "
|
||||
Multiple Android devices are connected to this host. Either disable deployment
|
||||
and execution of the built .apk using:
|
||||
\"${script_name} DEPLOY=0 LAUNCH=0\"
|
||||
|
||||
or specify a device to deploy to using:
|
||||
\"${script_name} ADB_DEVICE=\${device_serial}\".
|
||||
|
||||
The Android devices connected to this machine are:
|
||||
$(adb devices -l)
|
||||
" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ $((disable_build)) -eq 0 ]]; then
|
||||
# Build the native target.
|
||||
build_native_targets "$@"
|
||||
fi
|
||||
|
||||
# Get the package name from the manifest.
|
||||
local -r package_name=$(get_package_name_from_manifest "${android_manifest}")
|
||||
if [[ "${package_name}" == "" ]]; then
|
||||
echo -e "No package name specified in ${android_manifest},"\
|
||||
"skipping apk build, deploy"
|
||||
"\nand launch steps." >&2
|
||||
exit 0
|
||||
fi
|
||||
local -r package_basename=${package_name/*./}
|
||||
local package_filename=$(get_library_name_from_manifest ${android_manifest})
|
||||
[[ "${package_filename}" == "" ]] && package_filename="${package_basename}"
|
||||
|
||||
# Output apk name.
|
||||
local -r output_apk="bin/${package_filename}-${ant_target}.apk"
|
||||
|
||||
if [[ $((disable_build)) -eq 0 && $((build_package)) -eq 1 ]]; then
|
||||
# Build the apk.
|
||||
build_apk "${output_apk}" "${package_filename}" "${ant_target}"
|
||||
fi
|
||||
|
||||
# Deploy to the device.
|
||||
if [[ $((disable_deploy)) -eq 0 ]]; then
|
||||
install_apk "${package_name}" "${output_apk}" "${adb_device}"
|
||||
fi
|
||||
|
||||
if [[ "${ant_target}" == "debug" && $((run_debugger)) -eq 1 ]]; then
|
||||
# Start debugging.
|
||||
ndk-gdb ${adb_device} --start
|
||||
elif [[ $((launch)) -eq 1 ]]; then
|
||||
launch_package "${package_name}" "${adb_device}"
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
33
android/jni/Android.mk
Executable file
@@ -0,0 +1,33 @@
|
||||
# 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)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
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
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
$(call import-module,android/native_app_glue)
|
||||
|
||||
$(call import-add-path,../..)
|
||||
22
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
|
||||
26
android/jni/main.cpp
Executable file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <android_native_app_glue.h>
|
||||
|
||||
extern int main(int argc, char **argv);
|
||||
|
||||
void android_main(android_app *app) {
|
||||
// Make sure glue isn't stripped.
|
||||
app_dummy();
|
||||
|
||||
main(0, NULL);
|
||||
}
|
||||
20
android/res/values/strings.xml
Executable file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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.
|
||||
-->
|
||||
<resources>
|
||||
<string name="app_name">FlatBufferTest</string>
|
||||
</resources>
|
||||
8
docs/documentation.html
Executable file
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;url=html/index.html">
|
||||
</head>
|
||||
<body>
|
||||
<a href="html/index.html">Click here if you are not redirected.</a>
|
||||
</body>
|
||||
</html>
|
||||
14
docs/footer.html
Executable file
@@ -0,0 +1,14 @@
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
BIN
docs/html/bc_s.png
Normal file
|
After Width: | Height: | Size: 676 B |
BIN
docs/html/bdwn.png
Normal file
|
After Width: | Height: | Size: 147 B |
BIN
docs/html/closed.png
Normal file
|
After Width: | Height: | Size: 132 B |
1357
docs/html/doxygen.css
Normal file
BIN
docs/html/doxygen.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
97
docs/html/dynsections.js
Normal file
@@ -0,0 +1,97 @@
|
||||
function toggleVisibility(linkObj)
|
||||
{
|
||||
var base = $(linkObj).attr('id');
|
||||
var summary = $('#'+base+'-summary');
|
||||
var content = $('#'+base+'-content');
|
||||
var trigger = $('#'+base+'-trigger');
|
||||
var src=$(trigger).attr('src');
|
||||
if (content.is(':visible')===true) {
|
||||
content.hide();
|
||||
summary.show();
|
||||
$(linkObj).addClass('closed').removeClass('opened');
|
||||
$(trigger).attr('src',src.substring(0,src.length-8)+'closed.png');
|
||||
} else {
|
||||
content.show();
|
||||
summary.hide();
|
||||
$(linkObj).removeClass('closed').addClass('opened');
|
||||
$(trigger).attr('src',src.substring(0,src.length-10)+'open.png');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function updateStripes()
|
||||
{
|
||||
$('table.directory tr').
|
||||
removeClass('even').filter(':visible:even').addClass('even');
|
||||
}
|
||||
function toggleLevel(level)
|
||||
{
|
||||
$('table.directory tr').each(function(){
|
||||
var l = this.id.split('_').length-1;
|
||||
var i = $('#img'+this.id.substring(3));
|
||||
var a = $('#arr'+this.id.substring(3));
|
||||
if (l<level+1) {
|
||||
i.attr('src','ftv2folderopen.png');
|
||||
a.attr('src','ftv2mnode.png');
|
||||
$(this).show();
|
||||
} else if (l==level+1) {
|
||||
i.attr('src','ftv2folderclosed.png');
|
||||
a.attr('src','ftv2pnode.png');
|
||||
$(this).show();
|
||||
} else {
|
||||
$(this).hide();
|
||||
}
|
||||
});
|
||||
updateStripes();
|
||||
}
|
||||
|
||||
function toggleFolder(id)
|
||||
{
|
||||
//The clicked row
|
||||
var currentRow = $('#row_'+id);
|
||||
var currentRowImages = currentRow.find("img");
|
||||
|
||||
//All rows after the clicked row
|
||||
var rows = currentRow.nextAll("tr");
|
||||
|
||||
//Only match elements AFTER this one (can't hide elements before)
|
||||
var childRows = rows.filter(function() {
|
||||
var re = new RegExp('^row_'+id+'\\d+_$', "i"); //only one sub
|
||||
return this.id.match(re);
|
||||
});
|
||||
|
||||
//First row is visible we are HIDING
|
||||
if (childRows.filter(':first').is(':visible')===true) {
|
||||
currentRowImages.filter("[id^=arr]").attr('src', 'ftv2pnode.png');
|
||||
currentRowImages.filter("[id^=img]").attr('src', 'ftv2folderclosed.png');
|
||||
rows.filter("[id^=row_"+id+"]").hide();
|
||||
} else { //We are SHOWING
|
||||
//All sub images
|
||||
var childImages = childRows.find("img");
|
||||
var childImg = childImages.filter("[id^=img]");
|
||||
var childArr = childImages.filter("[id^=arr]");
|
||||
|
||||
currentRow.find("[id^=arr]").attr('src', 'ftv2mnode.png'); //open row
|
||||
currentRow.find("[id^=img]").attr('src', 'ftv2folderopen.png'); //open row
|
||||
childImg.attr('src','ftv2folderclosed.png'); //children closed
|
||||
childArr.attr('src','ftv2pnode.png'); //children closed
|
||||
childRows.show(); //show all children
|
||||
}
|
||||
updateStripes();
|
||||
}
|
||||
|
||||
|
||||
function toggleInherit(id)
|
||||
{
|
||||
var rows = $('tr.inherit.'+id);
|
||||
var img = $('tr.inherit_header.'+id+' img');
|
||||
var src = $(img).attr('src');
|
||||
if (rows.filter(':first').is(':visible')===true) {
|
||||
rows.css('display','none');
|
||||
$(img).attr('src',src.substring(0,src.length-8)+'closed.png');
|
||||
} else {
|
||||
rows.css('display','table-row'); // using show() causes jump in firefox
|
||||
$(img).attr('src',src.substring(0,src.length-10)+'open.png');
|
||||
}
|
||||
}
|
||||
|
||||
BIN
docs/html/ftv2blank.png
Normal file
|
After Width: | Height: | Size: 86 B |
BIN
docs/html/ftv2cl.png
Normal file
|
After Width: | Height: | Size: 453 B |
BIN
docs/html/ftv2doc.png
Normal file
|
After Width: | Height: | Size: 746 B |
BIN
docs/html/ftv2folderclosed.png
Normal file
|
After Width: | Height: | Size: 616 B |
BIN
docs/html/ftv2folderopen.png
Normal file
|
After Width: | Height: | Size: 597 B |
BIN
docs/html/ftv2lastnode.png
Normal file
|
After Width: | Height: | Size: 86 B |
BIN
docs/html/ftv2link.png
Normal file
|
After Width: | Height: | Size: 746 B |
BIN
docs/html/ftv2mlastnode.png
Normal file
|
After Width: | Height: | Size: 246 B |
BIN
docs/html/ftv2mnode.png
Normal file
|
After Width: | Height: | Size: 246 B |
BIN
docs/html/ftv2mo.png
Normal file
|
After Width: | Height: | Size: 403 B |
BIN
docs/html/ftv2node.png
Normal file
|
After Width: | Height: | Size: 86 B |
BIN
docs/html/ftv2ns.png
Normal file
|
After Width: | Height: | Size: 388 B |
BIN
docs/html/ftv2plastnode.png
Normal file
|
After Width: | Height: | Size: 229 B |
BIN
docs/html/ftv2pnode.png
Normal file
|
After Width: | Height: | Size: 229 B |
BIN
docs/html/ftv2splitbar.png
Normal file
|
After Width: | Height: | Size: 314 B |
BIN
docs/html/ftv2vertline.png
Normal file
|
After Width: | Height: | Size: 86 B |
115
docs/html/index.html
Normal file
@@ -0,0 +1,115 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Main Page</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('index.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">FlatBuffers Documentation</div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>FlatBuffers is an efficient cross platform serialization library in for C++ and Java. 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>
|
||||
<li><b>Access to serialized data without parsing/unpacking</b> - What sets FlatBuffers apart is that it represents hierarchical data in a flat binary buffer in such a way that it can still be accessed directly without parsing/unpacking, while also still supporting data structure evolution (forwards/backwards compatibility).</li>
|
||||
<li><b>Memory efficiency and speed</b> - The only memory needed to access your data is that of the buffer. It requires 0 additional allocations. FlatBuffers is also very suitable for use with mmap (or streaming), requiring only part of the buffer to be in memory. Access is close to the speed of raw struct access with only one extra indirection (a kind of vtable) to allow for format evolution and optional fields. It is aimed at projects where spending time and space (many memory allocations) to be able to access or construct serialized data is undesirable, such as in games or any other performance sensitive applications. See the <a href="md__benchmarks.html">benchmarks</a> for details.</li>
|
||||
<li><b>Flexible</b> - Optional fields means not only do you get great forwards and backwards compatibility (increasingly important for long-lived games: don't have to update all data with each new version!). It also means you have a lot of choice in what data you write and what data you don't, and how you design data structures.</li>
|
||||
<li><b>Tiny code footprint</b> - Small amounts of generated code, and just a single small header as the minimum dependency, which is very easy to integrate. Again, see the benchmark section for details.</li>
|
||||
<li><b>Strongly typed</b> - Errors happen at compile time rather than manually having to write repetitive and error prone run-time checks. Useful code can be generated for you.</li>
|
||||
<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 code supports object-reuse.</p>
|
||||
</li>
|
||||
<li><b>Cross platform C++11/Java 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>
|
||||
<h3>But all the cool kids use JSON!</h3>
|
||||
<p>JSON is very readable (which is why we use it as our optional text format) and very convenient when used together with dynamically typed languages (such as JavaScript). When serializing data from statically typed languages, however, JSON not only has the obvious drawback of runtime inefficiency, but also forces you to write <em>more</em> code to access data (counterintuitively) due to its dynamic-typing serialization system. In this context, it is only a better choice for systems that have very little to no information ahead of time about what data needs to be stored.</p>
|
||||
<p>Read more about the "why" of FlatBuffers in the <a href="md__white_paper.html">white paper</a>.</p>
|
||||
<h2>Usage in brief</h2>
|
||||
<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 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>
|
||||
</ul>
|
||||
<h2>In-depth documentation</h2>
|
||||
<ul>
|
||||
<li>How to <a href="md__building.html">build the compiler</a> and samples on various platforms.</li>
|
||||
<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>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>
|
||||
<li>A description of the <a href="md__internals.html">internals</a> of FlatBuffers.</li>
|
||||
<li>A formal <a href="md__grammar.html">grammar</a> of the schema language.</li>
|
||||
</ul>
|
||||
<h2>Online resources</h2>
|
||||
<ul>
|
||||
<li><a href="http://github.com/google/flatbuffers">github repository</a></li>
|
||||
<li><a href="http://google.github.io/flatbuffers">landing page</a></li>
|
||||
<li><a href="http://group.google.com/group/flatbuffers">FlatBuffers Google Group</a></li>
|
||||
<li><a href="http://github.com/google/flatbuffers/issues">FlatBuffers Issues Tracker</a> </li>
|
||||
</ul>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
72
docs/html/jquery.js
vendored
Normal file
103
docs/html/md__benchmarks.html
Normal file
@@ -0,0 +1,103 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Benchmarks</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__benchmarks.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Benchmarks </div> </div>
|
||||
</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), and Rapid JSON, one of the fastest C++ JSON parsers around.</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></tr>
|
||||
<tr>
|
||||
<td>Decode + Traverse + Dealloc (1 million times, seconds) </td><td>0.08 </td><td>305 </td><td>583 </td><td>105 </td></tr>
|
||||
<tr>
|
||||
<td>Decode / Traverse / Dealloc (breakdown) </td><td>0 / 0.08 / 0 </td><td>220 / 3.6 / 81 </td><td>294 / 0.9 / 287 </td><td>70 / 0.08 / 35 </td></tr>
|
||||
<tr>
|
||||
<td>Encode (1 million times, seconds) </td><td>3.2 </td><td>185 </td><td>650 </td><td>169 </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></tr>
|
||||
<tr>
|
||||
<td>Memory needed to store decoded wire (bytes / blocks) </td><td>0 / 0 </td><td>760 / 20 </td><td>65689 / 40 </td><td>328 / 1 </td></tr>
|
||||
<tr>
|
||||
<td>Transient memory allocated during decode (KB) </td><td>0 </td><td>1 </td><td>131 </td><td>4 </td></tr>
|
||||
<tr>
|
||||
<td>Generated source code size (KB) </td><td>4 </td><td>61 </td><td>0 </td><td>4 </td></tr>
|
||||
<tr>
|
||||
<td>Field access in handwritten traversal code </td><td>accessors </td><td>accessors </td><td>manual error checking </td><td>accessors </td></tr>
|
||||
<tr>
|
||||
<td>Library source code (KB) </td><td>15 </td><td>some subset of 3800 </td><td>87 </td><td>43 </td></tr>
|
||||
</table>
|
||||
<h3>Some other serialization systems we compared against but did not benchmark (yet), in rough order of applicability:</h3>
|
||||
<ul>
|
||||
<li>Cap'n'Proto promises to reduce Protocol Buffers much like FlatBuffers does, though with a more complicated binary encoding and less flexibility (no optional fields to allow deprecating fields or serializing with missing fields for which defaults exist). It currently also isn't fully cross-platform portable (lack of VS support).</li>
|
||||
<li>msgpack: has very minimal forwards/backwards compatability support when used with the typed C++ interface. Also lacks VS2010 support.</li>
|
||||
<li>Thrift: very similar to Protocol Buffers, but appears to be less efficient, and have more dependencies.</li>
|
||||
<li>XML: typically even slower than JSON, but has the advantage that it can be parsed with a schema to reduce error-checking boilerplate code.</li>
|
||||
<li>YAML: a superset of JSON and otherwise very similar. Used by e.g. Unity.</li>
|
||||
<li>C# comes with built-in serialization functionality, as used by Unity also. Being tied to the language, and having no automatic versioning support limits its applicability.</li>
|
||||
<li>Project Anarchy (the free mobile engine by Havok) comes with a serialization system, that however does no automatic versioning (have to code around new fields manually), is very much tied to the rest of the engine, and works without a schema to generate code (tied to your C++ class definition). </li>
|
||||
</ul>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
78
docs/html/md__building.html
Normal file
@@ -0,0 +1,78 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Building</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__building.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Building </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>The system comes with a <code>cmake</code> file that should allow you to build the compiler <code>flatc</code> and the tests (optionally). For details on <code>cmake</code>, see <a href="http://www.cmake.org">http://www.cmake.org</a>. In brief, depending on your platform, use one of e.g.: </p>
|
||||
<pre class="fragment">cmake -G "Unix Makefiles"
|
||||
cmake -G "Visual Studio 10"
|
||||
cmake -G "Xcode"
|
||||
</pre><p>Then, build as normal for your platform. This should result in a <code>flatc</code> executable, essential for the next steps. Note that to use clang instead of gcc, you may need to set up your environment variables, e.g. <code>CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -G "Unix Makefiles"</code>.</p>
|
||||
<p>Optionally, run the <code>flattests</code> executable. to ensure everything is working correctly on your system. If this fails, please contact us!</p>
|
||||
<p>The cmake file will also build two sample executables, <code>sample_binary</code> and <code>sample_text</code>, see the corresponding <code>.cpp</code> file in the samples directory.</p>
|
||||
<p>There is an <code>android</code> directory that contains all you need to build the test executable on android (use the included <code>build_apk.sh</code> script, or use <code>ndk_build</code> / <code>adb</code> etc. as usual). Upon running, it will output to the log if tests succeeded or not.</p>
|
||||
<p>There is usually no runtime to compile, as the code consists of a single header, <code>include/flatbuffers/flatbuffers.h</code>. You should add the <code>include</code> folder to your include paths. If you wish to be able to load schemas and/or parse text into binary buffers at runtime, you additionally need the other headers in <code>include/flatbuffers</code>. You must also compile/link <code>src/idl_parser.cpp</code> (and <code>src/idl_gen_text.cpp</code> if you also want to be able convert binary to text).</p>
|
||||
<p>For applications on Google Play that integrate this library, usage is tracked. This tracking is done automatically using the embedded version string (flatbuffer_version_string), and helps us continue to optimize it. Aside from consuming a few extra bytes in your application binary, it shouldn't affect your application at all. We use this information to let us know if FlatBuffers is useful and if we should continue to invest in it. Since this is open source, you are free to remove the version string but we would appreciate if you would leave it in. </p>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
77
docs/html/md__compiler.html
Normal file
@@ -0,0 +1,77 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Using the schema compiler</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__compiler.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Using the schema compiler </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>Usage: </p>
|
||||
<pre class="fragment">flatc [ -c ] [ -j ] [ -b ] [ -t ] file1 file2 ..
|
||||
</pre><p>The files are read and parsed in order, and can contain either schemas or data (see below). Later files can make use of definitions in earlier files. Depending on the flags passed, additional files may be generated for each file processed:</p>
|
||||
<ul>
|
||||
<li><code>-c</code> : Generate a C++ header for all definitions in this file (as <code>filename_generated.h</code>). Skips data.</li>
|
||||
<li><code>-j</code> : Generate Java classes.</li>
|
||||
<li><code>-b</code> : If data is contained in this file, generate a <code>filename_wire.bin</code> containing the binary flatbuffer.</li>
|
||||
<li><code>-t</code> : If data is contained in this file, generate a <code>filename_wire.txt</code> (for debugging). </li>
|
||||
</ul>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
142
docs/html/md__cpp_usage.html
Normal file
@@ -0,0 +1,142 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Use in C++</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__cpp_usage.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Use in C++ </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>Assuming you have written a schema using the above language in say <code>mygame.fbs</code> (FlatBuffer Schema, though the extension doesn't matter), you've generated a C++ header called <code>mygame_generated.h</code> using the compiler (e.g. <code>flatc -c mygame.fbs</code>), you can now start using this in your program by including the header. As noted, this header relies on <code>flatbuffers/flatbuffers.h</code>, which should be in your include path.</p>
|
||||
<h3>Writing in C++</h3>
|
||||
<p>To start creating a buffer, create an instance of <code>FlatBufferBuilder</code> which will contain the buffer as it grows: </p>
|
||||
<pre class="fragment">FlatBufferBuilder fbb;
|
||||
</pre><p>Before we serialize a Monster, we need to first serialize any objects that are contained there-in, i.e. we serialize the data tree using depth first, pre-order traversal. This is generally easy to do on any tree structures. For example: </p>
|
||||
<pre class="fragment">auto name = fbb.CreateString("MyMonster");
|
||||
|
||||
unsigned char inv[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
auto inventory = fbb.CreateVector(inv, 10);
|
||||
</pre><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>
|
||||
<pre class="fragment">Vec3 vec(1, 2, 3);
|
||||
</pre><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>
|
||||
<pre class="fragment">auto mloc = CreateMonster(fbb, &vec, 150, 80, name, inventory, Color_Red, Offset<void>(0), Any_NONE);
|
||||
</pre><p>Note that we're passing <code>150</code> for the <code>mana</code> field, which happens to be the default value: this means the field will not actually be written to the buffer, since we'll get that value anyway when we query it. This is a nice space savings, since it is very common for fields to be at their default. It means we also don't need to be scared to add fields only used in a minority of cases, since they won't bloat up the buffer sizes if they're not actually used.</p>
|
||||
<p>We do something similarly for the union field <code>test</code> by specifying a <code>0</code> offset and the <code>NONE</code> enum value (part of every union) to indicate we don't actually want to write this field.</p>
|
||||
<p>Tables (like <code>Monster</code>) give you full flexibility on what fields you write (unlike <code>Vec3</code>, which always has all fields set because it is a <code>struct</code>). If you want even more control over this (i.e. skip fields even when they are not default), instead of the convenient <code>CreateMonster</code> call we can also build the object field-by-field manually: </p>
|
||||
<pre class="fragment">MonsterBuilder mb(fbb);
|
||||
mb.add_pos(&vec);
|
||||
mb.add_hp(80);
|
||||
mb.add_name(name);
|
||||
mb.add_inventory(inventory);
|
||||
auto mloc = mb.Finish();
|
||||
</pre><p>We start with a temporary helper class <code>MonsterBuilder</code> (which is defined in our generated code also), then call the various <code>add_</code> methods to set fields, and <code>Finish</code> to complete the object. This is pretty much the same code as you find inside <code>CreateMonster</code>, except we're leaving out a few fields. Fields may also be added in any order, though orderings with fields of the same size adjacent to each other most efficient in size, due to alignment. You should not nest these Builder classes (serialize your data in pre-order).</p>
|
||||
<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>
|
||||
<pre class="fragment">fbb.Finish(mloc);
|
||||
</pre><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><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>
|
||||
<pre class="fragment">auto monster = GetMonster(buffer_pointer);
|
||||
</pre><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>
|
||||
<pre class="fragment">assert(monster->hp() == 80);
|
||||
assert(monster->mana() == 150); // default
|
||||
assert(strcmp(monster->name()->c_str(), "MyMonster") == 0);
|
||||
</pre><p>These should all be true. Note that we never stored a <code>mana</code> value, so it will return the default.</p>
|
||||
<p>To access sub-objects, in this case the <code>Vec3</code>: </p>
|
||||
<pre class="fragment">auto pos = monster->pos();
|
||||
assert(pos);
|
||||
assert(pos->z() == 3);
|
||||
</pre><p>If we had not set the <code>pos</code> field during serialization, it would be <code>NULL</code>.</p>
|
||||
<p>Similarly, we can access elements of the inventory array: </p>
|
||||
<pre class="fragment">auto inv = monster->inventory();
|
||||
assert(inv);
|
||||
assert(inv->Get(9) == 9);
|
||||
</pre><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>
|
||||
<p>It is important to note is that structs are still little endian on all machines, so only use tricks like this if you can guarantee you're not shipping on a big endian machine (an <code>assert(FLATBUFFERS_LITTLEENDIAN)</code> would be wise).</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>
|
||||
<p>There are two ways to use text formats:</p>
|
||||
<h3>Using the compiler as a conversion tool</h3>
|
||||
<p>This is the preferred path, as it doesn't require you to add any new code to your program, and is maximally efficient since you can ship with binary data. The disadvantage is that it is an extra step for your users/developers to perform, though you might be able to automate it. </p>
|
||||
<pre class="fragment">flatc -b myschema.fbs mydata.json
|
||||
</pre><p>This will generate the binary file <code>mydata_wire.bin</code> which can be loaded as before.</p>
|
||||
<h3>Making your program capable of loading text directly</h3>
|
||||
<p>This gives you maximum flexibility. You could even opt to support both, i.e. check for both files, and regenerate the binary from text when required, otherwise just load the binary.</p>
|
||||
<p>This option is currently only available for C++, or Java through JNI.</p>
|
||||
<p>As mentioned in the section "Building" above, this technique requires you to link a few more files into your program, and you'll want to include <code>flatbuffers/idl.h</code>.</p>
|
||||
<p>Load text (either a schema or json) into an in-memory buffer (there is a convenient <code>LoadFile()</code> utility function in <code>flatbuffers/util.h</code> if you wish). Construct a parser: </p>
|
||||
<pre class="fragment">flatbuffers::Parser parser;
|
||||
</pre><p>Now you can parse any number of text files in sequence: </p>
|
||||
<pre class="fragment">parser.Parse(text_file.c_str());
|
||||
</pre><p>This works similarly to how the command-line compiler works: a sequence of files parsed by the same <code>Parser</code> object allow later files to reference definitions in earlier files. Typically this means you first load a schema file (which populates <code>Parser</code> with definitions), followed by one or more JSON files.</p>
|
||||
<p>If there were any parsing errors, <code>Parse</code> will return <code>false</code>, and <code>Parser::err</code> contains a human readable error string with a line number etc, which you should present to the creator of that file.</p>
|
||||
<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>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
81
docs/html/md__grammar.html
Normal file
@@ -0,0 +1,81 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Formal Grammar of the schema language</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__grammar.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Formal Grammar of the schema language </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>schema = namespace_decl | type_decl | enum_decl | root_decl | object</p>
|
||||
<p>namespace_decl = <code>namespace</code> ident ( <code>.</code> ident )* <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>
|
||||
<p>field_decl = type <code>:</code> ident [ <code>=</code> scalar ] metadata <code>;</code></p>
|
||||
<p>type = <code>bool</code> | <code>byte</code> | <code>ubyte</code> | <code>short</code> | <code>ushort</code> | <code>int</code> | <code>uint</code> | <code>float</code> | <code>long</code> | <code>ulong</code> | <code>double</code> | <code>string</code> | <code>[</code> type <code>]</code> | ident</p>
|
||||
<p>enumval_decl = ident [ <code>=</code> integer_constant ]</p>
|
||||
<p>metadata = [ <code>(</code> commasep( ident [ <code>:</code> scalar ] ) <code>)</code> ]</p>
|
||||
<p>scalar = integer_constant | float_constant | <code>true</code> | <code>false</code></p>
|
||||
<p>object = { commasep( ident <code>:</code> value ) }</p>
|
||||
<p>value = scalar | object | string_constant | <code>[</code> commasep( value ) <code>]</code></p>
|
||||
<p>commasep(x) = [ x ( <code>,</code> x )* ] </p>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
179
docs/html/md__internals.html
Normal file
@@ -0,0 +1,179 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: FlatBuffer Internals</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__internals.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">FlatBuffer Internals </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>This section is entirely optional for the use of FlatBuffers. In normal usage, you should never need the information contained herein. If you're interested however, it should give you more of an appreciation of why FlatBuffers is both efficient and convenient.</p>
|
||||
<h3>Format components</h3>
|
||||
<p>A FlatBuffer is a binary file and in-memory format consisting mostly of scalars of various sizes, all aligned to their own size. Each scalar is also always represented in little-endian format, as this corresponds to all commonly used CPUs today. FlatBuffers will also work on big-endian machines, but will be slightly slower because of additional byte-swap intrinsics.</p>
|
||||
<p>On purpose, the format leaves a lot of details about where exactly things live in memory undefined, e.g. fields in a table can have any order, and objects to some extend can be stored in many orders. This is because the format doesn't need this information to be efficient, and it leaves room for optimization and extension (for example, fields can be packed in a way that is most compact). Instead, the format is defined in terms of offsets and adjacency only.</p>
|
||||
<h3>Format identification</h3>
|
||||
<p>The format also doesn't contain information for format identification and versioning, which is also by design. FlatBuffers is a statically typed system, meaning the user of a buffer needs to know what kind of buffer it is. FlatBuffers can of course be wrapped inside other containers where needed, or you can use its union feature to dynamically identify multiple possible sub-objects stored. Additionally, it can be used together with the schema parser if full reflective capabilities are desired.</p>
|
||||
<p>Versioning is something that is intrinsically part of the format (the optionality / extensibility of fields), so the format itself does not need a version number (it's a meta-format, in a sense). We're hoping that this format can accommodate all data needed. If format breaking changes are ever necessary, it would become a new kind of format rather than just a variation.</p>
|
||||
<h3>Offsets</h3>
|
||||
<p>The most important and generic offset type (see <code>flatbuffers.h</code>) is <code>offset_t</code>, which is currently always a <code>uint32_t</code>, and is used to refer to all tables/unions/strings/vectors. 32bit is intentional, since we want to keep the format binary compatible between 32 and 64bit systems, and a 64bit offset would bloat the size for almost all uses. A version of this format with 64bit (or 16bit) offsets is easy to set when needed. Unsigned means they can only point in one direction, which typically is forward (towards a higher memory location). Any backwards offsets will be explicitly marked as such.</p>
|
||||
<p>The format starts with an <code>offset_t</code> to the root object in the buffer.</p>
|
||||
<p>We have two kinds of objects, structs and tables.</p>
|
||||
<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>offset_t</code>, since vtables may be stored anywhere), followed by all the fields as aligned scalars. 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 currently 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 N the offsets, where N is the amount of field 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 count.</p>
|
||||
<h3>Construction</h3>
|
||||
<p>The current implementation constructs these buffers backwards, since that significantly reduces the amount of bookkeeping and simplifies the construction API.</p>
|
||||
<h3>Code example</h3>
|
||||
<p>Here's an example of the code that gets generated for the <code>samples/monster.fbs</code>. What follows is the entire file, broken up by comments: </p>
|
||||
<pre class="fragment">// automatically generated, do not modify
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
namespace MyGame {
|
||||
namespace Sample {
|
||||
</pre><p>Nested namespace support. </p>
|
||||
<pre class="fragment">enum {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
static const char *names[] = { "Red", "Green", "Blue", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
</pre><p>Enums and convenient reverse lookup. </p>
|
||||
<pre class="fragment">enum {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesAny() {
|
||||
static const char *names[] = { "NONE", "Monster", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
</pre><p>Unions share a lot with enums. </p>
|
||||
<pre class="fragment">struct Vec3;
|
||||
struct Monster;
|
||||
</pre><p>Predeclare all datatypes since there may be circular references. </p>
|
||||
<pre class="fragment">MANUALLY_ALIGNED_STRUCT(4) Vec3 {
|
||||
private:
|
||||
float x_;
|
||||
float y_;
|
||||
float z_;
|
||||
|
||||
public:
|
||||
Vec3(float x, float y, float z)
|
||||
: x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)) {}
|
||||
|
||||
float x() const { return flatbuffers::EndianScalar(x_); }
|
||||
float y() const { return flatbuffers::EndianScalar(y_); }
|
||||
float z() const { return flatbuffers::EndianScalar(z_); }
|
||||
};
|
||||
STRUCT_END(Vec3, 12);
|
||||
</pre><p>These ugly macros do a couple of things: they turn off any padding the compiler might normally do, since we add padding manually (though none in this example), and they enforce alignment chosen by FlatBuffers. This ensures the layout of this struct will look the same regardless of compiler and platform. Note that the fields are private: this is because these store little endian scalars regardless of platform (since this is part of the serialized data). <code>EndianScalar</code> then converts back and forth, which is a no-op on all current mobile and desktop platforms, and a single machine instruction on the few remaining big endian platforms. </p>
|
||||
<pre class="fragment">struct Monster : 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); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
int8_t color() const { return GetField<int8_t>(16, 2); }
|
||||
};
|
||||
</pre><p>Tables are a bit more complicated. A table accessor struct is used to point at the serialized data for a table, which always starts with an offset to its vtable. It derives from <code>Table</code>, which contains the <code>GetField</code> helper functions. GetField takes a vtable offset, and a default value. It will look in the vtable at that offset. If the offset is out of bounds (data from an older version) or the vtable entry is 0, the field is not present and the default is returned. Otherwise, it uses the entry as an offset into the table to locate the field. </p>
|
||||
<pre class="fragment">struct MonsterBuilder {
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_pos(const Vec3 *pos) { fbb_.AddStruct(4, pos); }
|
||||
void add_mana(int16_t mana) { fbb_.AddElement<int16_t>(6, mana, 150); }
|
||||
void add_hp(int16_t hp) { fbb_.AddElement<int16_t>(8, hp, 100); }
|
||||
void add_name(flatbuffers::Offset<flatbuffers::String> name) { fbb_.AddOffset(10, name); }
|
||||
void add_inventory(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory) { fbb_.AddOffset(14, inventory); }
|
||||
void add_color(int8_t color) { fbb_.AddElement<int8_t>(16, color, 2); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
flatbuffers::Offset<Monster> Finish() { return flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 7)); }
|
||||
};
|
||||
</pre><p><code>MonsterBuilder</code> is the base helper struct to construct a table using a <code>FlatBufferBuilder</code>. You can add the fields in any order, and the <code>Finish</code> call will ensure the correct vtable gets generated. </p>
|
||||
<pre class="fragment">inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const Vec3 *pos, int16_t mana, int16_t hp, flatbuffers::Offset<flatbuffers::String> name, flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory, int8_t color) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_inventory(inventory);
|
||||
builder_.add_name(name);
|
||||
builder_.add_pos(pos);
|
||||
builder_.add_hp(hp);
|
||||
builder_.add_mana(mana);
|
||||
builder_.add_color(color);
|
||||
return builder_.Finish();
|
||||
}
|
||||
</pre><p><code>CreateMonster</code> is a convenience function that calls all functions in <code>MonsterBuilder</code> above for you. Note that if you pass values which are defaults as arguments, it will not actually construct that field, so you can probably use this function instead of the builder class in almost all cases. </p>
|
||||
<pre class="fragment">inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
|
||||
</pre><p>This function is only generated for the root table type, to be able to start traversing a FlatBuffer from a raw buffer pointer. </p>
|
||||
<pre class="fragment">}; // namespace MyGame
|
||||
}; // namespace Sample</pre> </div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
102
docs/html/md__java_usage.html
Normal file
@@ -0,0 +1,102 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Use in Java</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__java_usage.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Use in Java </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>There's experimental support for reading FlatBuffers in Java. Generate code for Java with the <code>-j</code> option to <code>flatc</code>.</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>getRootAsMonster</code> function: </p>
|
||||
<pre class="fragment">ByteBuffer bb = ByteBuffer.wrap(data);
|
||||
Monster monster = Monster.getRootAsMonster(bb);
|
||||
</pre><p>Now you can access values much like C++: </p>
|
||||
<pre class="fragment">short hp = monster.hp();
|
||||
Vec3 pos = monster.pos();
|
||||
</pre><p>Note that whenever you access a new object like in the <code>pos</code> example above, a new temporary accessor object gets created. If your code is very performance sensitive (you iterate through a lot of objects), there's a second <code>pos()</code> method to which you can pass a <code>Vec3</code> object you've already created. This allows you to reuse it across many calls and reduce the amount of object allocation (and thus garbage collection) your program does.</p>
|
||||
<p>Sadly the string accessors currently always create a new string when accessed, since FlatBuffer's UTF-8 strings can't be read in-place by Java.</p>
|
||||
<p>Vector access is also a bit different from C++: you pass an extra index to the vector field accessor. Then a second method with the same name suffixed by <code>_length</code> let's you know the number of elements you can access: </p>
|
||||
<pre class="fragment">for (int i = 0; i < monster.inventory_length(); i++)
|
||||
monster.inventory(i); // do something here
|
||||
</pre><p>You can also construct these buffers in Java using the static methods found in the generated code, and the FlatBufferBuilder class: </p>
|
||||
<pre class="fragment">FlatBufferBuilder fbb = new FlatBufferBuilder();
|
||||
</pre><p>Create strings: </p>
|
||||
<pre class="fragment">int str = fbb.createString("MyMonster");
|
||||
</pre><p>Create a table with a struct contained therein: </p>
|
||||
<pre class="fragment">Monster.startMonster(fbb);
|
||||
Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0, (byte)4, (short)5, (byte)6));
|
||||
Monster.addHp(fbb, (short)80);
|
||||
Monster.addName(fbb, str);
|
||||
Monster.addInventory(fbb, inv);
|
||||
Monster.addTest_type(fbb, (byte)1);
|
||||
Monster.addTest(fbb, mon2);
|
||||
Monster.addTest4(fbb, test4s);
|
||||
int mon = Monster.endMonster(fbb);
|
||||
</pre><p>As you can see, the Java code for tables does not use a convenient <code>createMonster</code> call like the C++ code. 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 even have arguments for nested structs.</p>
|
||||
<p>Vectors also use this start/end pattern to allow vectors of both scalar types and structs: </p>
|
||||
<pre class="fragment">Monster.startInventoryVector(fbb, 5);
|
||||
for (byte i = 4; i >=0; i--) fbb.addByte(i);
|
||||
int inv = fbb.endVector();
|
||||
</pre><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. You write the elements backwards since the buffer is being constructed back to front.</p>
|
||||
<h2>Text Parsing</h2>
|
||||
<p>There currently is no support for parsing text (Schema's and JSON) directly from Java, though you could use the C++ parser through JNI. Please see the C++ documentation for more on text parsing. </p>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
144
docs/html/md__schemas.html
Normal file
@@ -0,0 +1,144 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Writing a schema</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__schemas.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Writing a schema </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>The syntax of the schema language (aka IDL, Interface Definition Language) should look quite familiar to users of any of the C family of languages, and also to users of other IDLs. Let's look at an example first: </p>
|
||||
<pre class="fragment">// example IDL file
|
||||
|
||||
namespace MyGame;
|
||||
|
||||
enum Color : byte { Red = 1, Green, Blue }
|
||||
|
||||
union Any { Monster, Weapon, Pickup }
|
||||
|
||||
struct Vec3 {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3;
|
||||
mana:short = 150;
|
||||
hp:short = 100;
|
||||
name:string;
|
||||
friendly:bool = false (deprecated, priority: 1);
|
||||
inventory:[ubyte];
|
||||
color:Color = Blue;
|
||||
test:Any;
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
</pre><p>(Weapon & Pickup not defined as part of this example).</p>
|
||||
<h3>Tables</h3>
|
||||
<p>Tables are the main way of defining objects in FlatBuffers, and consist of a name (here <code>Monster</code>) and a list of fields. Each field has a name, a type, and optionally a default value (if omitted, it defaults to 0 / NULL).</p>
|
||||
<p>Each field is optional: It does not have to appear in the wire representation, and you can choose to omit fields for each individual object. As a result, you have the flexibility to add fields without fear of bloating your data. This design is also FlatBuffer's mechanism for forward and backwards compatibility. Note that:</p>
|
||||
<ul>
|
||||
<li>You can add new fields in the schema ONLY at the end of a table definition. Older data will still read correctly, and give you the default value when read. Older code will simply ignore the new field.</li>
|
||||
<li>You cannot delete fields you don't use anymore from the schema, but you can simply stop writing them into your data for almost the same effect. Additionally you can mark them as <code>deprecated</code> as in the example above, which will prevent the generation of accessors in the generated C++, as a way to enforce the field not being used any more. (careful: this may break code!).</li>
|
||||
<li>You may change field names and table names, if you're ok with your code breaking until you've renamed them there too.</li>
|
||||
</ul>
|
||||
<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>
|
||||
<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>
|
||||
<li>Vector of any other type (denoted with <code>[type]</code>). Nesting vectors require you wrap the inner vector in a struct/table rather than writing <code>[[type]]</code>.</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>
|
||||
</ul>
|
||||
<p>You can't change types of fields once they're used, with the exception of same-size data where a <code>reinterpret_cast</code> would give you a desirable result, e.g. you could change a <code>uint</code> to an <code>int</code> if no values in current data use the high bit yet.</p>
|
||||
<h3>(Default) Values</h3>
|
||||
<p>Values are a sequence of digits, optionally followed by a <code>.</code> and more digits for float constants, and optionally prefixed by a <code>-</code>. Non-scalar defaults are currently not supported (always NULL).</p>
|
||||
<p>You generally do not want to change default values after they're initially defined. Fields that have the default value are not actually stored in the serialized data but are generated in code, so when you change the default, you'd now get a different value than from code generated from an older version of the schema. There are situations however where this may be desirable, especially if you can ensure a simultaneous rebuild of all code.</p>
|
||||
<h3>Enums</h3>
|
||||
<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. If you omit the underlying type, it will be <code>short</code>.</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>
|
||||
<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>Root type</h3>
|
||||
<p>This declares what you consider to be the root table (or struct) of the serialized data.</p>
|
||||
<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>Current understood attributes:</p>
|
||||
<ul>
|
||||
<li><code>deprecated</code> (on a field): do not generate accessors for this field anymore, code should stop using this data.</li>
|
||||
<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>
|
||||
</ul>
|
||||
<h2>Gotchas</h2>
|
||||
<h3>Schemas and version control</h3>
|
||||
<p>FlatBuffers relies on new field declarations being added at the end, and earlier declarations to not be removed, but be marked deprecated when needed. We think this is an improvement over the manual number assignment that happens in Protocol Buffers.</p>
|
||||
<p>One place where this is possibly problematic however is source control. If user A adds a field, generates new binary data with this new schema, then tries to commit both to source control after user B already committed a new field also, and just auto-merges the schema, the binary files are now invalid compared to the new schema.</p>
|
||||
<p>The solution of course is that you should not be generating binary data before your schema changes have been committed, ensuring consistency with the rest of the world. </p>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
101
docs/html/md__white_paper.html
Normal file
@@ -0,0 +1,101 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: FlatBuffers white paper</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('md__white_paper.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">FlatBuffers white paper </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>This document tries to shed some light on to the "why" of FlatBuffers, a new serialization library.</p>
|
||||
<h2>Motivation</h2>
|
||||
<p>Back in the good old days, performance was all about instructions and cycles. Nowadays, processing units have run so far ahead of the memory subsystem, that making an efficient application should start and finish with thinking about memory. How much you use of it. How you lay it out and access it. How you allocate it. When you copy it.</p>
|
||||
<p>Serialization is a pervasive activity in a lot programs, and a common source of memory inefficiency, with lots of temporary data structures needed to parse and represent data, and inefficient allocation patterns and locality.</p>
|
||||
<p>If it would be possible to do serialization with no temporary objects, no additional allocation, no copying, and good locality, this could be of great value. The reason serialization systems usually don't manage this is because it goes counter to forwards/backwards compatability, and platform specifics like endianness and alignment.</p>
|
||||
<p>FlatBuffers is what you get if you try anyway.</p>
|
||||
<p>In particular, FlatBuffers focus is on mobile hardware (where memory size and memory bandwidth is even more constrained than on desktop hardware), and applications that have the highest performance needs: games.</p>
|
||||
<h2>FlatBuffers</h2>
|
||||
<p><em>This is a summary of FlatBuffers functionality, with some rationale. A more detailed description can be found in the FlatBuffers documentation.</em></p>
|
||||
<h3>Summary</h3>
|
||||
<p>A FlatBuffer is a binary buffer containing nested objects (structs, tables, vectors,..) organized using offsets so that the data can be traversed in-place just like any pointer-based data structure. Unlike most in-memory data structures however, it uses strict rules of alignment and endianness (always little) to ensure these buffers are cross platform. Additionally, for objects that are tables, FlatBuffers provides forwards/backwards compatibility and general optionality of fields, to support most forms of format evolution.</p>
|
||||
<p>You define your object types in a schema, which can then be compiled to C++ or Java for low to zero overhead reading & writing. Optionally, JSON data can be dynamically parsed into buffers.</p>
|
||||
<h3>Tables</h3>
|
||||
<p>Tables are the cornerstone of FlatBuffers, since format evolution is essential for most applications of serialization. Typically, dealing with format changes is something that can be done transparently during the parsing process of most serialization solutions out there. But a FlatBuffer isn't parsed before it is accessed.</p>
|
||||
<p>Tables get around this by using an extra indirection to access fields, through a <em>vtable</em>. Each table comes with a vtable (which may be shared between multiple tables with the same layout), and contains information where fields for this particular kind of instance of vtable are stored. The vtable may also indicate that the field is not present (because this FlatBuffer was written with an older version of the software, of simply because the information was not necessary for this instance, or deemed deprecated), in which case a default value is returned.</p>
|
||||
<p>Tables have a low overhead in memory (since vtables are small and shared) and in access cost (an extra indirection), but provide great flexibility. Tables may even cost less memory than the equivalent struct, since fields do not need to be stored when they are equal to their default.</p>
|
||||
<p>FlatBuffers additionally offers "naked" structs, which do not offer forwards/backwards compatibility, but can be even smaller (useful for very small objects that are unlikely to change, like e.g. a coordinate pair or a RGBA color).</p>
|
||||
<h3>Schemas</h3>
|
||||
<p>While schemas reduce some generality (you can't just read any data without having its schema), they have a lot of upsides:</p>
|
||||
<ul>
|
||||
<li>Most information about the format can be factored into the generated code, reducing memory needed to store data, and time to access it.</li>
|
||||
<li>The strong typing of the data definitions means less error checking/handling at runtime (less can go wrong).</li>
|
||||
<li>A schema enables us to access a buffer without parsing.</li>
|
||||
</ul>
|
||||
<p>FlatBuffer schemas are fairly similar to those of the incumbent, Protocol Buffers, and generally should be readable to those familiar with the C family of languages. We chose to improve upon the features offered by .proto files in the following ways:</p>
|
||||
<ul>
|
||||
<li>Deprecation of fields instead of manual field id assignment. Extending an object in a .proto means hunting for a free slot among the numbers (preferring lower numbers since they have a more compact representation). Besides being inconvenient, it also makes removing fields problematic: you either have to keep them, not making it obvious that this field shouldn't be read/written anymore, and still generating accessors. Or you remove it, but now you risk that there's still old data around that uses that field by the time someone reuses that field id, with nasty consequences.</li>
|
||||
<li>Differentiating between tables and structs (see above). Effectively all table fields are <code>optional</code>, and all struct fields are <code>required</code>.</li>
|
||||
<li>Having a native vector type instead of <code>repeated</code>. This gives you a length without having to collect all items, and in the case of scalars provides for a more compact representation, and one that guarantees adjacency.</li>
|
||||
<li>Having a native <code>union</code> type instead of using a series of <code>optional</code> fields, all of which must be checked individually.</li>
|
||||
<li>Being able to define defaults for all scalars, instead of having to deal with their optionality at each access.</li>
|
||||
<li>A parser that can deal with both schemas and data definitions (JSON compatible) uniformly. </li>
|
||||
</ul>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
BIN
docs/html/nav_f.png
Normal file
|
After Width: | Height: | Size: 153 B |
BIN
docs/html/nav_g.png
Normal file
|
After Width: | Height: | Size: 95 B |
BIN
docs/html/nav_h.png
Normal file
|
After Width: | Height: | Size: 98 B |
143
docs/html/navtree.css
Normal file
@@ -0,0 +1,143 @@
|
||||
#nav-tree .children_ul {
|
||||
margin:0;
|
||||
padding:4px;
|
||||
}
|
||||
|
||||
#nav-tree ul {
|
||||
list-style:none outside none;
|
||||
margin:0px;
|
||||
padding:0px;
|
||||
}
|
||||
|
||||
#nav-tree li {
|
||||
white-space:nowrap;
|
||||
margin:0px;
|
||||
padding:0px;
|
||||
}
|
||||
|
||||
#nav-tree .plus {
|
||||
margin:0px;
|
||||
}
|
||||
|
||||
#nav-tree .selected {
|
||||
background-image: url('tab_a.png');
|
||||
background-repeat:repeat-x;
|
||||
color: #fff;
|
||||
text-shadow: 0px 1px 1px rgba(0, 0, 0, 1.0);
|
||||
}
|
||||
|
||||
#nav-tree img {
|
||||
margin:0px;
|
||||
padding:0px;
|
||||
border:0px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
#nav-tree a {
|
||||
text-decoration:none;
|
||||
padding:0px;
|
||||
margin:0px;
|
||||
outline:none;
|
||||
}
|
||||
|
||||
#nav-tree .label {
|
||||
margin:0px;
|
||||
padding:0px;
|
||||
font: 12px 'Lucida Grande',Geneva,Helvetica,Arial,sans-serif;
|
||||
}
|
||||
|
||||
#nav-tree .label a {
|
||||
padding:2px;
|
||||
}
|
||||
|
||||
#nav-tree .selected a {
|
||||
text-decoration:none;
|
||||
color:#fff;
|
||||
}
|
||||
|
||||
#nav-tree .children_ul {
|
||||
margin:0px;
|
||||
padding:0px;
|
||||
}
|
||||
|
||||
#nav-tree .item {
|
||||
margin:0px;
|
||||
padding:0px;
|
||||
}
|
||||
|
||||
#nav-tree {
|
||||
padding: 0px 0px;
|
||||
background-color: #FAFAFF;
|
||||
font-size:14px;
|
||||
overflow:auto;
|
||||
}
|
||||
|
||||
#doc-content {
|
||||
overflow:auto;
|
||||
display:block;
|
||||
padding:0px;
|
||||
margin:0px;
|
||||
-webkit-overflow-scrolling : touch; /* iOS 5+ */
|
||||
}
|
||||
|
||||
#side-nav {
|
||||
padding:0 6px 0 0;
|
||||
margin: 0px;
|
||||
display:block;
|
||||
position: absolute;
|
||||
left: 0px;
|
||||
width: 250px;
|
||||
}
|
||||
|
||||
.ui-resizable .ui-resizable-handle {
|
||||
display:block;
|
||||
}
|
||||
|
||||
.ui-resizable-e {
|
||||
background:url("ftv2splitbar.png") repeat scroll right center transparent;
|
||||
cursor:e-resize;
|
||||
height:100%;
|
||||
right:0;
|
||||
top:0;
|
||||
width:6px;
|
||||
}
|
||||
|
||||
.ui-resizable-handle {
|
||||
display:none;
|
||||
font-size:0.1px;
|
||||
position:absolute;
|
||||
z-index:1;
|
||||
}
|
||||
|
||||
#nav-tree-contents {
|
||||
margin: 6px 0px 0px 0px;
|
||||
}
|
||||
|
||||
#nav-tree {
|
||||
background-image:url('nav_h.png');
|
||||
background-repeat:repeat-x;
|
||||
background-color: #F9FAFC;
|
||||
-webkit-overflow-scrolling : touch; /* iOS 5+ */
|
||||
}
|
||||
|
||||
#nav-sync {
|
||||
position:absolute;
|
||||
top:5px;
|
||||
right:24px;
|
||||
z-index:0;
|
||||
}
|
||||
|
||||
#nav-sync img {
|
||||
opacity:0.3;
|
||||
}
|
||||
|
||||
#nav-sync img:hover {
|
||||
opacity:0.9;
|
||||
}
|
||||
|
||||
@media print
|
||||
{
|
||||
#nav-tree { display: none; }
|
||||
div.ui-resizable-handle { display: none; position: relative; }
|
||||
}
|
||||
|
||||
537
docs/html/navtree.js
Normal file
@@ -0,0 +1,537 @@
|
||||
var NAVTREE =
|
||||
[
|
||||
[ "FlatBuffers", "index.html", [
|
||||
[ "Building", "md__building.html", null ],
|
||||
[ "Using the schema compiler", "md__compiler.html", null ],
|
||||
[ "Writing a schema", "md__schemas.html", null ],
|
||||
[ "Use in C++", "md__cpp_usage.html", null ],
|
||||
[ "Use in Java", "md__java_usage.html", null ],
|
||||
[ "Benchmarks", "md__benchmarks.html", null ],
|
||||
[ "FlatBuffers white paper", "md__white_paper.html", null ],
|
||||
[ "FlatBuffer Internals", "md__internals.html", null ],
|
||||
[ "Formal Grammar of the schema language", "md__grammar.html", null ]
|
||||
] ]
|
||||
];
|
||||
|
||||
var NAVTREEINDEX =
|
||||
[
|
||||
"index.html"
|
||||
];
|
||||
|
||||
var SYNCONMSG = 'click to disable panel synchronisation';
|
||||
var SYNCOFFMSG = 'click to enable panel synchronisation';
|
||||
var SYNCONMSG = 'click to disable panel synchronisation';
|
||||
var SYNCOFFMSG = 'click to enable panel synchronisation';
|
||||
var navTreeSubIndices = new Array();
|
||||
|
||||
function getData(varName)
|
||||
{
|
||||
var i = varName.lastIndexOf('/');
|
||||
var n = i>=0 ? varName.substring(i+1) : varName;
|
||||
return eval(n.replace(/\-/g,'_'));
|
||||
}
|
||||
|
||||
function stripPath(uri)
|
||||
{
|
||||
return uri.substring(uri.lastIndexOf('/')+1);
|
||||
}
|
||||
|
||||
function stripPath2(uri)
|
||||
{
|
||||
var i = uri.lastIndexOf('/');
|
||||
var s = uri.substring(i+1);
|
||||
var m = uri.substring(0,i+1).match(/\/d\w\/d\w\w\/$/);
|
||||
return m ? uri.substring(i-6) : s;
|
||||
}
|
||||
|
||||
function localStorageSupported()
|
||||
{
|
||||
try {
|
||||
return 'localStorage' in window && window['localStorage'] !== null && window.localStorage.getItem;
|
||||
}
|
||||
catch(e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function storeLink(link)
|
||||
{
|
||||
if (!$("#nav-sync").hasClass('sync') && localStorageSupported()) {
|
||||
window.localStorage.setItem('navpath',link);
|
||||
}
|
||||
}
|
||||
|
||||
function deleteLink()
|
||||
{
|
||||
if (localStorageSupported()) {
|
||||
window.localStorage.setItem('navpath','');
|
||||
}
|
||||
}
|
||||
|
||||
function cachedLink()
|
||||
{
|
||||
if (localStorageSupported()) {
|
||||
return window.localStorage.getItem('navpath');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function getScript(scriptName,func,show)
|
||||
{
|
||||
var head = document.getElementsByTagName("head")[0];
|
||||
var script = document.createElement('script');
|
||||
script.id = scriptName;
|
||||
script.type = 'text/javascript';
|
||||
script.onload = func;
|
||||
script.src = scriptName+'.js';
|
||||
if ($.browser.msie && $.browser.version<=8) {
|
||||
// script.onload does not work with older versions of IE
|
||||
script.onreadystatechange = function() {
|
||||
if (script.readyState=='complete' || script.readyState=='loaded') {
|
||||
func(); if (show) showRoot();
|
||||
}
|
||||
}
|
||||
}
|
||||
head.appendChild(script);
|
||||
}
|
||||
|
||||
function createIndent(o,domNode,node,level)
|
||||
{
|
||||
var level=-1;
|
||||
var n = node;
|
||||
while (n.parentNode) { level++; n=n.parentNode; }
|
||||
if (node.childrenData) {
|
||||
var imgNode = document.createElement("img");
|
||||
imgNode.style.paddingLeft=(16*level).toString()+'px';
|
||||
imgNode.width = 16;
|
||||
imgNode.height = 22;
|
||||
imgNode.border = 0;
|
||||
node.plus_img = imgNode;
|
||||
node.expandToggle = document.createElement("a");
|
||||
node.expandToggle.href = "javascript:void(0)";
|
||||
node.expandToggle.onclick = function() {
|
||||
if (node.expanded) {
|
||||
$(node.getChildrenUL()).slideUp("fast");
|
||||
node.plus_img.src = node.relpath+"ftv2pnode.png";
|
||||
node.expanded = false;
|
||||
} else {
|
||||
expandNode(o, node, false, false);
|
||||
}
|
||||
}
|
||||
node.expandToggle.appendChild(imgNode);
|
||||
domNode.appendChild(node.expandToggle);
|
||||
imgNode.src = node.relpath+"ftv2pnode.png";
|
||||
} else {
|
||||
var span = document.createElement("span");
|
||||
span.style.display = 'inline-block';
|
||||
span.style.width = 16*(level+1)+'px';
|
||||
span.style.height = '22px';
|
||||
span.innerHTML = ' ';
|
||||
domNode.appendChild(span);
|
||||
}
|
||||
}
|
||||
|
||||
var animationInProgress = false;
|
||||
|
||||
function gotoAnchor(anchor,aname,updateLocation)
|
||||
{
|
||||
var pos, docContent = $('#doc-content');
|
||||
if (anchor.parent().attr('class')=='memItemLeft' ||
|
||||
anchor.parent().attr('class')=='fieldtype' ||
|
||||
anchor.parent().is(':header'))
|
||||
{
|
||||
pos = anchor.parent().position().top;
|
||||
} else if (anchor.position()) {
|
||||
pos = anchor.position().top;
|
||||
}
|
||||
if (pos) {
|
||||
var dist = Math.abs(Math.min(
|
||||
pos-docContent.offset().top,
|
||||
docContent[0].scrollHeight-
|
||||
docContent.height()-docContent.scrollTop()));
|
||||
animationInProgress=true;
|
||||
docContent.animate({
|
||||
scrollTop: pos + docContent.scrollTop() - docContent.offset().top
|
||||
},Math.max(50,Math.min(500,dist)),function(){
|
||||
if (updateLocation) window.location.href=aname;
|
||||
animationInProgress=false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function newNode(o, po, text, link, childrenData, lastNode)
|
||||
{
|
||||
var node = new Object();
|
||||
node.children = Array();
|
||||
node.childrenData = childrenData;
|
||||
node.depth = po.depth + 1;
|
||||
node.relpath = po.relpath;
|
||||
node.isLast = lastNode;
|
||||
|
||||
node.li = document.createElement("li");
|
||||
po.getChildrenUL().appendChild(node.li);
|
||||
node.parentNode = po;
|
||||
|
||||
node.itemDiv = document.createElement("div");
|
||||
node.itemDiv.className = "item";
|
||||
|
||||
node.labelSpan = document.createElement("span");
|
||||
node.labelSpan.className = "label";
|
||||
|
||||
createIndent(o,node.itemDiv,node,0);
|
||||
node.itemDiv.appendChild(node.labelSpan);
|
||||
node.li.appendChild(node.itemDiv);
|
||||
|
||||
var a = document.createElement("a");
|
||||
node.labelSpan.appendChild(a);
|
||||
node.label = document.createTextNode(text);
|
||||
node.expanded = false;
|
||||
a.appendChild(node.label);
|
||||
if (link) {
|
||||
var url;
|
||||
if (link.substring(0,1)=='^') {
|
||||
url = link.substring(1);
|
||||
link = url;
|
||||
} else {
|
||||
url = node.relpath+link;
|
||||
}
|
||||
a.className = stripPath(link.replace('#',':'));
|
||||
if (link.indexOf('#')!=-1) {
|
||||
var aname = '#'+link.split('#')[1];
|
||||
var srcPage = stripPath($(location).attr('pathname'));
|
||||
var targetPage = stripPath(link.split('#')[0]);
|
||||
a.href = srcPage!=targetPage ? url : "javascript:void(0)";
|
||||
a.onclick = function(){
|
||||
storeLink(link);
|
||||
if (!$(a).parent().parent().hasClass('selected'))
|
||||
{
|
||||
$('.item').removeClass('selected');
|
||||
$('.item').removeAttr('id');
|
||||
$(a).parent().parent().addClass('selected');
|
||||
$(a).parent().parent().attr('id','selected');
|
||||
}
|
||||
var anchor = $(aname);
|
||||
gotoAnchor(anchor,aname,true);
|
||||
};
|
||||
} else {
|
||||
a.href = url;
|
||||
a.onclick = function() { storeLink(link); }
|
||||
}
|
||||
} else {
|
||||
if (childrenData != null)
|
||||
{
|
||||
a.className = "nolink";
|
||||
a.href = "javascript:void(0)";
|
||||
a.onclick = node.expandToggle.onclick;
|
||||
}
|
||||
}
|
||||
|
||||
node.childrenUL = null;
|
||||
node.getChildrenUL = function() {
|
||||
if (!node.childrenUL) {
|
||||
node.childrenUL = document.createElement("ul");
|
||||
node.childrenUL.className = "children_ul";
|
||||
node.childrenUL.style.display = "none";
|
||||
node.li.appendChild(node.childrenUL);
|
||||
}
|
||||
return node.childrenUL;
|
||||
};
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
function showRoot()
|
||||
{
|
||||
var headerHeight = $("#top").height();
|
||||
var footerHeight = $("#nav-path").height();
|
||||
var windowHeight = $(window).height() - headerHeight - footerHeight;
|
||||
(function (){ // retry until we can scroll to the selected item
|
||||
try {
|
||||
var navtree=$('#nav-tree');
|
||||
navtree.scrollTo('#selected',0,{offset:-windowHeight/2});
|
||||
} catch (err) {
|
||||
setTimeout(arguments.callee, 0);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
function expandNode(o, node, imm, showRoot)
|
||||
{
|
||||
if (node.childrenData && !node.expanded) {
|
||||
if (typeof(node.childrenData)==='string') {
|
||||
var varName = node.childrenData;
|
||||
getScript(node.relpath+varName,function(){
|
||||
node.childrenData = getData(varName);
|
||||
expandNode(o, node, imm, showRoot);
|
||||
}, showRoot);
|
||||
} else {
|
||||
if (!node.childrenVisited) {
|
||||
getNode(o, node);
|
||||
} if (imm || ($.browser.msie && $.browser.version>8)) {
|
||||
// somehow slideDown jumps to the start of tree for IE9 :-(
|
||||
$(node.getChildrenUL()).show();
|
||||
} else {
|
||||
$(node.getChildrenUL()).slideDown("fast");
|
||||
}
|
||||
if (node.isLast) {
|
||||
node.plus_img.src = node.relpath+"ftv2mlastnode.png";
|
||||
} else {
|
||||
node.plus_img.src = node.relpath+"ftv2mnode.png";
|
||||
}
|
||||
node.expanded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function glowEffect(n,duration)
|
||||
{
|
||||
n.addClass('glow').delay(duration).queue(function(next){
|
||||
$(this).removeClass('glow');next();
|
||||
});
|
||||
}
|
||||
|
||||
function highlightAnchor()
|
||||
{
|
||||
var aname = $(location).attr('hash');
|
||||
var anchor = $(aname);
|
||||
if (anchor.parent().attr('class')=='memItemLeft'){
|
||||
var rows = $('.memberdecls tr[class$="'+
|
||||
window.location.hash.substring(1)+'"]');
|
||||
glowEffect(rows.children(),300); // member without details
|
||||
} else if (anchor.parents().slice(2).prop('tagName')=='TR') {
|
||||
glowEffect(anchor.parents('div.memitem'),1000); // enum value
|
||||
} else if (anchor.parent().attr('class')=='fieldtype'){
|
||||
glowEffect(anchor.parent().parent(),1000); // struct field
|
||||
} else if (anchor.parent().is(":header")) {
|
||||
glowEffect(anchor.parent(),1000); // section header
|
||||
} else {
|
||||
glowEffect(anchor.next(),1000); // normal member
|
||||
}
|
||||
gotoAnchor(anchor,aname,false);
|
||||
}
|
||||
|
||||
function selectAndHighlight(hash,n)
|
||||
{
|
||||
var a;
|
||||
if (hash) {
|
||||
var link=stripPath($(location).attr('pathname'))+':'+hash.substring(1);
|
||||
a=$('.item a[class$="'+link+'"]');
|
||||
}
|
||||
if (a && a.length) {
|
||||
a.parent().parent().addClass('selected');
|
||||
a.parent().parent().attr('id','selected');
|
||||
highlightAnchor();
|
||||
} else if (n) {
|
||||
$(n.itemDiv).addClass('selected');
|
||||
$(n.itemDiv).attr('id','selected');
|
||||
}
|
||||
if ($('#nav-tree-contents .item:first').hasClass('selected')) {
|
||||
$('#nav-sync').css('top','30px');
|
||||
} else {
|
||||
$('#nav-sync').css('top','5px');
|
||||
}
|
||||
showRoot();
|
||||
}
|
||||
|
||||
function showNode(o, node, index, hash)
|
||||
{
|
||||
if (node && node.childrenData) {
|
||||
if (typeof(node.childrenData)==='string') {
|
||||
var varName = node.childrenData;
|
||||
getScript(node.relpath+varName,function(){
|
||||
node.childrenData = getData(varName);
|
||||
showNode(o,node,index,hash);
|
||||
},true);
|
||||
} else {
|
||||
if (!node.childrenVisited) {
|
||||
getNode(o, node);
|
||||
}
|
||||
$(node.getChildrenUL()).css({'display':'block'});
|
||||
if (node.isLast) {
|
||||
node.plus_img.src = node.relpath+"ftv2mlastnode.png";
|
||||
} else {
|
||||
node.plus_img.src = node.relpath+"ftv2mnode.png";
|
||||
}
|
||||
node.expanded = true;
|
||||
var n = node.children[o.breadcrumbs[index]];
|
||||
if (index+1<o.breadcrumbs.length) {
|
||||
showNode(o,n,index+1,hash);
|
||||
} else {
|
||||
if (typeof(n.childrenData)==='string') {
|
||||
var varName = n.childrenData;
|
||||
getScript(n.relpath+varName,function(){
|
||||
n.childrenData = getData(varName);
|
||||
node.expanded=false;
|
||||
showNode(o,node,index,hash); // retry with child node expanded
|
||||
},true);
|
||||
} else {
|
||||
var rootBase = stripPath(o.toroot.replace(/\..+$/, ''));
|
||||
if (rootBase=="index" || rootBase=="pages" || rootBase=="search") {
|
||||
expandNode(o, n, true, true);
|
||||
}
|
||||
selectAndHighlight(hash,n);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
selectAndHighlight(hash);
|
||||
}
|
||||
}
|
||||
|
||||
function removeToInsertLater(element) {
|
||||
var parentNode = element.parentNode;
|
||||
var nextSibling = element.nextSibling;
|
||||
parentNode.removeChild(element);
|
||||
return function() {
|
||||
if (nextSibling) {
|
||||
parentNode.insertBefore(element, nextSibling);
|
||||
} else {
|
||||
parentNode.appendChild(element);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function getNode(o, po)
|
||||
{
|
||||
var insertFunction = removeToInsertLater(po.li);
|
||||
po.childrenVisited = true;
|
||||
var l = po.childrenData.length-1;
|
||||
for (var i in po.childrenData) {
|
||||
var nodeData = po.childrenData[i];
|
||||
po.children[i] = newNode(o, po, nodeData[0], nodeData[1], nodeData[2],
|
||||
i==l);
|
||||
}
|
||||
insertFunction();
|
||||
}
|
||||
|
||||
function gotoNode(o,subIndex,root,hash,relpath)
|
||||
{
|
||||
var nti = navTreeSubIndices[subIndex][root+hash];
|
||||
o.breadcrumbs = $.extend(true, [], nti ? nti : navTreeSubIndices[subIndex][root]);
|
||||
if (!o.breadcrumbs && root!=NAVTREE[0][1]) { // fallback: show index
|
||||
navTo(o,NAVTREE[0][1],"",relpath);
|
||||
$('.item').removeClass('selected');
|
||||
$('.item').removeAttr('id');
|
||||
}
|
||||
if (o.breadcrumbs) {
|
||||
o.breadcrumbs.unshift(0); // add 0 for root node
|
||||
showNode(o, o.node, 0, hash);
|
||||
}
|
||||
}
|
||||
|
||||
function navTo(o,root,hash,relpath)
|
||||
{
|
||||
var link = cachedLink();
|
||||
if (link) {
|
||||
var parts = link.split('#');
|
||||
root = parts[0];
|
||||
if (parts.length>1) hash = '#'+parts[1];
|
||||
else hash='';
|
||||
}
|
||||
if (hash.match(/^#l\d+$/)) {
|
||||
var anchor=$('a[name='+hash.substring(1)+']');
|
||||
glowEffect(anchor.parent(),1000); // line number
|
||||
hash=''; // strip line number anchors
|
||||
//root=root.replace(/_source\./,'.'); // source link to doc link
|
||||
}
|
||||
var url=root+hash;
|
||||
var i=-1;
|
||||
while (NAVTREEINDEX[i+1]<=url) i++;
|
||||
if (i==-1) { i=0; root=NAVTREE[0][1]; } // fallback: show index
|
||||
if (navTreeSubIndices[i]) {
|
||||
gotoNode(o,i,root,hash,relpath)
|
||||
} else {
|
||||
getScript(relpath+'navtreeindex'+i,function(){
|
||||
navTreeSubIndices[i] = eval('NAVTREEINDEX'+i);
|
||||
if (navTreeSubIndices[i]) {
|
||||
gotoNode(o,i,root,hash,relpath);
|
||||
}
|
||||
},true);
|
||||
}
|
||||
}
|
||||
|
||||
function showSyncOff(n,relpath)
|
||||
{
|
||||
n.html('<img src="'+relpath+'sync_off.png" title="'+SYNCOFFMSG+'"/>');
|
||||
}
|
||||
|
||||
function showSyncOn(n,relpath)
|
||||
{
|
||||
n.html('<img src="'+relpath+'sync_on.png" title="'+SYNCONMSG+'"/>');
|
||||
}
|
||||
|
||||
function toggleSyncButton(relpath)
|
||||
{
|
||||
var navSync = $('#nav-sync');
|
||||
if (navSync.hasClass('sync')) {
|
||||
navSync.removeClass('sync');
|
||||
showSyncOff(navSync,relpath);
|
||||
storeLink(stripPath2($(location).attr('pathname'))+$(location).attr('hash'));
|
||||
} else {
|
||||
navSync.addClass('sync');
|
||||
showSyncOn(navSync,relpath);
|
||||
deleteLink();
|
||||
}
|
||||
}
|
||||
|
||||
function initNavTree(toroot,relpath)
|
||||
{
|
||||
var o = new Object();
|
||||
o.toroot = toroot;
|
||||
o.node = new Object();
|
||||
o.node.li = document.getElementById("nav-tree-contents");
|
||||
o.node.childrenData = NAVTREE;
|
||||
o.node.children = new Array();
|
||||
o.node.childrenUL = document.createElement("ul");
|
||||
o.node.getChildrenUL = function() { return o.node.childrenUL; };
|
||||
o.node.li.appendChild(o.node.childrenUL);
|
||||
o.node.depth = 0;
|
||||
o.node.relpath = relpath;
|
||||
o.node.expanded = false;
|
||||
o.node.isLast = true;
|
||||
o.node.plus_img = document.createElement("img");
|
||||
o.node.plus_img.src = relpath+"ftv2pnode.png";
|
||||
o.node.plus_img.width = 16;
|
||||
o.node.plus_img.height = 22;
|
||||
|
||||
if (localStorageSupported()) {
|
||||
var navSync = $('#nav-sync');
|
||||
if (cachedLink()) {
|
||||
showSyncOff(navSync,relpath);
|
||||
navSync.removeClass('sync');
|
||||
} else {
|
||||
showSyncOn(navSync,relpath);
|
||||
}
|
||||
navSync.click(function(){ toggleSyncButton(relpath); });
|
||||
}
|
||||
|
||||
$(window).load(function(){
|
||||
navTo(o,toroot,window.location.hash,relpath);
|
||||
showRoot();
|
||||
});
|
||||
|
||||
$(window).bind('hashchange', function(){
|
||||
if (window.location.hash && window.location.hash.length>1){
|
||||
var a;
|
||||
if ($(location).attr('hash')){
|
||||
var clslink=stripPath($(location).attr('pathname'))+':'+
|
||||
$(location).attr('hash').substring(1);
|
||||
a=$('.item a[class$="'+clslink+'"]');
|
||||
}
|
||||
if (a==null || !$(a).parent().parent().hasClass('selected')){
|
||||
$('.item').removeClass('selected');
|
||||
$('.item').removeAttr('id');
|
||||
}
|
||||
var link=stripPath2($(location).attr('pathname'));
|
||||
navTo(o,link,$(location).attr('hash'),relpath);
|
||||
} else if (!animationInProgress) {
|
||||
$('#doc-content').scrollTop(0);
|
||||
$('.item').removeClass('selected');
|
||||
$('.item').removeAttr('id');
|
||||
navTo(o,toroot,window.location.hash,relpath);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
14
docs/html/navtreeindex0.js
Normal file
@@ -0,0 +1,14 @@
|
||||
var NAVTREEINDEX0 =
|
||||
{
|
||||
"index.html":[],
|
||||
"md__benchmarks.html":[5],
|
||||
"md__building.html":[0],
|
||||
"md__compiler.html":[1],
|
||||
"md__cpp_usage.html":[3],
|
||||
"md__grammar.html":[8],
|
||||
"md__internals.html":[7],
|
||||
"md__java_usage.html":[4],
|
||||
"md__schemas.html":[2],
|
||||
"md__white_paper.html":[6],
|
||||
"pages.html":[]
|
||||
};
|
||||
BIN
docs/html/open.png
Normal file
|
After Width: | Height: | Size: 123 B |
81
docs/html/pages.html
Normal file
@@ -0,0 +1,81 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<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.5"/>
|
||||
<title>FlatBuffers: Related Pages</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>
|
||||
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="resize.js"></script>
|
||||
<script type="text/javascript" src="navtree.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(initResizable);
|
||||
$(window).load(resizeHeight);
|
||||
</script>
|
||||
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td style="padding-left: 0.5em;">
|
||||
<div id="projectname">FlatBuffers
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
<div id="nav-tree-contents">
|
||||
<div id="nav-sync" class="sync"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="splitbar" style="-moz-user-select:none;"
|
||||
class="ui-resizable-handle">
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function(){initNavTree('pages.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Related Pages</div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock">Here is a list of all related documentation pages:</div><div class="directory">
|
||||
<table class="directory">
|
||||
<tr id="row_0_" class="even"><td class="entry"><img src="ftv2node.png" alt="o" width="16" height="22" /><a class="el" href="md__building.html" target="_self">Building</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_1_"><td class="entry"><img src="ftv2node.png" alt="o" width="16" height="22" /><a class="el" href="md__compiler.html" target="_self">Using the schema compiler</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_2_" class="even"><td class="entry"><img src="ftv2node.png" alt="o" width="16" height="22" /><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"><img src="ftv2node.png" alt="o" width="16" height="22" /><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"><img src="ftv2node.png" alt="o" width="16" height="22" /><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"><img src="ftv2node.png" alt="o" width="16" height="22" /><a class="el" href="md__benchmarks.html" target="_self">Benchmarks</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_6_" class="even"><td class="entry"><img src="ftv2node.png" alt="o" width="16" height="22" /><a class="el" href="md__white_paper.html" target="_self">FlatBuffers white paper</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_7_"><td class="entry"><img src="ftv2node.png" alt="o" width="16" height="22" /><a class="el" href="md__internals.html" target="_self">FlatBuffer Internals</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_8_" class="even"><td class="entry"><img src="ftv2lastnode.png" alt="\" width="16" height="22" /><a class="el" href="md__grammar.html" target="_self">Formal Grammar of the schema language</a></td><td class="desc"></td></tr>
|
||||
</table>
|
||||
</div><!-- directory -->
|
||||
</div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-49880327-7', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
93
docs/html/resize.js
Normal file
@@ -0,0 +1,93 @@
|
||||
var cookie_namespace = 'doxygen';
|
||||
var sidenav,navtree,content,header;
|
||||
|
||||
function readCookie(cookie)
|
||||
{
|
||||
var myCookie = cookie_namespace+"_"+cookie+"=";
|
||||
if (document.cookie)
|
||||
{
|
||||
var index = document.cookie.indexOf(myCookie);
|
||||
if (index != -1)
|
||||
{
|
||||
var valStart = index + myCookie.length;
|
||||
var valEnd = document.cookie.indexOf(";", valStart);
|
||||
if (valEnd == -1)
|
||||
{
|
||||
valEnd = document.cookie.length;
|
||||
}
|
||||
var val = document.cookie.substring(valStart, valEnd);
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function writeCookie(cookie, val, expiration)
|
||||
{
|
||||
if (val==undefined) return;
|
||||
if (expiration == null)
|
||||
{
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
|
||||
expiration = date.toGMTString();
|
||||
}
|
||||
document.cookie = cookie_namespace + "_" + cookie + "=" + val + "; expires=" + expiration+"; path=/";
|
||||
}
|
||||
|
||||
function resizeWidth()
|
||||
{
|
||||
var windowWidth = $(window).width() + "px";
|
||||
var sidenavWidth = $(sidenav).outerWidth();
|
||||
content.css({marginLeft:parseInt(sidenavWidth)+"px"});
|
||||
writeCookie('width',sidenavWidth, null);
|
||||
}
|
||||
|
||||
function restoreWidth(navWidth)
|
||||
{
|
||||
var windowWidth = $(window).width() + "px";
|
||||
content.css({marginLeft:parseInt(navWidth)+6+"px"});
|
||||
sidenav.css({width:navWidth + "px"});
|
||||
}
|
||||
|
||||
function resizeHeight()
|
||||
{
|
||||
var headerHeight = header.outerHeight();
|
||||
var footerHeight = footer.outerHeight();
|
||||
var windowHeight = $(window).height() - headerHeight - footerHeight;
|
||||
content.css({height:windowHeight + "px"});
|
||||
navtree.css({height:windowHeight + "px"});
|
||||
sidenav.css({height:windowHeight + "px",top: headerHeight+"px"});
|
||||
}
|
||||
|
||||
function initResizable()
|
||||
{
|
||||
header = $("#top");
|
||||
sidenav = $("#side-nav");
|
||||
content = $("#doc-content");
|
||||
navtree = $("#nav-tree");
|
||||
footer = $("#nav-path");
|
||||
$(".side-nav-resizable").resizable({resize: function(e, ui) { resizeWidth(); } });
|
||||
$(window).resize(function() { resizeHeight(); });
|
||||
var width = readCookie('width');
|
||||
if (width) { restoreWidth(width); } else { resizeWidth(); }
|
||||
resizeHeight();
|
||||
var url = location.href;
|
||||
var i=url.indexOf("#");
|
||||
if (i>=0) window.location.hash=url.substr(i);
|
||||
var _preventDefault = function(evt) { evt.preventDefault(); };
|
||||
$("#splitbar").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault);
|
||||
$(document).bind('touchmove',function(e){
|
||||
try {
|
||||
var target = e.target;
|
||||
while (target) {
|
||||
if ($(target).css('-webkit-overflow-scrolling')=='touch') return;
|
||||
target = target.parentNode;
|
||||
}
|
||||
e.preventDefault();
|
||||
} catch(err) {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
BIN
docs/html/sync_off.png
Normal file
|
After Width: | Height: | Size: 853 B |
BIN
docs/html/sync_on.png
Normal file
|
After Width: | Height: | Size: 845 B |
BIN
docs/html/tab_a.png
Normal file
|
After Width: | Height: | Size: 142 B |
BIN
docs/html/tab_b.png
Normal file
|
After Width: | Height: | Size: 169 B |
BIN
docs/html/tab_h.png
Normal file
|
After Width: | Height: | Size: 177 B |
BIN
docs/html/tab_s.png
Normal file
|
After Width: | Height: | Size: 184 B |
60
docs/html/tabs.css
Normal file
@@ -0,0 +1,60 @@
|
||||
.tabs, .tabs2, .tabs3 {
|
||||
background-image: url('tab_b.png');
|
||||
width: 100%;
|
||||
z-index: 101;
|
||||
font-size: 13px;
|
||||
font-family: 'Lucida Grande',Geneva,Helvetica,Arial,sans-serif;
|
||||
}
|
||||
|
||||
.tabs2 {
|
||||
font-size: 10px;
|
||||
}
|
||||
.tabs3 {
|
||||
font-size: 9px;
|
||||
}
|
||||
|
||||
.tablist {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: table;
|
||||
}
|
||||
|
||||
.tablist li {
|
||||
float: left;
|
||||
display: table-cell;
|
||||
background-image: url('tab_b.png');
|
||||
line-height: 36px;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.tablist a {
|
||||
display: block;
|
||||
padding: 0 20px;
|
||||
font-weight: bold;
|
||||
background-image:url('tab_s.png');
|
||||
background-repeat:no-repeat;
|
||||
background-position:right;
|
||||
color: #283A5D;
|
||||
text-shadow: 0px 1px 1px rgba(255, 255, 255, 0.9);
|
||||
text-decoration: none;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.tabs3 .tablist a {
|
||||
padding: 0 10px;
|
||||
}
|
||||
|
||||
.tablist a:hover {
|
||||
background-image: url('tab_h.png');
|
||||
background-repeat:repeat-x;
|
||||
color: #fff;
|
||||
text-shadow: 0px 1px 1px rgba(0, 0, 0, 1.0);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.tablist li.current a {
|
||||
background-image: url('tab_a.png');
|
||||
background-repeat:repeat-x;
|
||||
color: #fff;
|
||||
text-shadow: 0px 1px 1px rgba(0, 0, 0, 1.0);
|
||||
}
|
||||
49
docs/source/Benchmarks.md
Executable file
@@ -0,0 +1,49 @@
|
||||
# Benchmarks
|
||||
|
||||
Comparing against other serialization solutions, running on Windows 7
|
||||
64bit. We use the LITE runtime for Protocol Buffers (less code / lower
|
||||
overhead), and Rapid JSON, one of the fastest C++ JSON parsers around.
|
||||
|
||||
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).
|
||||
|
||||
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) |
|
||||
|--------------------------------------------------------|-----------------------|-----------------------|-----------------------|-----------------------|
|
||||
| Decode + Traverse + Dealloc (1 million times, seconds) | 0.08 | 305 | 583 | 105 |
|
||||
| Decode / Traverse / Dealloc (breakdown) | 0 / 0.08 / 0 | 220 / 3.6 / 81 | 294 / 0.9 / 287 | 70 / 0.08 / 35 |
|
||||
| Encode (1 million times, seconds) | 3.2 | 185 | 650 | 169 |
|
||||
| Wire format size (normal / zlib, bytes) | 344 / 220 | 228 / 174 | 1475 / 322 | 1029 / 298 |
|
||||
| Memory needed to store decoded wire (bytes / blocks) | 0 / 0 | 760 / 20 | 65689 / 40 | 328 / 1 |
|
||||
| Transient memory allocated during decode (KB) | 0 | 1 | 131 | 4 |
|
||||
| Generated source code size (KB) | 4 | 61 | 0 | 4 |
|
||||
| Field access in handwritten traversal code | accessors | accessors | manual error checking | accessors |
|
||||
| Library source code (KB) | 15 | some subset of 3800 | 87 | 43 |
|
||||
|
||||
### Some other serialization systems we compared against but did not benchmark (yet), in rough order of applicability:
|
||||
|
||||
- Cap'n'Proto promises to reduce Protocol Buffers much like FlatBuffers does,
|
||||
though with a more complicated binary encoding and less flexibility (no
|
||||
optional fields to allow deprecating fields or serializing with missing
|
||||
fields for which defaults exist).
|
||||
It currently also isn't fully cross-platform portable (lack of VS support).
|
||||
- msgpack: has very minimal forwards/backwards compatability support when used
|
||||
with the typed C++ interface. Also lacks VS2010 support.
|
||||
- Thrift: very similar to Protocol Buffers, but appears to be less efficient,
|
||||
and have more dependencies.
|
||||
- XML: typically even slower than JSON, but has the advantage that it can be
|
||||
parsed with a schema to reduce error-checking boilerplate code.
|
||||
- YAML: a superset of JSON and otherwise very similar. Used by e.g. Unity.
|
||||
- C# comes with built-in serialization functionality, as used by Unity also.
|
||||
Being tied to the language, and having no automatic versioning support
|
||||
limits its applicability.
|
||||
- Project Anarchy (the free mobile engine by Havok) comes with a serialization
|
||||
system, that however does no automatic versioning (have to code around new
|
||||
fields manually), is very much tied to the rest of the engine, and works
|
||||
without a schema to generate code (tied to your C++ class definition).
|
||||
|
||||
45
docs/source/Building.md
Executable file
@@ -0,0 +1,45 @@
|
||||
# Building
|
||||
|
||||
The system comes with a `cmake` file that should allow you to build the
|
||||
compiler `flatc` and the tests (optionally). For details on `cmake`, see
|
||||
<http://www.cmake.org>. In brief, depending on your platform, use one of
|
||||
e.g.:
|
||||
|
||||
cmake -G "Unix Makefiles"
|
||||
cmake -G "Visual Studio 10"
|
||||
cmake -G "Xcode"
|
||||
|
||||
Then, build as normal for your platform. This should result in a `flatc`
|
||||
executable, essential for the next steps.
|
||||
Note that to use clang instead of gcc, you may need to set up your environment
|
||||
variables, e.g.
|
||||
`CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -G "Unix Makefiles"`.
|
||||
|
||||
Optionally, run the `flattests` executable.
|
||||
to ensure everything is working correctly on your system. If this fails,
|
||||
please contact us!
|
||||
|
||||
The cmake file will also build two sample executables, `sample_binary` and
|
||||
`sample_text`, see the corresponding `.cpp` file in the samples directory.
|
||||
|
||||
There is an `android` directory that contains all you need to build the test
|
||||
executable on android (use the included `build_apk.sh` script, or use
|
||||
`ndk_build` / `adb` etc. as usual). Upon running, it will output to the log
|
||||
if tests succeeded or not.
|
||||
|
||||
There is usually no runtime to compile, as the code consists of a single
|
||||
header, `include/flatbuffers/flatbuffers.h`. You should add the
|
||||
`include` folder to your include paths. If you wish to be
|
||||
able to load schemas and/or parse text into binary buffers at runtime,
|
||||
you additionally need the other headers in `include/flatbuffers`. You must
|
||||
also compile/link `src/idl_parser.cpp` (and `src/idl_gen_text.cpp` if you
|
||||
also want to be able convert binary to text).
|
||||
|
||||
For applications on Google Play that integrate this library, usage is tracked.
|
||||
This tracking is done automatically using the embedded version string
|
||||
(flatbuffer_version_string), and helps us continue to optimize it.
|
||||
Aside from consuming a few extra bytes in your application binary, it shouldn't
|
||||
affect your application at all. We use this information to let us know if
|
||||
FlatBuffers is useful and if we should continue to invest in it. Since this is
|
||||
open source, you are free to remove the version string but we would appreciate
|
||||
if you would leave it in.
|
||||
22
docs/source/Compiler.md
Executable file
@@ -0,0 +1,22 @@
|
||||
# Using the schema compiler
|
||||
|
||||
Usage:
|
||||
|
||||
flatc [ -c ] [ -j ] [ -b ] [ -t ] file1 file2 ..
|
||||
|
||||
The files are read and parsed in order, and can contain either schemas
|
||||
or data (see below). Later files can make use of definitions in earlier
|
||||
files. Depending on the flags passed, additional files may
|
||||
be generated for each file processed:
|
||||
|
||||
- `-c` : Generate a C++ header for all definitions in this file (as
|
||||
`filename_generated.h`). Skips data.
|
||||
|
||||
- `-j` : Generate Java classes.
|
||||
|
||||
- `-b` : If data is contained in this file, generate a
|
||||
`filename_wire.bin` containing the binary flatbuffer.
|
||||
|
||||
- `-t` : If data is contained in this file, generate a
|
||||
`filename_wire.txt` (for debugging).
|
||||
|
||||
226
docs/source/CppUsage.md
Executable file
@@ -0,0 +1,226 @@
|
||||
# Use in C++
|
||||
|
||||
Assuming you have written a schema using the above language in say
|
||||
`mygame.fbs` (FlatBuffer Schema, though the extension doesn't matter),
|
||||
you've generated a C++ header called `mygame_generated.h` using the
|
||||
compiler (e.g. `flatc -c mygame.fbs`), you can now start using this in
|
||||
your program by including the header. As noted, this header relies on
|
||||
`flatbuffers/flatbuffers.h`, which should be in your include path.
|
||||
|
||||
### Writing in C++
|
||||
|
||||
To start creating a buffer, create an instance of `FlatBufferBuilder`
|
||||
which will contain the buffer as it grows:
|
||||
|
||||
FlatBufferBuilder fbb;
|
||||
|
||||
Before we serialize a Monster, we need to first serialize any objects
|
||||
that are contained there-in, i.e. we serialize the data tree using
|
||||
depth first, pre-order traversal. This is generally easy to do on
|
||||
any tree structures. For example:
|
||||
|
||||
auto name = fbb.CreateString("MyMonster");
|
||||
|
||||
unsigned char inv[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
auto inventory = fbb.CreateVector(inv, 10);
|
||||
|
||||
`CreateString` and `CreateVector` serialize these two built-in
|
||||
datatypes, and return offsets into the serialized data indicating where
|
||||
they are stored, such that `Monster` below can refer to them.
|
||||
|
||||
`CreateString` can also take an `std::string`, or a `const char *` with
|
||||
an explicit length, and is suitable for holding UTF-8 and binary
|
||||
data if needed.
|
||||
|
||||
`CreateVector` can also take an `std::vector`. 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 `CreateVectorOfStructs`
|
||||
instead.
|
||||
|
||||
Vec3 vec(1, 2, 3);
|
||||
|
||||
`Vec3` 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.
|
||||
|
||||
We have now serialized the non-scalar components of of the monster
|
||||
example, so we could create the monster something like this:
|
||||
|
||||
auto mloc = CreateMonster(fbb, &vec, 150, 80, name, inventory, Color_Red, Offset<void>(0), Any_NONE);
|
||||
|
||||
Note that we're passing `150` for the `mana` field, which happens to be the
|
||||
default value: this means the field will not actually be written to the buffer,
|
||||
since we'll get that value anyway when we query it. This is a nice space
|
||||
savings, since it is very common for fields to be at their default. It means
|
||||
we also don't need to be scared to add fields only used in a minority of cases,
|
||||
since they won't bloat up the buffer sizes if they're not actually used.
|
||||
|
||||
We do something similarly for the union field `test` by specifying a `0` offset
|
||||
and the `NONE` enum value (part of every union) to indicate we don't actually
|
||||
want to write this field.
|
||||
|
||||
Tables (like `Monster`) give you full flexibility on what fields you write
|
||||
(unlike `Vec3`, which always has all fields set because it is a `struct`).
|
||||
If you want even more control over this (i.e. skip fields even when they are
|
||||
not default), instead of the convenient `CreateMonster` call we can also
|
||||
build the object field-by-field manually:
|
||||
|
||||
MonsterBuilder mb(fbb);
|
||||
mb.add_pos(&vec);
|
||||
mb.add_hp(80);
|
||||
mb.add_name(name);
|
||||
mb.add_inventory(inventory);
|
||||
auto mloc = mb.Finish();
|
||||
|
||||
We start with a temporary helper class `MonsterBuilder` (which is
|
||||
defined in our generated code also), then call the various `add_`
|
||||
methods to set fields, and `Finish` to complete the object. This is
|
||||
pretty much the same code as you find inside `CreateMonster`, except
|
||||
we're leaving out a few fields. Fields may also be added in any order,
|
||||
though orderings with fields of the same size adjacent
|
||||
to each other most efficient in size, due to alignment. You should
|
||||
not nest these Builder classes (serialize your
|
||||
data in pre-order).
|
||||
|
||||
Regardless of whether you used `CreateMonster` or `MonsterBuilder`, you
|
||||
now have an offset to the root of your data, and you can finish the
|
||||
buffer using:
|
||||
|
||||
fbb.Finish(mloc);
|
||||
|
||||
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 `fbb.GetBufferPointer()`, and it's size from
|
||||
`fbb.GetSize()`.
|
||||
|
||||
`samples/sample_binary.cpp` is a complete code sample similar to
|
||||
the code above, that also includes the reading code below.
|
||||
|
||||
### Reading in C++
|
||||
|
||||
If you've received a buffer from somewhere (disk, network, etc.) you can
|
||||
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
|
||||
convenient accessors for all fields, e.g.
|
||||
|
||||
assert(monster->hp() == 80);
|
||||
assert(monster->mana() == 150); // default
|
||||
assert(strcmp(monster->name()->c_str(), "MyMonster") == 0);
|
||||
|
||||
These should all be true. Note that we never stored a `mana` value, so
|
||||
it will return the default.
|
||||
|
||||
To access sub-objects, in this case the `Vec3`:
|
||||
|
||||
auto pos = monster->pos();
|
||||
assert(pos);
|
||||
assert(pos->z() == 3);
|
||||
|
||||
If we had not set the `pos` field during serialization, it would be
|
||||
`NULL`.
|
||||
|
||||
Similarly, we can access elements of the inventory array:
|
||||
|
||||
auto inv = monster->inventory();
|
||||
assert(inv);
|
||||
assert(inv->Get(9) == 9);
|
||||
|
||||
### Direct memory access
|
||||
|
||||
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.
|
||||
|
||||
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 `sizeof()` and
|
||||
`memcpy` on the pointer to a struct, or even an array of structs.
|
||||
|
||||
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 `glVertexAttribPointer`
|
||||
in OpenGL or similar APIs.
|
||||
|
||||
It is important to note is that structs are still little endian on all
|
||||
machines, so only use tricks like this if you can guarantee you're not
|
||||
shipping on a big endian machine (an `assert(FLATBUFFERS_LITTLEENDIAN)`
|
||||
would be wise).
|
||||
|
||||
## Text & schema parsing
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
There are two ways to use text formats:
|
||||
|
||||
### Using the compiler as a conversion tool
|
||||
|
||||
This is the preferred path, as it doesn't require you to add any new
|
||||
code to your program, and is maximally efficient since you can ship with
|
||||
binary data. The disadvantage is that it is an extra step for your
|
||||
users/developers to perform, though you might be able to automate it.
|
||||
|
||||
flatc -b myschema.fbs mydata.json
|
||||
|
||||
This will generate the binary file `mydata_wire.bin` which can be loaded
|
||||
as before.
|
||||
|
||||
### Making your program capable of loading text directly
|
||||
|
||||
This gives you maximum flexibility. You could even opt to support both,
|
||||
i.e. check for both files, and regenerate the binary from text when
|
||||
required, otherwise just load the binary.
|
||||
|
||||
This option is currently only available for C++, or Java through JNI.
|
||||
|
||||
As mentioned in the section "Building" above, this technique requires
|
||||
you to link a few more files into your program, and you'll want to include
|
||||
`flatbuffers/idl.h`.
|
||||
|
||||
Load text (either a schema or json) into an in-memory buffer (there is a
|
||||
convenient `LoadFile()` utility function in `flatbuffers/util.h` if you
|
||||
wish). Construct a parser:
|
||||
|
||||
flatbuffers::Parser parser;
|
||||
|
||||
Now you can parse any number of text files in sequence:
|
||||
|
||||
parser.Parse(text_file.c_str());
|
||||
|
||||
This works similarly to how the command-line compiler works: a sequence
|
||||
of files parsed by the same `Parser` object allow later files to
|
||||
reference definitions in earlier files. Typically this means you first
|
||||
load a schema file (which populates `Parser` with definitions), followed
|
||||
by one or more JSON files.
|
||||
|
||||
If there were any parsing errors, `Parse` will return `false`, and
|
||||
`Parser::err` contains a human readable error string with a line number
|
||||
etc, which you should present to the creator of that file.
|
||||
|
||||
After each JSON file, the `Parser::fbb` member variable is the
|
||||
`FlatBufferBuilder` that contains the binary buffer version of that
|
||||
file, that you can access as described above.
|
||||
|
||||
`samples/sample_text.cpp` is a code sample showing the above operations.
|
||||
|
||||
### 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.
|
||||
|
||||
126
docs/source/FlatBuffers.md
Normal file
@@ -0,0 +1,126 @@
|
||||
# FlatBuffers
|
||||
|
||||
FlatBuffers is an efficient cross platform serialization library in for C++ and
|
||||
Java. 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).
|
||||
|
||||
## Why use FlatBuffers?
|
||||
|
||||
- **Access to serialized data without parsing/unpacking** - What sets
|
||||
FlatBuffers apart is that it represents hierarchical data in a flat
|
||||
binary buffer in such a way that it can still be accessed directly
|
||||
without parsing/unpacking, while also still supporting data
|
||||
structure evolution (forwards/backwards compatibility).
|
||||
|
||||
- **Memory efficiency and speed** - The only memory needed to access
|
||||
your data is that of the buffer. It requires 0 additional allocations.
|
||||
FlatBuffers is also very
|
||||
suitable for use with mmap (or streaming), requiring only part of the
|
||||
buffer to be in memory. Access is close to the speed of raw
|
||||
struct access with only one extra indirection (a kind of vtable) to
|
||||
allow for format evolution and optional fields. It is aimed at
|
||||
projects where spending time and space (many memory allocations) to
|
||||
be able to access or construct serialized data is undesirable, such
|
||||
as in games or any other performance sensitive applications. See the
|
||||
[benchmarks](md__benchmarks.html) for details.
|
||||
|
||||
- **Flexible** - Optional fields means not only do you get great
|
||||
forwards and backwards compatibility (increasingly important for
|
||||
long-lived games: don't have to update all data with each new
|
||||
version!). It also means you have a lot of choice in what data you
|
||||
write and what data you don't, and how you design data structures.
|
||||
|
||||
- **Tiny code footprint** - Small amounts of generated code, and just
|
||||
a single small header as the minimum dependency, which is very easy
|
||||
to integrate. Again, see the benchmark section for details.
|
||||
|
||||
- **Strongly typed** - Errors happen at compile time rather than
|
||||
manually having to write repetitive and error prone run-time checks.
|
||||
Useful code can be generated for you.
|
||||
|
||||
- **Convenient to use** - 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).
|
||||
|
||||
Java code supports object-reuse.
|
||||
|
||||
- **Cross platform C++11/Java 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 .. ?
|
||||
|
||||
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.
|
||||
|
||||
### But all the cool kids use JSON!
|
||||
|
||||
JSON is very readable (which is why we use it as our optional text
|
||||
format) and very convenient when used together with dynamically typed
|
||||
languages (such as JavaScript). When serializing data from statically
|
||||
typed languages, however, JSON not only has the obvious drawback of runtime
|
||||
inefficiency, but also forces you to write *more* code to access data
|
||||
(counterintuitively) due to its dynamic-typing serialization system.
|
||||
In this context, it is only a better choice for systems that have very
|
||||
little to no information ahead of time about what data needs to be stored.
|
||||
|
||||
Read more about the "why" of FlatBuffers in the
|
||||
[white paper](md__white_paper.html).
|
||||
|
||||
## Usage in brief
|
||||
|
||||
This section is a quick rundown of how to use this system. Subsequent
|
||||
sections provide a more in-depth usage guide.
|
||||
|
||||
- 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.
|
||||
|
||||
- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or Java
|
||||
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
|
||||
buffer recursively, often as simply as making a single function call.
|
||||
|
||||
- Store or send your buffer somewhere!
|
||||
|
||||
- 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 `object->field()`.
|
||||
|
||||
## In-depth documentation
|
||||
|
||||
- How to [build the compiler](md__building.html) and samples on various
|
||||
platforms.
|
||||
- How to [use the compiler](md__compiler.html).
|
||||
- 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
|
||||
programs.
|
||||
- Some [benchmarks](md__benchmarks.html) showing the advantage of using
|
||||
FlatBuffers.
|
||||
- A [white paper](md__white_paper.html) explaining the "why" of FlatBuffers.
|
||||
- A description of the [internals](md__internals.html) of FlatBuffers.
|
||||
- A formal [grammar](md__grammar.html) of the schema language.
|
||||
|
||||
## Online resources
|
||||
|
||||
- [github repository](http://github.com/google/flatbuffers)
|
||||
- [landing page](http://google.github.io/flatbuffers)
|
||||
- [FlatBuffers Google Group](http://group.google.com/group/flatbuffers)
|
||||
- [FlatBuffers Issues Tracker](http://github.com/google/flatbuffers/issues)
|
||||
30
docs/source/Grammar.md
Executable file
@@ -0,0 +1,30 @@
|
||||
# Formal Grammar of the schema language
|
||||
|
||||
schema = namespace\_decl | type\_decl | enum\_decl | root\_decl | object
|
||||
|
||||
namespace\_decl = `namespace` ident ( `.` ident )* `;`
|
||||
|
||||
type\_decl = ( `table` | `struct` ) ident metadata `{` field\_decl+ `}`
|
||||
|
||||
enum\_decl = ( `enum` | `union` ) ident [ `:` type ] metadata `{` commasep(
|
||||
enumval\_decl ) `}`
|
||||
|
||||
root\_decl = `root_type` ident `;`
|
||||
|
||||
field\_decl = type `:` ident [ `=` scalar ] metadata `;`
|
||||
|
||||
type = `bool` | `byte` | `ubyte` | `short` | `ushort` | `int` | `uint` |
|
||||
`float` | `long` | `ulong` | `double`
|
||||
| `string` | `[` type `]` | ident
|
||||
|
||||
enumval\_decl = ident [ `=` integer\_constant ]
|
||||
|
||||
metadata = [ `(` commasep( ident [ `:` scalar ] ) `)` ]
|
||||
|
||||
scalar = integer\_constant | float\_constant | `true` | `false`
|
||||
|
||||
object = { commasep( ident `:` value ) }
|
||||
|
||||
value = scalar | object | string\_constant | `[` commasep( value ) `]`
|
||||
|
||||
commasep(x) = [ x ( `,` x )\* ]
|
||||
244
docs/source/Internals.md
Executable file
@@ -0,0 +1,244 @@
|
||||
# FlatBuffer Internals
|
||||
|
||||
This section is entirely optional for the use of FlatBuffers. In normal
|
||||
usage, you should never need the information contained herein. If you're
|
||||
interested however, it should give you more of an appreciation of why
|
||||
FlatBuffers is both efficient and convenient.
|
||||
|
||||
### Format components
|
||||
|
||||
A FlatBuffer is a binary file and in-memory format consisting mostly of
|
||||
scalars of various sizes, all aligned to their own size. Each scalar is
|
||||
also always represented in little-endian format, as this corresponds to
|
||||
all commonly used CPUs today. FlatBuffers will also work on big-endian
|
||||
machines, but will be slightly slower because of additional
|
||||
byte-swap intrinsics.
|
||||
|
||||
On purpose, the format leaves a lot of details about where exactly
|
||||
things live in memory undefined, e.g. fields in a table can have any
|
||||
order, and objects to some extend can be stored in many orders. This is
|
||||
because the format doesn't need this information to be efficient, and it
|
||||
leaves room for optimization and extension (for example, fields can be
|
||||
packed in a way that is most compact). Instead, the format is defined in
|
||||
terms of offsets and adjacency only.
|
||||
|
||||
### Format identification
|
||||
|
||||
The format also doesn't contain information for format identification
|
||||
and versioning, which is also by design. FlatBuffers is a statically typed
|
||||
system, meaning the user of a buffer needs to know what kind of buffer
|
||||
it is. FlatBuffers can of course be wrapped inside other containers
|
||||
where needed, or you can use its union feature to dynamically identify
|
||||
multiple possible sub-objects stored. Additionally, it can be used
|
||||
together with the schema parser if full reflective capabilities are
|
||||
desired.
|
||||
|
||||
Versioning is something that is intrinsically part of the format (the
|
||||
optionality / extensibility of fields), so the format itself does not
|
||||
need a version number (it's a meta-format, in a sense). We're hoping
|
||||
that this format can accommodate all data needed. If format breaking
|
||||
changes are ever necessary, it would become a new kind of format rather
|
||||
than just a variation.
|
||||
|
||||
### Offsets
|
||||
|
||||
The most important and generic offset type (see `flatbuffers.h`) is
|
||||
`offset_t`, which is currently always a `uint32_t`, and is used to
|
||||
refer to all tables/unions/strings/vectors. 32bit is
|
||||
intentional, since we want to keep the format binary compatible between
|
||||
32 and 64bit systems, and a 64bit offset would bloat the size for almost
|
||||
all uses. A version of this format with 64bit (or 16bit) offsets is easy to set
|
||||
when needed. Unsigned means they can only point in one direction, which
|
||||
typically is forward (towards a higher memory location). Any backwards
|
||||
offsets will be explicitly marked as such.
|
||||
|
||||
The format starts with an `offset_t` to the root object in the buffer.
|
||||
|
||||
We have two kinds of objects, structs and tables.
|
||||
|
||||
### Structs
|
||||
|
||||
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.
|
||||
|
||||
### Tables
|
||||
|
||||
These start with an `soffset_t` to a vtable (signed version of
|
||||
`offset_t`, since vtables may be stored anywhere), followed by all the
|
||||
fields as aligned scalars. Unlike structs, not all fields need to be
|
||||
present. There is no set order and layout.
|
||||
|
||||
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.
|
||||
|
||||
The elements of a vtable are all of type `voffset_t`, which is currently
|
||||
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
|
||||
how many bytes to read to be able to access all fields of the object.
|
||||
The remaining elements are N the offsets, where N is the amount of field
|
||||
declared in the schema when the code that constructed this buffer was
|
||||
compiled (thus, the size of the table is N + 2).
|
||||
|
||||
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.
|
||||
|
||||
### Strings and Vectors
|
||||
|
||||
Strings are simply a vector of bytes, and are always
|
||||
null-terminated. Vectors are stored as contiguous aligned scalar
|
||||
elements prefixed by a count.
|
||||
|
||||
### Construction
|
||||
|
||||
The current implementation constructs these buffers backwards, since
|
||||
that significantly reduces the amount of bookkeeping and simplifies the
|
||||
construction API.
|
||||
|
||||
### Code example
|
||||
|
||||
Here's an example of the code that gets generated for the `samples/monster.fbs`.
|
||||
What follows is the entire file, broken up by comments:
|
||||
|
||||
// automatically generated, do not modify
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
namespace MyGame {
|
||||
namespace Sample {
|
||||
|
||||
Nested namespace support.
|
||||
|
||||
enum {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
static const char *names[] = { "Red", "Green", "Blue", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
|
||||
Enums and convenient reverse lookup.
|
||||
|
||||
enum {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesAny() {
|
||||
static const char *names[] = { "NONE", "Monster", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
|
||||
Unions share a lot with enums.
|
||||
|
||||
struct Vec3;
|
||||
struct Monster;
|
||||
|
||||
Predeclare all datatypes since there may be circular references.
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(4) Vec3 {
|
||||
private:
|
||||
float x_;
|
||||
float y_;
|
||||
float z_;
|
||||
|
||||
public:
|
||||
Vec3(float x, float y, float z)
|
||||
: x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)) {}
|
||||
|
||||
float x() const { return flatbuffers::EndianScalar(x_); }
|
||||
float y() const { return flatbuffers::EndianScalar(y_); }
|
||||
float z() const { return flatbuffers::EndianScalar(z_); }
|
||||
};
|
||||
STRUCT_END(Vec3, 12);
|
||||
|
||||
These ugly macros do a couple of things: they turn off any padding the compiler
|
||||
might normally do, since we add padding manually (though none in this example),
|
||||
and they enforce alignment chosen by FlatBuffers. This ensures the layout of
|
||||
this struct will look the same regardless of compiler and platform. Note that
|
||||
the fields are private: this is because these store little endian scalars
|
||||
regardless of platform (since this is part of the serialized data).
|
||||
`EndianScalar` then converts back and forth, which is a no-op on all current
|
||||
mobile and desktop platforms, and a single machine instruction on the few
|
||||
remaining big endian platforms.
|
||||
|
||||
struct Monster : 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); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
int8_t color() const { return GetField<int8_t>(16, 2); }
|
||||
};
|
||||
|
||||
Tables are a bit more complicated. A table accessor struct is used to point at
|
||||
the serialized data for a table, which always starts with an offset to its
|
||||
vtable. It derives from `Table`, which contains the `GetField` helper functions.
|
||||
GetField takes a vtable offset, and a default value. It will look in the vtable
|
||||
at that offset. If the offset is out of bounds (data from an older version) or
|
||||
the vtable entry is 0, the field is not present and the default is returned.
|
||||
Otherwise, it uses the entry as an offset into the table to locate the field.
|
||||
|
||||
struct MonsterBuilder {
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_pos(const Vec3 *pos) { fbb_.AddStruct(4, pos); }
|
||||
void add_mana(int16_t mana) { fbb_.AddElement<int16_t>(6, mana, 150); }
|
||||
void add_hp(int16_t hp) { fbb_.AddElement<int16_t>(8, hp, 100); }
|
||||
void add_name(flatbuffers::Offset<flatbuffers::String> name) { fbb_.AddOffset(10, name); }
|
||||
void add_inventory(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory) { fbb_.AddOffset(14, inventory); }
|
||||
void add_color(int8_t color) { fbb_.AddElement<int8_t>(16, color, 2); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
flatbuffers::Offset<Monster> Finish() { return flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 7)); }
|
||||
};
|
||||
|
||||
`MonsterBuilder` is the base helper struct to construct a table using a
|
||||
`FlatBufferBuilder`. You can add the fields in any order, and the `Finish`
|
||||
call will ensure the correct vtable gets generated.
|
||||
|
||||
inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const Vec3 *pos, int16_t mana, int16_t hp, flatbuffers::Offset<flatbuffers::String> name, flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory, int8_t color) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_inventory(inventory);
|
||||
builder_.add_name(name);
|
||||
builder_.add_pos(pos);
|
||||
builder_.add_hp(hp);
|
||||
builder_.add_mana(mana);
|
||||
builder_.add_color(color);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
`CreateMonster` is a convenience function that calls all functions in
|
||||
`MonsterBuilder` above for you. Note that if you pass values which are
|
||||
defaults as arguments, it will not actually construct that field, so
|
||||
you can probably use this function instead of the builder class in
|
||||
almost all cases.
|
||||
|
||||
inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
|
||||
|
||||
This function is only generated for the root table type, to be able to
|
||||
start traversing a FlatBuffer from a raw buffer pointer.
|
||||
|
||||
}; // namespace MyGame
|
||||
}; // namespace Sample
|
||||
|
||||
|
||||
79
docs/source/JavaUsage.md
Executable file
@@ -0,0 +1,79 @@
|
||||
# Use in Java
|
||||
|
||||
There's experimental support for reading FlatBuffers in Java. Generate code
|
||||
for Java with the `-j` option to `flatc`.
|
||||
|
||||
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
|
||||
the `getRootAsMonster` function:
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(data);
|
||||
Monster monster = Monster.getRootAsMonster(bb);
|
||||
|
||||
Now you can access values much like C++:
|
||||
|
||||
short hp = monster.hp();
|
||||
Vec3 pos = monster.pos();
|
||||
|
||||
Note that whenever you access a new object like in the `pos` example above,
|
||||
a new temporary accessor object gets created. If your code is very performance
|
||||
sensitive (you iterate through a lot of objects), there's a second `pos()`
|
||||
method to which you can pass a `Vec3` object you've already created. This allows
|
||||
you to reuse it across many calls and reduce the amount of object allocation (and
|
||||
thus garbage collection) your program does.
|
||||
|
||||
Sadly the string accessors currently always create a new string when accessed,
|
||||
since FlatBuffer's UTF-8 strings can't be read in-place by Java.
|
||||
|
||||
Vector access is also a bit different from C++: you pass an extra index
|
||||
to the vector field accessor. Then a second method with the same name
|
||||
suffixed by `_length` let's you know the number of elements you can access:
|
||||
|
||||
for (int i = 0; i < monster.inventory_length(); i++)
|
||||
monster.inventory(i); // do something here
|
||||
|
||||
You can also construct these buffers in Java using the static methods found
|
||||
in the generated code, and the FlatBufferBuilder class:
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder();
|
||||
|
||||
Create strings:
|
||||
|
||||
int str = fbb.createString("MyMonster");
|
||||
|
||||
Create a table with a struct contained therein:
|
||||
|
||||
Monster.startMonster(fbb);
|
||||
Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0, (byte)4, (short)5, (byte)6));
|
||||
Monster.addHp(fbb, (short)80);
|
||||
Monster.addName(fbb, str);
|
||||
Monster.addInventory(fbb, inv);
|
||||
Monster.addTest_type(fbb, (byte)1);
|
||||
Monster.addTest(fbb, mon2);
|
||||
Monster.addTest4(fbb, test4s);
|
||||
int mon = Monster.endMonster(fbb);
|
||||
|
||||
As you can see, the Java code for tables does not use a convenient
|
||||
`createMonster` call like the C++ code. 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).
|
||||
Structs do have convenient methods that even have arguments for nested structs.
|
||||
|
||||
Vectors also use this start/end pattern to allow vectors of both scalar types
|
||||
and structs:
|
||||
|
||||
Monster.startInventoryVector(fbb, 5);
|
||||
for (byte i = 4; i >=0; i--) fbb.addByte(i);
|
||||
int inv = fbb.endVector();
|
||||
|
||||
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. You write the elements backwards since the buffer
|
||||
is being constructed back to front.
|
||||
|
||||
## Text Parsing
|
||||
|
||||
There currently is no support for parsing text (Schema's and JSON) directly
|
||||
from Java, though you could use the C++ parser through JNI. Please see the
|
||||
C++ documentation for more on text parsing.
|
||||
198
docs/source/Schemas.md
Executable file
@@ -0,0 +1,198 @@
|
||||
# Writing a schema
|
||||
|
||||
The syntax of the schema language (aka IDL, Interface Definition
|
||||
Language) should look quite familiar to users of any of the C family of
|
||||
languages, and also to users of other IDLs. Let's look at an example
|
||||
first:
|
||||
|
||||
// example IDL file
|
||||
|
||||
namespace MyGame;
|
||||
|
||||
enum Color : byte { Red = 1, Green, Blue }
|
||||
|
||||
union Any { Monster, Weapon, Pickup }
|
||||
|
||||
struct Vec3 {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3;
|
||||
mana:short = 150;
|
||||
hp:short = 100;
|
||||
name:string;
|
||||
friendly:bool = false (deprecated, priority: 1);
|
||||
inventory:[ubyte];
|
||||
color:Color = Blue;
|
||||
test:Any;
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
|
||||
(Weapon & Pickup not defined as part of this example).
|
||||
|
||||
### Tables
|
||||
|
||||
Tables are the main way of defining objects in FlatBuffers, and consist
|
||||
of a name (here `Monster`) and a list of fields. Each field has a name,
|
||||
a type, and optionally a default value (if omitted, it defaults to 0 /
|
||||
NULL).
|
||||
|
||||
Each field is optional: It does not have to appear in the wire
|
||||
representation, and you can choose to omit fields for each individual
|
||||
object. As a result, you have the flexibility to add fields without fear of
|
||||
bloating your data. This design is also FlatBuffer's mechanism for forward
|
||||
and backwards compatibility. Note that:
|
||||
|
||||
- You can add new fields in the schema ONLY at the end of a table
|
||||
definition. Older data will still
|
||||
read correctly, and give you the default value when read. Older code
|
||||
will simply ignore the new field.
|
||||
|
||||
- You cannot delete fields you don't use anymore from the schema,
|
||||
but you can simply
|
||||
stop writing them into your data for almost the same effect.
|
||||
Additionally you can mark them as `deprecated` as in the example
|
||||
above, which will prevent the generation of accessors in the
|
||||
generated C++, as a way to enforce the field not being used any more.
|
||||
(careful: this may break code!).
|
||||
|
||||
- You may change field names and table names, if you're ok with your
|
||||
code breaking until you've renamed them there too.
|
||||
|
||||
|
||||
|
||||
### Structs
|
||||
|
||||
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 `Vec3`). 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).
|
||||
|
||||
### Types
|
||||
|
||||
Builtin scalar types are:
|
||||
|
||||
- 8 bit: `byte ubyte bool`
|
||||
|
||||
- 16 bit: `short ushort`
|
||||
|
||||
- 32 bit: `int uint float`
|
||||
|
||||
- 64 bit: `long ulong double`
|
||||
|
||||
- Vector of any other type (denoted with `[type]`). Nesting vectors
|
||||
require you wrap the inner vector in a struct/table rather than
|
||||
writing `[[type]]`.
|
||||
|
||||
- `string`, which may only hold UTF-8 or 7-bit ASCII. For other text encodings
|
||||
or general binary data use vectors (`[byte]` or `[ubyte]`) instead.
|
||||
|
||||
- References to other tables or structs, enums or unions (see
|
||||
below).
|
||||
|
||||
You can't change types of fields once they're used, with the exception
|
||||
of same-size data where a `reinterpret_cast` would give you a desirable result,
|
||||
e.g. you could change a `uint` to an `int` if no values in current data use the
|
||||
high bit yet.
|
||||
|
||||
### (Default) Values
|
||||
|
||||
Values are a sequence of digits, optionally followed by a `.` and more digits
|
||||
for float constants, and optionally prefixed by a `-`. Non-scalar defaults are
|
||||
currently not supported (always NULL).
|
||||
|
||||
You generally do not want to change default values after they're initially
|
||||
defined. Fields that have the default value are not actually stored in the
|
||||
serialized data but are generated in code, so when you change the default, you'd
|
||||
now get a different value than from code generated from an older version of
|
||||
the schema. There are situations however where this may be
|
||||
desirable, especially if you can ensure a simultaneous rebuild of
|
||||
all code.
|
||||
|
||||
### Enums
|
||||
|
||||
Define a sequence of named constants, each with a given value, or
|
||||
increasing by one from the previous one. The default first value
|
||||
is `0`. As you can see in the enum declaration, you specify the underlying
|
||||
integral type of the enum with `:` (in this case `byte`), which then determines
|
||||
the type of any fields declared with this enum type. If you omit the underlying
|
||||
type, it will be `short`.
|
||||
|
||||
### Unions
|
||||
|
||||
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 `_type` is generated that
|
||||
holds the corresponding enum value, allowing you to know which type to
|
||||
cast to at runtime.
|
||||
|
||||
### Namespaces
|
||||
|
||||
These will generate the corresponding namespace in C++ for all helper
|
||||
code, and packages in Java. You can use `.` to specify nested namespaces /
|
||||
packages.
|
||||
|
||||
### Root type
|
||||
|
||||
This declares what you consider to be the root table (or struct) of the
|
||||
serialized data.
|
||||
|
||||
### Comments & documentation
|
||||
|
||||
May be written as in most C-based languages. Additionally, a triple
|
||||
comment (`///`) 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.
|
||||
|
||||
### Attributes
|
||||
|
||||
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.
|
||||
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).
|
||||
|
||||
Current understood attributes:
|
||||
|
||||
- `deprecated` (on a field): do not generate accessors for this field
|
||||
anymore, code should stop using this data.
|
||||
- `original_order` (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.
|
||||
- `force_align: size` (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 `FlatBufferBuilder`).
|
||||
|
||||
## Gotchas
|
||||
|
||||
### Schemas and version control
|
||||
|
||||
FlatBuffers relies on new field declarations being added at the end, and earlier
|
||||
declarations to not be removed, but be marked deprecated when needed. We think
|
||||
this is an improvement over the manual number assignment that happens in
|
||||
Protocol Buffers.
|
||||
|
||||
One place where this is possibly problematic however is source control. If user
|
||||
A adds a field, generates new binary data with this new schema, then tries to
|
||||
commit both to source control after user B already committed a new field also,
|
||||
and just auto-merges the schema, the binary files are now invalid compared to
|
||||
the new schema.
|
||||
|
||||
The solution of course is that you should not be generating binary data before
|
||||
your schema changes have been committed, ensuring consistency with the rest of
|
||||
the world.
|
||||
|
||||
127
docs/source/WhitePaper.md
Executable file
@@ -0,0 +1,127 @@
|
||||
# FlatBuffers white paper
|
||||
|
||||
This document tries to shed some light on to the "why" of FlatBuffers, a
|
||||
new serialization library.
|
||||
|
||||
## Motivation
|
||||
|
||||
Back in the good old days, performance was all about instructions and
|
||||
cycles. Nowadays, processing units have run so far ahead of the memory
|
||||
subsystem, that making an efficient application should start and finish
|
||||
with thinking about memory. How much you use of it. How you lay it out
|
||||
and access it. How you allocate it. When you copy it.
|
||||
|
||||
Serialization is a pervasive activity in a lot programs, and a common
|
||||
source of memory inefficiency, with lots of temporary data structures
|
||||
needed to parse and represent data, and inefficient allocation patterns
|
||||
and locality.
|
||||
|
||||
If it would be possible to do serialization with no temporary objects,
|
||||
no additional allocation, no copying, and good locality, this could be
|
||||
of great value. The reason serialization systems usually don't manage
|
||||
this is because it goes counter to forwards/backwards compatability, and
|
||||
platform specifics like endianness and alignment.
|
||||
|
||||
FlatBuffers is what you get if you try anyway.
|
||||
|
||||
In particular, FlatBuffers focus is on mobile hardware (where memory
|
||||
size and memory bandwidth is even more constrained than on desktop
|
||||
hardware), and applications that have the highest performance needs:
|
||||
games.
|
||||
|
||||
## FlatBuffers
|
||||
|
||||
*This is a summary of FlatBuffers functionality, with some rationale.
|
||||
A more detailed description can be found in the FlatBuffers
|
||||
documentation.*
|
||||
|
||||
### Summary
|
||||
|
||||
A FlatBuffer is a binary buffer containing nested objects (structs,
|
||||
tables, vectors,..) organized using offsets so that the data can be
|
||||
traversed in-place just like any pointer-based data structure. Unlike
|
||||
most in-memory data structures however, it uses strict rules of
|
||||
alignment and endianness (always little) to ensure these buffers are
|
||||
cross platform. Additionally, for objects that are tables, FlatBuffers
|
||||
provides forwards/backwards compatibility and general optionality of
|
||||
fields, to support most forms of format evolution.
|
||||
|
||||
You define your object types in a schema, which can then be compiled to
|
||||
C++ or Java for low to zero overhead reading & writing.
|
||||
Optionally, JSON data can be dynamically parsed into buffers.
|
||||
|
||||
### Tables
|
||||
|
||||
Tables are the cornerstone of FlatBuffers, since format evolution is
|
||||
essential for most applications of serialization. Typically, dealing
|
||||
with format changes is something that can be done transparently during
|
||||
the parsing process of most serialization solutions out there.
|
||||
But a FlatBuffer isn't parsed before it is accessed.
|
||||
|
||||
Tables get around this by using an extra indirection to access fields,
|
||||
through a *vtable*. Each table comes with a vtable (which may be shared
|
||||
between multiple tables with the same layout), and contains information
|
||||
where fields for this particular kind of instance of vtable are stored.
|
||||
The vtable may also indicate that the field is not present (because this
|
||||
FlatBuffer was written with an older version of the software, of simply
|
||||
because the information was not necessary for this instance, or deemed
|
||||
deprecated), in which case a default value is returned.
|
||||
|
||||
Tables have a low overhead in memory (since vtables are small and
|
||||
shared) and in access cost (an extra indirection), but provide great
|
||||
flexibility. Tables may even cost less memory than the equivalent
|
||||
struct, since fields do not need to be stored when they are equal to
|
||||
their default.
|
||||
|
||||
FlatBuffers additionally offers "naked" structs, which do not offer
|
||||
forwards/backwards compatibility, but can be even smaller (useful for
|
||||
very small objects that are unlikely to change, like e.g. a coordinate
|
||||
pair or a RGBA color).
|
||||
|
||||
### Schemas
|
||||
|
||||
While schemas reduce some generality (you can't just read any data
|
||||
without having its schema), they have a lot of upsides:
|
||||
|
||||
- Most information about the format can be factored into the generated
|
||||
code, reducing memory needed to store data, and time to access it.
|
||||
|
||||
- The strong typing of the data definitions means less error
|
||||
checking/handling at runtime (less can go wrong).
|
||||
|
||||
- A schema enables us to access a buffer without parsing.
|
||||
|
||||
FlatBuffer schemas are fairly similar to those of the incumbent,
|
||||
Protocol Buffers, and generally should be readable to those familiar
|
||||
with the C family of languages. We chose to improve upon the features
|
||||
offered by .proto files in the following ways:
|
||||
|
||||
- Deprecation of fields instead of manual field id assignment.
|
||||
Extending an object in a .proto means hunting for a free slot among
|
||||
the numbers (preferring lower numbers since they have a more compact
|
||||
representation). Besides being inconvenient, it also makes removing
|
||||
fields problematic: you either have to keep them, not making it
|
||||
obvious that this field shouldn't be read/written anymore, and still
|
||||
generating accessors. Or you remove it, but now you risk that
|
||||
there's still old data around that uses that field by the time
|
||||
someone reuses that field id, with nasty consequences.
|
||||
|
||||
- Differentiating between tables and structs (see above). Effectively
|
||||
all table fields are `optional`, and all struct fields are
|
||||
`required`.
|
||||
|
||||
- Having a native vector type instead of `repeated`. This gives you a
|
||||
length without having to collect all items, and in the case of
|
||||
scalars provides for a more compact representation, and one that
|
||||
guarantees adjacency.
|
||||
|
||||
- Having a native `union` type instead of using a series of `optional`
|
||||
fields, all of which must be checked individually.
|
||||
|
||||
- Being able to define defaults for all scalars, instead of having to
|
||||
deal with their optionality at each access.
|
||||
|
||||
- A parser that can deal with both schemas and data definitions (JSON
|
||||
compatible) uniformly.
|
||||
|
||||
|
||||
2359
docs/source/doxyfile
Executable file
685
include/flatbuffers/flatbuffers.h
Normal file
@@ -0,0 +1,685 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FLATBUFFERS_H_
|
||||
#define FLATBUFFERS_H_
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#if __cplusplus <= 199711L && \
|
||||
(!defined(_MSC_VER) || _MSC_VER < 1600) && \
|
||||
(!defined(__GNUC__) || \
|
||||
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40603))
|
||||
#error A C++11 compatible compiler is required for FlatBuffers.
|
||||
#error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
|
||||
#endif
|
||||
|
||||
// The wire format uses a little endian encoding (since that's efficient for
|
||||
// the common platforms).
|
||||
#if !defined(FLATBUFFERS_LITTLEENDIAN)
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#ifdef __BIG_ENDIAN__
|
||||
#define FLATBUFFERS_LITTLEENDIAN 0
|
||||
#else
|
||||
#define FLATBUFFERS_LITTLEENDIAN 1
|
||||
#endif // __BIG_ENDIAN__
|
||||
#elif defined(_MSC_VER)
|
||||
#define FLATBUFFERS_LITTLEENDIAN 1
|
||||
#else
|
||||
#error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
|
||||
#endif
|
||||
#endif // !defined(FLATBUFFERS_LITTLEENDIAN)
|
||||
|
||||
#define FLATBUFFERS_VERSION_MAJOR 1
|
||||
#define FLATBUFFERS_VERSION_MINOR 0
|
||||
#define FLATBUFFERS_VERSION_REVISION 0
|
||||
#define FLATBUFFERS_STRING_EXPAND(X) #X
|
||||
#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// Our default offset / size type, 32bit on purpose on 64bit systems.
|
||||
// Also, using a consistent offset type maintains compatibility of serialized
|
||||
// offset values between 32bit and 64bit systems.
|
||||
typedef uint32_t uoffset_t;
|
||||
|
||||
// Signed offsets for references that can go in both directions.
|
||||
typedef int32_t soffset_t;
|
||||
|
||||
// Offset/index used in v-tables, can be changed to uint8_t in
|
||||
// format forks to save a bit of space if desired.
|
||||
typedef uint16_t voffset_t;
|
||||
|
||||
typedef uintmax_t largest_scalar_t;
|
||||
|
||||
// Wrapper for uoffset_t to allow safe template specialization.
|
||||
template<typename T> struct Offset {
|
||||
uoffset_t o;
|
||||
Offset() : o(0) {}
|
||||
explicit Offset(uoffset_t _o) : o(_o) {}
|
||||
Offset<void> Union() const { return Offset<void>(o); }
|
||||
};
|
||||
|
||||
inline void EndianCheck() {
|
||||
int endiantest = 1;
|
||||
// If this fails, see FLATBUFFERS_LITTLEENDIAN above.
|
||||
assert(*reinterpret_cast<char *>(&endiantest) == FLATBUFFERS_LITTLEENDIAN);
|
||||
(void)endiantest;
|
||||
}
|
||||
|
||||
template<typename T> T EndianScalar(T t) {
|
||||
#if FLATBUFFERS_LITTLEENDIAN
|
||||
return t;
|
||||
#else
|
||||
// If you're on the few remaining big endian platforms, we make the bold
|
||||
// assumption you're also on gcc/clang, and thus have bswap intrinsics:
|
||||
if (sizeof(T) == 1) { // Compile-time if-then's.
|
||||
return t;
|
||||
} else if (sizeof(T) == 2) {
|
||||
auto r = __builtin_bswap16(*reinterpret_cast<uint16_t *>(&t));
|
||||
return *reinterpret_cast<T *>(&r);
|
||||
} else if (sizeof(T) == 4) {
|
||||
auto r = __builtin_bswap32(*reinterpret_cast<uint32_t *>(&t));
|
||||
return *reinterpret_cast<T *>(&r);
|
||||
} else if (sizeof(T) == 8) {
|
||||
auto r = __builtin_bswap64(*reinterpret_cast<uint64_t *>(&t));
|
||||
return *reinterpret_cast<T *>(&r);
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename T> T ReadScalar(const void *p) {
|
||||
return EndianScalar(*reinterpret_cast<const T *>(p));
|
||||
}
|
||||
|
||||
template<typename T> void WriteScalar(void *p, T t) {
|
||||
*reinterpret_cast<T *>(p) = EndianScalar(t);
|
||||
}
|
||||
|
||||
template<typename T> size_t AlignOf() {
|
||||
#ifdef _MSC_VER
|
||||
return __alignof(T);
|
||||
#else
|
||||
return alignof(T);
|
||||
#endif
|
||||
}
|
||||
|
||||
// When we read serialized data from memory, in the case of most scalars,
|
||||
// we want to just read T, but in the case of Offset, we want to actually
|
||||
// perform the indirection and return a pointer.
|
||||
// The template specialization below does just that.
|
||||
// It is wrapped in a struct since function templates can't overload on the
|
||||
// return type like this.
|
||||
// The typedef is for the convenience of callers of this function
|
||||
// (avoiding the need for a trailing return decltype)
|
||||
template<typename T> struct IndirectHelper {
|
||||
typedef T return_type;
|
||||
static return_type Read(const uint8_t *p, uoffset_t i) {
|
||||
return EndianScalar((reinterpret_cast<const T *>(p))[i]);
|
||||
}
|
||||
};
|
||||
template<typename T> struct IndirectHelper<Offset<T>> {
|
||||
typedef const T *return_type;
|
||||
static return_type Read(const uint8_t *p, uoffset_t i) {
|
||||
p += i * sizeof(uoffset_t);
|
||||
return EndianScalar(reinterpret_cast<return_type>(
|
||||
p + ReadScalar<uoffset_t>(p)));
|
||||
}
|
||||
};
|
||||
template<typename T> struct IndirectHelper<const T *> {
|
||||
typedef const T &return_type;
|
||||
static return_type Read(const uint8_t *p, uoffset_t i) {
|
||||
return *reinterpret_cast<const T *>(p + i * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
// This is used as a helper type for accessing vectors.
|
||||
// Vector::data() assumes the vector elements start after the length field.
|
||||
template<typename T> class Vector {
|
||||
public:
|
||||
uoffset_t Length() const { return EndianScalar(length_); }
|
||||
|
||||
typedef typename IndirectHelper<T>::return_type return_type;
|
||||
|
||||
return_type Get(uoffset_t i) const {
|
||||
assert(i < Length());
|
||||
return IndirectHelper<T>::Read(Data(), i);
|
||||
}
|
||||
|
||||
const void *GetStructFromOffset(size_t o) const {
|
||||
return reinterpret_cast<const void *>(Data() + o);
|
||||
}
|
||||
|
||||
protected:
|
||||
// This class is only used to access pre-existing data. Don't ever
|
||||
// try to construct these manually.
|
||||
Vector();
|
||||
|
||||
const uint8_t *Data() const {
|
||||
return reinterpret_cast<const uint8_t *>(&length_ + 1);
|
||||
}
|
||||
|
||||
uoffset_t length_;
|
||||
};
|
||||
|
||||
struct String : public Vector<char> {
|
||||
const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
|
||||
};
|
||||
|
||||
// This is a minimal replication of std::vector<uint8_t> functionality,
|
||||
// except growing from higher to lower addresses. i.e push_back() inserts data
|
||||
// in the lowest address in the vector.
|
||||
class vector_downward {
|
||||
public:
|
||||
explicit vector_downward(uoffset_t initial_size)
|
||||
: reserved_(initial_size),
|
||||
buf_(new uint8_t[reserved_]),
|
||||
cur_(buf_ + reserved_) {
|
||||
assert((initial_size & (sizeof(largest_scalar_t) - 1)) == 0);
|
||||
}
|
||||
|
||||
~vector_downward() { delete[] buf_; }
|
||||
|
||||
void clear() { cur_ = buf_ + reserved_; }
|
||||
|
||||
uoffset_t growth_policy(uoffset_t size) {
|
||||
return (size / 2) & ~(sizeof(largest_scalar_t) - 1);
|
||||
}
|
||||
|
||||
uint8_t *make_space(uoffset_t len) {
|
||||
if (buf_ > cur_ - len) {
|
||||
auto old_size = size();
|
||||
reserved_ += std::max(len, growth_policy(reserved_));
|
||||
auto new_buf = new uint8_t[reserved_];
|
||||
auto new_cur = new_buf + reserved_ - old_size;
|
||||
memcpy(new_cur, cur_, old_size);
|
||||
cur_ = new_cur;
|
||||
delete[] buf_;
|
||||
buf_ = new_buf;
|
||||
}
|
||||
cur_ -= len;
|
||||
// Beyond this, signed offsets may not have enough range:
|
||||
// (FlatBuffers > 2GB not supported).
|
||||
assert(size() < (1UL << (sizeof(soffset_t) * 8 - 1)) - 1);
|
||||
return cur_;
|
||||
}
|
||||
|
||||
uoffset_t size() const {
|
||||
return static_cast<uoffset_t>(reserved_ - (cur_ - buf_));
|
||||
}
|
||||
|
||||
uint8_t *data() const { return cur_; }
|
||||
|
||||
uint8_t *data_at(uoffset_t offset) { return buf_ + reserved_ - offset; }
|
||||
|
||||
// push() & fill() are most frequently called with small byte counts (<= 4),
|
||||
// which is why we're using loops rather than calling memcpy/memset.
|
||||
void push(const uint8_t *bytes, size_t size) {
|
||||
auto dest = make_space(size);
|
||||
for (size_t i = 0; i < size; i++) dest[i] = bytes[i];
|
||||
}
|
||||
|
||||
void fill(size_t zero_pad_bytes) {
|
||||
auto dest = make_space(zero_pad_bytes);
|
||||
for (size_t i = 0; i < zero_pad_bytes; i++) dest[i] = 0;
|
||||
}
|
||||
|
||||
void pop(size_t bytes_to_remove) { cur_ += bytes_to_remove; }
|
||||
|
||||
private:
|
||||
uoffset_t reserved_;
|
||||
uint8_t *buf_;
|
||||
uint8_t *cur_; // Points at location between empty (below) and used (above).
|
||||
};
|
||||
|
||||
// Converts a Field ID to a virtual table offset.
|
||||
inline voffset_t FieldIndexToOffset(voffset_t field_id) {
|
||||
// Should correspond to what EndTable() below builds up.
|
||||
const int fixed_fields = 2; // Vtable size and Object Size.
|
||||
return (field_id + fixed_fields) * sizeof(voffset_t);
|
||||
}
|
||||
|
||||
// Computes how many bytes you'd have to pad to be able to write an
|
||||
// "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in
|
||||
// memory).
|
||||
inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
|
||||
return ((~buf_size) + 1) & (scalar_size - 1);
|
||||
}
|
||||
|
||||
// Helper class to hold data needed in creation of a flat buffer.
|
||||
// To serialize data, you typically call one of the Create*() functions in
|
||||
// the generated code, which in turn call a sequence of StartTable/PushElement/
|
||||
// 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 {
|
||||
public:
|
||||
explicit FlatBufferBuilder(uoffset_t initial_size = 1024)
|
||||
: buf_(initial_size), minalign_(1), force_defaults_(false) {
|
||||
offsetbuf_.reserve(16); // Avoid first few reallocs.
|
||||
vtables_.reserve(16);
|
||||
EndianCheck();
|
||||
flatbuffer_version_string =
|
||||
"FlatBuffers "
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
|
||||
}
|
||||
|
||||
// Reset all the state in this FlatBufferBuilder so it can be reused
|
||||
// to construct another buffer.
|
||||
void Clear() {
|
||||
buf_.clear();
|
||||
offsetbuf_.clear();
|
||||
vtables_.clear();
|
||||
}
|
||||
|
||||
// The current size of the serialized buffer, counting from the end.
|
||||
uoffset_t GetSize() const { return buf_.size(); }
|
||||
|
||||
// Get the serialized buffer (after you call Finish()).
|
||||
uint8_t *GetBufferPointer() const { return buf_.data(); }
|
||||
|
||||
const char *GetVersionString() { return flatbuffer_version_string; }
|
||||
|
||||
void ForceDefaults(bool fd) { force_defaults_ = fd; }
|
||||
|
||||
void Pad(size_t num_bytes) { buf_.fill(num_bytes); }
|
||||
|
||||
void Align(size_t elem_size) {
|
||||
if (elem_size > minalign_) minalign_ = elem_size;
|
||||
buf_.fill(PaddingBytes(buf_.size(), elem_size));
|
||||
}
|
||||
|
||||
void PushBytes(const uint8_t *bytes, size_t size) {
|
||||
buf_.push(bytes, size);
|
||||
}
|
||||
|
||||
void PopBytes(size_t amount) { buf_.pop(amount); }
|
||||
|
||||
template<typename T> void AssertScalarT() {
|
||||
// The code assumes power of 2 sizes and endian-swap-ability.
|
||||
static_assert(std::is_scalar<T>::value
|
||||
// The Offset<T> type is essentially a scalar but fails is_scalar.
|
||||
|| sizeof(T) == sizeof(Offset<void>),
|
||||
"T must be a scalar type");
|
||||
}
|
||||
|
||||
// Write a single aligned scalar to the buffer
|
||||
template<typename T> uoffset_t PushElement(T element) {
|
||||
AssertScalarT<T>();
|
||||
T litle_endian_element = EndianScalar(element);
|
||||
Align(sizeof(T));
|
||||
PushBytes(reinterpret_cast<uint8_t *>(&litle_endian_element), sizeof(T));
|
||||
return GetSize();
|
||||
}
|
||||
|
||||
template<typename T> uoffset_t PushElement(Offset<T> off) {
|
||||
// Special case for offsets: see ReferTo below.
|
||||
return PushElement(ReferTo(off.o));
|
||||
}
|
||||
|
||||
// When writing fields, we track where they are, so we can create correct
|
||||
// vtables later.
|
||||
void TrackField(voffset_t field, uoffset_t off) {
|
||||
FieldLoc fl = { off, field };
|
||||
offsetbuf_.push_back(fl);
|
||||
}
|
||||
|
||||
// Like PushElement, but additionally tracks the field this represents.
|
||||
template<typename T> void AddElement(voffset_t field, T e, T def) {
|
||||
// We don't serialize values equal to the default.
|
||||
if (e == def && !force_defaults_) return;
|
||||
auto off = PushElement(e);
|
||||
TrackField(field, off);
|
||||
}
|
||||
|
||||
template<typename T> void AddOffset(voffset_t field, Offset<T> off) {
|
||||
if (!off.o) return; // An offset of 0 means NULL, don't store.
|
||||
AddElement(field, ReferTo(off.o), static_cast<uoffset_t>(0));
|
||||
}
|
||||
|
||||
template<typename T> void AddStruct(voffset_t field, const T *structptr) {
|
||||
if (!structptr) return; // Default, don't store.
|
||||
Align(AlignOf<T>());
|
||||
PushBytes(reinterpret_cast<const uint8_t *>(structptr), sizeof(T));
|
||||
TrackField(field, GetSize());
|
||||
}
|
||||
|
||||
void AddStructOffset(voffset_t field, uoffset_t off) {
|
||||
TrackField(field, off);
|
||||
}
|
||||
|
||||
// Offsets initially are relative to the end of the buffer (downwards).
|
||||
// This function converts them to be relative to the current location
|
||||
// in the buffer (when stored here), pointing upwards.
|
||||
uoffset_t ReferTo(uoffset_t off) {
|
||||
Align(sizeof(uoffset_t)); // To ensure GetSize() below is correct.
|
||||
assert(off <= GetSize()); // Must refer to something already in buffer.
|
||||
return GetSize() - off + sizeof(uoffset_t);
|
||||
}
|
||||
|
||||
void NotNested() {
|
||||
// If you hit this, you're trying to construct an object when another
|
||||
// hasn't finished yet.
|
||||
assert(!offsetbuf_.size());
|
||||
}
|
||||
|
||||
// From generated code (or from the parser), we call StartTable/EndTable
|
||||
// with a sequence of AddElement calls in between.
|
||||
uoffset_t StartTable() {
|
||||
NotNested();
|
||||
return GetSize();
|
||||
}
|
||||
|
||||
// This finishes one serialized object by generating the vtable if it's a
|
||||
// table, comparing it against existing vtables, and writing the
|
||||
// resulting vtable offset.
|
||||
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);
|
||||
// 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:
|
||||
buf_.fill(numfields * sizeof(voffset_t));
|
||||
auto table_object_size = vtableoffsetloc - start;
|
||||
assert(table_object_size < 0x10000); // Vtable use 16bit offsets.
|
||||
PushElement<voffset_t>(table_object_size);
|
||||
PushElement<voffset_t>(FieldIndexToOffset(numfields));
|
||||
// Write the offsets into the table
|
||||
for (auto field_location = offsetbuf_.begin();
|
||||
field_location != offsetbuf_.end();
|
||||
++field_location) {
|
||||
auto pos = (vtableoffsetloc - field_location->off);
|
||||
// If this asserts, it means you've set a field twice.
|
||||
assert(!ReadScalar<voffset_t>(buf_.data() + field_location->id));
|
||||
WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
|
||||
}
|
||||
offsetbuf_.clear();
|
||||
auto vt1 = reinterpret_cast<voffset_t *>(buf_.data());
|
||||
auto vt1_size = *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;
|
||||
vt_use = *it;
|
||||
buf_.pop(GetSize() - vtableoffsetloc);
|
||||
break;
|
||||
}
|
||||
// If this is a new vtable, remember it.
|
||||
if (vt_use == GetSize()) {
|
||||
vtables_.push_back(vt_use);
|
||||
}
|
||||
// Fill the vtable offset we created above.
|
||||
// The offset points from the beginning of the object to where the
|
||||
// vtable is stored.
|
||||
// Offsets default direction is downward in memory for future format
|
||||
// flexibility (storing all vtables at the start of the file).
|
||||
WriteScalar(buf_.data_at(vtableoffsetloc),
|
||||
static_cast<soffset_t>(vt_use) -
|
||||
static_cast<soffset_t>(vtableoffsetloc));
|
||||
return vtableoffsetloc;
|
||||
}
|
||||
|
||||
uoffset_t StartStruct(size_t alignment) {
|
||||
Align(alignment);
|
||||
return GetSize();
|
||||
}
|
||||
|
||||
uoffset_t EndStruct() { return GetSize(); }
|
||||
|
||||
void ClearOffsets() { offsetbuf_.clear(); }
|
||||
|
||||
// Aligns such that when "len" bytes are written, an object can be written
|
||||
// after it with "alignment" without padding.
|
||||
void PreAlign(size_t len, size_t alignment) {
|
||||
buf_.fill(PaddingBytes(GetSize() + len, alignment));
|
||||
}
|
||||
template<typename T> void PreAlign(size_t len) {
|
||||
AssertScalarT<T>();
|
||||
PreAlign(len, sizeof(T));
|
||||
}
|
||||
|
||||
// Functions to store strings, which are allowed to contain any binary data.
|
||||
Offset<String> CreateString(const char *str, size_t len) {
|
||||
NotNested();
|
||||
PreAlign<uoffset_t>(len + 1); // Always 0-terminated.
|
||||
buf_.fill(1);
|
||||
PushBytes(reinterpret_cast<const uint8_t *>(str), len);
|
||||
PushElement(static_cast<uoffset_t>(len));
|
||||
return Offset<String>(GetSize());
|
||||
}
|
||||
|
||||
Offset<String> CreateString(const char *str) {
|
||||
return CreateString(str, strlen(str));
|
||||
}
|
||||
|
||||
Offset<String> CreateString(const std::string &str) {
|
||||
return CreateString(str.c_str(), str.length());
|
||||
}
|
||||
|
||||
uoffset_t EndVector(size_t len) {
|
||||
return PushElement(static_cast<uoffset_t>(len));
|
||||
}
|
||||
|
||||
void StartVector(size_t len, size_t elemsize) {
|
||||
PreAlign<uoffset_t>(len * elemsize);
|
||||
PreAlign(len * elemsize, elemsize); // Just in case elemsize > uoffset_t.
|
||||
}
|
||||
|
||||
uint8_t *ReserveElements(size_t len, size_t elemsize) {
|
||||
return buf_.make_space(len * elemsize);
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
|
||||
NotNested();
|
||||
StartVector(len, sizeof(T));
|
||||
auto i = len;
|
||||
do {
|
||||
PushElement(v[--i]);
|
||||
} while (i);
|
||||
return Offset<Vector<T>>(EndVector(len));
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<T>> CreateVector(const std::vector<T> &v){
|
||||
return CreateVector(&v[0], v.size());
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||
const T *v, size_t len) {
|
||||
NotNested();
|
||||
StartVector(len, AlignOf<T>());
|
||||
PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len);
|
||||
return Offset<Vector<const T *>>(EndVector(len));
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||
const std::vector<T> &v) {
|
||||
return CreateVector(&v[0], v.size());
|
||||
}
|
||||
|
||||
// Finish serializing a buffer by writing the root offset.
|
||||
template<typename T> void Finish(Offset<T> root) {
|
||||
// This will cause the whole buffer to be aligned.
|
||||
PreAlign(sizeof(uoffset_t), minalign_);
|
||||
PushElement(ReferTo(root.o)); // Location of root.
|
||||
}
|
||||
|
||||
private:
|
||||
struct FieldLoc {
|
||||
uoffset_t off;
|
||||
voffset_t id;
|
||||
};
|
||||
|
||||
vector_downward buf_;
|
||||
|
||||
// Accumulating offsets of table members while it is being built.
|
||||
std::vector<FieldLoc> offsetbuf_;
|
||||
|
||||
std::vector<uoffset_t> vtables_; // todo: Could make this into a map?
|
||||
|
||||
size_t minalign_;
|
||||
|
||||
bool force_defaults_; // Serialize values equal to their defaults anyway.
|
||||
|
||||
// String which identifies the current version of FlatBuffers.
|
||||
// flatbuffer_version_string is used by Google developers to identify which
|
||||
// applications uploaded to Google Play are using this library. This allows
|
||||
// the development team at Google to determine the popularity of the library.
|
||||
// How it works: Applications that are uploaded to the Google Play Store are
|
||||
// scanned for this version string. We track which applications are using it
|
||||
// to measure popularity. You are free to remove it (of course) but we would
|
||||
// appreciate if you left it in.
|
||||
const char *flatbuffer_version_string;
|
||||
};
|
||||
|
||||
// Helper to get a typed pointer to the root object contained in the buffer.
|
||||
template<typename T> const T *GetRoot(const void *buf) {
|
||||
EndianCheck();
|
||||
return reinterpret_cast<const T *>(reinterpret_cast<const uint8_t *>(buf) +
|
||||
EndianScalar(*reinterpret_cast<const uoffset_t *>(buf)));
|
||||
}
|
||||
|
||||
// "structs_" are flat structures that do not have an offset table, thus
|
||||
// always have all members present and do not support forwards/backwards
|
||||
// compatible extensions.
|
||||
|
||||
class Struct {
|
||||
public:
|
||||
template<typename T> T GetField(uoffset_t o) const {
|
||||
return ReadScalar<T>(&data_[o]);
|
||||
}
|
||||
|
||||
template<typename T> T GetPointer(uoffset_t o) const {
|
||||
auto p = &data_[o];
|
||||
return reinterpret_cast<T>(p + ReadScalar<uoffset_t>(p));
|
||||
}
|
||||
|
||||
template<typename T> T GetStruct(uoffset_t o) const {
|
||||
return reinterpret_cast<T>(&data_[o]);
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t data_[1];
|
||||
};
|
||||
|
||||
// "tables" use an offset table (possibly shared) that allows fields to be
|
||||
// omitted and added at will, but uses an extra indirection to read.
|
||||
class Table {
|
||||
public:
|
||||
// This gets the field offset for any of the functions below it, or 0
|
||||
// if the field was not present.
|
||||
voffset_t GetOptionalFieldOffset(voffset_t field) const {
|
||||
// The vtable offset is always at the start.
|
||||
auto vtable = &data_ - ReadScalar<soffset_t>(&data_);
|
||||
// The first element is the size of the vtable (fields + type id + itself).
|
||||
auto vtsize = ReadScalar<voffset_t>(vtable);
|
||||
// If the field we're accessing is outside the vtable, we're reading older
|
||||
// data, so it's the same as if the offset was 0 (not present).
|
||||
return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
|
||||
}
|
||||
|
||||
template<typename T> T GetField(voffset_t field, T defaultval) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return field_offset ? ReadScalar<T>(&data_[field_offset]) : defaultval;
|
||||
}
|
||||
|
||||
template<typename P> P GetPointer(voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
auto p = &data_[field_offset];
|
||||
return field_offset
|
||||
? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p))
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
template<typename P> P GetStruct(voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return field_offset ? reinterpret_cast<P>(&data_[field_offset]) : nullptr;
|
||||
}
|
||||
|
||||
template<typename T> void SetField(voffset_t field, T val) {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
// If this asserts, you're trying to set a field that's not there
|
||||
// (or should we return a bool instead?).
|
||||
// check if it exists first using CheckField()
|
||||
assert(field_offset);
|
||||
WriteScalar(&data_[field_offset], val);
|
||||
}
|
||||
|
||||
bool CheckField(voffset_t field) const {
|
||||
return GetOptionalFieldOffset(field) != 0;
|
||||
}
|
||||
|
||||
private:
|
||||
// private constructor & copy constructor: you obtain instances of this
|
||||
// class by pointing to existing data only
|
||||
Table() {};
|
||||
Table(const Table &other) {};
|
||||
|
||||
uint8_t data_[1];
|
||||
};
|
||||
|
||||
// Utility function for reverse lookups on the EnumNames*() functions
|
||||
// (in the generated C++ code)
|
||||
// names must be NULL terminated.
|
||||
inline size_t LookupEnum(const char **names, const char *name) {
|
||||
for (const char **p = names; *p; p++)
|
||||
if (!strcmp(*p, name))
|
||||
return p - names;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// These macros allow us to layout a struct with a guarantee that they'll end
|
||||
// up looking the same on different compilers and platforms.
|
||||
// It does this by disallowing the compiler to do any padding, and then
|
||||
// does padding itself by inserting extra padding fields that make every
|
||||
// element aligned to its own size.
|
||||
// Additionally, it manually sets the alignment of the struct as a whole,
|
||||
// which is typically its largest element, or a custom size set in the schema
|
||||
// by the force_align attribute.
|
||||
// These are used in the generated code only.
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define MANUALLY_ALIGNED_STRUCT(alignment) \
|
||||
__pragma(pack(1)); \
|
||||
struct __declspec(align(alignment))
|
||||
#define STRUCT_END(name, size) \
|
||||
__pragma(pack()); \
|
||||
static_assert(sizeof(name) == size, "compiler breaks packing rules");
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#define MANUALLY_ALIGNED_STRUCT(alignment) \
|
||||
_Pragma("pack(1)"); \
|
||||
struct __attribute__((aligned(alignment)))
|
||||
#define STRUCT_END(name, size) \
|
||||
_Pragma("pack()"); \
|
||||
static_assert(sizeof(name) == size, "compiler breaks packing rules");
|
||||
#else
|
||||
#error Unknown compiler, please define structure alignment macros
|
||||
#endif
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_H_
|
||||
317
include/flatbuffers/idl.h
Normal file
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FLATBUFFERS_IDL_H_
|
||||
#define FLATBUFFERS_IDL_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
// This file defines the data types representing a parsed IDL (Interface
|
||||
// Definition Language) / schema file.
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// The order of these matters for Is*() functions below.
|
||||
// Additionally, Parser::ParseType assumes bool..string is a contiguous range
|
||||
// of type tokens.
|
||||
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
|
||||
TD(NONE, "", uint8_t, byte ) \
|
||||
TD(UTYPE, "", uint8_t, byte ) /* begin scalars, ints */ \
|
||||
TD(BOOL, "bool", uint8_t, byte ) \
|
||||
TD(CHAR, "byte", int8_t, byte ) \
|
||||
TD(UCHAR, "ubyte", uint8_t, byte ) \
|
||||
TD(SHORT, "short", int16_t, short ) \
|
||||
TD(USHORT, "ushort", uint16_t, short ) \
|
||||
TD(INT, "int", int32_t, int ) \
|
||||
TD(UINT, "uint", uint32_t, int ) \
|
||||
TD(LONG, "long", int64_t, long ) \
|
||||
TD(ULONG, "ulong", uint64_t, long ) /* end ints */ \
|
||||
TD(FLOAT, "float", float, float ) /* begin floats */ \
|
||||
TD(DOUBLE, "double", double, double) /* end floats, scalars */
|
||||
#define FLATBUFFERS_GEN_TYPES_POINTER(TD) \
|
||||
TD(STRING, "string", Offset<void>, int) \
|
||||
TD(VECTOR, "", Offset<void>, int) \
|
||||
TD(STRUCT, "", Offset<void>, int) \
|
||||
TD(UNION, "", Offset<void>, int)
|
||||
|
||||
|
||||
// using these macros, we can now write code dealing with types just once, e.g.
|
||||
|
||||
/*
|
||||
switch (type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
// do something specific to CTYPE here
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
}
|
||||
*/
|
||||
|
||||
#define FLATBUFFERS_GEN_TYPES(TD) \
|
||||
FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
|
||||
FLATBUFFERS_GEN_TYPES_POINTER(TD)
|
||||
|
||||
// Create an enum for all the types above
|
||||
enum BaseType {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) BASE_TYPE_ ## ENUM,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
static_assert(sizeof(CTYPE) <= sizeof(largest_scalar_t), \
|
||||
"define largest_scalar_t as " #CTYPE);
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
|
||||
inline bool IsScalar (BaseType t) { return t >= BASE_TYPE_UTYPE &&
|
||||
t <= BASE_TYPE_DOUBLE; }
|
||||
inline bool IsInteger(BaseType t) { return t >= BASE_TYPE_UTYPE &&
|
||||
t <= BASE_TYPE_ULONG; }
|
||||
inline bool IsFloat (BaseType t) { return t == BASE_TYPE_FLOAT ||
|
||||
t == BASE_TYPE_DOUBLE; }
|
||||
|
||||
extern const char *const kTypeNames[];
|
||||
extern const char kTypeSizes[];
|
||||
|
||||
inline size_t SizeOf(BaseType t) {
|
||||
return kTypeSizes[t];
|
||||
}
|
||||
|
||||
struct StructDef;
|
||||
struct EnumDef;
|
||||
|
||||
// Represents any type in the IDL, which is a combination of the BaseType
|
||||
// and additional information for vectors/structs_.
|
||||
struct Type {
|
||||
explicit Type(BaseType _base_type = BASE_TYPE_NONE, StructDef *_sd = nullptr)
|
||||
: base_type(_base_type),
|
||||
element(BASE_TYPE_NONE),
|
||||
struct_def(_sd),
|
||||
enum_def(nullptr)
|
||||
{}
|
||||
|
||||
Type VectorType() const { return Type(element, struct_def); }
|
||||
|
||||
BaseType base_type;
|
||||
BaseType element; // only set if t == BASE_TYPE_VECTOR
|
||||
StructDef *struct_def; // only set if t or element == BASE_TYPE_STRUCT
|
||||
EnumDef *enum_def; // only set if t == BASE_TYPE_UNION / BASE_TYPE_UTYPE
|
||||
};
|
||||
|
||||
// Represents a parsed scalar value, it's type, and field offset.
|
||||
struct Value {
|
||||
Value() : constant("0"), offset(-1) {}
|
||||
|
||||
Type type;
|
||||
std::string constant;
|
||||
int offset;
|
||||
};
|
||||
|
||||
// Helper class that retains the original order of a set of identifiers and
|
||||
// also provides quick lookup.
|
||||
template<typename T> class SymbolTable {
|
||||
public:
|
||||
~SymbolTable() {
|
||||
for (auto it = vec.begin(); it != vec.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
}
|
||||
|
||||
bool Add(const std::string &name, T *e) {
|
||||
vec.emplace_back(e);
|
||||
auto it = dict.find(name);
|
||||
if (it != dict.end()) return true;
|
||||
dict[name] = e;
|
||||
return false;
|
||||
}
|
||||
|
||||
T *Lookup(const std::string &name) const {
|
||||
auto it = dict.find(name);
|
||||
return it == dict.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, T *> dict; // quick lookup
|
||||
|
||||
public:
|
||||
std::vector<T *> vec; // Used to iterate in order of insertion
|
||||
};
|
||||
|
||||
// Base class for all definition types (fields, structs_, enums_).
|
||||
struct Definition {
|
||||
Definition() : generated(false) {}
|
||||
|
||||
std::string name;
|
||||
std::string doc_comment;
|
||||
SymbolTable<Value> attributes;
|
||||
bool generated; // did we already output code for this definition?
|
||||
};
|
||||
|
||||
struct FieldDef : public Definition {
|
||||
FieldDef() : deprecated(false), padding(0) {}
|
||||
|
||||
Value value;
|
||||
bool deprecated;
|
||||
size_t padding; // bytes to always pad after this field
|
||||
};
|
||||
|
||||
struct StructDef : public Definition {
|
||||
StructDef()
|
||||
: fixed(false),
|
||||
predecl(true),
|
||||
sortbysize(true),
|
||||
minalign(1),
|
||||
bytesize(0)
|
||||
{}
|
||||
|
||||
void PadLastField(size_t minalign) {
|
||||
auto padding = PaddingBytes(bytesize, minalign);
|
||||
bytesize += padding;
|
||||
if (fields.vec.size()) fields.vec.back()->padding = padding;
|
||||
}
|
||||
|
||||
SymbolTable<FieldDef> fields;
|
||||
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.
|
||||
size_t minalign; // What the whole object needs to be aligned to.
|
||||
size_t bytesize; // Size if fixed.
|
||||
};
|
||||
|
||||
inline bool IsStruct(const Type &type) {
|
||||
return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed;
|
||||
}
|
||||
|
||||
inline size_t InlineSize(const Type &type) {
|
||||
return IsStruct(type) ? type.struct_def->bytesize : SizeOf(type.base_type);
|
||||
}
|
||||
|
||||
inline size_t InlineAlignment(const Type &type) {
|
||||
return IsStruct(type) ? type.struct_def->minalign : SizeOf(type.base_type);
|
||||
}
|
||||
|
||||
struct EnumVal {
|
||||
EnumVal(const std::string &_name, int _val)
|
||||
: name(_name), value(_val), struct_def(nullptr) {}
|
||||
|
||||
std::string name;
|
||||
std::string doc_comment;
|
||||
int value;
|
||||
StructDef *struct_def; // only set if this is a union
|
||||
};
|
||||
|
||||
struct EnumDef : public Definition {
|
||||
EnumDef() : is_union(false) {}
|
||||
|
||||
StructDef *ReverseLookup(int enum_idx) {
|
||||
assert(is_union);
|
||||
for (auto it = vals.vec.begin() + 1; it != vals.vec.end(); ++it) {
|
||||
if ((*it)->value == enum_idx) {
|
||||
return (*it)->struct_def;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SymbolTable<EnumVal> vals;
|
||||
bool is_union;
|
||||
Type underlying_type;
|
||||
};
|
||||
|
||||
class Parser {
|
||||
public:
|
||||
Parser() :
|
||||
root_struct_def(nullptr),
|
||||
source_(nullptr),
|
||||
cursor_(nullptr),
|
||||
line_(1) {}
|
||||
|
||||
// Parse the string containing either schema or JSON data, which will
|
||||
// populate the SymbolTable's or the FlatBufferBuilder above.
|
||||
bool Parse(const char *_source);
|
||||
|
||||
// Set the root type. May override the one set in the schema.
|
||||
bool SetRootType(const char *name);
|
||||
|
||||
private:
|
||||
void Next();
|
||||
bool IsNext(int t);
|
||||
void Expect(int t);
|
||||
void ParseType(Type &type);
|
||||
FieldDef &AddField(StructDef &struct_def,
|
||||
const std::string &name,
|
||||
const Type &type);
|
||||
void ParseField(StructDef &struct_def);
|
||||
void ParseAnyValue(Value &val, FieldDef *field);
|
||||
uoffset_t ParseTable(const StructDef &struct_def);
|
||||
void SerializeStruct(const StructDef &struct_def, const Value &val);
|
||||
void AddVector(bool sortbysize, int count);
|
||||
uoffset_t ParseVector(const Type &type);
|
||||
void ParseMetaData(Definition &def);
|
||||
bool TryTypedValue(int dtoken, bool check, Value &e, BaseType req);
|
||||
void ParseSingleValue(Value &e);
|
||||
StructDef *LookupCreateStruct(const std::string &name);
|
||||
void ParseEnum(bool is_union);
|
||||
void ParseDecl();
|
||||
|
||||
public:
|
||||
SymbolTable<StructDef> structs_;
|
||||
SymbolTable<EnumDef> enums_;
|
||||
std::vector<std::string> name_space_; // As set in the schema.
|
||||
std::string error_; // User readable error_ if Parse() == false
|
||||
|
||||
FlatBufferBuilder builder_; // any data contained in the file
|
||||
StructDef *root_struct_def;
|
||||
|
||||
private:
|
||||
const char *source_, *cursor_;
|
||||
int line_; // the current line being parsed
|
||||
int token_;
|
||||
std::string attribute_, doc_comment_;
|
||||
|
||||
std::vector<std::pair<Value, FieldDef *>> field_stack_;
|
||||
std::vector<uint8_t> struct_stack_;
|
||||
};
|
||||
|
||||
// Generate text (JSON) from a given FlatBuffer, and a given Parser
|
||||
// object that has been populated with the corresponding schema.
|
||||
// See idl_gen_text.cpp.
|
||||
extern void GenerateText(const Parser &parser,
|
||||
const void *flatbuffer,
|
||||
int indent_step,
|
||||
std::string *text);
|
||||
|
||||
// Generate a C++ header from the definitions in the Parser object.
|
||||
// See idl_gen_cpp.
|
||||
extern std::string GenerateCPP(const Parser &parser);
|
||||
extern bool GenerateCPP(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
|
||||
// Generate Java files from the definitions in the Parser object.
|
||||
// See idl_gen_java.cpp.
|
||||
extern bool GenerateJava(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_IDL_H_
|
||||
|
||||
99
include/flatbuffers/util.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FLATBUFFERS_UTIL_H_
|
||||
#define FLATBUFFERS_UTIL_H_
|
||||
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// Convert an integer or floating point value to a string.
|
||||
// In contrast to std::stringstream, "char" values are
|
||||
// converted to a string of digits.
|
||||
template<typename T> std::string NumToString(T t) {
|
||||
// to_string() prints different numbers of digits for floats depending on
|
||||
// platform and isn't available on Android, so we use stringstream
|
||||
std::stringstream ss;
|
||||
if (sizeof(T) > 1) ss << t;
|
||||
else ss << static_cast<int>(t); // Avoid char types used as character data.
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// Convert an integer value to a hexadecimal string.
|
||||
// The returned string length is the number of nibbles in
|
||||
// the supplied value prefixed by 0 digits. For example,
|
||||
// IntToStringHex(static_cast<int>(0x23)) returns the
|
||||
// string "00000023".
|
||||
template<typename T> std::string IntToStringHex(T i) {
|
||||
std::stringstream ss;
|
||||
ss << std::setw(sizeof(T) * 2)
|
||||
<< std::setfill('0')
|
||||
<< std::hex
|
||||
<< std::uppercase
|
||||
<< i;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// Portable implementation of strtoull().
|
||||
inline int64_t StringToInt(const char *str) {
|
||||
#ifdef _MSC_VER
|
||||
return _strtoui64(str, nullptr, 10);
|
||||
#else
|
||||
return strtoull(str, nullptr, 10);
|
||||
#endif
|
||||
}
|
||||
|
||||
// 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
|
||||
// no transcoding.
|
||||
inline bool LoadFile(const char *name, bool binary, std::string *buf) {
|
||||
std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in);
|
||||
if (!ifs.is_open()) return false;
|
||||
*buf = std::string(std::istreambuf_iterator<char>(ifs),
|
||||
std::istreambuf_iterator<char>());
|
||||
return !ifs.bad();
|
||||
}
|
||||
|
||||
// Save data "buf" of length "len" bytes into a file
|
||||
// "name" returning true if successful, false otherwise.
|
||||
// If "binary" is false data is written using ifstream's
|
||||
// text mode, otherwise data is written with no
|
||||
// transcoding.
|
||||
inline bool SaveFile(const char *name, const char *buf, size_t len,
|
||||
bool binary) {
|
||||
std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
|
||||
if (!ofs.is_open()) return false;
|
||||
ofs.write(buf, len);
|
||||
return !ofs.bad();
|
||||
}
|
||||
|
||||
// Save data "buf" into file "name" returning true if
|
||||
// successful, false otherwise. If "binary" is false
|
||||
// data is written using ifstream's text mode, otherwise
|
||||
// data is written with no transcoding.
|
||||
inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
|
||||
return SaveFile(name, buf.c_str(), buf.size(), binary);
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_UTIL_H_
|
||||
248
java/flatbuffers/FlatBufferBuilder.java
Executable file
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
* 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 flatbuffers;
|
||||
|
||||
import java.lang.String;
|
||||
import java.util.Arrays;
|
||||
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.
|
||||
|
||||
public class FlatBufferBuilder {
|
||||
ByteBuffer bb; // Where we construct the FlatBuffer.
|
||||
int space; // Remaining space in the ByteBuffer.
|
||||
final Charset utf8charset = Charset.forName("UTF-8");
|
||||
int minalign = 1; // Minimum alignment encountered so far.
|
||||
int[] vtable; // The vtable for the current table, null otherwise.
|
||||
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.
|
||||
|
||||
// Java doesn't seem to have these.
|
||||
final int SIZEOF_SHORT = 2;
|
||||
final int SIZEOF_INT = 4;
|
||||
|
||||
// Start with a buffer of size `initial_size`, then grow as required.
|
||||
public FlatBufferBuilder(int initial_size) {
|
||||
space = initial_size;
|
||||
bb = newByteBuffer(new byte[initial_size]);
|
||||
}
|
||||
|
||||
ByteBuffer newByteBuffer(byte[] buf) {
|
||||
ByteBuffer newbb = ByteBuffer.wrap(buf);
|
||||
newbb.order(ByteOrder.LITTLE_ENDIAN);
|
||||
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).
|
||||
ByteBuffer growByteBuffer(ByteBuffer bb) {
|
||||
byte[] old_buf = bb.array();
|
||||
int old_buf_size = old_buf.length;
|
||||
int new_buf_size = old_buf_size * 2;
|
||||
byte[] new_buf = new byte[new_buf_size];
|
||||
System.arraycopy(old_buf, 0, new_buf, new_buf_size - old_buf_size, old_buf_size);
|
||||
ByteBuffer nbb = newByteBuffer(new_buf);
|
||||
nbb.position(bb.position());
|
||||
return nbb;
|
||||
}
|
||||
|
||||
// Offset relative to the end of the buffer.
|
||||
public int offset() {
|
||||
return bb.array().length - space;
|
||||
}
|
||||
|
||||
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.
|
||||
public void prep(int size, int additional_bytes) {
|
||||
// Track the biggest thing we've ever aligned to.
|
||||
if (size > minalign) minalign = size;
|
||||
// Find the amount of alignment needed such that `size` is properly
|
||||
// aligned after `additional_bytes`
|
||||
int align_size = ((~(bb.array().length - space + additional_bytes)) + 1) & (size - 1);
|
||||
// Reallocate the buffer if needed.
|
||||
while (space < align_size + size + additional_bytes) {
|
||||
int old_buf_size = bb.array().length;
|
||||
bb = growByteBuffer(bb);
|
||||
space += bb.array().length - old_buf_size;
|
||||
}
|
||||
pad(align_size);
|
||||
}
|
||||
|
||||
// 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); }
|
||||
|
||||
// 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); }
|
||||
|
||||
// Adds on offset, relative to where it will be written.
|
||||
public void addOffset(int off) {
|
||||
prep(SIZEOF_INT, 0); // Ensure alignment is already done.
|
||||
assert off <= offset();
|
||||
off = offset() - off + SIZEOF_INT;
|
||||
putInt(off);
|
||||
}
|
||||
|
||||
public void startVector(int elem_size, int num_elems) {
|
||||
notNested();
|
||||
vector_num_elems = num_elems;
|
||||
prep(SIZEOF_INT, elem_size * num_elems);
|
||||
}
|
||||
|
||||
public int endVector() {
|
||||
putInt(vector_num_elems);
|
||||
return offset();
|
||||
}
|
||||
|
||||
public int createString(String s) {
|
||||
byte[] utf8 = s.getBytes(utf8charset);
|
||||
bb.put(--space, (byte)0);
|
||||
startVector(1, utf8.length);
|
||||
System.arraycopy(utf8, 0, bb.array(), space -= utf8.length, utf8.length);
|
||||
return endVector();
|
||||
}
|
||||
|
||||
public void notNested() {
|
||||
// You should not be creating any other objects or strings/vectors
|
||||
// while an object is being constructed
|
||||
if (vtable != null)
|
||||
throw new AssertionError("FlatBuffers: object serialization must not be nested.");
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
public void startObject(int numfields) {
|
||||
notNested();
|
||||
vtable = new int[numfields];
|
||||
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); } }
|
||||
|
||||
// Structs are stored inline, so nothing additional is being added. `d` is always 0.
|
||||
public void addStruct(int voffset, int x, int d) {
|
||||
if(x != d) {
|
||||
Nested(x);
|
||||
slot(voffset);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the current vtable at `voffset` to the current location in the buffer.
|
||||
public void slot(int voffset) {
|
||||
vtable[voffset] = offset();
|
||||
}
|
||||
|
||||
public int endObject() {
|
||||
assert vtable != null; // calling endObject without a startObject
|
||||
addInt(0);
|
||||
int vtableloc = offset();
|
||||
// Write out the current vtable.
|
||||
for (int i = vtable.length - 1; i >= 0 ; i--) {
|
||||
// Offset relative to the start of the table.
|
||||
short off = (short)(vtable[i] != 0 ? vtableloc - vtable[i] : 0);
|
||||
putShort(off);
|
||||
}
|
||||
|
||||
final int standard_fields = 2; // The fields below:
|
||||
putShort((short)(vtableloc - object_start));
|
||||
putShort((short)((vtable.length + standard_fields) * SIZEOF_SHORT));
|
||||
|
||||
// Search for an existing vtable that matches the current one.
|
||||
int existing_vtable = 0;
|
||||
outer_loop:
|
||||
for (int i = 0; i < num_vtables; i++) {
|
||||
int vt1 = bb.array().length - vtables[i];
|
||||
int vt2 = space;
|
||||
short len = bb.getShort(vt1);
|
||||
if (len == bb.getShort(vt2)) {
|
||||
for (int j = SIZEOF_SHORT; j < len; j += SIZEOF_SHORT) {
|
||||
if (bb.getShort(vt1 + j) != bb.getShort(vt2 + j)) {
|
||||
continue outer_loop;
|
||||
}
|
||||
}
|
||||
existing_vtable = vtables[i];
|
||||
break outer_loop;
|
||||
}
|
||||
}
|
||||
|
||||
if (existing_vtable != 0) {
|
||||
// Found a match:
|
||||
// Remove the current vtable.
|
||||
space = bb.array().length - vtableloc;
|
||||
// Point table to existing vtable.
|
||||
bb.putInt(space, existing_vtable - vtableloc);
|
||||
} else {
|
||||
// No match:
|
||||
// Add the location of the current vtable to the list of vtables.
|
||||
if (num_vtables == vtables.length) vtables = Arrays.copyOf(vtables, num_vtables * 2);
|
||||
vtables[num_vtables++] = offset();
|
||||
// Point table to current vtable.
|
||||
bb.putInt(bb.array().length - vtableloc, offset() - vtableloc);
|
||||
}
|
||||
|
||||
vtable = null;
|
||||
return vtableloc;
|
||||
}
|
||||
|
||||
public void finish(int root_table) {
|
||||
prep(minalign, SIZEOF_INT);
|
||||
addOffset(root_table);
|
||||
}
|
||||
|
||||
public ByteBuffer dataBuffer() { return bb; }
|
||||
|
||||
// The FlatBuffer data doesn't start at offset 0 in the ByteBuffer:
|
||||
public int dataStart() {
|
||||
return bb.array().length - offset();
|
||||
}
|
||||
}
|
||||
25
java/flatbuffers/Struct.java
Executable file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 flatbuffers;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
// All structs in the generated code derive from this class, and add their own accessors.
|
||||
public class Struct {
|
||||
protected int bb_pos;
|
||||
protected ByteBuffer bb;
|
||||
}
|
||||
69
java/flatbuffers/Table.java
Executable file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 flatbuffers;
|
||||
|
||||
import java.lang.String;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
// All tables in the generated code derive from this class, and add their own accessors.
|
||||
public class Table {
|
||||
protected int bb_pos;
|
||||
protected ByteBuffer bb;
|
||||
|
||||
final int SIZEOF_INT = 4;
|
||||
|
||||
// Look up a field in the vtable, return an offset into the object, or 0 if the field is not
|
||||
// present.
|
||||
protected int __offset(int vtable_offset) {
|
||||
int vtable = bb_pos - bb.getInt(bb_pos);
|
||||
return vtable_offset < bb.getShort(vtable) ? bb.getShort(vtable + vtable_offset) : 0;
|
||||
}
|
||||
|
||||
// Retrieve the relative offset stored at "offset"
|
||||
protected int __indirect(int offset) {
|
||||
return offset + bb.getInt(offset);
|
||||
}
|
||||
|
||||
// Create a java String from UTF-8 data stored inside the flatbuffer.
|
||||
protected String __string(int offset) {
|
||||
offset += bb_pos;
|
||||
offset += bb.getInt(offset);
|
||||
return new String(bb.array(), offset + SIZEOF_INT, bb.getInt(offset), Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
// Get the length of a vector whose offset is stored at "offset" in this object.
|
||||
protected int __vector_len(int offset) {
|
||||
offset += bb_pos;
|
||||
offset += bb.getInt(offset);
|
||||
return bb.getInt(offset);
|
||||
}
|
||||
|
||||
// Get the start of data of a vector whose offset is stored at "offset" in this object.
|
||||
protected int __vector(int offset) {
|
||||
offset += bb_pos;
|
||||
return offset + bb.getInt(offset) + SIZEOF_INT; // data starts after the length
|
||||
}
|
||||
|
||||
// Initialize any Table-derived type to point to the union at the given offset.
|
||||
protected Table __union(Table t, int offset) {
|
||||
offset += bb_pos;
|
||||
t.bb_pos = offset + bb.getInt(offset);
|
||||
t.bb = bb;
|
||||
return t;
|
||||
}
|
||||
}
|
||||
30
readme.md
Executable file
@@ -0,0 +1,30 @@
|
||||
FlatBuffers Version 1.0
|
||||
|
||||
# Welcome to FlatBuffers!
|
||||
|
||||
FlatBuffers is a serialization library for games and other memory constrained
|
||||
apps. Go to our [landing page][] to browse our documentation.
|
||||
|
||||
FlatBuffers allows you to directly access serialized data without
|
||||
unpacking/parsing it first, while still having great forwards/backwards
|
||||
compatibility. FlatBuffers can be built for many different systems (Android,
|
||||
Windows, OS X, Linux), see `docs/html/index.html`
|
||||
|
||||
Discuss FlatBuffers with other developers and users on the
|
||||
[FlatBuffers Google Group][]. File issues on the [FlatBuffers Issues Tracker][]
|
||||
or post your questions to [stackoverflow.com][] with a mention of
|
||||
**flatbuffers**.
|
||||
|
||||
For applications on Google Play that integrate this tool, usage is tracked.
|
||||
This tracking is done automatically using the embedded version string
|
||||
(flatbuffer_version_string), and helps us continue to optimize it. Aside from
|
||||
consuming a few extra bytes in your application binary, it shouldn't affect
|
||||
your application at all. We use this information to let us know if FlatBuffers
|
||||
is useful and if we should continue to invest in it. Since this is open
|
||||
source, you are free to remove the version string but we would appreciate if
|
||||
you would leave it in.
|
||||
|
||||
[FlatBuffers Google Group]: http://group.google.com/group/flatbuffers
|
||||
[FlatBuffers Issues Tracker]: http://github.com/google/flatbuffers/issues
|
||||
[stackoverflow.com]: http://www.stackoverflow.com
|
||||
[landing page]: http://google.github.io/flatbuffers
|
||||
25
samples/monster.fbs
Executable file
@@ -0,0 +1,25 @@
|
||||
// example IDL file
|
||||
|
||||
namespace MyGame.Sample;
|
||||
|
||||
enum Color:byte { Red = 0, Green, Blue = 2 }
|
||||
|
||||
union Any { Monster } // add more elements..
|
||||
|
||||
struct Vec3 {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3;
|
||||
mana:short = 150;
|
||||
hp:short = 100;
|
||||
name:string;
|
||||
friendly:bool = false (deprecated);
|
||||
inventory:[ubyte];
|
||||
color:Color = Blue;
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
88
samples/monster_generated.h
Executable file
@@ -0,0 +1,88 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
namespace MyGame {
|
||||
namespace Sample {
|
||||
|
||||
enum {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
static const char *names[] = { "Red", "Green", "Blue", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
|
||||
enum {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesAny() {
|
||||
static const char *names[] = { "NONE", "Monster", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
|
||||
struct Vec3;
|
||||
struct Monster;
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(4) Vec3 {
|
||||
private:
|
||||
float x_;
|
||||
float y_;
|
||||
float z_;
|
||||
|
||||
public:
|
||||
Vec3(float x, float y, float z)
|
||||
: x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)) {}
|
||||
|
||||
float x() const { return flatbuffers::EndianScalar(x_); }
|
||||
float y() const { return flatbuffers::EndianScalar(y_); }
|
||||
float z() const { return flatbuffers::EndianScalar(z_); }
|
||||
};
|
||||
STRUCT_END(Vec3, 12);
|
||||
|
||||
struct Monster : 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); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
int8_t color() const { return GetField<int8_t>(16, 2); }
|
||||
};
|
||||
|
||||
struct MonsterBuilder {
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_pos(const Vec3 *pos) { fbb_.AddStruct(4, pos); }
|
||||
void add_mana(int16_t mana) { fbb_.AddElement<int16_t>(6, mana, 150); }
|
||||
void add_hp(int16_t hp) { fbb_.AddElement<int16_t>(8, hp, 100); }
|
||||
void add_name(flatbuffers::Offset<flatbuffers::String> name) { fbb_.AddOffset(10, name); }
|
||||
void add_inventory(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory) { fbb_.AddOffset(14, inventory); }
|
||||
void add_color(int8_t color) { fbb_.AddElement<int8_t>(16, color, 2); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
flatbuffers::Offset<Monster> Finish() { return flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 7)); }
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const Vec3 *pos, int16_t mana, int16_t hp, flatbuffers::Offset<flatbuffers::String> name, flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory, int8_t color) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_inventory(inventory);
|
||||
builder_.add_name(name);
|
||||
builder_.add_pos(pos);
|
||||
builder_.add_hp(hp);
|
||||
builder_.add_mana(mana);
|
||||
builder_.add_color(color);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
|
||||
|
||||
}; // namespace MyGame
|
||||
}; // namespace Sample
|
||||
9
samples/monsterdata.json
Executable file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
pos: {
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3
|
||||
},
|
||||
hp: 80,
|
||||
name: "MyMonster"
|
||||
}
|
||||
62
samples/sample_binary.cpp
Executable file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
#include "monster_generated.h"
|
||||
|
||||
using namespace MyGame::Sample;
|
||||
|
||||
// Example how to use FlatBuffers to create and read binary buffers.
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
// Build up a serialized buffer algorithmically:
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
auto vec = Vec3(1, 2, 3);
|
||||
|
||||
auto name = builder.CreateString("MyMonster");
|
||||
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
auto inventory = builder.CreateVector(inv_data, 10);
|
||||
|
||||
// Shortcut for creating monster with all fields set:
|
||||
auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory,
|
||||
Color_Blue);
|
||||
|
||||
builder.Finish(mloc);
|
||||
// We now have a FlatBuffer we can store or send somewhere.
|
||||
|
||||
// ** file/network code goes here :) **
|
||||
// access builder.GetBufferPointer() for builder.GetSize() bytes
|
||||
|
||||
// Instead, we're going to access it straight away.
|
||||
// Get access to the root:
|
||||
auto monster = GetMonster(builder.GetBufferPointer());
|
||||
|
||||
assert(monster->hp() == 80);
|
||||
assert(monster->mana() == 150); // default
|
||||
assert(!strcmp(monster->name()->c_str(), "MyMonster"));
|
||||
|
||||
auto pos = monster->pos();
|
||||
assert(pos);
|
||||
assert(pos->z() == 3);
|
||||
|
||||
auto inv = monster->inventory();
|
||||
assert(inv);
|
||||
assert(inv->Get(9) == 9);
|
||||
}
|
||||
|
||||
54
samples/sample_text.cpp
Executable file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#include "monster_generated.h"
|
||||
|
||||
using namespace MyGame::Sample;
|
||||
|
||||
// This is an example of parsing text straight into a buffer and then
|
||||
// generating flatbuffer (JSON) text from the buffer.
|
||||
int main(int argc, const char *argv[]) {
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
bool ok = flatbuffers::LoadFile("samples/monster.fbs", false, &schemafile) &&
|
||||
flatbuffers::LoadFile("samples/monsterdata.json", false, &jsonfile);
|
||||
if (!ok) {
|
||||
printf("couldn't load files!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// parse schema first, so we can use it to parse the data after
|
||||
flatbuffers::Parser parser;
|
||||
ok = parser.Parse(schemafile.c_str()) &&
|
||||
parser.Parse(jsonfile.c_str());
|
||||
assert(ok);
|
||||
|
||||
// here, parser.builder_ contains a binary buffer that is the parsed data.
|
||||
|
||||
// to ensure it is correct, we now generate text back from the binary,
|
||||
// and compare the two:
|
||||
std::string jsongen;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), 2, &jsongen);
|
||||
|
||||
if (jsongen != jsonfile) {
|
||||
printf("%s----------------\n%s", jsongen.c_str(), jsonfile.c_str());
|
||||
}
|
||||
}
|
||||
180
src/flatc.cpp
Executable file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
void Error(const char *err, const char *obj = nullptr, bool usage = false);
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
bool GenerateBinary(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
return !parser.builder_.GetSize() ||
|
||||
flatbuffers::SaveFile(
|
||||
(path + file_name + "_wire.bin").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) {
|
||||
if (!parser.builder_.GetSize()) return true;
|
||||
if (!parser.root_struct_def) Error("root_type not set");
|
||||
std::string text;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), 2, &text);
|
||||
return flatbuffers::SaveFile((path + file_name + "_wire.txt").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 {
|
||||
bool (*generate)(const flatbuffers::Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
const char *extension;
|
||||
const char *name;
|
||||
const char *help;
|
||||
};
|
||||
|
||||
const Generator generators[] = {
|
||||
{ flatbuffers::GenerateBinary, "b", "binary",
|
||||
"Generate wire format binaries for any data definitions" },
|
||||
{ flatbuffers::GenerateTextFile, "t", "text",
|
||||
"Generate text output for any data definitions" },
|
||||
{ flatbuffers::GenerateCPP, "c", "C++",
|
||||
"Generate C++ headers for tables/structs" },
|
||||
{ flatbuffers::GenerateJava, "j", "Java",
|
||||
"Generate Java classes for tables/structs" },
|
||||
};
|
||||
|
||||
const char *program_name = NULL;
|
||||
|
||||
void Error(const char *err, const char *obj, bool usage) {
|
||||
printf("%s: %s\n", program_name, err);
|
||||
if (obj) printf(": %s", obj);
|
||||
printf("\n");
|
||||
if (usage) {
|
||||
printf("usage: %s [OPTION]... FILE...\n", program_name);
|
||||
for (size_t i = 0; i < sizeof(generators) / sizeof(generators[0]); ++i)
|
||||
printf(" -%s %s.\n", generators[i].extension, generators[i].help);
|
||||
printf(" -o PATH Prefix PATH to all generated files.\n"
|
||||
"FILEs may depend on declarations in earlier files.\n"
|
||||
"Output files are named using the base file name of the input,"
|
||||
"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);
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::string StripExtension(const std::string &filename) {
|
||||
size_t i = filename.find_last_of(".");
|
||||
return i != std::string::npos ? filename.substr(0, i) : filename;
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
program_name = argv[0];
|
||||
flatbuffers::Parser parser;
|
||||
std::string output_path;
|
||||
const size_t num_generators = sizeof(generators) / sizeof(generators[0]);
|
||||
bool generator_enabled[num_generators] = { false };
|
||||
bool any_generator = false;
|
||||
std::vector<std::string> filenames;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
const char *arg = argv[i];
|
||||
if (arg[0] == '-') {
|
||||
if (filenames.size())
|
||||
Error("invalid option location", arg, true);
|
||||
if (strlen(arg) != 2)
|
||||
Error("invalid commandline argument", arg, true);
|
||||
switch (arg[1]) {
|
||||
case 'o':
|
||||
if (++i >= argc) Error("missing path following", arg, true);
|
||||
output_path = argv[i];
|
||||
break;
|
||||
default:
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
if(!strcmp(arg+1, generators[i].extension)) {
|
||||
generator_enabled[i] = true;
|
||||
any_generator = true;
|
||||
goto found;
|
||||
}
|
||||
}
|
||||
Error("unknown commandline argument", arg, true);
|
||||
found:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
filenames.push_back(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!filenames.size()) Error("missing input files", nullptr, true);
|
||||
|
||||
if (!any_generator)
|
||||
Error("no options: no output files generated.",
|
||||
"specify one of -c -j -t -b etc.", true);
|
||||
|
||||
// Now process the files:
|
||||
for (auto file_it = filenames.begin();
|
||||
file_it != filenames.end();
|
||||
++file_it) {
|
||||
std::string contents;
|
||||
if (!flatbuffers::LoadFile(file_it->c_str(), true, &contents))
|
||||
Error("unable to load file", file_it->c_str());
|
||||
|
||||
if (!parser.Parse(contents.c_str()))
|
||||
Error(parser.error_.c_str());
|
||||
|
||||
std::string filebase = StripExtension(*file_it);
|
||||
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
if (generator_enabled[i]) {
|
||||
if (!generators[i].generate(parser, output_path, filebase)) {
|
||||
Error((std::string("Unable to generate ") +
|
||||
generators[i].name +
|
||||
" for " +
|
||||
filebase).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Since the Parser object retains definitions across files, we must
|
||||
// ensure we only output code for these once, in the file they are first
|
||||
// declared:
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
(*it)->generated = true;
|
||||
}
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
(*it)->generated = true;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
371
src/idl_gen_cpp.cpp
Normal file
@@ -0,0 +1,371 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace cpp {
|
||||
|
||||
// Return a C++ type from the table in idl.h
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) #CTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
return ctypename[type.base_type];
|
||||
}
|
||||
|
||||
static std::string GenTypeWire(const Type &type, const char *postfix);
|
||||
|
||||
// Return a C++ pointer type, specialized to the actual struct/table types,
|
||||
// and vector element types.
|
||||
static std::string GenTypePointer(const Type &type) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING:
|
||||
return "flatbuffers::String";
|
||||
case BASE_TYPE_VECTOR:
|
||||
return "flatbuffers::Vector<" + GenTypeWire(type.VectorType(), "") + ">";
|
||||
case BASE_TYPE_STRUCT:
|
||||
return type.struct_def->name;
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
default:
|
||||
return "void";
|
||||
}
|
||||
}
|
||||
|
||||
// Return a C++ type for any type (scalar/pointer) specifically for
|
||||
// building a flatbuffer.
|
||||
static std::string GenTypeWire(const Type &type, const char *postfix) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type) + postfix
|
||||
: IsStruct(type)
|
||||
? "const " + GenTypePointer(type) + " *"
|
||||
: "flatbuffers::Offset<" + GenTypePointer(type) + ">" + postfix;
|
||||
}
|
||||
|
||||
// Return a C++ type for any type (scalar/pointer) specifically for
|
||||
// using a flatbuffer.
|
||||
static std::string GenTypeGet(const Type &type, const char *afterbasic,
|
||||
const char *beforeptr, const char *afterptr) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type) + afterbasic
|
||||
: beforeptr + GenTypePointer(type) + afterptr;
|
||||
}
|
||||
|
||||
// Generate a documentation comment, if available.
|
||||
static void GenComment(const std::string &dc,
|
||||
std::string *code_ptr,
|
||||
const char *prefix = "") {
|
||||
std::string &code = *code_ptr;
|
||||
if (dc.length()) {
|
||||
code += std::string(prefix) + "///" + dc + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Generate an enum declaration and an enum string lookup table.
|
||||
static void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
|
||||
if (enum_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
GenComment(enum_def.doc_comment, code_ptr);
|
||||
code += "enum {\n";
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
GenComment(ev.doc_comment, code_ptr, " ");
|
||||
code += " " + enum_def.name + "_" + ev.name + " = ";
|
||||
code += NumToString(ev.value) + ",\n";
|
||||
}
|
||||
code += "};\n\n";
|
||||
|
||||
// Generate a generate string table for enum values.
|
||||
// Problem is, if values are very sparse that could generate really big
|
||||
// tables. Ideally in that case we generate a map lookup instead, but for
|
||||
// the moment we simply don't output a table at all.
|
||||
int range = enum_def.vals.vec.back()->value -
|
||||
enum_def.vals.vec.front()->value + 1;
|
||||
// Average distance between values above which we consider a table
|
||||
// "too sparse". Change at will.
|
||||
static const int kMaxSparseness = 5;
|
||||
if (range / static_cast<int>(enum_def.vals.vec.size()) < kMaxSparseness) {
|
||||
code += "inline const char **EnumNames" + enum_def.name + "() {\n";
|
||||
code += " static const char *names[] = { ";
|
||||
int val = enum_def.vals.vec.front()->value;
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
while (val++ != (*it)->value) code += "\"\", ";
|
||||
code += "\"" + (*it)->name + "\", ";
|
||||
}
|
||||
code += "nullptr };\n return names;\n}\n\n";
|
||||
code += "inline const char *EnumName" + enum_def.name;
|
||||
code += "(int e) { return EnumNames" + enum_def.name + "()[e";
|
||||
if (enum_def.vals.vec.front()->value)
|
||||
code += " - " + enum_def.name + "_" + enum_def.vals.vec.front()->name;
|
||||
code += "]; }\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Generate an accessor struct, builder structs & function for a table.
|
||||
static void GenTable(StructDef &struct_def, std::string *code_ptr) {
|
||||
if (struct_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
// 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 += " {\n";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) { // Deprecated fields won't be accessible.
|
||||
GenComment(field.doc_comment, code_ptr, " ");
|
||||
code += " " + GenTypeGet(field.value.type, " ", "const ", " *");
|
||||
code += field.name + "() const { return ";
|
||||
// Call a different accessor for pointers, that indirects.
|
||||
code += IsScalar(field.value.type.base_type)
|
||||
? "GetField<"
|
||||
: (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<");
|
||||
code += GenTypeGet(field.value.type, "", "const ", " *") + ">(";
|
||||
code += NumToString(field.value.offset);
|
||||
// Default value as second arg for non-pointer types.
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += ", " + field.value.constant;
|
||||
code += "); }\n";
|
||||
}
|
||||
}
|
||||
code += "};\n\n";
|
||||
|
||||
// Generate a builder struct, with methods of the form:
|
||||
// void add_name(type name) { fbb_.AddElement<type>(offset, name, default); }
|
||||
code += "struct " + struct_def.name;
|
||||
code += "Builder {\n flatbuffers::FlatBufferBuilder &fbb_;\n";
|
||||
code += " flatbuffers::uoffset_t start_;\n";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) {
|
||||
code += " void add_" + field.name + "(";
|
||||
code += GenTypeWire(field.value.type, " ") + field.name + ") { fbb_.Add";
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += "Element<" + GenTypeWire(field.value.type, "") + ">";
|
||||
else if (IsStruct(field.value.type))
|
||||
code += "Struct";
|
||||
else
|
||||
code += "Offset";
|
||||
code += "(" + NumToString(field.value.offset) + ", " + field.name;
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += ", " + field.value.constant;
|
||||
code += "); }\n";
|
||||
}
|
||||
}
|
||||
code += " " + struct_def.name;
|
||||
code += "Builder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) ";
|
||||
code += "{ start_ = fbb_.StartTable(); }\n";
|
||||
code += " flatbuffers::Offset<" + struct_def.name;
|
||||
code += "> Finish() { return flatbuffers::Offset<" + struct_def.name;
|
||||
code += ">(fbb_.EndTable(start_, ";
|
||||
code += NumToString(struct_def.fields.vec.size()) + ")); }\n};\n\n";
|
||||
|
||||
// Generate a convenient CreateX function that uses the above builder
|
||||
// to create a table in one go.
|
||||
code += "inline flatbuffers::Offset<" + struct_def.name + "> Create";
|
||||
code += struct_def.name;
|
||||
code += "(flatbuffers::FlatBufferBuilder &_fbb";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) {
|
||||
code += ", " + GenTypeWire(field.value.type, " ") + field.name;
|
||||
}
|
||||
}
|
||||
code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n";
|
||||
for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1;
|
||||
size;
|
||||
size /= 2) {
|
||||
for (auto it = struct_def.fields.vec.rbegin();
|
||||
it != struct_def.fields.vec.rend();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (!field.deprecated &&
|
||||
(!struct_def.sortbysize ||
|
||||
size == SizeOf(field.value.type.base_type))) {
|
||||
code += " builder_.add_" + field.name + "(" + field.name + ");\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
code += " return builder_.Finish();\n}\n\n";
|
||||
}
|
||||
|
||||
// Generate an accessor struct with constructor for a flatbuffers struct.
|
||||
static void GenStruct(StructDef &struct_def, std::string *code_ptr) {
|
||||
if (struct_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
// Generate an accessor struct, with private variables of the form:
|
||||
// type name_;
|
||||
// Generates manual padding and alignment.
|
||||
// Variables are private because they contain little endian data on all
|
||||
// platforms.
|
||||
GenComment(struct_def.doc_comment, code_ptr);
|
||||
code += "MANUALLY_ALIGNED_STRUCT(" + NumToString(struct_def.minalign) + ") ";
|
||||
code += struct_def.name + " {\n private:\n";
|
||||
int padding_id = 0;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
code += " " + GenTypeGet(field.value.type, " ", "", " ");
|
||||
code += field.name + "_;\n";
|
||||
if (field.padding) {
|
||||
for (int i = 0; i < 4; i++)
|
||||
if (static_cast<int>(field.padding) & (1 << i))
|
||||
code += " int" + NumToString((1 << i) * 8) +
|
||||
"_t __padding" + NumToString(padding_id++) + ";\n";
|
||||
assert(!(field.padding & ~0xF));
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a constructor that takes all fields as arguments.
|
||||
code += "\n public:\n " + struct_def.name + "(";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (it != struct_def.fields.vec.begin()) code += ", ";
|
||||
code += GenTypeGet(field.value.type, " ", "const ", " &") + field.name;
|
||||
}
|
||||
code += ")\n : ";
|
||||
padding_id = 0;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (it != struct_def.fields.vec.begin()) code += ", ";
|
||||
code += field.name + "_(";
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += "flatbuffers::EndianScalar(" + field.name + "))";
|
||||
else
|
||||
code += field.name + ")";
|
||||
if (field.padding)
|
||||
code += ", __padding" + NumToString(padding_id++) + "(0)";
|
||||
}
|
||||
code += " {}\n\n";
|
||||
|
||||
// Generate accessor methods of the form:
|
||||
// type name() const { return flatbuffers::EndianScalar(name_); }
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
GenComment(field.doc_comment, code_ptr, " ");
|
||||
code += " " + GenTypeGet(field.value.type, " ", "const ", " &");
|
||||
code += field.name + "() const { return ";
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += "flatbuffers::EndianScalar(" + field.name + "_)";
|
||||
else
|
||||
code += field.name + "_";
|
||||
code += "; }\n";
|
||||
}
|
||||
code += "};\nSTRUCT_END(" + struct_def.name + ", ";
|
||||
code += NumToString(struct_def.bytesize) + ");\n\n";
|
||||
}
|
||||
|
||||
} // namespace cpp
|
||||
|
||||
// Iterate through all definitions we haven't generate code for (enums, structs,
|
||||
// and tables) and output them to a single file.
|
||||
static std::string GenerateCPP(const Parser &parser) {
|
||||
using namespace cpp;
|
||||
|
||||
// Generate code for all the enum declarations.
|
||||
std::string enum_code;
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
GenEnum(**it, &enum_code);
|
||||
}
|
||||
|
||||
// Generate forward declarations for all structs/tables, since they may
|
||||
// have circular references.
|
||||
std::string forward_decl_code;
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
if (!(*it)->generated)
|
||||
forward_decl_code += "struct " + (*it)->name + ";\n";
|
||||
}
|
||||
|
||||
// Generate code for all structs, then all tables.
|
||||
std::string decl_code;
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
if ((**it).fixed) GenStruct(**it, &decl_code);
|
||||
}
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
if (!(**it).fixed) GenTable(**it, &decl_code);
|
||||
}
|
||||
|
||||
// Only output file-level code if there were any declarations.
|
||||
if (enum_code.length() || forward_decl_code.length() || decl_code.length()) {
|
||||
std::string code;
|
||||
code = "// automatically generated, do not modify\n\n";
|
||||
code += "#include \"flatbuffers/flatbuffers.h\"\n\n";
|
||||
for (auto it = parser.name_space_.begin();
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
code += "namespace " + *it + " {\n";
|
||||
}
|
||||
code += "\n";
|
||||
code += enum_code;
|
||||
code += forward_decl_code;
|
||||
code += "\n";
|
||||
code += decl_code;
|
||||
if (parser.root_struct_def) {
|
||||
code += "inline const " + parser.root_struct_def->name + " *Get";
|
||||
code += parser.root_struct_def->name;
|
||||
code += "(const void *buf) { return flatbuffers::GetRoot<";
|
||||
code += parser.root_struct_def->name + ">(buf); }\n\n";
|
||||
}
|
||||
for (auto it = parser.name_space_.begin();
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
code += "}; // namespace " + *it + "\n";
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
|
||||
bool GenerateCPP(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
auto code = GenerateCPP(parser);
|
||||
return !code.length() ||
|
||||
SaveFile((path + file_name + "_generated.h").c_str(), code, false);
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
399
src/idl_gen_java.cpp
Executable file
@@ -0,0 +1,399 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#define PATH_SEPARATOR "\\"
|
||||
#define mkdir(n, m) _mkdir(n)
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#define PATH_SEPARATOR "/"
|
||||
#endif
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace java {
|
||||
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) #JTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
return ctypename[type.base_type];
|
||||
}
|
||||
|
||||
static std::string GenTypeGet(const Type &type);
|
||||
|
||||
static std::string GenTypePointer(const Type &type) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING:
|
||||
return "String";
|
||||
case BASE_TYPE_VECTOR:
|
||||
return GenTypeGet(type.VectorType());
|
||||
case BASE_TYPE_STRUCT:
|
||||
return type.struct_def->name;
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
default:
|
||||
return "Table";
|
||||
}
|
||||
}
|
||||
|
||||
static std::string GenTypeGet(const Type &type) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
: GenTypePointer(type);
|
||||
}
|
||||
|
||||
static void GenComment(const std::string &dc,
|
||||
std::string *code_ptr,
|
||||
const char *prefix = "") {
|
||||
std::string &code = *code_ptr;
|
||||
if (dc.length()) {
|
||||
code += std::string(prefix) + "///" + dc + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Convert an underscore_based_indentifier in to camelCase.
|
||||
// Also uppercases the first character if first is true.
|
||||
static std::string MakeCamel(const std::string &in, bool first = true) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < in.length(); i++) {
|
||||
if (!i && first) s += toupper(in[0]);
|
||||
else if (in[i] == '_' && i + 1 < in.length()) s += toupper(in[++i]);
|
||||
else s += in[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
if (enum_def.generated) return;
|
||||
|
||||
// Generate enum definitions of the form:
|
||||
// public static final int name = value;
|
||||
// We use ints rather than the Java Enum feature, because we want them
|
||||
// to map directly to how they're used in C/C++ and file formats.
|
||||
// That, and Java Enums are expensive, and not universally liked.
|
||||
GenComment(enum_def.doc_comment, code_ptr);
|
||||
code += "public class " + enum_def.name + " {\n";
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
GenComment(ev.doc_comment, code_ptr, " ");
|
||||
code += " public static final " + GenTypeBasic(enum_def.underlying_type);
|
||||
code += " " + ev.name + " = ";
|
||||
code += NumToString(ev.value) + ";\n";
|
||||
}
|
||||
code += "};\n\n";
|
||||
}
|
||||
|
||||
// Returns the function name that is able to read a value of the given type.
|
||||
static std::string GenGetter(const Type &type) {
|
||||
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_VECTOR: return GenGetter(type.VectorType());
|
||||
default:
|
||||
return "bb.get" + (SizeOf(type.base_type) > 1
|
||||
? MakeCamel(GenTypeGet(type))
|
||||
: "");
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the method name for use with add/put calls.
|
||||
static std::string GenMethod(const FieldDef &field) {
|
||||
return IsScalar(field.value.type.base_type)
|
||||
? MakeCamel(GenTypeBasic(field.value.type))
|
||||
: (IsStruct(field.value.type) ? "Struct" : "Offset");
|
||||
}
|
||||
|
||||
// Recursively generate arguments for a constructor, to deal with nested
|
||||
// structs.
|
||||
static void GenStructArgs(const StructDef &struct_def, std::string *code_ptr,
|
||||
const char *nameprefix) {
|
||||
std::string &code = *code_ptr;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (IsStruct(field.value.type)) {
|
||||
// Generate arguments for a struct inside a struct. To ensure names
|
||||
// don't clash, and to make it obvious these arguments are constructing
|
||||
// a nested struct, prefix the name with the struct name.
|
||||
GenStructArgs(*field.value.type.struct_def, code_ptr,
|
||||
(field.value.type.struct_def->name + "_").c_str());
|
||||
} else {
|
||||
code += ", " + GenTypeBasic(field.value.type) + " " + nameprefix;
|
||||
code += MakeCamel(field.name, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recusively generate struct construction statements of the form:
|
||||
// builder.putType(name);
|
||||
// and insert manual padding.
|
||||
static void GenStructBody(const StructDef &struct_def, std::string *code_ptr,
|
||||
const char *nameprefix) {
|
||||
std::string &code = *code_ptr;
|
||||
code += " builder.prep(" + NumToString(struct_def.minalign) + ", 0);\n";
|
||||
for (auto it = struct_def.fields.vec.rbegin();
|
||||
it != struct_def.fields.vec.rend();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (field.padding)
|
||||
code += " builder.pad(" + NumToString(field.padding) + ");\n";
|
||||
if (IsStruct(field.value.type)) {
|
||||
GenStructBody(*field.value.type.struct_def, code_ptr,
|
||||
(field.value.type.struct_def->name + "_").c_str());
|
||||
} else {
|
||||
code += " builder.put" + GenMethod(field) + "(";
|
||||
code += nameprefix + MakeCamel(field.name, false) + ");\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void GenStruct(StructDef &struct_def,
|
||||
std::string *code_ptr,
|
||||
StructDef *root_struct_def) {
|
||||
if (struct_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
// Generate a struct accessor class, with methods of the form:
|
||||
// public type name() { return bb.getType(i + offset); }
|
||||
// or for tables of the form:
|
||||
// public type name() {
|
||||
// int o = __offset(offset); return o != 0 ? bb.getType(o + i) : default;
|
||||
// }
|
||||
GenComment(struct_def.doc_comment, code_ptr);
|
||||
code += "public class " + struct_def.name + " extends ";
|
||||
code += struct_def.fixed ? "Struct" : "Table";
|
||||
code += " {\n";
|
||||
if (&struct_def == root_struct_def) {
|
||||
// Generate a special accessor for the table that has been declared as
|
||||
// the root type.
|
||||
code += " public static " + struct_def.name + " getRootAs";
|
||||
code += struct_def.name;
|
||||
code += "(ByteBuffer _bb, int offset) { ";
|
||||
code += "_bb.order(ByteOrder.LITTLE_ENDIAN); ";
|
||||
code += "return (new " + struct_def.name;
|
||||
code += "()).__init(_bb.getInt(offset) + offset, _bb); }\n";
|
||||
}
|
||||
// Generate the __init method that sets the field in a pre-existing
|
||||
// accessor object. This is to allow object reuse.
|
||||
code += " public " + struct_def.name;
|
||||
code += " __init(int _i, ByteBuffer _bb) ";
|
||||
code += "{ bb_pos = _i; bb = _bb; return this; }\n";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
GenComment(field.doc_comment, code_ptr, " ");
|
||||
std::string type_name = GenTypeGet(field.value.type);
|
||||
std::string method_start = " public " + type_name + " " +
|
||||
MakeCamel(field.name, false);
|
||||
// Generate the accessors that don't do object reuse.
|
||||
if (field.value.type.base_type == BASE_TYPE_STRUCT) {
|
||||
// Calls the accessor that takes an accessor object with a new object.
|
||||
code += method_start + "() { return " + MakeCamel(field.name, false);
|
||||
code += "(new ";
|
||||
code += type_name + "()); }\n";
|
||||
} else if (field.value.type.base_type == BASE_TYPE_VECTOR &&
|
||||
field.value.type.element == BASE_TYPE_STRUCT) {
|
||||
// Accessors for vectors of structs also take accessor objects, this
|
||||
// generates a variant without that argument.
|
||||
code += method_start + "(int j) { return " + MakeCamel(field.name, false);
|
||||
code += "(new ";
|
||||
code += type_name + "(), j); }\n";
|
||||
}
|
||||
std::string getter = GenGetter(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:
|
||||
auto offset_prefix = ") { int o = __offset(" +
|
||||
NumToString(field.value.offset) +
|
||||
"); return o != 0 ? ";
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
code += ") { return " + getter;
|
||||
code += "(bb_pos + " + NumToString(field.value.offset) + ")";
|
||||
} else {
|
||||
code += offset_prefix + getter;
|
||||
code += "(o + bb_pos) : " + field.value.constant;
|
||||
}
|
||||
} else {
|
||||
switch (field.value.type.base_type) {
|
||||
case BASE_TYPE_STRUCT:
|
||||
code += type_name + " obj";
|
||||
if (struct_def.fixed) {
|
||||
code += ") { return obj.__init(bb_pos + ";
|
||||
code += NumToString(field.value.offset) + ", bb)";
|
||||
} else {
|
||||
code += offset_prefix;
|
||||
code += "obj.__init(";
|
||||
code += field.value.type.struct_def->fixed
|
||||
? "o + bb_pos"
|
||||
: "__indirect(o + i)";
|
||||
code += ", bb) : null";
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
code += offset_prefix + getter +"(o) : null";
|
||||
break;
|
||||
case BASE_TYPE_VECTOR: {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
if (vectortype.base_type == BASE_TYPE_STRUCT) {
|
||||
code += type_name + " obj, ";
|
||||
getter = "obj.__init";
|
||||
}
|
||||
code += "int j" + offset_prefix + getter +"(";
|
||||
auto index = "__vector(o) + j * " +
|
||||
NumToString(InlineSize(vectortype));
|
||||
if (vectortype.base_type == BASE_TYPE_STRUCT) {
|
||||
code += vectortype.struct_def->fixed
|
||||
? index
|
||||
: "__indirect(" + index + ")";
|
||||
code += ", bb";
|
||||
} else {
|
||||
code += index;
|
||||
}
|
||||
code += ") : ";
|
||||
code += IsScalar(field.value.type.element) ? "0" : "null";
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
code += type_name + " obj" + offset_prefix + getter;
|
||||
code += "(obj, o) : null";
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
code += "; }\n";
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
code += " public int " + MakeCamel(field.name, false) + "Length(";
|
||||
code += offset_prefix;
|
||||
code += "__vector_len(o) : 0; }\n";
|
||||
}
|
||||
}
|
||||
code += "\n";
|
||||
if (struct_def.fixed) {
|
||||
// create a struct constructor function
|
||||
code += " public static int create" + struct_def.name;
|
||||
code += "(FlatBufferBuilder builder";
|
||||
GenStructArgs(struct_def, code_ptr, "");
|
||||
code += ") {\n";
|
||||
GenStructBody(struct_def, code_ptr, "");
|
||||
code += " return builder.offset();\n }\n";
|
||||
} else {
|
||||
// Create a set of static methods that allow table construction,
|
||||
// of the form:
|
||||
// public static void addName(FlatBufferBuilder builder, short name)
|
||||
// { builder.addShort(id, name, default); }
|
||||
code += " public static void start" + struct_def.name;
|
||||
code += "(FlatBufferBuilder builder) { builder.startObject(";
|
||||
code += NumToString(struct_def.fields.vec.size()) + "); }\n";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
code += " public static void add" + MakeCamel(field.name);
|
||||
code += "(FlatBufferBuilder builder, " + GenTypeBasic(field.value.type);
|
||||
code += " " + MakeCamel(field.name, false) + ") { builder.add";
|
||||
code += GenMethod(field) + "(";
|
||||
code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
|
||||
code += MakeCamel(field.name, false) + ", " + field.value.constant;
|
||||
code += "); }\n";
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
code += " public static void start" + MakeCamel(field.name);
|
||||
code += "Vector(FlatBufferBuilder builder, int numElems) ";
|
||||
code += "{ builder.startVector(";
|
||||
code += NumToString(InlineSize(field.value.type));
|
||||
code += ", numElems); }\n";
|
||||
}
|
||||
}
|
||||
code += " public static int end" + struct_def.name;
|
||||
code += "(FlatBufferBuilder builder) { return builder.endObject(); }\n";
|
||||
}
|
||||
code += "};\n\n";
|
||||
}
|
||||
|
||||
// Save out the generated code for a single Java class while adding
|
||||
// declaration boilerplate.
|
||||
static bool SaveClass(const Parser &parser, const Definition &def,
|
||||
const std::string &classcode, const std::string &path) {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string name_space_java;
|
||||
std::string name_space_dir = path;
|
||||
for (auto it = parser.name_space_.begin();
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
if (name_space_java.length()) {
|
||||
name_space_java += ".";
|
||||
name_space_dir += PATH_SEPARATOR;
|
||||
}
|
||||
name_space_java += *it;
|
||||
name_space_dir += *it;
|
||||
mkdir(name_space_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
|
||||
}
|
||||
|
||||
std::string code = "// automatically generated, do not modify\n\n";
|
||||
code += "package " + name_space_java + ";\n\n";
|
||||
code += "import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\n";
|
||||
code += "import flatbuffers.*;\n\n";
|
||||
code += classcode;
|
||||
auto filename = name_space_dir + PATH_SEPARATOR + def.name + ".java";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
} // namespace java
|
||||
|
||||
bool GenerateJava(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
using namespace java;
|
||||
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
std::string enumcode;
|
||||
GenEnum(**it, &enumcode);
|
||||
if (!SaveClass(parser, **it, enumcode, path))
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
std::string declcode;
|
||||
GenStruct(**it, &declcode, parser.root_struct_def);
|
||||
if (!SaveClass(parser, **it, declcode, path))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
214
src/idl_gen_text.cpp
Normal file
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
static void GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, int indent_step, std::string *_text);
|
||||
|
||||
// Print (and its template specialization below for pointers) generate text
|
||||
// for a single FlatBuffer value into JSON format.
|
||||
// The general case for scalars:
|
||||
template<typename T> void Print(T val, Type type, int indent, int indent_step,
|
||||
StructDef * /*union_sd*/, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += NumToString(val);
|
||||
}
|
||||
|
||||
// Print a vector a sequence of JSON values, comma separated, wrapped in "[]".
|
||||
template<typename T> void PrintVector(const Vector<T> &v, Type type,
|
||||
int indent, int indent_step,
|
||||
std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "[\n";
|
||||
for (uoffset_t i = 0; i < v.Length(); i++) {
|
||||
if (i) text += ",\n";
|
||||
text.append(indent + indent_step, ' ');
|
||||
if (IsStruct(type))
|
||||
Print(v.GetStructFromOffset(i * type.struct_def->bytesize), type,
|
||||
indent + indent_step, indent_step, nullptr, _text);
|
||||
else
|
||||
Print(v.Get(i), type, indent + indent_step, indent_step, nullptr, _text);
|
||||
}
|
||||
text += "\n";
|
||||
text.append(indent, ' ');
|
||||
text += "]";
|
||||
}
|
||||
|
||||
static void EscapeString(const String &s, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "\"";
|
||||
for (uoffset_t i = 0; i < s.Length(); i++) {
|
||||
char c = s.Get(i);
|
||||
switch (c) {
|
||||
case '\n': text += "\\n"; break;
|
||||
case '\t': text += "\\t"; break;
|
||||
case '\r': text += "\\r"; break;
|
||||
case '\"': text += "\\\""; break;
|
||||
case '\\': text += "\\\\"; break;
|
||||
default:
|
||||
if (c >= ' ' && c <= '~') {
|
||||
text += c;
|
||||
} else {
|
||||
auto u = static_cast<unsigned char>(c);
|
||||
text += "\\x" + IntToStringHex(u);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
text += "\"";
|
||||
}
|
||||
|
||||
// Specialization of Print above for pointer types.
|
||||
template<> void Print<const void *>(const void *val,
|
||||
Type type, int indent, int indent_step,
|
||||
StructDef *union_sd, std::string *_text) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_UNION:
|
||||
// If this assert hits, you have an corrupt buffer, a union type field
|
||||
// was not present or was out of range.
|
||||
assert(union_sd);
|
||||
GenStruct(*union_sd,
|
||||
reinterpret_cast<const Table *>(val),
|
||||
indent,
|
||||
indent_step,
|
||||
_text);
|
||||
break;
|
||||
case BASE_TYPE_STRUCT:
|
||||
GenStruct(*type.struct_def,
|
||||
reinterpret_cast<const Table *>(val),
|
||||
indent,
|
||||
indent_step,
|
||||
_text);
|
||||
break;
|
||||
case BASE_TYPE_STRING: {
|
||||
EscapeString(*reinterpret_cast<const String *>(val), _text);
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_VECTOR:
|
||||
type = type.VectorType();
|
||||
// Call PrintVector above specifically for each element type:
|
||||
switch (type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
PrintVector<CTYPE>( \
|
||||
*reinterpret_cast<const Vector<CTYPE> *>(val), \
|
||||
type, indent, indent_step, _text); break;
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
}
|
||||
break;
|
||||
default: assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate text for a scalar field.
|
||||
template<typename T> static void GenField(const FieldDef &fd,
|
||||
const Table *table, bool fixed,
|
||||
int indent_step, int indent,
|
||||
std::string *_text) {
|
||||
Print(fixed ?
|
||||
reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset) :
|
||||
table->GetField<T>(fd.value.offset, 0), fd.value.type, indent, indent_step,
|
||||
nullptr, _text);
|
||||
}
|
||||
|
||||
// Generate text for non-scalar field.
|
||||
static void GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
||||
int indent, int indent_step, StructDef *union_sd,
|
||||
std::string *_text) {
|
||||
const void *val = nullptr;
|
||||
if (fixed) {
|
||||
// The only non-scalar fields in structs are structs.
|
||||
assert(IsStruct(fd.value.type));
|
||||
val = reinterpret_cast<const Struct *>(table)->
|
||||
GetStruct<const void *>(fd.value.offset);
|
||||
} else {
|
||||
val = IsStruct(fd.value.type)
|
||||
? table->GetStruct<const void *>(fd.value.offset)
|
||||
: table->GetPointer<const void *>(fd.value.offset);
|
||||
}
|
||||
Print(val, fd.value.type, indent, indent_step, union_sd, _text);
|
||||
}
|
||||
|
||||
// Generate text for a struct or table, values separated by commas, indented,
|
||||
// and bracketed by "{}"
|
||||
static void GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, int indent_step, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "{\n";
|
||||
int fieldout = 0;
|
||||
StructDef *union_sd = nullptr;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
FieldDef &fd = **it;
|
||||
if (struct_def.fixed || table->CheckField(fd.value.offset)) {
|
||||
// The field is present.
|
||||
if (fieldout++) text += ",\n";
|
||||
text.append(indent + indent_step, ' ');
|
||||
text += fd.name;
|
||||
text += ": ";
|
||||
switch (fd.value.type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
GenField<CTYPE>(fd, table, struct_def.fixed, \
|
||||
indent + indent_step, indent_step, _text); \
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// Generate drop-thru case statements for all pointer types:
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
case BASE_TYPE_ ## ENUM:
|
||||
FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
GenFieldOffset(fd, table, struct_def.fixed, indent + indent_step,
|
||||
indent_step, union_sd, _text);
|
||||
break;
|
||||
}
|
||||
if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
|
||||
union_sd = fd.value.type.enum_def->ReverseLookup(
|
||||
table->GetField<uint8_t>(fd.value.offset, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
text += "\n";
|
||||
text.append(indent, ' ');
|
||||
text += "}";
|
||||
}
|
||||
|
||||
// Generate a text representation of a flatbuffer in JSON format.
|
||||
void GenerateText(const Parser &parser, const void *flatbuffer,
|
||||
int indent_step, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
assert(parser.root_struct_def); // call SetRootType()
|
||||
text.reserve(1024); // Reduce amount of inevitable reallocs.
|
||||
GenStruct(*parser.root_struct_def,
|
||||
GetRoot<Table>(flatbuffer),
|
||||
0,
|
||||
indent_step,
|
||||
_text);
|
||||
text += "\n";
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
724
src/idl_parser.cpp
Normal file
@@ -0,0 +1,724 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
const char *const kTypeNames[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) IDLTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
nullptr
|
||||
};
|
||||
|
||||
const char kTypeSizes[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) sizeof(CTYPE),
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
|
||||
static void Error(const std::string &msg) {
|
||||
throw msg;
|
||||
}
|
||||
|
||||
// Ensure that integer values we parse fit inside the declared integer type.
|
||||
static void CheckBitsFit(int64_t val, size_t bits) {
|
||||
auto mask = (1ll << bits) - 1; // Bits we allow to be used.
|
||||
if (bits < 64 &&
|
||||
(val & ~mask) != 0 && // Positive or unsigned.
|
||||
(val | mask) != -1) // Negative.
|
||||
Error("constant does not fit in a " + NumToString(bits) + "-bit field");
|
||||
}
|
||||
|
||||
// atot: templated version of atoi/atof: convert a string to an instance of T.
|
||||
template<typename T> inline T atot(const char *s) {
|
||||
auto val = StringToInt(s);
|
||||
CheckBitsFit(val, sizeof(T) * 8);
|
||||
return (T)val;
|
||||
}
|
||||
template<> inline bool atot<bool>(const char *s) {
|
||||
return 0 != atoi(s);
|
||||
}
|
||||
template<> inline float atot<float>(const char *s) {
|
||||
return static_cast<float>(strtod(s, nullptr));
|
||||
}
|
||||
template<> inline double atot<double>(const char *s) {
|
||||
return strtod(s, nullptr);
|
||||
}
|
||||
|
||||
template<> inline Offset<void> atot<Offset<void>>(const char *s) {
|
||||
return Offset<void>(atoi(s));
|
||||
}
|
||||
|
||||
// Declare tokens we'll use. Single character tokens are represented by their
|
||||
// ascii character code (e.g. '{'), others above 256.
|
||||
#define FLATBUFFERS_GEN_TOKENS(TD) \
|
||||
TD(Eof, 256, "end of file") \
|
||||
TD(StringConstant, 257, "string constant") \
|
||||
TD(IntegerConstant, 258, "integer constant") \
|
||||
TD(FloatConstant, 259, "float constant") \
|
||||
TD(Identifier, 260, "identifier") \
|
||||
TD(Table, 261, "table") \
|
||||
TD(Struct, 262, "struct") \
|
||||
TD(Enum, 263, "enum") \
|
||||
TD(Union, 264, "union") \
|
||||
TD(NameSpace, 265, "namespace") \
|
||||
TD(RootType, 266, "root_type")
|
||||
enum {
|
||||
#define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME,
|
||||
FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
|
||||
#undef FLATBUFFERS_TOKEN
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) kToken ## ENUM,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
|
||||
static std::string TokenToString(int t) {
|
||||
static const char *tokens[] = {
|
||||
#define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
|
||||
FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
|
||||
#undef FLATBUFFERS_TOKEN
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) IDLTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
if (t < 256) { // A single ascii char token.
|
||||
std::string s;
|
||||
s.append(1, t);
|
||||
return s;
|
||||
} else { // Other tokens.
|
||||
return tokens[t - 256];
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::Next() {
|
||||
doc_comment_.clear();
|
||||
bool seen_newline = false;
|
||||
for (;;) {
|
||||
char c = *cursor_++;
|
||||
token_ = c;
|
||||
switch (c) {
|
||||
case '\0': cursor_--; token_ = kTokenEof; return;
|
||||
case ' ': case '\r': case '\t': break;
|
||||
case '\n': line_++; seen_newline = true; break;
|
||||
case '{': case '}': case '(': case ')': case '[': case ']': return;
|
||||
case ',': case ':': case ';': case '=': return;
|
||||
case '.':
|
||||
if(!isdigit(*cursor_)) return;
|
||||
Error("floating point constant can\'t start with \".\"");
|
||||
break;
|
||||
case '\"':
|
||||
attribute_ = "";
|
||||
while (*cursor_ != '\"') {
|
||||
if (*cursor_ < ' ' && *cursor_ >= 0)
|
||||
Error("illegal character in string constant");
|
||||
if (*cursor_ == '\\') {
|
||||
cursor_++;
|
||||
switch (*cursor_) {
|
||||
case 'n': attribute_ += '\n'; cursor_++; break;
|
||||
case 't': attribute_ += '\t'; cursor_++; break;
|
||||
case 'r': attribute_ += '\r'; cursor_++; break;
|
||||
case '\"': attribute_ += '\"'; cursor_++; break;
|
||||
case '\\': attribute_ += '\\'; cursor_++; break;
|
||||
default: Error("unknown escape code in string constant"); break;
|
||||
}
|
||||
} else { // printable chars + UTF-8 bytes
|
||||
attribute_ += *cursor_++;
|
||||
}
|
||||
}
|
||||
cursor_++;
|
||||
token_ = kTokenStringConstant;
|
||||
return;
|
||||
case '/':
|
||||
if (*cursor_ == '/') {
|
||||
const char *start = ++cursor_;
|
||||
while (*cursor_ && *cursor_ != '\n') cursor_++;
|
||||
if (*start == '/') { // documentation comment
|
||||
if (!seen_newline)
|
||||
Error("a documentation comment should be on a line on its own");
|
||||
// todo: do we want to support multiline comments instead?
|
||||
doc_comment_ += std::string(start + 1, cursor_);
|
||||
}
|
||||
break;
|
||||
}
|
||||
// fall thru
|
||||
default:
|
||||
if (isalpha(static_cast<unsigned char>(c))) {
|
||||
// Collect all chars of an identifier:
|
||||
const char *start = cursor_ - 1;
|
||||
while (isalnum(static_cast<unsigned char>(*cursor_)) ||
|
||||
*cursor_ == '_')
|
||||
cursor_++;
|
||||
attribute_.clear();
|
||||
attribute_.append(start, cursor_);
|
||||
// First, see if it is a type keyword from the table of types:
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
if (attribute_ == IDLTYPE) { \
|
||||
token_ = kToken ## ENUM; \
|
||||
return; \
|
||||
}
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// If it's a boolean constant keyword, turn those into integers,
|
||||
// which simplifies our logic downstream.
|
||||
if (attribute_ == "true" || attribute_ == "false") {
|
||||
attribute_ = NumToString(attribute_ == "true");
|
||||
token_ = kTokenIntegerConstant;
|
||||
return;
|
||||
}
|
||||
// Check for declaration keywords:
|
||||
if (attribute_ == "table") { token_ = kTokenTable; return; }
|
||||
if (attribute_ == "struct") { token_ = kTokenStruct; return; }
|
||||
if (attribute_ == "enum") { token_ = kTokenEnum; return; }
|
||||
if (attribute_ == "union") { token_ = kTokenUnion; return; }
|
||||
if (attribute_ == "namespace") { token_ = kTokenNameSpace; return; }
|
||||
if (attribute_ == "root_type") { token_ = kTokenRootType; return; }
|
||||
// If not, it is a user-defined identifier:
|
||||
token_ = kTokenIdentifier;
|
||||
return;
|
||||
} else if (isdigit(static_cast<unsigned char>(c)) || c == '-') {
|
||||
const char *start = cursor_ - 1;
|
||||
while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
|
||||
if (*cursor_ == '.') {
|
||||
cursor_++;
|
||||
while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
|
||||
token_ = kTokenFloatConstant;
|
||||
} else {
|
||||
token_ = kTokenIntegerConstant;
|
||||
}
|
||||
attribute_.clear();
|
||||
attribute_.append(start, cursor_);
|
||||
return;
|
||||
}
|
||||
std::string ch;
|
||||
ch = c;
|
||||
if (c < ' ' || c > '~') ch = "code: " + NumToString(c);
|
||||
Error("illegal character: " + ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a given token is next, if so, consume it as well.
|
||||
bool Parser::IsNext(int t) {
|
||||
bool isnext = t == token_;
|
||||
if (isnext) Next();
|
||||
return isnext;
|
||||
}
|
||||
|
||||
// Expect a given token to be next, consume it, or error if not present.
|
||||
void Parser::Expect(int t) {
|
||||
if (t != token_) {
|
||||
Error("expecting: " + TokenToString(t) + " instead got: " +
|
||||
TokenToString(token_));
|
||||
}
|
||||
Next();
|
||||
}
|
||||
|
||||
// Parse any IDL type.
|
||||
void Parser::ParseType(Type &type) {
|
||||
if (token_ >= kTokenBOOL && token_ <= kTokenSTRING) {
|
||||
type.base_type = static_cast<BaseType>(token_ - kTokenNONE);
|
||||
} else {
|
||||
if (token_ == kTokenIdentifier) {
|
||||
auto enum_def = enums_.Lookup(attribute_);
|
||||
if (enum_def) {
|
||||
type = enum_def->underlying_type;
|
||||
if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
|
||||
} else {
|
||||
type.base_type = BASE_TYPE_STRUCT;
|
||||
type.struct_def = LookupCreateStruct(attribute_);
|
||||
}
|
||||
} else if (token_ == '[') {
|
||||
Next();
|
||||
Type subtype;
|
||||
ParseType(subtype);
|
||||
if (subtype.base_type == BASE_TYPE_VECTOR) {
|
||||
// We could support this, but it will complicate things, and it's
|
||||
// easier to work around with a struct around the inner vector.
|
||||
Error("nested vector types not supported (wrap in table first).");
|
||||
}
|
||||
if (subtype.base_type == BASE_TYPE_UNION) {
|
||||
// We could support this if we stored a struct of 2 elements per
|
||||
// union element.
|
||||
Error("vector of union types not supported (wrap in table first).");
|
||||
}
|
||||
type = Type(BASE_TYPE_VECTOR, subtype.struct_def);
|
||||
type.element = subtype.base_type;
|
||||
Expect(']');
|
||||
return;
|
||||
} else {
|
||||
Error("illegal type syntax");
|
||||
}
|
||||
}
|
||||
Next();
|
||||
}
|
||||
|
||||
FieldDef &Parser::AddField(StructDef &struct_def,
|
||||
const std::string &name,
|
||||
const Type &type) {
|
||||
auto &field = *new FieldDef();
|
||||
field.value.offset =
|
||||
FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
|
||||
field.name = name;
|
||||
field.value.type = type;
|
||||
if (struct_def.fixed) { // statically compute the field offset
|
||||
auto size = InlineSize(type);
|
||||
auto alignment = InlineAlignment(type);
|
||||
// structs_ need to have a predictable format, so we need to align to
|
||||
// the largest scalar
|
||||
struct_def.minalign = std::max(struct_def.minalign, alignment);
|
||||
struct_def.PadLastField(alignment);
|
||||
field.value.offset = static_cast<uoffset_t>(struct_def.bytesize);
|
||||
struct_def.bytesize += size;
|
||||
}
|
||||
if (struct_def.fields.Add(name, &field))
|
||||
Error("field already exists: " + name);
|
||||
return field;
|
||||
}
|
||||
|
||||
void Parser::ParseField(StructDef &struct_def) {
|
||||
std::string name = attribute_;
|
||||
std::string dc = doc_comment_;
|
||||
Expect(kTokenIdentifier);
|
||||
Expect(':');
|
||||
Type type;
|
||||
ParseType(type);
|
||||
|
||||
if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type))
|
||||
Error("structs_ may contain only scalar or struct fields");
|
||||
|
||||
if (type.base_type == BASE_TYPE_UNION) {
|
||||
// For union fields, add a second auto-generated field to hold the type,
|
||||
// with _type appended as the name.
|
||||
AddField(struct_def, name + "_type", type.enum_def->underlying_type);
|
||||
}
|
||||
|
||||
auto &field = AddField(struct_def, name, type);
|
||||
|
||||
if (token_ == '=') {
|
||||
Next();
|
||||
ParseSingleValue(field.value);
|
||||
}
|
||||
|
||||
field.doc_comment = dc;
|
||||
ParseMetaData(field);
|
||||
field.deprecated = field.attributes.Lookup("deprecated") != nullptr;
|
||||
if (field.deprecated && struct_def.fixed)
|
||||
Error("can't deprecate fields in a struct");
|
||||
|
||||
Expect(';');
|
||||
}
|
||||
|
||||
void Parser::ParseAnyValue(Value &val, FieldDef *field) {
|
||||
switch (val.type.base_type) {
|
||||
case BASE_TYPE_UNION: {
|
||||
assert(field);
|
||||
if (!field_stack_.size() ||
|
||||
field_stack_.back().second->value.type.base_type != BASE_TYPE_UTYPE)
|
||||
Error("missing type field before this union value: " + field->name);
|
||||
auto enum_idx = atot<unsigned char>(
|
||||
field_stack_.back().first.constant.c_str());
|
||||
auto struct_def = val.type.enum_def->ReverseLookup(enum_idx);
|
||||
if (!struct_def) Error("illegal type id for: " + field->name);
|
||||
val.constant = NumToString(ParseTable(*struct_def));
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_STRUCT:
|
||||
val.constant = NumToString(ParseTable(*val.type.struct_def));
|
||||
break;
|
||||
case BASE_TYPE_STRING: {
|
||||
auto s = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
val.constant = NumToString(builder_.CreateString(s).o);
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_VECTOR: {
|
||||
Expect('[');
|
||||
val.constant = NumToString(ParseVector(val.type.VectorType()));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ParseSingleValue(val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
|
||||
auto off = atot<uoffset_t>(val.constant.c_str());
|
||||
assert(struct_stack_.size() - off == struct_def.bytesize);
|
||||
builder_.Align(struct_def.minalign);
|
||||
builder_.PushBytes(&struct_stack_[off], struct_def.bytesize);
|
||||
struct_stack_.resize(struct_stack_.size() - struct_def.bytesize);
|
||||
builder_.AddStructOffset(val.offset, builder_.GetSize());
|
||||
}
|
||||
|
||||
uoffset_t Parser::ParseTable(const StructDef &struct_def) {
|
||||
Expect('{');
|
||||
size_t fieldn = 0;
|
||||
for (;;) {
|
||||
std::string name = attribute_;
|
||||
if (!IsNext(kTokenStringConstant)) Expect(kTokenIdentifier);
|
||||
auto field = struct_def.fields.Lookup(name);
|
||||
if (!field) Error("unknown field: " + name);
|
||||
if (struct_def.fixed && (fieldn >= struct_def.fields.vec.size()
|
||||
|| struct_def.fields.vec[fieldn] != field)) {
|
||||
Error("struct field appearing out of order: " + name);
|
||||
}
|
||||
Expect(':');
|
||||
Value val = field->value;
|
||||
ParseAnyValue(val, field);
|
||||
field_stack_.push_back(std::make_pair(val, field));
|
||||
fieldn++;
|
||||
if (IsNext('}')) break;
|
||||
Expect(',');
|
||||
}
|
||||
if (struct_def.fixed && fieldn != struct_def.fields.vec.size())
|
||||
Error("incomplete struct initialization: " + struct_def.name);
|
||||
auto start = struct_def.fixed
|
||||
? builder_.StartStruct(struct_def.minalign)
|
||||
: builder_.StartTable();
|
||||
|
||||
for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1;
|
||||
size;
|
||||
size /= 2) {
|
||||
// Go through elements in reverse, since we're building the data backwards.
|
||||
for (auto it = field_stack_.rbegin();
|
||||
it != field_stack_.rbegin() + fieldn; ++it) {
|
||||
auto &value = it->first;
|
||||
auto field = it->second;
|
||||
if (!struct_def.sortbysize || size == SizeOf(value.type.base_type)) {
|
||||
switch (value.type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
builder_.Pad(field->padding); \
|
||||
builder_.AddElement(value.offset, \
|
||||
atot<CTYPE>( value.constant.c_str()), \
|
||||
atot<CTYPE>(field->value.constant.c_str())); \
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
|
||||
#undef FLATBUFFERS_TD
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
builder_.Pad(field->padding); \
|
||||
if (IsStruct(field->value.type)) { \
|
||||
SerializeStruct(*field->value.type.struct_def, value); \
|
||||
} else { \
|
||||
builder_.AddOffset(value.offset, \
|
||||
atot<CTYPE>(value.constant.c_str())); \
|
||||
} \
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD);
|
||||
#undef FLATBUFFERS_TD
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < fieldn; i++) field_stack_.pop_back();
|
||||
|
||||
if (struct_def.fixed) {
|
||||
builder_.ClearOffsets();
|
||||
builder_.EndStruct();
|
||||
// Temporarily store this struct in a side buffer, since this data has to
|
||||
// be stored in-line later in the parent object.
|
||||
auto off = struct_stack_.size();
|
||||
struct_stack_.insert(struct_stack_.end(),
|
||||
builder_.GetBufferPointer(),
|
||||
builder_.GetBufferPointer() + struct_def.bytesize);
|
||||
builder_.PopBytes(struct_def.bytesize);
|
||||
return static_cast<uoffset_t>(off);
|
||||
} else {
|
||||
return builder_.EndTable(
|
||||
start,
|
||||
static_cast<voffset_t>(struct_def.fields.vec.size()));
|
||||
}
|
||||
}
|
||||
|
||||
uoffset_t Parser::ParseVector(const Type &type) {
|
||||
int count = 0;
|
||||
if (token_ != ']') for (;;) {
|
||||
Value val;
|
||||
val.type = type;
|
||||
ParseAnyValue(val, NULL);
|
||||
field_stack_.push_back(std::make_pair(val, nullptr));
|
||||
count++;
|
||||
if (token_ == ']') break;
|
||||
Expect(',');
|
||||
}
|
||||
Next();
|
||||
|
||||
builder_.StartVector(count * InlineSize(type), InlineAlignment((type)));
|
||||
for (int i = 0; i < count; i++) {
|
||||
// start at the back, since we're building the data backwards.
|
||||
auto &val = field_stack_.back().first;
|
||||
switch (val.type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
|
||||
else builder_.PushElement(atot<CTYPE>(val.constant.c_str())); \
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
}
|
||||
field_stack_.pop_back();
|
||||
}
|
||||
|
||||
builder_.ClearOffsets();
|
||||
return builder_.EndVector(count);
|
||||
}
|
||||
|
||||
void Parser::ParseMetaData(Definition &def) {
|
||||
if (IsNext('(')) {
|
||||
for (;;) {
|
||||
auto name = attribute_;
|
||||
Expect(kTokenIdentifier);
|
||||
auto e = new Value();
|
||||
def.attributes.Add(name, e);
|
||||
if (IsNext(':')) {
|
||||
ParseSingleValue(*e);
|
||||
}
|
||||
if (IsNext(')')) break;
|
||||
Expect(',');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Parser::TryTypedValue(int dtoken,
|
||||
bool check,
|
||||
Value &e,
|
||||
BaseType req) {
|
||||
bool match = dtoken == token_;
|
||||
if (match) {
|
||||
e.constant = attribute_;
|
||||
if (!check) {
|
||||
if (e.type.base_type == BASE_TYPE_NONE) {
|
||||
e.type.base_type = req;
|
||||
} else {
|
||||
Error(std::string("type mismatch: expecting: ") +
|
||||
kTypeNames[e.type.base_type] +
|
||||
", found: " +
|
||||
kTypeNames[req]);
|
||||
}
|
||||
}
|
||||
Next();
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
void Parser::ParseSingleValue(Value &e) {
|
||||
if (TryTypedValue(kTokenIntegerConstant,
|
||||
IsScalar(e.type.base_type),
|
||||
e,
|
||||
BASE_TYPE_INT) ||
|
||||
TryTypedValue(kTokenFloatConstant,
|
||||
IsFloat(e.type.base_type),
|
||||
e,
|
||||
BASE_TYPE_FLOAT) ||
|
||||
TryTypedValue(kTokenStringConstant,
|
||||
e.type.base_type == BASE_TYPE_STRING,
|
||||
e,
|
||||
BASE_TYPE_STRING)) {
|
||||
} else if (token_ == kTokenIdentifier) {
|
||||
for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
|
||||
auto ev = (*it)->vals.Lookup(attribute_);
|
||||
if (ev) {
|
||||
attribute_ = NumToString(ev->value);
|
||||
TryTypedValue(kTokenIdentifier,
|
||||
IsInteger(e.type.base_type),
|
||||
e,
|
||||
BASE_TYPE_INT);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Error("not valid enum value: " + attribute_);
|
||||
} else {
|
||||
Error("cannot parse value starting with: " + TokenToString(token_));
|
||||
}
|
||||
}
|
||||
|
||||
StructDef *Parser::LookupCreateStruct(const std::string &name) {
|
||||
auto struct_def = structs_.Lookup(name);
|
||||
if (!struct_def) {
|
||||
// Rather than failing, we create a "pre declared" StructDef, due to
|
||||
// circular references, and check for errors at the end of parsing.
|
||||
struct_def = new StructDef();
|
||||
structs_.Add(name, struct_def);
|
||||
struct_def->name = name;
|
||||
struct_def->predecl = true;
|
||||
}
|
||||
return struct_def;
|
||||
}
|
||||
|
||||
void Parser::ParseEnum(bool is_union) {
|
||||
std::string dc = doc_comment_;
|
||||
Next();
|
||||
std::string name = attribute_;
|
||||
Expect(kTokenIdentifier);
|
||||
auto &enum_def = *new EnumDef();
|
||||
enum_def.name = name;
|
||||
enum_def.doc_comment = dc;
|
||||
enum_def.is_union = is_union;
|
||||
if (enums_.Add(name, &enum_def)) Error("enum already exists: " + name);
|
||||
if (is_union) {
|
||||
enum_def.underlying_type.base_type = BASE_TYPE_UTYPE;
|
||||
enum_def.underlying_type.enum_def = &enum_def;
|
||||
} else if (IsNext(':')) {
|
||||
// short is the default type for fields when you use enums,
|
||||
// though people are encouraged to pick any integer type instead.
|
||||
ParseType(enum_def.underlying_type);
|
||||
if (!IsInteger(enum_def.underlying_type.base_type))
|
||||
Error("underlying enum type must be integral");
|
||||
} else {
|
||||
enum_def.underlying_type.base_type = BASE_TYPE_SHORT;
|
||||
}
|
||||
ParseMetaData(enum_def);
|
||||
Expect('{');
|
||||
if (is_union) enum_def.vals.Add("NONE", new EnumVal("NONE", 0));
|
||||
do {
|
||||
std::string name = attribute_;
|
||||
std::string dc = doc_comment_;
|
||||
Expect(kTokenIdentifier);
|
||||
auto prevsize = enum_def.vals.vec.size();
|
||||
auto &ev = *new EnumVal(name, static_cast<int>(
|
||||
enum_def.vals.vec.size()
|
||||
? enum_def.vals.vec.back()->value + 1
|
||||
: 0));
|
||||
if (enum_def.vals.Add(name, &ev))
|
||||
Error("enum value already exists: " + name);
|
||||
ev.doc_comment = dc;
|
||||
if (is_union) {
|
||||
ev.struct_def = LookupCreateStruct(name);
|
||||
}
|
||||
if (IsNext('=')) {
|
||||
ev.value = atoi(attribute_.c_str());
|
||||
Expect(kTokenIntegerConstant);
|
||||
if (prevsize && enum_def.vals.vec[prevsize - 1]->value >= ev.value)
|
||||
Error("enum values must be specified in ascending order");
|
||||
}
|
||||
} while (IsNext(','));
|
||||
Expect('}');
|
||||
}
|
||||
|
||||
void Parser::ParseDecl() {
|
||||
std::string dc = doc_comment_;
|
||||
bool fixed = IsNext(kTokenStruct);
|
||||
if (!fixed) Expect(kTokenTable);
|
||||
std::string name = attribute_;
|
||||
Expect(kTokenIdentifier);
|
||||
auto &struct_def = *LookupCreateStruct(name);
|
||||
if (!struct_def.predecl) Error("datatype already exists: " + name);
|
||||
struct_def.predecl = false;
|
||||
struct_def.name = name;
|
||||
struct_def.doc_comment = dc;
|
||||
struct_def.fixed = fixed;
|
||||
// Move this struct to the back of the vector just in case it was predeclared,
|
||||
// to preserve declartion order.
|
||||
remove(structs_.vec.begin(), structs_.vec.end(), &struct_def);
|
||||
structs_.vec.back() = &struct_def;
|
||||
ParseMetaData(struct_def);
|
||||
struct_def.sortbysize =
|
||||
struct_def.attributes.Lookup("original_order") == nullptr && !fixed;
|
||||
Expect('{');
|
||||
while (token_ != '}') ParseField(struct_def);
|
||||
struct_def.PadLastField(struct_def.minalign);
|
||||
Expect('}');
|
||||
auto force_align = struct_def.attributes.Lookup("force_align");
|
||||
if (fixed && force_align) {
|
||||
auto align = static_cast<size_t>(atoi(force_align->constant.c_str()));
|
||||
if (force_align->type.base_type != BASE_TYPE_INT ||
|
||||
align < struct_def.minalign ||
|
||||
align > 256 ||
|
||||
align & (align - 1))
|
||||
Error("force_align must be a power of two integer ranging from the"
|
||||
"struct\'s natural alignment to 256");
|
||||
struct_def.minalign = align;
|
||||
}
|
||||
}
|
||||
|
||||
bool Parser::SetRootType(const char *name) {
|
||||
root_struct_def = structs_.Lookup(name);
|
||||
return root_struct_def != nullptr;
|
||||
}
|
||||
|
||||
bool Parser::Parse(const char *source) {
|
||||
source_ = cursor_ = source;
|
||||
line_ = 1;
|
||||
error_.clear();
|
||||
builder_.Clear();
|
||||
try {
|
||||
Next();
|
||||
while (token_ != kTokenEof) {
|
||||
if (token_ == kTokenNameSpace) {
|
||||
Next();
|
||||
for (;;) {
|
||||
name_space_.push_back(attribute_);
|
||||
Expect(kTokenIdentifier);
|
||||
if (!IsNext('.')) break;
|
||||
}
|
||||
Expect(';');
|
||||
} else if (token_ == '{') {
|
||||
if (!root_struct_def) Error("no root type set to parse json with");
|
||||
if (builder_.GetSize()) {
|
||||
Error("cannot have more than one json object in a file");
|
||||
}
|
||||
builder_.Finish(Offset<Table>(ParseTable(*root_struct_def)));
|
||||
} else if (token_ == kTokenEnum) {
|
||||
ParseEnum(false);
|
||||
} else if (token_ == kTokenUnion) {
|
||||
ParseEnum(true);
|
||||
} else if (token_ == kTokenRootType) {
|
||||
Next();
|
||||
auto root_type = attribute_;
|
||||
Expect(kTokenIdentifier);
|
||||
Expect(';');
|
||||
if (!SetRootType(root_type.c_str()))
|
||||
Error("unknown root type: " + root_type);
|
||||
if (root_struct_def->fixed)
|
||||
Error("root type must be a table");
|
||||
} else {
|
||||
ParseDecl();
|
||||
}
|
||||
}
|
||||
for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
|
||||
if ((*it)->predecl)
|
||||
Error("type referenced but not defined: " + (*it)->name);
|
||||
}
|
||||
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;
|
||||
if (val.struct_def && val.struct_def->fixed)
|
||||
Error("only tables can be union elements: " + val.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const std::string &msg) {
|
||||
error_ = "line " + NumToString(line_) + ": " + msg;
|
||||
return false;
|
||||
}
|
||||
assert(!struct_stack_.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
21
tests/JavaTest.bat
Executable file
@@ -0,0 +1,21 @@
|
||||
@echo off
|
||||
rem Copyright 2014 Google Inc. All rights reserved.
|
||||
rem
|
||||
rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
rem you may not use this file except in compliance with the License.
|
||||
rem You may obtain a copy of the License at
|
||||
rem
|
||||
rem http://www.apache.org/licenses/LICENSE-2.0
|
||||
rem
|
||||
rem Unless required by applicable law or agreed to in writing, software
|
||||
rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
rem See the License for the specific language governing permissions and
|
||||
rem limitations under the License.
|
||||
|
||||
rem Compile then run the Java test.
|
||||
|
||||
set batch_file_dir=%~d0%~p0
|
||||
|
||||
javac -classpath %batch_file_dir%\..\java;%batch_file_dir% JavaTest.java
|
||||
java -classpath %batch_file_dir%\..\java;%batch_file_dir% JavaTest
|
||||
145
tests/JavaTest.java
Executable file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import MyGame.Example.*;
|
||||
import flatbuffers.FlatBufferBuilder;
|
||||
|
||||
class JavaTest {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// First, let's test reading a FlatBuffer generated by C++ code:
|
||||
// This file was generated from monsterdata_test.json
|
||||
|
||||
byte[] data = null;
|
||||
File file = new File("monsterdata_test_wire.bin");
|
||||
RandomAccessFile f = null;
|
||||
try {
|
||||
f = new RandomAccessFile(file, "r");
|
||||
data = new byte[(int)f.length()];
|
||||
f.readFully(data);
|
||||
f.close();
|
||||
} catch(java.io.IOException e) {
|
||||
System.out.println("FlatBuffers test: couldn't read file");
|
||||
return;
|
||||
}
|
||||
|
||||
// Now test it:
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(data);
|
||||
TestBuffer(bb, 0);
|
||||
|
||||
// Second, let's create a FlatBuffer from scratch in Java, and test it also.
|
||||
// We set up the same values as monsterdata.json:
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(1024);
|
||||
|
||||
int str = fbb.createString("MyMonster");
|
||||
|
||||
Monster.startInventoryVector(fbb, 5);
|
||||
for (byte i = 4; i >=0; i--) fbb.addByte(i);
|
||||
int inv = fbb.endVector();
|
||||
|
||||
Monster.startMonster(fbb);
|
||||
Monster.addHp(fbb, (short)20);
|
||||
int mon2 = Monster.endMonster(fbb);
|
||||
|
||||
Monster.startTest4Vector(fbb, 2);
|
||||
Test.createTest(fbb, (short)10, (byte)20);
|
||||
Test.createTest(fbb, (short)30, (byte)40);
|
||||
int test4 = fbb.endVector();
|
||||
|
||||
Monster.startMonster(fbb);
|
||||
Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
|
||||
(byte)4, (short)5, (byte)6));
|
||||
Monster.addHp(fbb, (short)80);
|
||||
Monster.addName(fbb, str);
|
||||
Monster.addInventory(fbb, inv);
|
||||
Monster.addTestType(fbb, (byte)1);
|
||||
Monster.addTest(fbb, mon2);
|
||||
Monster.addTest4(fbb, test4);
|
||||
int mon = Monster.endMonster(fbb);
|
||||
|
||||
fbb.finish(mon);
|
||||
|
||||
// Write the result to a file for debugging purposes:
|
||||
// Note that the binaries are not necessarily identical, since the JSON
|
||||
// parser may serialize in a slightly different order than the above
|
||||
// Java code. They are functionally equivalent though.
|
||||
|
||||
try {
|
||||
DataOutputStream os = new DataOutputStream(new FileOutputStream(
|
||||
"monsterdata_java_wire.bin"));
|
||||
os.write(fbb.dataBuffer().array(), fbb.dataStart(), fbb.offset());
|
||||
os.close();
|
||||
} catch(java.io.IOException e) {
|
||||
System.out.println("FlatBuffers test: couldn't write file");
|
||||
return;
|
||||
}
|
||||
|
||||
// Test it:
|
||||
|
||||
TestBuffer(fbb.dataBuffer(), fbb.dataStart());
|
||||
|
||||
System.out.println("FlatBuffers test: completed successfully");
|
||||
}
|
||||
|
||||
static void TestBuffer(ByteBuffer bb, int start) {
|
||||
Monster monster = Monster.getRootAsMonster(bb, start);
|
||||
|
||||
TestEq(monster.hp(), (short)80);
|
||||
TestEq(monster.mana(), (short)150); // default
|
||||
|
||||
TestEq(monster.name(), "MyMonster");
|
||||
// monster.friendly() // can't access, deprecated
|
||||
|
||||
Vec3 pos = monster.pos();
|
||||
TestEq(pos.x(), 1.0f);
|
||||
TestEq(pos.y(), 2.0f);
|
||||
TestEq(pos.z(), 3.0f);
|
||||
TestEq(pos.test1(), 3.0);
|
||||
TestEq(pos.test2(), (byte)4);
|
||||
Test t = pos.test3();
|
||||
TestEq(t.a(), (short)5);
|
||||
TestEq(t.b(), (byte)6);
|
||||
|
||||
TestEq(monster.testType(), (byte)Any.Monster);
|
||||
Monster monster2 = new Monster();
|
||||
TestEq(monster.test(monster2) != null, true);
|
||||
TestEq(monster2.hp(), (short)20);
|
||||
|
||||
TestEq(monster.inventoryLength(), 5);
|
||||
int invsum = 0;
|
||||
for (int i = 0; i < monster.inventoryLength(); i++)
|
||||
invsum += monster.inventory(i);
|
||||
TestEq(invsum, 10);
|
||||
|
||||
Test test_0 = monster.test4(0);
|
||||
Test test_1 = monster.test4(1);
|
||||
TestEq(monster.test4Length(), 2);
|
||||
TestEq(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100);
|
||||
}
|
||||
|
||||
static <T> void TestEq(T a, T b) {
|
||||
if (!a.equals(b)) {
|
||||
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());
|
||||
System.out.println("FlatBuffers test FAILED: \'" + a + "\' != \'" + b + "\'");
|
||||
assert false;
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
tests/MyGame/Example/Any.java
Executable file
@@ -0,0 +1,14 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package MyGame.Example;
|
||||
|
||||
import java.nio.*;
|
||||
import java.lang.*;
|
||||
import java.util.*;
|
||||
import flatbuffers.*;
|
||||
|
||||
public class Any {
|
||||
public static final byte NONE = 0;
|
||||
public static final byte Monster = 1;
|
||||
};
|
||||
|
||||
15
tests/MyGame/Example/Color.java
Executable file
@@ -0,0 +1,15 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package MyGame.Example;
|
||||
|
||||
import java.nio.*;
|
||||
import java.lang.*;
|
||||
import java.util.*;
|
||||
import flatbuffers.*;
|
||||
|
||||
public class Color {
|
||||
public static final byte Red = 0;
|
||||
public static final byte Green = 1;
|
||||
public static final byte Blue = 2;
|
||||
};
|
||||
|
||||
42
tests/MyGame/Example/Monster.java
Executable file
@@ -0,0 +1,42 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package MyGame.Example;
|
||||
|
||||
import java.nio.*;
|
||||
import java.lang.*;
|
||||
import java.util.*;
|
||||
import flatbuffers.*;
|
||||
|
||||
public class Monster extends Table {
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb, int offset) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Monster()).__init(_bb.getInt(offset) + offset, _bb); }
|
||||
public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
public Vec3 pos() { return pos(new Vec3()); }
|
||||
public Vec3 pos(Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__init(o + bb_pos, bb) : null; }
|
||||
public short mana() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 150; }
|
||||
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) : null; }
|
||||
public byte inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int inventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; }
|
||||
/// an example documentation comment: this will end up in the generated code multiline too
|
||||
public byte color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 2; }
|
||||
public byte testType() { int o = __offset(18); return o != 0 ? bb.get(o + bb_pos) : 0; }
|
||||
public Table test(Table obj) { int o = __offset(20); return o != 0 ? __union(obj, o) : null; }
|
||||
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 static void startMonster(FlatBufferBuilder builder) { builder.startObject(10); }
|
||||
public static void addPos(FlatBufferBuilder builder, int pos) { builder.addStruct(0, pos, 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); }
|
||||
public static void addName(FlatBufferBuilder builder, int name) { builder.addOffset(3, name, 0); }
|
||||
public static void addInventory(FlatBufferBuilder builder, int inventory) { builder.addOffset(5, inventory, 0); }
|
||||
public static void startInventoryVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems); }
|
||||
public static void addColor(FlatBufferBuilder builder, byte color) { builder.addByte(6, color, 2); }
|
||||
public static void addTestType(FlatBufferBuilder builder, byte testType) { builder.addByte(7, testType, 0); }
|
||||
public static void addTest(FlatBufferBuilder builder, int test) { builder.addOffset(8, test, 0); }
|
||||
public static void addTest4(FlatBufferBuilder builder, int test4) { builder.addOffset(9, test4, 0); }
|
||||
public static void startTest4Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems); }
|
||||
public static int endMonster(FlatBufferBuilder builder) { return builder.endObject(); }
|
||||
};
|
||||
|
||||
23
tests/MyGame/Example/Test.java
Executable file
@@ -0,0 +1,23 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package MyGame.Example;
|
||||
|
||||
import java.nio.*;
|
||||
import java.lang.*;
|
||||
import java.util.*;
|
||||
import flatbuffers.*;
|
||||
|
||||
public class Test extends Struct {
|
||||
public Test __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
public short a() { return bb.getShort(bb_pos + 0); }
|
||||
public byte b() { return bb.get(bb_pos + 2); }
|
||||
|
||||
public static int createTest(FlatBufferBuilder builder, short a, byte b) {
|
||||
builder.prep(2, 0);
|
||||
builder.pad(1);
|
||||
builder.putByte(b);
|
||||
builder.putShort(a);
|
||||
return builder.offset();
|
||||
}
|
||||
};
|
||||
|
||||
37
tests/MyGame/Example/Vec3.java
Executable file
@@ -0,0 +1,37 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package MyGame.Example;
|
||||
|
||||
import java.nio.*;
|
||||
import java.lang.*;
|
||||
import java.util.*;
|
||||
import flatbuffers.*;
|
||||
|
||||
public class Vec3 extends Struct {
|
||||
public Vec3 __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
|
||||
public float x() { return bb.getFloat(bb_pos + 0); }
|
||||
public float y() { return bb.getFloat(bb_pos + 4); }
|
||||
public float z() { return bb.getFloat(bb_pos + 8); }
|
||||
public double test1() { return bb.getDouble(bb_pos + 16); }
|
||||
public byte test2() { return bb.get(bb_pos + 24); }
|
||||
public Test test3() { return test3(new Test()); }
|
||||
public Test test3(Test obj) { return obj.__init(bb_pos + 26, bb); }
|
||||
|
||||
public static int createVec3(FlatBufferBuilder builder, float x, float y, float z, double test1, byte test2, short Test_a, byte Test_b) {
|
||||
builder.prep(16, 0);
|
||||
builder.pad(2);
|
||||
builder.prep(2, 0);
|
||||
builder.pad(1);
|
||||
builder.putByte(Test_b);
|
||||
builder.putShort(Test_a);
|
||||
builder.pad(1);
|
||||
builder.putByte(test2);
|
||||
builder.putDouble(test1);
|
||||
builder.pad(4);
|
||||
builder.putFloat(z);
|
||||
builder.putFloat(y);
|
||||
builder.putFloat(x);
|
||||
return builder.offset();
|
||||
}
|
||||
};
|
||||
|
||||
34
tests/monster_test.fbs
Executable file
@@ -0,0 +1,34 @@
|
||||
// example IDL file
|
||||
|
||||
namespace MyGame.Example;
|
||||
|
||||
enum Color:byte { Red = 0, Green, Blue = 2 }
|
||||
|
||||
union Any { Monster } // TODO: add more elements
|
||||
|
||||
struct Test { a:short; b:byte; }
|
||||
|
||||
struct Vec3 (force_align: 16) {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
test1:double;
|
||||
test2:byte;
|
||||
test3:Test;
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3;
|
||||
mana:short = 150;
|
||||
hp:short = 100;
|
||||
name:string;
|
||||
friendly:bool = false (deprecated, priority: 1);
|
||||
inventory:[ubyte];
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
color:Color = Blue;
|
||||
test:Any;
|
||||
test4:[Test];
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
123
tests/monster_test_generated.h
Executable file
@@ -0,0 +1,123 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
namespace MyGame {
|
||||
namespace Example {
|
||||
|
||||
enum {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
static const char *names[] = { "Red", "Green", "Blue", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
|
||||
enum {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1,
|
||||
};
|
||||
|
||||
inline const char **EnumNamesAny() {
|
||||
static const char *names[] = { "NONE", "Monster", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
|
||||
struct Test;
|
||||
struct Vec3;
|
||||
struct Monster;
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(2) Test {
|
||||
private:
|
||||
int16_t a_;
|
||||
int8_t b_;
|
||||
int8_t __padding0;
|
||||
|
||||
public:
|
||||
Test(int16_t a, int8_t b)
|
||||
: a_(flatbuffers::EndianScalar(a)), b_(flatbuffers::EndianScalar(b)), __padding0(0) {}
|
||||
|
||||
int16_t a() const { return flatbuffers::EndianScalar(a_); }
|
||||
int8_t b() const { return flatbuffers::EndianScalar(b_); }
|
||||
};
|
||||
STRUCT_END(Test, 4);
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(16) Vec3 {
|
||||
private:
|
||||
float x_;
|
||||
float y_;
|
||||
float z_;
|
||||
int32_t __padding0;
|
||||
double test1_;
|
||||
int8_t test2_;
|
||||
int8_t __padding1;
|
||||
Test test3_;
|
||||
int16_t __padding2;
|
||||
|
||||
public:
|
||||
Vec3(float x, float y, float z, double test1, int8_t test2, const Test &test3)
|
||||
: x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)), __padding0(0), test1_(flatbuffers::EndianScalar(test1)), test2_(flatbuffers::EndianScalar(test2)), __padding1(0), test3_(test3), __padding2(0) {}
|
||||
|
||||
float x() const { return flatbuffers::EndianScalar(x_); }
|
||||
float y() const { return flatbuffers::EndianScalar(y_); }
|
||||
float z() const { return flatbuffers::EndianScalar(z_); }
|
||||
double test1() const { return flatbuffers::EndianScalar(test1_); }
|
||||
int8_t test2() const { return flatbuffers::EndianScalar(test2_); }
|
||||
const Test &test3() const { return test3_; }
|
||||
};
|
||||
STRUCT_END(Vec3, 32);
|
||||
|
||||
struct Monster : 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); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
/// an example documentation comment: this will end up in the generated code multiline too
|
||||
int8_t color() const { return GetField<int8_t>(16, 2); }
|
||||
uint8_t test_type() const { return GetField<uint8_t>(18, 0); }
|
||||
const void *test() const { return GetPointer<const void *>(20); }
|
||||
const flatbuffers::Vector<const Test *> *test4() const { return GetPointer<const flatbuffers::Vector<const Test *> *>(22); }
|
||||
};
|
||||
|
||||
struct MonsterBuilder {
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_pos(const Vec3 *pos) { fbb_.AddStruct(4, pos); }
|
||||
void add_mana(int16_t mana) { fbb_.AddElement<int16_t>(6, mana, 150); }
|
||||
void add_hp(int16_t hp) { fbb_.AddElement<int16_t>(8, hp, 100); }
|
||||
void add_name(flatbuffers::Offset<flatbuffers::String> name) { fbb_.AddOffset(10, name); }
|
||||
void add_inventory(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory) { fbb_.AddOffset(14, inventory); }
|
||||
void add_color(int8_t color) { fbb_.AddElement<int8_t>(16, color, 2); }
|
||||
void add_test_type(uint8_t test_type) { fbb_.AddElement<uint8_t>(18, test_type, 0); }
|
||||
void add_test(flatbuffers::Offset<void> test) { fbb_.AddOffset(20, test); }
|
||||
void add_test4(flatbuffers::Offset<flatbuffers::Vector<const Test *>> test4) { fbb_.AddOffset(22, test4); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
flatbuffers::Offset<Monster> Finish() { return flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 10)); }
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const Vec3 *pos, int16_t mana, int16_t hp, flatbuffers::Offset<flatbuffers::String> name, flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory, int8_t color, uint8_t test_type, flatbuffers::Offset<void> test, flatbuffers::Offset<flatbuffers::Vector<const Test *>> test4) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_test4(test4);
|
||||
builder_.add_test(test);
|
||||
builder_.add_inventory(inventory);
|
||||
builder_.add_name(name);
|
||||
builder_.add_pos(pos);
|
||||
builder_.add_hp(hp);
|
||||
builder_.add_mana(mana);
|
||||
builder_.add_test_type(test_type);
|
||||
builder_.add_color(color);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
|
||||
|
||||
}; // namespace MyGame
|
||||
}; // namespace Example
|
||||
36
tests/monsterdata_test.json
Executable file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
pos: {
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3,
|
||||
test1: 3,
|
||||
test2: 4,
|
||||
test3: {
|
||||
a: 5,
|
||||
b: 6
|
||||
}
|
||||
},
|
||||
hp: 80,
|
||||
name: "MyMonster",
|
||||
inventory: [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
],
|
||||
test_type: 1,
|
||||
test: {
|
||||
hp: 20
|
||||
},
|
||||
test4: [
|
||||
{
|
||||
a: 10,
|
||||
b: 20
|
||||
},
|
||||
{
|
||||
a: 30,
|
||||
b: 40
|
||||
}
|
||||
]
|
||||
}
|
||||