mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
Compare commits
63 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdb0dca39d | ||
|
|
f2908b7787 | ||
|
|
0b47e69d4d | ||
|
|
620d8d6f7c | ||
|
|
39d4b7e2bf | ||
|
|
5da7bda826 | ||
|
|
be3c874258 | ||
|
|
d8a173ddc7 | ||
|
|
8f80fecc44 | ||
|
|
7057033116 | ||
|
|
15b10dd9f3 | ||
|
|
52f4f4573e | ||
|
|
7a99b3c7cb | ||
|
|
0477a54f65 | ||
|
|
9c3de1e2a0 | ||
|
|
bba042d723 | ||
|
|
63f21cb2a5 | ||
|
|
60acef94e1 | ||
|
|
3f53f73c63 | ||
|
|
d03ba640be | ||
|
|
127d35085a | ||
|
|
bd86bf60ec | ||
|
|
3e201a99b2 | ||
|
|
9143a93312 | ||
|
|
74d5f3701f | ||
|
|
3fb6a86d02 | ||
|
|
2811a3eac8 | ||
|
|
b632f8129f | ||
|
|
df2e0db63d | ||
|
|
cc112ce96f | ||
|
|
d1efa95369 | ||
|
|
362268d2df | ||
|
|
93df5697a0 | ||
|
|
2208de0676 | ||
|
|
75349ae8c3 | ||
|
|
b3ee52c0a7 | ||
|
|
7fcbe723fc | ||
|
|
9140144d51 | ||
|
|
a5f50019bc | ||
|
|
b863ac0171 | ||
|
|
f0b3c75779 | ||
|
|
350011f581 | ||
|
|
1256307a38 | ||
|
|
a0b6ffc25b | ||
|
|
59043114ac | ||
|
|
d58da1db14 | ||
|
|
5b4c39250f | ||
|
|
82ef4ae2e1 | ||
|
|
76227f201d | ||
|
|
ff0d7a89d8 | ||
|
|
e2c02b833e | ||
|
|
65cfa18855 | ||
|
|
58703a4f4c | ||
|
|
0b60ece438 | ||
|
|
69dc8cbbc3 | ||
|
|
fadb71f2a2 | ||
|
|
aab06c6bea | ||
|
|
ad84a663be | ||
|
|
66583e8a3f | ||
|
|
66de19ace8 | ||
|
|
41a6d35e74 | ||
|
|
1485180517 | ||
|
|
f694bf0913 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -33,4 +33,5 @@ flattests
|
||||
flatsamplebinary
|
||||
flatsampletext
|
||||
snapshot.sh
|
||||
|
||||
tests/go_gen
|
||||
CMakeLists.txt.user
|
||||
|
||||
56
CMake/FindFlatBuffers.cmake
Normal file
56
CMake/FindFlatBuffers.cmake
Normal file
@@ -0,0 +1,56 @@
|
||||
# Copyright 2014 Stefan.Eilemann@epfl.ch
|
||||
# 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.
|
||||
|
||||
# Find the flatbuffers schema compiler
|
||||
#
|
||||
# Output Variables:
|
||||
# * FLATBUFFERS_FLATC_EXECUTABLE the flatc compiler executable
|
||||
# * FLATBUFFERS_FOUND
|
||||
#
|
||||
# Provides:
|
||||
# * FLATBUFFERS_GENERATE_C_HEADERS(Name <files>) creates the C++ headers
|
||||
# for the given flatbuffer schema files.
|
||||
# Returns the header files in ${Name}_OUTPUTS
|
||||
|
||||
find_program(FLATBUFFERS_FLATC_EXECUTABLE NAMES flatc)
|
||||
find_path(FLATBUFFERS_INCLUDE_DIR NAMES flatbuffers/flatbuffers.h)
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(flatbuffers
|
||||
DEFAULT_MSG FLATBUFFERS_FLATC_EXECUTABLE FLATBUFFERS_INCLUDE_DIR)
|
||||
|
||||
if(FLATBUFFERS_FOUND)
|
||||
function(FLATBUFFERS_GENERATE_C_HEADERS Name)
|
||||
set(FLATC_OUTPUTS)
|
||||
foreach(FILE ${ARGN})
|
||||
get_filename_component(FLATC_OUTPUT ${FILE} NAME_WE)
|
||||
set(FLATC_OUTPUT
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${FLATC_OUTPUT}_generated.h")
|
||||
list(APPEND FLATC_OUTPUTS ${FLATC_OUTPUT})
|
||||
|
||||
add_custom_command(OUTPUT ${FLATC_OUTPUT}
|
||||
COMMAND ${FLATBUFFERS_FLATC_EXECUTABLE}
|
||||
ARGS -c -o "${CMAKE_CURRENT_BINARY_DIR}/" ${FILE}
|
||||
COMMENT "Building C++ header for ${FILE}"
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
endforeach()
|
||||
set(${Name}_OUTPUTS ${FLATC_OUTPUTS} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
set(FLATBUFFERS_INCLUDE_DIRS ${FLATBUFFERS_INCLUDE_DIR})
|
||||
include_directories(${CMAKE_BINARY_DIR})
|
||||
else()
|
||||
set(FLATBUFFERS_INCLUDE_DIR)
|
||||
endif()
|
||||
@@ -12,6 +12,7 @@ set(FlatBuffers_Compiler_SRCS
|
||||
src/idl_parser.cpp
|
||||
src/idl_gen_cpp.cpp
|
||||
src/idl_gen_java.cpp
|
||||
src/idl_gen_go.cpp
|
||||
src/idl_gen_text.cpp
|
||||
src/flatc.cpp
|
||||
)
|
||||
@@ -50,17 +51,18 @@ 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")
|
||||
if(APPLE)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
|
||||
elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra")
|
||||
|
||||
if(FLATBUFFERS_CODE_COVERAGE)
|
||||
add_definitions("-g -fprofile-arcs -ftest-coverage")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
|
||||
set(CMAKE_EXE_LINKER_FLAGS
|
||||
"${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||
endif()
|
||||
|
||||
include_directories(include)
|
||||
@@ -70,6 +72,9 @@ add_executable(flattests ${FlatBuffers_Tests_SRCS})
|
||||
add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
|
||||
add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
|
||||
|
||||
install(DIRECTORY include/flatbuffers DESTINATION include)
|
||||
install(TARGETS flatc DESTINATION bin)
|
||||
|
||||
add_test(NAME flattest
|
||||
CONFIGURATIONS Debug
|
||||
WORKING_DIRECTORY tests
|
||||
|
||||
@@ -11,43 +11,43 @@ EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
MinSizeRel|Win32 = MinSizeRel|Win32
|
||||
Debug|x64 = Debug|x64
|
||||
Release|Win32 = Release|Win32
|
||||
RelWithDebInfo|Win32 = RelWithDebInfo|Win32
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.MinSizeRel|Win32.ActiveCfg = Release|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.MinSizeRel|Win32.Build.0 = Release|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Debug|x64.Build.0 = Debug|x64
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Release|Win32.Build.0 = Release|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.RelWithDebInfo|Win32.ActiveCfg = Release|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.RelWithDebInfo|Win32.Build.0 = Release|Win32
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Release|x64.ActiveCfg = Release|x64
|
||||
{5B5857E1-64E2-4CED-A12E-45E1B3880496}.Release|x64.Build.0 = Release|x64
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.MinSizeRel|Win32.ActiveCfg = Release|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.MinSizeRel|Win32.Build.0 = Release|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Debug|x64.Build.0 = Debug|x64
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Release|Win32.Build.0 = Release|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.RelWithDebInfo|Win32.ActiveCfg = Release|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.RelWithDebInfo|Win32.Build.0 = Release|Win32
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Release|x64.ActiveCfg = Release|x64
|
||||
{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}.Release|x64.Build.0 = Release|x64
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.MinSizeRel|Win32.ActiveCfg = Release|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.MinSizeRel|Win32.Build.0 = Release|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Debug|x64.Build.0 = Debug|x64
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Release|Win32.Build.0 = Release|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.RelWithDebInfo|Win32.ActiveCfg = Release|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.RelWithDebInfo|Win32.Build.0 = Release|Win32
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Release|x64.ActiveCfg = Release|x64
|
||||
{F0A15675-1017-4217-BB5B-3372F2C636AB}.Release|x64.Build.0 = Release|x64
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.MinSizeRel|Win32.ActiveCfg = Release|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.MinSizeRel|Win32.Build.0 = Release|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Debug|x64.Build.0 = Debug|x64
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Release|Win32.Build.0 = Release|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.RelWithDebInfo|Win32.ActiveCfg = Release|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.RelWithDebInfo|Win32.Build.0 = Release|Win32
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Release|x64.ActiveCfg = Release|x64
|
||||
{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -5,10 +5,18 @@
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGUID>{5B5857E1-64E2-4CED-A12E-45E1B3880496}</ProjectGUID>
|
||||
@@ -22,11 +30,21 @@
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
@@ -37,17 +55,29 @@
|
||||
<PropertyGroup>
|
||||
<_ProjectFileVersion>10.0.20506.1</_ProjectFileVersion>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flatc.dir\Debug\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flatc.dir\Debug\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flatc</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flatc</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</GenerateManifest>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flatc.dir\Release\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flatc.dir\Release\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flatc</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flatc</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</GenerateManifest>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
@@ -61,11 +91,12 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flatc.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -94,6 +125,52 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flatc.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalOptions>/debug %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Debug/flatc.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Debug/flatc.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
@@ -104,13 +181,14 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flatc.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -139,21 +217,64 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flatc.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Release/flatc.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Release/flatc.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\include\flatbuffers\flatbuffers.h" />
|
||||
<ClInclude Include="..\..\include\flatbuffers\idl.h" />
|
||||
<ClInclude Include="..\..\include\flatbuffers\util.h" />
|
||||
<ClCompile Include="..\..\src\idl_gen_go.cpp">
|
||||
<WarningLevel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Level4</WarningLevel>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\idl_parser.cpp" />
|
||||
<ClCompile Include="..\..\src\idl_gen_cpp.cpp" />
|
||||
<ClCompile Include="..\..\src\idl_gen_java.cpp" />
|
||||
<ClCompile Include="..\..\src\idl_gen_text.cpp" />
|
||||
<ClCompile Include="..\..\src\flatc.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../ZERO_CHECK.vcxproj">
|
||||
<Project>71030BD1-9039-4724-A6C4-F7CB2C700B47</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
||||
@@ -1,11 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<LocalDebuggerWorkingDirectory>..\..\tests</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
<LocalDebuggerCommandArguments>-j -c -g -b -t monster_test.fbs monsterdata_test.golden</LocalDebuggerCommandArguments>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
<LocalDebuggerCommandArguments>-j -c -g -b -t monster_test.fbs monsterdata_test.golden</LocalDebuggerCommandArguments>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LocalDebuggerCommandArguments>-j -c -g -b -t monster_test.fbs monsterdata_test.golden</LocalDebuggerCommandArguments>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..\tests</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LocalDebuggerCommandArguments>-j -c -g -b -t monster_test.fbs monsterdata_test.json</LocalDebuggerCommandArguments>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..\tests</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -5,10 +5,18 @@
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGUID>{16FA5518-3DE1-4B15-A1E0-F4734C276FB4}</ProjectGUID>
|
||||
@@ -22,11 +30,21 @@
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
@@ -37,17 +55,29 @@
|
||||
<PropertyGroup>
|
||||
<_ProjectFileVersion>10.0.20506.1</_ProjectFileVersion>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flatsamplebinary.dir\Debug\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flatsamplebinary.dir\Debug\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flatsamplebinary</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flatsamplebinary</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</GenerateManifest>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flatsamplebinary.dir\Release\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flatsamplebinary.dir\Release\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flatsamplebinary</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flatsamplebinary</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</GenerateManifest>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
@@ -61,11 +91,12 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flatsamplebinary.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -94,6 +125,52 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flatsamplebinary.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalOptions>/debug %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Debug/flatsamplebinary.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Debug/flatsamplebinary.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
@@ -104,13 +181,14 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flatsamplebinary.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -139,16 +217,56 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flatsamplebinary.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Release/flatsamplebinary.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Release/flatsamplebinary.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\include\flatbuffers\flatbuffers.h" />
|
||||
<ClInclude Include="..\..\samples\monster_generated.h" />
|
||||
<ClCompile Include="..\..\samples\sample_binary.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../ZERO_CHECK.vcxproj">
|
||||
<Project>71030BD1-9039-4724-A6C4-F7CB2C700B47</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -5,10 +5,18 @@
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGUID>{F0A15675-1017-4217-BB5B-3372F2C636AB}</ProjectGUID>
|
||||
@@ -22,11 +30,21 @@
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
@@ -37,17 +55,29 @@
|
||||
<PropertyGroup>
|
||||
<_ProjectFileVersion>10.0.20506.1</_ProjectFileVersion>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flatsampletext.dir\Debug\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flatsampletext.dir\Debug\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flatsampletext</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flatsampletext</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</GenerateManifest>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flatsampletext.dir\Release\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flatsampletext.dir\Release\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flatsampletext</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flatsampletext</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</GenerateManifest>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
@@ -61,11 +91,12 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flatsampletext.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -94,6 +125,52 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flatsampletext.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalOptions>/debug %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Debug/flatsampletext.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Debug/flatsampletext.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
@@ -104,13 +181,14 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flatsampletext.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -139,6 +217,51 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flatsampletext.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Release/flatsampletext.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Release/flatsampletext.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\include\flatbuffers\flatbuffers.h" />
|
||||
<ClInclude Include="..\..\include\flatbuffers\idl.h" />
|
||||
@@ -148,11 +271,6 @@
|
||||
<ClCompile Include="..\..\src\idl_gen_text.cpp" />
|
||||
<ClCompile Include="..\..\samples\sample_text.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../ZERO_CHECK.vcxproj">
|
||||
<Project>71030BD1-9039-4724-A6C4-F7CB2C700B47</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
||||
@@ -8,4 +8,12 @@
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -5,10 +5,18 @@
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGUID>{DC7BBA00-9FC6-48AF-B7E9-12CA91AC02AA}</ProjectGUID>
|
||||
@@ -22,11 +30,21 @@
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
@@ -37,17 +55,29 @@
|
||||
<PropertyGroup>
|
||||
<_ProjectFileVersion>10.0.20506.1</_ProjectFileVersion>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flattests.dir\Debug\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flattests.dir\Debug\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">flattests</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">flattests</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</GenerateManifest>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)</OutDir>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flattests.dir\Release\</IntDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flattests.dir\Release\</IntDir>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">flattests</TargetName>
|
||||
<TargetName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">flattests</TargetName>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.exe</TargetExt>
|
||||
<TargetExt Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.exe</TargetExt>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</GenerateManifest>
|
||||
<GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</GenerateManifest>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
@@ -61,11 +91,12 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flattests.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -94,6 +125,52 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Debug</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Debug/flattests.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalOptions>/debug %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Debug/flattests.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Debug/flattests.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
@@ -104,13 +181,14 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flattests.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
@@ -139,6 +217,51 @@
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<CompileAs>CompileAsCpp</CompileAs>
|
||||
<ExceptionHandling>Sync</ExceptionHandling>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<DebugInformationFormat>
|
||||
</DebugInformationFormat>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AssemblerListingLocation>Release</AssemblerListingLocation>
|
||||
<ObjectFileName>$(IntDir)</ObjectFileName>
|
||||
<ProgramDataBaseFileName>../../Release/flattests.pdb</ProgramDataBaseFileName>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
</ClCompile>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ResourceCompile>
|
||||
<Midl>
|
||||
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<OutputDirectory>$(IntDir)</OutputDirectory>
|
||||
<HeaderFileName>%(Filename).h</HeaderFileName>
|
||||
<TypeLibraryName>%(Filename).tlb</TypeLibraryName>
|
||||
<InterfaceIdentifierFileName>%(Filename)_i.c</InterfaceIdentifierFileName>
|
||||
<ProxyFileName>%(Filename)_p.c</ProxyFileName>
|
||||
</Midl>
|
||||
<Link>
|
||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<ImportLibrary>../../Release/flattests.lib</ImportLibrary>
|
||||
<ProgramDataBaseFile>../../Release/flattests.pdb</ProgramDataBaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<Version>
|
||||
</Version>
|
||||
</Link>
|
||||
<ProjectReference>
|
||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\include\flatbuffers\flatbuffers.h" />
|
||||
<ClInclude Include="..\..\include\flatbuffers\idl.h" />
|
||||
@@ -148,11 +271,6 @@
|
||||
<ClCompile Include="..\..\src\idl_gen_text.cpp" />
|
||||
<ClCompile Include="..\..\tests\test.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../ZERO_CHECK.vcxproj">
|
||||
<Project>71030BD1-9039-4724-A6C4-F7CB2C700B47</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
||||
@@ -8,4 +8,12 @@
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LocalDebuggerWorkingDirectory>..\..</LocalDebuggerWorkingDirectory>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -11,6 +11,7 @@
|
||||
3343DD4ED370434BBA148FAB /* idl_gen_java.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3803689175184C7E8CB3EED0 /* idl_gen_java.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
61823BBC53544106B6DBC38E /* idl_parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3709AC883348409592530AE6 /* idl_parser.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
61FF3C34FBEC4819A1C30F92 /* sample_text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECCEBFFA6977404F858F9739 /* sample_text.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
8C303C591975D6A700D7C1C5 /* idl_gen_go.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C303C581975D6A700D7C1C5 /* idl_gen_go.cpp */; };
|
||||
A9C9A99F719A4ED58DC2D2FC /* idl_parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3709AC883348409592530AE6 /* idl_parser.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
AA9BACF55EB3456BA2F633BB /* flatc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0DFD29781D8E490284B06504 /* flatc.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
AD71FEBEE4E846529002C1F0 /* idl_gen_text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6C5D81DBF864365B12E269D /* idl_gen_text.cpp */; settings = {COMPILER_FLAGS = ""; }; };
|
||||
@@ -32,6 +33,7 @@
|
||||
423CA92401AE442B91546E63 /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = CMakeLists.txt; path = /Users/wvo/flatbuffers_snapshot9/CMakeLists.txt; sourceTree = "<absolute>"; };
|
||||
5EE44BFFAF8E43F485859145 /* sample_binary.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = sample_binary.cpp; path = samples/sample_binary.cpp; sourceTree = SOURCE_ROOT; };
|
||||
6AD24EEB3D024825A37741FF /* test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = test.cpp; path = tests/test.cpp; sourceTree = SOURCE_ROOT; };
|
||||
8C303C581975D6A700D7C1C5 /* idl_gen_go.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = idl_gen_go.cpp; path = src/idl_gen_go.cpp; sourceTree = "<group>"; };
|
||||
A13F25CDAD23435DA293690D /* flattests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; path = flattests; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
AB70F1FBA50E4120BCF37C8D /* monster_test_generated.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = monster_test_generated.h; path = tests/monster_test_generated.h; sourceTree = SOURCE_ROOT; };
|
||||
AD3682C6E1DD4EABB822C0CC /* monster_generated.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = monster_generated.h; path = samples/monster_generated.h; sourceTree = SOURCE_ROOT; };
|
||||
@@ -53,6 +55,7 @@
|
||||
28237E300FE042DEADA302D3 /* Source Files */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
8C303C581975D6A700D7C1C5 /* idl_gen_go.cpp */,
|
||||
0DFD29781D8E490284B06504 /* flatc.cpp */,
|
||||
CD90A7F6B2BE4D0384294DD1 /* idl_gen_cpp.cpp */,
|
||||
3803689175184C7E8CB3EED0 /* idl_gen_java.cpp */,
|
||||
@@ -314,6 +317,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
8C303C591975D6A700D7C1C5 /* idl_gen_go.cpp in Sources */,
|
||||
AA9BACF55EB3456BA2F633BB /* flatc.cpp in Sources */,
|
||||
BE03D7B0C9584DD58B50ED34 /* idl_gen_cpp.cpp in Sources */,
|
||||
3343DD4ED370434BBA148FAB /* idl_gen_java.cpp in Sources */,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* The standard CSS for doxygen 1.8.5 */
|
||||
/* The standard CSS for doxygen 1.8.7 */
|
||||
|
||||
body, table, div, p, dl {
|
||||
font: 400 14px/22px Roboto,sans-serif;
|
||||
@@ -56,10 +56,14 @@ div.multicol {
|
||||
-webkit-column-count: 3;
|
||||
}
|
||||
|
||||
p.startli, p.startdd, p.starttd {
|
||||
p.startli, p.startdd {
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
p.starttd {
|
||||
margin-top: 0px;
|
||||
}
|
||||
|
||||
p.endli {
|
||||
margin-bottom: 0px;
|
||||
}
|
||||
@@ -169,8 +173,8 @@ pre.fragment {
|
||||
}
|
||||
|
||||
div.fragment {
|
||||
padding: 0px;
|
||||
margin: 0px;
|
||||
padding: 4px 6px;
|
||||
margin: 4px 8px 4px 2px;
|
||||
background-color: #FBFCFD;
|
||||
border: 1px solid #C4CFE5;
|
||||
}
|
||||
@@ -666,12 +670,12 @@ span.mlabel {
|
||||
|
||||
/* @end */
|
||||
|
||||
/* these are for tree view when not used as main index */
|
||||
/* these are for tree view inside a (index) page */
|
||||
|
||||
div.directory {
|
||||
margin: 10px 0px;
|
||||
border-top: 1px solid #A8B8D9;
|
||||
border-bottom: 1px solid #A8B8D9;
|
||||
border-top: 1px solid #9CAFD4;
|
||||
border-bottom: 1px solid #9CAFD4;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@@ -730,6 +734,80 @@ div.directory {
|
||||
color: #3D578C;
|
||||
}
|
||||
|
||||
.arrow {
|
||||
color: #9CAFD4;
|
||||
-webkit-user-select: none;
|
||||
-khtml-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
user-select: none;
|
||||
cursor: pointer;
|
||||
font-size: 80%;
|
||||
display: inline-block;
|
||||
width: 16px;
|
||||
height: 22px;
|
||||
}
|
||||
|
||||
.icon {
|
||||
font-family: Arial, Helvetica;
|
||||
font-weight: bold;
|
||||
font-size: 12px;
|
||||
height: 14px;
|
||||
width: 16px;
|
||||
display: inline-block;
|
||||
background-color: #728DC1;
|
||||
color: white;
|
||||
text-align: center;
|
||||
border-radius: 4px;
|
||||
margin-left: 2px;
|
||||
margin-right: 2px;
|
||||
}
|
||||
|
||||
.icona {
|
||||
width: 24px;
|
||||
height: 22px;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.iconfopen {
|
||||
width: 24px;
|
||||
height: 18px;
|
||||
margin-bottom: 4px;
|
||||
background-image:url('ftv2folderopen.png');
|
||||
background-position: 0px -4px;
|
||||
background-repeat: repeat-y;
|
||||
vertical-align:top;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.iconfclosed {
|
||||
width: 24px;
|
||||
height: 18px;
|
||||
margin-bottom: 4px;
|
||||
background-image:url('ftv2folderclosed.png');
|
||||
background-position: 0px -4px;
|
||||
background-repeat: repeat-y;
|
||||
vertical-align:top;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.icondoc {
|
||||
width: 24px;
|
||||
height: 18px;
|
||||
margin-bottom: 4px;
|
||||
background-image:url('ftv2doc.png');
|
||||
background-position: 0px -4px;
|
||||
background-repeat: repeat-y;
|
||||
vertical-align:top;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
table.directory {
|
||||
font: 400 14px Roboto,sans-serif;
|
||||
}
|
||||
|
||||
/* @end */
|
||||
|
||||
div.dynheader {
|
||||
margin-top: 8px;
|
||||
-webkit-touch-callout: none;
|
||||
@@ -1065,6 +1143,11 @@ dl.section dd {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.diagraph
|
||||
{
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.caption
|
||||
{
|
||||
font-weight: bold;
|
||||
|
||||
@@ -24,19 +24,20 @@ function updateStripes()
|
||||
$('table.directory tr').
|
||||
removeClass('even').filter(':visible:even').addClass('even');
|
||||
}
|
||||
|
||||
function toggleLevel(level)
|
||||
{
|
||||
$('table.directory tr').each(function(){
|
||||
$('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');
|
||||
i.removeClass('iconfopen iconfclosed').addClass('iconfopen');
|
||||
a.html('▼');
|
||||
$(this).show();
|
||||
} else if (l==level+1) {
|
||||
i.attr('src','ftv2folderclosed.png');
|
||||
a.attr('src','ftv2pnode.png');
|
||||
i.removeClass('iconfclosed iconfopen').addClass('iconfclosed');
|
||||
a.html('►');
|
||||
$(this).show();
|
||||
} else {
|
||||
$(this).hide();
|
||||
@@ -47,34 +48,33 @@ function toggleLevel(level)
|
||||
|
||||
function toggleFolder(id)
|
||||
{
|
||||
//The clicked row
|
||||
// the clicked row
|
||||
var currentRow = $('#row_'+id);
|
||||
var currentRowImages = currentRow.find("img");
|
||||
|
||||
//All rows after the clicked row
|
||||
// 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);
|
||||
});
|
||||
var re = new RegExp('^row_'+id+'\\d+_$', "i"); //only one sub
|
||||
|
||||
//First row is visible we are HIDING
|
||||
// only match elements AFTER this one (can't hide elements before)
|
||||
var childRows = rows.filter(function() { 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
|
||||
// replace down arrow by right arrow for current row
|
||||
var currentRowSpans = currentRow.find("span");
|
||||
currentRowSpans.filter(".iconfopen").removeClass("iconfopen").addClass("iconfclosed");
|
||||
currentRowSpans.filter(".arrow").html('►');
|
||||
rows.filter("[id^=row_"+id+"]").hide(); // hide all children
|
||||
} else { // we are SHOWING
|
||||
// replace right arrow by down arrow for current row
|
||||
var currentRowSpans = currentRow.find("span");
|
||||
currentRowSpans.filter(".iconfclosed").removeClass("iconfclosed").addClass("iconfopen");
|
||||
currentRowSpans.filter(".arrow").html('▼');
|
||||
// replace down arrows by right arrows for child rows
|
||||
var childRowsSpans = childRows.find("span");
|
||||
childRowsSpans.filter(".iconfopen").removeClass("iconfopen").addClass("iconfclosed");
|
||||
childRowsSpans.filter(".arrow").html('►');
|
||||
childRows.show(); //show all children
|
||||
}
|
||||
updateStripes();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Main Page</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -53,7 +53,7 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<div class="title">FlatBuffers Documentation</div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>FlatBuffers is an efficient cross platform serialization library in for C++ and Java. It was created at Google specifically for game development and other performance-critical applications.</p>
|
||||
<div class="textblock"><p>FlatBuffers is an efficient cross platform serialization library for C++, with support for Java and Go. It was created at Google specifically for game development and other performance-critical applications.</p>
|
||||
<p>It is available as open source under the Apache license, v2 (see LICENSE.txt).</p>
|
||||
<h2>Why use FlatBuffers?</h2>
|
||||
<ul>
|
||||
@@ -63,9 +63,9 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<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>
|
||||
<p class="startli">Java and Go 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>
|
||||
<li><b>Cross platform C++11/Java/Go code with no dependencies</b> - will work with any recent gcc/clang and VS2010. Comes with build files for the tests & samples (Android .mk files, and cmake for all other platforms).</li>
|
||||
</ul>
|
||||
<h3>Why not use Protocol Buffers, or .. ?</h3>
|
||||
<p>Protocol Buffers is indeed relatively similar to FlatBuffers, with the primary difference being that FlatBuffers does not need a parsing/ unpacking step to a secondary representation before you can access data, often coupled with per-object memory allocation. The code is an order of magnitude bigger, too. Protocol Buffers has neither optional text import/export nor schema language features like unions.</p>
|
||||
@@ -76,7 +76,7 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<p>This section is a quick rundown of how to use this system. Subsequent sections provide a more in-depth usage guide.</p>
|
||||
<ul>
|
||||
<li>Write a schema file that allows you to define the data structures you may want to serialize. Fields can have a scalar type (ints/floats of all sizes), or they can be a: string; array of any type; reference to yet another object; or, a set of possible objects (unions). Fields are optional and have defaults, so they don't need to be present for every object instance.</li>
|
||||
<li>Use <code>flatc</code> (the FlatBuffer compiler) to generate a C++ header (or Java classes) with helper classes to access and construct serialized data. This header (say <code>mydata_generated.h</code>) only depends on <code>flatbuffers.h</code>, which defines the core functionality.</li>
|
||||
<li>Use <code>flatc</code> (the FlatBuffer compiler) to generate a C++ header (or Java/Go classes) with helper classes to access and construct serialized data. This header (say <code>mydata_generated.h</code>) only depends on <code>flatbuffers.h</code>, which defines the core functionality.</li>
|
||||
<li>Use the <code>FlatBufferBuilder</code> class to construct a flat binary buffer. The generated functions allow you to add objects to this buffer recursively, often as simply as making a single function call.</li>
|
||||
<li>Store or send your buffer somewhere!</li>
|
||||
<li>When reading it back, you can obtain the pointer to the root object from the binary buffer, and from there traverse it conveniently in-place with <code>object->field()</code>.</li>
|
||||
@@ -88,6 +88,7 @@ $(document).ready(function(){initNavTree('index.html','');});
|
||||
<li>How to <a href="md__schemas.html">write a schema</a>.</li>
|
||||
<li>How to <a href="md__cpp_usage.html">use the generated C++ code</a> in your own programs.</li>
|
||||
<li>How to <a href="md__java_usage.html">use the generated Java code</a> in your own programs.</li>
|
||||
<li>How to <a href="md__go_usage.html">use the generated Go code</a> in your own programs.</li>
|
||||
<li>Some <a href="md__benchmarks.html">benchmarks</a> showing the advantage of using FlatBuffers.</li>
|
||||
<li>A <a href="md__white_paper.html">white paper</a> explaining the "why" of FlatBuffers.</li>
|
||||
<li>A description of the <a href="md__internals.html">internals</a> of FlatBuffers.</li>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Benchmarks</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -53,37 +53,36 @@ $(document).ready(function(){initNavTree('md__benchmarks.html','');});
|
||||
<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>
|
||||
<div class="textblock"><p>Comparing against other serialization solutions, running on Windows 7 64bit. We use the LITE runtime for Protocol Buffers (less code / lower overhead), Rapid JSON (one of the fastest C++ JSON parsers around), and pugixml, also one of the fastest XML parsers.</p>
|
||||
<p>We 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>
|
||||
<th></th><th>FlatBuffers (binary) </th><th>Protocol Buffers LITE </th><th>Rapid JSON </th><th>FlatBuffers (JSON) </th><th>pugixml </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>
|
||||
<td>Decode + Traverse + Dealloc (1 million times, seconds) </td><td>0.08 </td><td>302 </td><td>583 </td><td>105 </td><td>196 </td></tr>
|
||||
<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>
|
||||
<td>Decode / Traverse / Dealloc (breakdown) </td><td>0 / 0.08 / 0 </td><td>220 / 0.15 / 81 </td><td>294 / 0.9 / 287 </td><td>70 / 0.08 / 35 </td><td>41 / 3.9 / 150 </td></tr>
|
||||
<tr>
|
||||
<td>Encode (1 million times, seconds) </td><td>3.2 </td><td>185 </td><td>650 </td><td>169 </td></tr>
|
||||
<td>Encode (1 million times, seconds) </td><td>3.2 </td><td>185 </td><td>650 </td><td>169 </td><td>273 </td></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>
|
||||
<td>Wire format size (normal / zlib, bytes) </td><td>344 / 220 </td><td>228 / 174 </td><td>1475 / 322 </td><td>1029 / 298 </td><td>1137 / 341 </td></tr>
|
||||
<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>
|
||||
<td>Memory needed to store decoded wire (bytes / blocks) </td><td>0 / 0 </td><td>760 / 20 </td><td>65689 / 4 </td><td>328 / 1 </td><td>34194 / 3 </td></tr>
|
||||
<tr>
|
||||
<td>Transient memory allocated during decode (KB) </td><td>0 </td><td>1 </td><td>131 </td><td>4 </td></tr>
|
||||
<td>Transient memory allocated during decode (KB) </td><td>0 </td><td>1 </td><td>131 </td><td>4 </td><td>34 </td></tr>
|
||||
<tr>
|
||||
<td>Generated source code size (KB) </td><td>4 </td><td>61 </td><td>0 </td><td>4 </td></tr>
|
||||
<td>Generated source code size (KB) </td><td>4 </td><td>61 </td><td>0 </td><td>4 </td><td>0 </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>
|
||||
<td>Field access in handwritten traversal code </td><td>typed accessors </td><td>typed accessors </td><td>manual error checking </td><td>typed accessors </td><td>manual error checking </td></tr>
|
||||
<tr>
|
||||
<td>Library source code (KB) </td><td>15 </td><td>some subset of 3800 </td><td>87 </td><td>43 </td></tr>
|
||||
<td>Library source code (KB) </td><td>15 </td><td>some subset of 3800 </td><td>87 </td><td>43 </td><td>327 </td></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>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Building</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -54,12 +54,12 @@ $(document).ready(function(){initNavTree('md__building.html','');});
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>There are project files for Visual Studio and Xcode that should allow you to build the compiler <code>flatc</code>, the samples and the tests out of the box.</p>
|
||||
<p>Alternatively, the distribution comes with a <code>cmake</code> file that should allow you to build project/make files for any platform. 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"
|
||||
<p>Alternatively, the distribution comes with a <code>cmake</code> file that should allow you to build project/make files for any platform. 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>Optionally, run the <code>flattests</code> executable to ensure everything is working correctly on your system. If this fails, please contact us!</p>
|
||||
<p>Note that you MUST be in the root of the FlatBuffers distribution when you run 'flattests' (and the samples), or it will fail to load its files.</p>
|
||||
<p>Building should also produce 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>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Using the schema compiler</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -53,14 +53,18 @@ $(document).ready(function(){initNavTree('md__compiler.html','');});
|
||||
<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>
|
||||
<div class="textblock"><p>Usage: </p><pre class="fragment">flatc [ -c ] [ -j ] [ -b ] [ -t ] [ -o PATH ] [ -S ] FILES...
|
||||
[ -- FILES...]
|
||||
</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.</p>
|
||||
<p><code>--</code> indicates that the following files are binary files in FlatBuffer format conforming to the schema(s) indicated before it. Incompatible binary files currently will give unpredictable results (!)</p>
|
||||
<p>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>
|
||||
<li><code>-c</code> : Generate a C++ header for all definitions in this file (as <code>filename_generated.h</code>). Skipped for data.</li>
|
||||
<li><code>-j</code> : Generate Java classes. Skipped for data.</li>
|
||||
<li><code>-b</code> : If data is contained in this file, generate a <code>filename.bin</code> containing the binary flatbuffer.</li>
|
||||
<li><code>-t</code> : If data is contained in this file, generate a <code>filename.json</code> representing the data in the flatbuffer.</li>
|
||||
<li><code>-o PATH</code> : Output all generated files to PATH (either absolute, or relative to the current directory). If omitted, PATH will be the current directory. PATH should end in your systems path separator, e.g. <code>/</code> or <code>\</code>.</li>
|
||||
<li><code>-S</code> : Generate strict JSON (field names are enclosed in quotes). By default, no quotes are generated. </li>
|
||||
</ul>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Use in C++</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -55,49 +55,39 @@ $(document).ready(function(){initNavTree('md__cpp_usage.html','');});
|
||||
<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");
|
||||
<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);
|
||||
<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);
|
||||
<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, 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);
|
||||
<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. You can use <code>0</code> also as a default for other non-scalar types, such as strings, vectors and tables.</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);
|
||||
<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">FinishMonsterBuffer(fbb, 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);
|
||||
<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();
|
||||
<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();
|
||||
<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>
|
||||
@@ -105,22 +95,29 @@ assert(inv->Get(9) == 9);
|
||||
<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>
|
||||
<h3>Access of untrusted buffers</h3>
|
||||
<p>The generated accessor functions access fields over offsets, which is very quick. These offsets are not verified at run-time, so a malformed buffer could cause a program to crash by accessing random memory.</p>
|
||||
<p>When you're processing large amounts of data from a source you know (e.g. your own generated data on disk), this is acceptable, but when reading data from the network that can potentially have been modified by an attacker, this is undesirable.</p>
|
||||
<p>For this reason, you can optionally use a buffer verifier before you access the data. This verifier will check all offsets, all sizes of fields, and null termination of strings to ensure that when a buffer is accessed, all reads will end up inside the buffer.</p>
|
||||
<p>Each root type will have a verification function generated for it, e.g. for <code>Monster</code>, you can call:</p>
|
||||
<p>bool ok = VerifyMonsterBuffer(Verifier(buf, len));</p>
|
||||
<p>if <code>ok</code> is true, the buffer is safe to read.</p>
|
||||
<p>Besides untrusted data, this function may be useful to call in debug mode, as extra insurance against data being corrupted somewhere along the way.</p>
|
||||
<p>While verifying a buffer isn't "free", it is typically faster than a full traversal (since any scalar data is not actually touched), and since it may cause the buffer to be brought into cache before reading, the actual overhead may be even lower than expected.</p>
|
||||
<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>(see the schema documentation for some specifics on the JSON format accepted).</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
|
||||
<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());
|
||||
<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>
|
||||
|
||||
106
docs/html/md__go_usage.html
Normal file
106
docs/html/md__go_usage.html
Normal file
@@ -0,0 +1,106 @@
|
||||
<!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.7"/>
|
||||
<title>FlatBuffers: Use in Go</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.7 -->
|
||||
</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__go_usage.html','');});
|
||||
</script>
|
||||
<div id="doc-content">
|
||||
<div class="header">
|
||||
<div class="headertitle">
|
||||
<div class="title">Use in Go </div> </div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<div class="textblock"><p>There's experimental support for reading FlatBuffers in Go. Generate code for Go with the <code>-g</code> option to <code>flatc</code>.</p>
|
||||
<p>See <code>go_test.go</code> for an example. You import the generated code, read a FlatBuffer binary file into a <code>[]byte</code>, which you pass to the <code>GetRootAsMonster</code> function: </p><pre class="fragment">import (
|
||||
example "MyGame/Example"
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
|
||||
io/ioutil
|
||||
)
|
||||
|
||||
buf, err := ioutil.ReadFile("monster.dat")
|
||||
// handle err
|
||||
monster := example.GetRootAsMonster(buf, 0)
|
||||
</pre><p>Now you can access values like this: </p><pre class="fragment">hp := monster.Hp()
|
||||
pos := monster.Pos(nil)
|
||||
</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), you can replace nil with a pointer to 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>To access vectors 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 i := 0; i < monster.InventoryLength(); i++ {
|
||||
monster.Inventory(i) // do something here
|
||||
}
|
||||
</pre><p>You can also construct these buffers in Go using the functions found in the generated code, and the FlatBufferBuilder class: </p><pre class="fragment">builder := flatbuffers.NewBuilder(0)
|
||||
</pre><p>Create strings: </p><pre class="fragment">str := builder.CreateString("MyMonster")
|
||||
</pre><p>Create a table with a struct contained therein: </p><pre class="fragment">example.MonsterStart(builder)
|
||||
example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0, 3.0, 4, 5, 6))
|
||||
example.MonsterAddHp(builder, 80)
|
||||
example.MonsterAddName(builder, str)
|
||||
example.MonsterAddInventory(builder, inv)
|
||||
example.MonsterAddTest_Type(builder, 1)
|
||||
example.MonsterAddTest(builder, mon2)
|
||||
example.MonsterAddTest4(builder, test4s)
|
||||
mon := example.MonsterEnd(builder)
|
||||
</pre><p>Unlike C++, Go does not support table creation functions like 'createMonster()'. This is to create the buffer without using temporary object allocation (since the <code>Vec3</code> is an inline component of <code>Monster</code>, it has to be created right where it is added, whereas the name and the inventory are not inline). Structs do have convenient methods that allow you to construct them in one call. These also have arguments for nested structs, e.g. if a struct has a field <code>a</code> and a nested struct field <code>b</code> (which has fields <code>c</code> and <code>d</code>), then the arguments will be <code>a</code>, <code>c</code> and <code>d</code>.</p>
|
||||
<p>Vectors also use this start/end pattern to allow vectors of both scalar types and structs: </p><pre class="fragment">example.MonsterStartInventoryVector(builder, 5)
|
||||
for i := 4; i >= 0; i-- {
|
||||
builder.PrependByte(byte(i))
|
||||
}
|
||||
inv := builder.EndVector(5)
|
||||
</pre><p>The generated method 'StartInventoryVector' is provided as a convenience function which calls 'StartVector' with the correct element size of the vector type which in this case is 'ubyte' or 1 byte per vector element. You pass the number of elements you want to write. You write the elements backwards since the buffer is being constructed back to front.</p>
|
||||
<p>There are <code>Prepend</code> functions for all the scalar types. You use <code>PrependUOffset</code> for any previously constructed objects (such as other tables, strings, vectors). For structs, you use the appropriate <code>create</code> function in-line, as shown above in the <code>Monster</code> example.</p>
|
||||
<h2>Text Parsing</h2>
|
||||
<p>There currently is no support for parsing text (Schema's and JSON) directly from Go, though you could use the C++ parser through cgo. Please see the C++ documentation for more on text parsing. </p>
|
||||
</div></div><!-- contents -->
|
||||
</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>
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<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>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: FlatBuffer Internals</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -56,35 +56,33 @@ $(document).ready(function(){initNavTree('md__internals.html','');});
|
||||
<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>
|
||||
<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. This may mean two different implementations may produce different binaries given the same input values, and this is perfectly valid.</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>The most important and generic offset type (see <code>flatbuffers.h</code>) is <code>uoffset_t</code>, which is currently always a <code>uint32_t</code>, and is used to refer to all tables/unions/strings/vectors (these are never stored in-line). 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>uoffset_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>These start with an <code>soffset_t</code> to a vtable (signed version of <code>uoffset_t</code>, since vtables may be stored anywhere), followed by all the fields as aligned scalars (or offsets). Unlike structs, not all fields need to be present. There is no set order and layout.</p>
|
||||
<p>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>The elements of a vtable are all of type <code>voffset_t</code>, which is a <code>uint16_t</code>. The first element is the number of elements of the vtable, including this one. The second one is the size of the object, in bytes (including the vtable offset). This size is used for streaming, to know how many bytes to read to be able to access all fields of the object. The remaining elements are the N offsets, where N is the amount of fields declared in the schema when the code that constructed this buffer was compiled (thus, the size of the table is N + 2).</p>
|
||||
<p>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>
|
||||
<p>Strings are simply a vector of bytes, and are always null-terminated. Vectors are stored as contiguous aligned scalar elements prefixed by a 32bit element count (not including any null termination).</p>
|
||||
<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>
|
||||
<p>The current implementation constructs these buffers backwards (starting at the highest memory address of the buffer), 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
|
||||
<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 {
|
||||
</pre><p>Nested namespace support. </p><pre class="fragment">enum {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2,
|
||||
@@ -96,8 +94,7 @@ inline const char **EnumNamesColor() {
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
</pre><p>Enums and convenient reverse lookup. </p>
|
||||
<pre class="fragment">enum {
|
||||
</pre><p>Enums and convenient reverse lookup. </p><pre class="fragment">enum {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1,
|
||||
};
|
||||
@@ -108,11 +105,9 @@ inline const char **EnumNamesAny() {
|
||||
}
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
</pre><p>Unions share a lot with enums. </p>
|
||||
<pre class="fragment">struct Vec3;
|
||||
</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 {
|
||||
</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_;
|
||||
@@ -127,8 +122,7 @@ struct Monster;
|
||||
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 {
|
||||
</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); }
|
||||
@@ -136,8 +130,7 @@ STRUCT_END(Vec3, 12);
|
||||
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 {
|
||||
</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); }
|
||||
@@ -149,8 +142,7 @@ STRUCT_END(Vec3, 12);
|
||||
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) {
|
||||
</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);
|
||||
@@ -160,10 +152,8 @@ STRUCT_END(Vec3, 12);
|
||||
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
|
||||
</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 -->
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Use in Java</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -53,24 +53,19 @@ $(document).ready(function(){initNavTree('md__java_usage.html','');});
|
||||
<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);
|
||||
<div class="textblock"><p>FlatBuffers supports reading and writing binary FlatBuffers in Java. Generate code for Java with the <code>-j</code> option to <code>flatc</code>.</p>
|
||||
<p>See <code>javaTest.java</code> for an example. Essentially, you read a FlatBuffer binary file into a <code>byte[]</code>, which you then turn into a <code>ByteBuffer</code>, which you pass to the <code>getRootAsMyRootType</code> function: </p><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();
|
||||
</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>Java does not support unsigned scalars. This means that any unsigned types you use in your schema will actually be represented as a signed value. This means all bits are still present, but may represent a negative value when used. For example, to read a <code>byte b</code> as an unsigned number, you can do: <code>(short)(b & 0xFF)</code></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++)
|
||||
<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.inventoryLength(); 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);
|
||||
</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);
|
||||
@@ -79,12 +74,15 @@ 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);
|
||||
</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.</p>
|
||||
<p>It's important to understand that fields that are structs are inline (like <code>Vec3</code> above), and MUST thus be created between the start and end calls of a table. Everything else (other tables, strings, vectors) MUST be created before the start of the table they are referenced in.</p>
|
||||
<p>Structs do have convenient methods that even have arguments for nested structs.</p>
|
||||
<p>As you can see, references to other objects (e.g. the string above) are simple ints, and thus do not have the type-safety of the Offset type in C++. Extra case must thus be taken that you set the right offset on the right field.</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>
|
||||
<p>There are <code>add</code> functions for all the scalar types. You use <code>addOffset</code> for any previously constructed objects (such as other tables, strings, vectors). For structs, you use the appropriate <code>create</code> function in-line, as shown above in the <code>Monster</code> example.</p>
|
||||
<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 -->
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Writing a schema</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -53,8 +53,7 @@ $(document).ready(function(){initNavTree('md__schemas.html','');});
|
||||
<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
|
||||
<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;
|
||||
|
||||
@@ -85,7 +84,7 @@ root_type Monster;
|
||||
<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 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. If you want to have flexibility to use any order for fields in your schema, you can manually assign ids (much like Protocol Buffers), see the <code>id</code> attribute below.</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>
|
||||
@@ -98,7 +97,7 @@ root_type Monster;
|
||||
<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>Vector of any other type (denoted with <code>[type]</code>). Nesting vectors is not supported, instead you can wrap the inner vector in a table.</li>
|
||||
<li><code>string</code>, which may only hold UTF-8 or 7-bit ASCII. For other text encodings or general binary data use vectors (<code>[byte]</code> or <code>[ubyte]</code>) instead.</li>
|
||||
<li>References to other tables or structs, enums or unions (see below).</li>
|
||||
</ul>
|
||||
@@ -107,28 +106,45 @@ root_type Monster;
|
||||
<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>
|
||||
<p>Define a sequence of named constants, each with a given value, or increasing by one from the previous one. The default first value is <code>0</code>. As you can see in the enum declaration, you specify the underlying integral type of the enum with <code>:</code> (in this case <code>byte</code>), which then determines the type of any fields declared with this enum type.</p>
|
||||
<h3>Unions</h3>
|
||||
<p>Unions share a lot of properties with enums, but instead of new names for constants, you use names of tables. You can then declare a union field which can hold a reference to any of those types, and additionally a hidden field with the suffix <code>_type</code> is generated that holds the corresponding enum value, allowing you to know which type to cast to at runtime.</p>
|
||||
<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>This declares what you consider to be the root table (or struct) of the serialized data. This is particular important for parsing JSON data, which doesn't include object type information.</p>
|
||||
<h3>File identification and extension</h3>
|
||||
<p>Typically, a FlatBuffer binary buffer is not self-describing, i.e. it needs you to know its schema to parse it correctly. But if you want to use a FlatBuffer as a file format, it would be convenient to be able to have a "magic number" in there, like most file formats have, to be able to do a sanity check to see if you're reading the kind of file you're expecting.</p>
|
||||
<p>Now, you can always prefix a FlatBuffer with your own file header, but FlatBuffers has a built-in way to add an identifier to a FlatBuffer that takes up minimal space, and keeps the buffer compatible with buffers that don't have such an identifier.</p>
|
||||
<p>You can specify in a schema, similar to <code>root_type</code>, that you intend for this type of FlatBuffer to be used as a file format: </p><pre class="fragment">file_identifier "MYFI";
|
||||
</pre><p>Identifiers must always be exactly 4 characters long. These 4 characters will end up as bytes at offsets 4-7 (inclusive) in the buffer.</p>
|
||||
<p>For any schema that has such an identifier, <code>flatc</code> will automatically add the identifier to any binaries it generates (with <code>-b</code>), and generated calls like <code>FinishMonsterBuffer</code> also add the identifier. If you have specified an identifier and wish to generate a buffer without one, you can always still do so by calling <code>FlatBufferBuilder::Finish</code> explicitly.</p>
|
||||
<p>After loading a buffer, you can use a call like <code>MonsterBufferHasIdentifier</code> to check if the identifier is present.</p>
|
||||
<p>Additionally, by default <code>flatc</code> will output binary files as <code>.bin</code>. This declaration in the schema will change that to whatever you want: </p><pre class="fragment">file_extension "ext";
|
||||
</pre><h3>Comments & documentation</h3>
|
||||
<p>May be written as in most C-based languages. Additionally, a triple comment (<code>///</code>) on a line by itself signals that a comment is documentation for whatever is declared on the line after it (table/struct/field/enum/union/element), and the comment is output in the corresponding C++ code. Multiple such lines per item are allowed.</p>
|
||||
<h3>Attributes</h3>
|
||||
<p>Attributes may be attached to a declaration, behind a field, or after the name of a table/struct/enum/union. These may either have a value or not. Some attributes like <code>deprecated</code> are understood by the compiler, others are simply ignored (like <code>priority</code>), but are available to query if you parse the schema at runtime. This is useful if you write your own code generators/editors etc., and you wish to add additional information specific to your tool (such as a help text).</p>
|
||||
<p>Current understood attributes:</p>
|
||||
<ul>
|
||||
<li><code>id: n</code> (on a table field): manually set the field identifier to <code>n</code>. If you use this attribute, you must use it on ALL fields of this table, and the numbers must be a contiguous range from 0 onwards. Additionally, since a union type effectively adds two fields, its id must be that of the second field (the first field is the type field and not explicitly declared in the schema). For example, if the last field before the union field had id 6, the union field should have id 8, and the unions type field will implicitly be 7. IDs allow the fields to be placed in any order in the schema. When a new field is added to the schema is must use the next available ID.</li>
|
||||
<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>
|
||||
<li><code>bit_flags</code> (on an enum): the values of this field indicate bits, meaning that any value N specified in the schema will end up representing 1<<N, or if you don't specify values at all, you'll get the sequence 1, 2, 4, 8, ...</li>
|
||||
</ul>
|
||||
<h2>JSON Parsing</h2>
|
||||
<p>The same parser that parses the schema declarations above is also able to parse JSON objects that conform to this schema. So, unlike other JSON parsers, this parser is strongly typed, and parses directly into a FlatBuffer (see the compiler documentation on how to do this from the command line, or the C++ documentation on how to do this at runtime).</p>
|
||||
<p>Besides needing a schema, there are a few other changes to how it parses JSON:</p>
|
||||
<ul>
|
||||
<li>It accepts field names with and without quotes, like many JSON parsers already do. It outputs them without quotes as well, though can be made to output them using the <code>strict_json</code> flag.</li>
|
||||
<li>If a field has an enum type, the parser will recognize symbolic enum values (with or without quotes) instead of numbers, e.g. <code>field: EnumVal</code>. If a field is of integral type, you can still use symbolic names, but values need to be prefixed with their type and need to be quoted, e.g. <code>field: "Enum.EnumVal"</code>. For enums representing flags, you may place multiple inside a string separated by spaces to OR them, e.g. <code>field: "EnumVal1 EnumVal2"</code> or <code>field: "Enum.EnumVal1 Enum.EnumVal2"</code>.</li>
|
||||
</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>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 (and which is still an option using the <code>id</code> attribute mentioned above).</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>
|
||||
<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. If this is not practical for you, use explicit field ids, which should always generate a merge conflict if two people try to allocate the same id. </p>
|
||||
</div></div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- Google Analytics -->
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: FlatBuffers white paper</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
|
||||
@@ -5,6 +5,7 @@ var NAVTREE =
|
||||
[ "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 Go", "md__go_usage.html", null ],
|
||||
[ "Use in Java", "md__java_usage.html", null ],
|
||||
[ "Benchmarks", "md__benchmarks.html", null ],
|
||||
[ "FlatBuffers white paper", "md__white_paper.html", null ],
|
||||
@@ -18,8 +19,6 @@ 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();
|
||||
@@ -44,6 +43,21 @@ function stripPath2(uri)
|
||||
return m ? uri.substring(i-6) : s;
|
||||
}
|
||||
|
||||
function hashValue()
|
||||
{
|
||||
return $(location).attr('hash').substring(1).replace(/[^\w\-]/g,'');
|
||||
}
|
||||
|
||||
function hashUrl()
|
||||
{
|
||||
return '#'+hashValue();
|
||||
}
|
||||
|
||||
function pathName()
|
||||
{
|
||||
return $(location).attr('pathname').replace(/[^-A-Za-z0-9+&@#/%?=~_|!:,.;\(\)]/g, '');
|
||||
}
|
||||
|
||||
function localStorageSupported()
|
||||
{
|
||||
try {
|
||||
@@ -66,7 +80,7 @@ function deleteLink()
|
||||
{
|
||||
if (localStorageSupported()) {
|
||||
window.localStorage.setItem('navpath','');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cachedLink()
|
||||
@@ -128,7 +142,7 @@ function createIndent(o,domNode,node,level)
|
||||
span.style.display = 'inline-block';
|
||||
span.style.width = 16*(level+1)+'px';
|
||||
span.style.height = '22px';
|
||||
span.innerHTML = ' ';
|
||||
span.innerHTML = ' ';
|
||||
domNode.appendChild(span);
|
||||
}
|
||||
}
|
||||
@@ -138,11 +152,13 @@ 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'))
|
||||
var ancParent = $(anchor.parent());
|
||||
if (ancParent.hasClass('memItemLeft') ||
|
||||
ancParent.hasClass('fieldname') ||
|
||||
ancParent.hasClass('fieldtype') ||
|
||||
ancParent.is(':header'))
|
||||
{
|
||||
pos = anchor.parent().position().top;
|
||||
pos = ancParent.position().top;
|
||||
} else if (anchor.position()) {
|
||||
pos = anchor.position().top;
|
||||
}
|
||||
@@ -200,7 +216,7 @@ function newNode(o, po, text, link, childrenData, lastNode)
|
||||
a.className = stripPath(link.replace('#',':'));
|
||||
if (link.indexOf('#')!=-1) {
|
||||
var aname = '#'+link.split('#')[1];
|
||||
var srcPage = stripPath($(location).attr('pathname'));
|
||||
var srcPage = stripPath(pathName());
|
||||
var targetPage = stripPath(link.split('#')[0]);
|
||||
a.href = srcPage!=targetPage ? url : "javascript:void(0)";
|
||||
a.onclick = function(){
|
||||
@@ -294,14 +310,13 @@ function glowEffect(n,duration)
|
||||
|
||||
function highlightAnchor()
|
||||
{
|
||||
var aname = $(location).attr('hash');
|
||||
var aname = hashUrl();
|
||||
var anchor = $(aname);
|
||||
if (anchor.parent().attr('class')=='memItemLeft'){
|
||||
var rows = $('.memberdecls tr[class$="'+
|
||||
window.location.hash.substring(1)+'"]');
|
||||
var rows = $('.memberdecls tr[class$="'+hashValue()+'"]');
|
||||
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')=='fieldname'){
|
||||
glowEffect(anchor.parent().parent(),1000); // enum value
|
||||
} else if (anchor.parent().attr('class')=='fieldtype'){
|
||||
glowEffect(anchor.parent().parent(),1000); // struct field
|
||||
} else if (anchor.parent().is(":header")) {
|
||||
@@ -316,7 +331,7 @@ function selectAndHighlight(hash,n)
|
||||
{
|
||||
var a;
|
||||
if (hash) {
|
||||
var link=stripPath($(location).attr('pathname'))+':'+hash.substring(1);
|
||||
var link=stripPath(pathName())+':'+hash.substring(1);
|
||||
a=$('.item a[class$="'+link+'"]');
|
||||
}
|
||||
if (a && a.length) {
|
||||
@@ -427,14 +442,13 @@ function navTo(o,root,hash,relpath)
|
||||
if (link) {
|
||||
var parts = link.split('#');
|
||||
root = parts[0];
|
||||
if (parts.length>1) hash = '#'+parts[1];
|
||||
if (parts.length>1) hash = '#'+parts[1].replace(/[^\w\-]/g,'');
|
||||
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;
|
||||
@@ -468,7 +482,7 @@ function toggleSyncButton(relpath)
|
||||
if (navSync.hasClass('sync')) {
|
||||
navSync.removeClass('sync');
|
||||
showSyncOff(navSync,relpath);
|
||||
storeLink(stripPath2($(location).attr('pathname'))+$(location).attr('hash'));
|
||||
storeLink(stripPath2(pathName())+hashUrl());
|
||||
} else {
|
||||
navSync.addClass('sync');
|
||||
showSyncOn(navSync,relpath);
|
||||
@@ -508,7 +522,7 @@ function initNavTree(toroot,relpath)
|
||||
}
|
||||
|
||||
$(window).load(function(){
|
||||
navTo(o,toroot,window.location.hash,relpath);
|
||||
navTo(o,toroot,hashUrl(),relpath);
|
||||
showRoot();
|
||||
});
|
||||
|
||||
@@ -516,21 +530,20 @@ function initNavTree(toroot,relpath)
|
||||
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+'"]');
|
||||
var clslink=stripPath(pathName())+':'+hashValue();
|
||||
a=$('.item a[class$="'+clslink.replace(/</g,'\\3c ')+'"]');
|
||||
}
|
||||
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);
|
||||
var link=stripPath2(pathName());
|
||||
navTo(o,link,hashUrl(),relpath);
|
||||
} else if (!animationInProgress) {
|
||||
$('#doc-content').scrollTop(0);
|
||||
$('.item').removeClass('selected');
|
||||
$('.item').removeAttr('id');
|
||||
navTo(o,toroot,window.location.hash,relpath);
|
||||
navTo(o,toroot,hashUrl(),relpath);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
var NAVTREEINDEX0 =
|
||||
{
|
||||
"index.html":[],
|
||||
"md__benchmarks.html":[5],
|
||||
"md__benchmarks.html":[6],
|
||||
"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__go_usage.html":[4],
|
||||
"md__grammar.html":[9],
|
||||
"md__internals.html":[8],
|
||||
"md__java_usage.html":[5],
|
||||
"md__schemas.html":[2],
|
||||
"md__white_paper.html":[6],
|
||||
"md__white_paper.html":[7],
|
||||
"pages.html":[]
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<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"/>
|
||||
<meta name="generator" content="Doxygen 1.8.7"/>
|
||||
<title>FlatBuffers: Related Pages</title>
|
||||
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="jquery.js"></script>
|
||||
@@ -32,7 +32,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.8.5 -->
|
||||
<!-- Generated by Doxygen 1.8.7 -->
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
<div id="nav-tree">
|
||||
@@ -55,15 +55,16 @@ $(document).ready(function(){initNavTree('pages.html','');});
|
||||
<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>
|
||||
<tr id="row_0_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__building.html" target="_self">Building</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_1_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><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"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__schemas.html" target="_self">Writing a schema</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_3_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__cpp_usage.html" target="_self">Use in C++</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_4_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__go_usage.html" target="_self">Use in Go</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_5_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__java_usage.html" target="_self">Use in Java</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_6_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__benchmarks.html" target="_self">Benchmarks</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_7_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__white_paper.html" target="_self">FlatBuffers white paper</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_8_" class="even"><td class="entry"><span style="width:16px;display:inline-block;"> </span><a class="el" href="md__internals.html" target="_self">FlatBuffer Internals</a></td><td class="desc"></td></tr>
|
||||
<tr id="row_9_"><td class="entry"><span style="width:16px;display:inline-block;"> </span><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 -->
|
||||
|
||||
@@ -77,15 +77,19 @@ function initResizable()
|
||||
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;
|
||||
var device = navigator.userAgent.toLowerCase();
|
||||
var ios = device.match(/(iphone|ipod|ipad)/);
|
||||
if (ios) {
|
||||
try {
|
||||
var target = e.target;
|
||||
while (target) {
|
||||
if ($(target).css('-webkit-overflow-scrolling')=='touch') return;
|
||||
target = target.parentNode;
|
||||
}
|
||||
e.preventDefault();
|
||||
} catch(err) {
|
||||
e.preventDefault();
|
||||
}
|
||||
e.preventDefault();
|
||||
} catch(err) {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
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.
|
||||
overhead), Rapid JSON (one of the fastest C++ JSON parsers around),
|
||||
and pugixml, also one of the fastest XML parsers.
|
||||
|
||||
We compare against Flatbuffers with the binary wire format (as
|
||||
intended), and also with JSON as the wire format with the optional JSON
|
||||
@@ -13,17 +14,17 @@ The benchmark object is a set of about 10 objects containing an array, 4
|
||||
strings, and a large variety of int/float scalar values of all sizes,
|
||||
meant to be representative of game data, e.g. a scene format.
|
||||
|
||||
| | FlatBuffers (binary) | Protocol Buffers LITE | Rapid JSON | FlatBuffers (JSON) |
|
||||
|--------------------------------------------------------|-----------------------|-----------------------|-----------------------|-----------------------|
|
||||
| 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 |
|
||||
| | FlatBuffers (binary) | Protocol Buffers LITE | Rapid JSON | FlatBuffers (JSON) | pugixml |
|
||||
|--------------------------------------------------------|-----------------------|-----------------------|-----------------------|-----------------------| ----------------------|
|
||||
| Decode + Traverse + Dealloc (1 million times, seconds) | 0.08 | 302 | 583 | 105 | 196 |
|
||||
| Decode / Traverse / Dealloc (breakdown) | 0 / 0.08 / 0 | 220 / 0.15 / 81 | 294 / 0.9 / 287 | 70 / 0.08 / 35 | 41 / 3.9 / 150 |
|
||||
| Encode (1 million times, seconds) | 3.2 | 185 | 650 | 169 | 273 |
|
||||
| Wire format size (normal / zlib, bytes) | 344 / 220 | 228 / 174 | 1475 / 322 | 1029 / 298 | 1137 / 341 |
|
||||
| Memory needed to store decoded wire (bytes / blocks) | 0 / 0 | 760 / 20 | 65689 / 4 | 328 / 1 | 34194 / 3 |
|
||||
| Transient memory allocated during decode (KB) | 0 | 1 | 131 | 4 | 34 |
|
||||
| Generated source code size (KB) | 4 | 61 | 0 | 4 | 0 |
|
||||
| Field access in handwritten traversal code | typed accessors | typed accessors | manual error checking | typed accessors | manual error checking |
|
||||
| Library source code (KB) | 15 | some subset of 3800 | 87 | 43 | 327 |
|
||||
|
||||
### Some other serialization systems we compared against but did not benchmark (yet), in rough order of applicability:
|
||||
|
||||
@@ -36,8 +37,6 @@ meant to be representative of game data, e.g. a scene format.
|
||||
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
|
||||
|
||||
@@ -18,9 +18,11 @@ 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!
|
||||
Optionally, run the `flattests` executable to ensure everything is working
|
||||
correctly on your system. If this fails, please contact us!
|
||||
|
||||
Note that you MUST be in the root of the FlatBuffers distribution when you
|
||||
run 'flattests' (and the samples), or it will fail to load its files.
|
||||
|
||||
Building should also produce two sample executables, `sample_binary` and
|
||||
`sample_text`, see the corresponding `.cpp` file in the samples directory.
|
||||
|
||||
@@ -2,21 +2,35 @@
|
||||
|
||||
Usage:
|
||||
|
||||
flatc [ -c ] [ -j ] [ -b ] [ -t ] file1 file2 ..
|
||||
flatc [ -c ] [ -j ] [ -b ] [ -t ] [ -o PATH ] [ -S ] FILES...
|
||||
[ -- FILES...]
|
||||
|
||||
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
|
||||
files.
|
||||
|
||||
`--` indicates that the following files are binary files in
|
||||
FlatBuffer format conforming to the schema(s) indicated before it.
|
||||
Incompatible binary files currently will give unpredictable results (!)
|
||||
|
||||
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.
|
||||
`filename_generated.h`). Skipped for data.
|
||||
|
||||
- `-j` : Generate Java classes.
|
||||
- `-j` : Generate Java classes. Skipped for data.
|
||||
|
||||
- `-b` : If data is contained in this file, generate a
|
||||
`filename_wire.bin` containing the binary flatbuffer.
|
||||
`filename.bin` containing the binary flatbuffer.
|
||||
|
||||
- `-t` : If data is contained in this file, generate a
|
||||
`filename_wire.txt` (for debugging).
|
||||
`filename.json` representing the data in the flatbuffer.
|
||||
|
||||
- `-o PATH` : Output all generated files to PATH (either absolute, or
|
||||
relative to the current directory). If omitted, PATH will be the
|
||||
current directory. PATH should end in your systems path separator,
|
||||
e.g. `/` or `\`.
|
||||
|
||||
- `-S` : Generate strict JSON (field names are enclosed in quotes).
|
||||
By default, no quotes are generated.
|
||||
|
||||
@@ -47,7 +47,7 @@ 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);
|
||||
auto mloc = CreateMonster(fbb, &vec, 150, 80, name, inventory, Color_Red, 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,
|
||||
@@ -58,7 +58,8 @@ 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.
|
||||
want to write this field. You can use `0` also as a default for other
|
||||
non-scalar types, such as strings, vectors and tables.
|
||||
|
||||
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`).
|
||||
@@ -87,7 +88,7 @@ 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);
|
||||
FinishMonsterBuffer(fbb, 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
|
||||
@@ -155,6 +156,38 @@ 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).
|
||||
|
||||
### Access of untrusted buffers
|
||||
|
||||
The generated accessor functions access fields over offsets, which is
|
||||
very quick. These offsets are not verified at run-time, so a malformed
|
||||
buffer could cause a program to crash by accessing random memory.
|
||||
|
||||
When you're processing large amounts of data from a source you know (e.g.
|
||||
your own generated data on disk), this is acceptable, but when reading
|
||||
data from the network that can potentially have been modified by an
|
||||
attacker, this is undesirable.
|
||||
|
||||
For this reason, you can optionally use a buffer verifier before you
|
||||
access the data. This verifier will check all offsets, all sizes of
|
||||
fields, and null termination of strings to ensure that when a buffer
|
||||
is accessed, all reads will end up inside the buffer.
|
||||
|
||||
Each root type will have a verification function generated for it,
|
||||
e.g. for `Monster`, you can call:
|
||||
|
||||
bool ok = VerifyMonsterBuffer(Verifier(buf, len));
|
||||
|
||||
if `ok` is true, the buffer is safe to read.
|
||||
|
||||
Besides untrusted data, this function may be useful to call in debug
|
||||
mode, as extra insurance against data being corrupted somewhere along
|
||||
the way.
|
||||
|
||||
While verifying a buffer isn't "free", it is typically faster than
|
||||
a full traversal (since any scalar data is not actually touched),
|
||||
and since it may cause the buffer to be brought into cache before
|
||||
reading, the actual overhead may be even lower than expected.
|
||||
|
||||
## Text & schema parsing
|
||||
|
||||
Using binary buffers with the generated header provides a super low
|
||||
@@ -166,6 +199,9 @@ 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.
|
||||
|
||||
(see the schema documentation for some specifics on the JSON format
|
||||
accepted).
|
||||
|
||||
There are two ways to use text formats:
|
||||
|
||||
### Using the compiler as a conversion tool
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# 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.
|
||||
FlatBuffers is an efficient cross platform serialization library for C++,
|
||||
with support for Java and Go. It was created at Google specifically for game
|
||||
development and other performance-critical applications.
|
||||
|
||||
It is available as open source under the Apache license, v2 (see LICENSE.txt).
|
||||
|
||||
@@ -46,9 +46,9 @@ It is available as open source under the Apache license, v2 (see LICENSE.txt).
|
||||
needed (faster and more memory efficient than other JSON
|
||||
parsers).
|
||||
|
||||
Java code supports object-reuse.
|
||||
Java and Go code supports object-reuse.
|
||||
|
||||
- **Cross platform C++11/Java code with no dependencies** - will work with
|
||||
- **Cross platform C++11/Java/Go code with no dependencies** - will work with
|
||||
any recent gcc/clang and VS2010. Comes with build files for the tests &
|
||||
samples (Android .mk files, and cmake for all other platforms).
|
||||
|
||||
@@ -87,7 +87,7 @@ sections provide a more in-depth usage guide.
|
||||
Fields are optional and have defaults, so they don't need to be
|
||||
present for every object instance.
|
||||
|
||||
- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or Java
|
||||
- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or Java/Go
|
||||
classes) with helper classes to access and construct serialized data. This
|
||||
header (say `mydata_generated.h`) only depends on `flatbuffers.h`, which
|
||||
defines the core functionality.
|
||||
@@ -112,6 +112,8 @@ sections provide a more in-depth usage guide.
|
||||
programs.
|
||||
- How to [use the generated Java code](md__java_usage.html) in your own
|
||||
programs.
|
||||
- How to [use the generated Go code](md__go_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.
|
||||
|
||||
97
docs/source/GoUsage.md
Executable file
97
docs/source/GoUsage.md
Executable file
@@ -0,0 +1,97 @@
|
||||
# Use in Go
|
||||
|
||||
There's experimental support for reading FlatBuffers in Go. Generate code
|
||||
for Go with the `-g` option to `flatc`.
|
||||
|
||||
See `go_test.go` for an example. You import the generated code, read a
|
||||
FlatBuffer binary file into a `[]byte`, which you pass to the
|
||||
`GetRootAsMonster` function:
|
||||
|
||||
import (
|
||||
example "MyGame/Example"
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
|
||||
io/ioutil
|
||||
)
|
||||
|
||||
buf, err := ioutil.ReadFile("monster.dat")
|
||||
// handle err
|
||||
monster := example.GetRootAsMonster(buf, 0)
|
||||
|
||||
Now you can access values like this:
|
||||
|
||||
hp := monster.Hp()
|
||||
pos := monster.Pos(nil)
|
||||
|
||||
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), you can replace nil with a
|
||||
pointer to 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.
|
||||
|
||||
To access vectors 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 i := 0; i < monster.InventoryLength(); i++ {
|
||||
monster.Inventory(i) // do something here
|
||||
}
|
||||
|
||||
You can also construct these buffers in Go using the functions found in the
|
||||
generated code, and the FlatBufferBuilder class:
|
||||
|
||||
builder := flatbuffers.NewBuilder(0)
|
||||
|
||||
Create strings:
|
||||
|
||||
str := builder.CreateString("MyMonster")
|
||||
|
||||
Create a table with a struct contained therein:
|
||||
|
||||
example.MonsterStart(builder)
|
||||
example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0, 3.0, 4, 5, 6))
|
||||
example.MonsterAddHp(builder, 80)
|
||||
example.MonsterAddName(builder, str)
|
||||
example.MonsterAddInventory(builder, inv)
|
||||
example.MonsterAddTest_Type(builder, 1)
|
||||
example.MonsterAddTest(builder, mon2)
|
||||
example.MonsterAddTest4(builder, test4s)
|
||||
mon := example.MonsterEnd(builder)
|
||||
|
||||
Unlike C++, Go does not support table creation functions like 'createMonster()'.
|
||||
This is to create the buffer without
|
||||
using temporary object allocation (since the `Vec3` is an inline component of
|
||||
`Monster`, it has to be created right where it is added, whereas the name and
|
||||
the inventory are not inline).
|
||||
Structs do have convenient methods that allow you to construct them in one call.
|
||||
These also have arguments for nested structs, e.g. if a struct has a field `a`
|
||||
and a nested struct field `b` (which has fields `c` and `d`), then the arguments
|
||||
will be `a`, `c` and `d`.
|
||||
|
||||
Vectors also use this start/end pattern to allow vectors of both scalar types
|
||||
and structs:
|
||||
|
||||
example.MonsterStartInventoryVector(builder, 5)
|
||||
for i := 4; i >= 0; i-- {
|
||||
builder.PrependByte(byte(i))
|
||||
}
|
||||
inv := builder.EndVector(5)
|
||||
|
||||
The generated method 'StartInventoryVector' is provided as a convenience
|
||||
function which calls 'StartVector' with the correct element size of the vector
|
||||
type which in this case is 'ubyte' or 1 byte per vector element.
|
||||
You pass the number of elements you want to write.
|
||||
You write the elements backwards since the buffer
|
||||
is being constructed back to front.
|
||||
|
||||
There are `Prepend` functions for all the scalar types. You use
|
||||
`PrependUOffset` for any previously constructed objects (such as other tables,
|
||||
strings, vectors). For structs, you use the appropriate `create` function
|
||||
in-line, as shown above in the `Monster` example.
|
||||
|
||||
## Text Parsing
|
||||
|
||||
There currently is no support for parsing text (Schema's and JSON) directly
|
||||
from Go, though you could use the C++ parser through cgo. Please see the
|
||||
C++ documentation for more on text parsing.
|
||||
@@ -20,7 +20,9 @@ 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.
|
||||
terms of offsets and adjacency only. This may mean two different
|
||||
implementations may produce different binaries given the same input
|
||||
values, and this is perfectly valid.
|
||||
|
||||
### Format identification
|
||||
|
||||
@@ -43,8 +45,9 @@ 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
|
||||
`uoffset_t`, which is currently always a `uint32_t`, and is used to
|
||||
refer to all tables/unions/strings/vectors (these are never stored
|
||||
in-line). 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
|
||||
@@ -52,7 +55,7 @@ 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.
|
||||
The format starts with an `uoffset_t` to the root object in the buffer.
|
||||
|
||||
We have two kinds of objects, structs and tables.
|
||||
|
||||
@@ -71,20 +74,20 @@ 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.
|
||||
`uoffset_t`, since vtables may be stored anywhere), followed by all the
|
||||
fields as aligned scalars (or offsets). Unlike structs, not all fields
|
||||
need to be present. There is no set order and layout.
|
||||
|
||||
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
|
||||
The elements of a vtable are all of type `voffset_t`, which is
|
||||
a `uint16_t`. The first element is the number of elements of the vtable,
|
||||
including this one. The second one is the size of the object, in bytes
|
||||
(including the vtable offset). This size is used for streaming, to know
|
||||
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
|
||||
The remaining elements are the N offsets, where N is the amount of fields
|
||||
declared in the schema when the code that constructed this buffer was
|
||||
compiled (thus, the size of the table is N + 2).
|
||||
|
||||
@@ -100,11 +103,13 @@ field to be read.
|
||||
|
||||
Strings are simply a vector of bytes, and are always
|
||||
null-terminated. Vectors are stored as contiguous aligned scalar
|
||||
elements prefixed by a count.
|
||||
elements prefixed by a 32bit element count (not including any
|
||||
null termination).
|
||||
|
||||
### Construction
|
||||
|
||||
The current implementation constructs these buffers backwards, since
|
||||
The current implementation constructs these buffers backwards (starting
|
||||
at the highest memory address of the buffer), since
|
||||
that significantly reduces the amount of bookkeeping and simplifies the
|
||||
construction API.
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# Use in Java
|
||||
|
||||
There's experimental support for reading FlatBuffers in Java. Generate code
|
||||
FlatBuffers supports reading and writing binary 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:
|
||||
the `getRootAsMyRootType` function:
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(data);
|
||||
Monster monster = Monster.getRootAsMonster(bb);
|
||||
@@ -22,14 +22,20 @@ 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.
|
||||
|
||||
Java does not support unsigned scalars. This means that any unsigned types you
|
||||
use in your schema will actually be represented as a signed value. This means
|
||||
all bits are still present, but may represent a negative value when used.
|
||||
For example, to read a `byte b` as an unsigned number, you can do:
|
||||
`(short)(b & 0xFF)`
|
||||
|
||||
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:
|
||||
suffixed by `Length` let's you know the number of elements you can access:
|
||||
|
||||
for (int i = 0; i < monster.inventory_length(); i++)
|
||||
for (int i = 0; i < monster.inventoryLength(); i++)
|
||||
monster.inventory(i); // do something here
|
||||
|
||||
You can also construct these buffers in Java using the static methods found
|
||||
@@ -55,11 +61,19 @@ Create a table with a struct contained therein:
|
||||
|
||||
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).
|
||||
using temporary object allocation.
|
||||
|
||||
It's important to understand that fields that are structs are inline (like
|
||||
`Vec3` above), and MUST thus be created between the start and end calls of
|
||||
a table. Everything else (other tables, strings, vectors) MUST be created
|
||||
before the start of the table they are referenced in.
|
||||
|
||||
Structs do have convenient methods that even have arguments for nested structs.
|
||||
|
||||
As you can see, references to other objects (e.g. the string above) are simple
|
||||
ints, and thus do not have the type-safety of the Offset type in C++. Extra
|
||||
case must thus be taken that you set the right offset on the right field.
|
||||
|
||||
Vectors also use this start/end pattern to allow vectors of both scalar types
|
||||
and structs:
|
||||
|
||||
@@ -72,6 +86,11 @@ You can use the generated method `startInventoryVector` to conveniently call
|
||||
elements you want to write. You write the elements backwards since the buffer
|
||||
is being constructed back to front.
|
||||
|
||||
There are `add` functions for all the scalar types. You use `addOffset` for
|
||||
any previously constructed objects (such as other tables, strings, vectors).
|
||||
For structs, you use the appropriate `create` function in-line, as shown
|
||||
above in the `Monster` example.
|
||||
|
||||
## Text Parsing
|
||||
|
||||
There currently is no support for parsing text (Schema's and JSON) directly
|
||||
|
||||
@@ -51,6 +51,9 @@ and backwards compatibility. Note that:
|
||||
definition. Older data will still
|
||||
read correctly, and give you the default value when read. Older code
|
||||
will simply ignore the new field.
|
||||
If you want to have flexibility to use any order for fields in your
|
||||
schema, you can manually assign ids (much like Protocol Buffers),
|
||||
see the `id` attribute below.
|
||||
|
||||
- You cannot delete fields you don't use anymore from the schema,
|
||||
but you can simply
|
||||
@@ -88,8 +91,7 @@ Builtin scalar types are:
|
||||
- 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]]`.
|
||||
is not supported, instead you can wrap the inner vector in a table.
|
||||
|
||||
- `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.
|
||||
@@ -122,8 +124,7 @@ 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`.
|
||||
the type of any fields declared with this enum type.
|
||||
|
||||
### Unions
|
||||
|
||||
@@ -143,7 +144,45 @@ packages.
|
||||
### Root type
|
||||
|
||||
This declares what you consider to be the root table (or struct) of the
|
||||
serialized data.
|
||||
serialized data. This is particular important for parsing JSON data,
|
||||
which doesn't include object type information.
|
||||
|
||||
### File identification and extension
|
||||
|
||||
Typically, a FlatBuffer binary buffer is not self-describing, i.e. it
|
||||
needs you to know its schema to parse it correctly. But if you
|
||||
want to use a FlatBuffer as a file format, it would be convenient
|
||||
to be able to have a "magic number" in there, like most file formats
|
||||
have, to be able to do a sanity check to see if you're reading the
|
||||
kind of file you're expecting.
|
||||
|
||||
Now, you can always prefix a FlatBuffer with your own file header,
|
||||
but FlatBuffers has a built-in way to add an identifier to a
|
||||
FlatBuffer that takes up minimal space, and keeps the buffer
|
||||
compatible with buffers that don't have such an identifier.
|
||||
|
||||
You can specify in a schema, similar to `root_type`, that you intend
|
||||
for this type of FlatBuffer to be used as a file format:
|
||||
|
||||
file_identifier "MYFI";
|
||||
|
||||
Identifiers must always be exactly 4 characters long. These 4 characters
|
||||
will end up as bytes at offsets 4-7 (inclusive) in the buffer.
|
||||
|
||||
For any schema that has such an identifier, `flatc` will automatically
|
||||
add the identifier to any binaries it generates (with `-b`),
|
||||
and generated calls like `FinishMonsterBuffer` also add the identifier.
|
||||
If you have specified an identifier and wish to generate a buffer
|
||||
without one, you can always still do so by calling
|
||||
`FlatBufferBuilder::Finish` explicitly.
|
||||
|
||||
After loading a buffer, you can use a call like
|
||||
`MonsterBufferHasIdentifier` to check if the identifier is present.
|
||||
|
||||
Additionally, by default `flatc` will output binary files as `.bin`.
|
||||
This declaration in the schema will change that to whatever you want:
|
||||
|
||||
file_extension "ext";
|
||||
|
||||
### Comments & documentation
|
||||
|
||||
@@ -166,6 +205,17 @@ help text).
|
||||
|
||||
Current understood attributes:
|
||||
|
||||
- `id: n` (on a table field): manually set the field identifier to `n`.
|
||||
If you use this attribute, you must use it on ALL fields of this table,
|
||||
and the numbers must be a contiguous range from 0 onwards.
|
||||
Additionally, since a union type effectively adds two fields, its
|
||||
id must be that of the second field (the first field is the type
|
||||
field and not explicitly declared in the schema).
|
||||
For example, if the last field before the union field had id 6,
|
||||
the union field should have id 8, and the unions type field will
|
||||
implicitly be 7.
|
||||
IDs allow the fields to be placed in any order in the schema.
|
||||
When a new field is added to the schema is must use the next available ID.
|
||||
- `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
|
||||
@@ -176,6 +226,33 @@ Current understood attributes:
|
||||
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`).
|
||||
- `bit_flags` (on an enum): the values of this field indicate bits,
|
||||
meaning that any value N specified in the schema will end up
|
||||
representing 1<<N, or if you don't specify values at all, you'll get
|
||||
the sequence 1, 2, 4, 8, ...
|
||||
|
||||
## JSON Parsing
|
||||
|
||||
The same parser that parses the schema declarations above is also able
|
||||
to parse JSON objects that conform to this schema. So, unlike other JSON
|
||||
parsers, this parser is strongly typed, and parses directly into a FlatBuffer
|
||||
(see the compiler documentation on how to do this from the command line, or
|
||||
the C++ documentation on how to do this at runtime).
|
||||
|
||||
Besides needing a schema, there are a few other changes to how it parses
|
||||
JSON:
|
||||
|
||||
- It accepts field names with and without quotes, like many JSON parsers
|
||||
already do. It outputs them without quotes as well, though can be made
|
||||
to output them using the `strict_json` flag.
|
||||
- If a field has an enum type, the parser will recognize symbolic enum
|
||||
values (with or without quotes) instead of numbers, e.g.
|
||||
`field: EnumVal`. If a field is of integral type, you can still use
|
||||
symbolic names, but values need to be prefixed with their type and
|
||||
need to be quoted, e.g. `field: "Enum.EnumVal"`. For enums
|
||||
representing flags, you may place multiple inside a string
|
||||
separated by spaces to OR them, e.g.
|
||||
`field: "EnumVal1 EnumVal2"` or `field: "Enum.EnumVal1 Enum.EnumVal2"`.
|
||||
|
||||
## Gotchas
|
||||
|
||||
@@ -184,7 +261,8 @@ Current understood attributes:
|
||||
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.
|
||||
Protocol Buffers (and which is still an option using the `id` attribute
|
||||
mentioned above).
|
||||
|
||||
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
|
||||
@@ -194,5 +272,7 @@ 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.
|
||||
the world. If this is not practical for you, use explicit field ids, which
|
||||
should always generate a merge conflict if two people try to allocate the same
|
||||
id.
|
||||
|
||||
|
||||
@@ -748,6 +748,7 @@ INPUT = "FlatBuffers.md" \
|
||||
"Compiler.md" \
|
||||
"Schemas.md" \
|
||||
"CppUsage.md" \
|
||||
"GoUsage.md" \
|
||||
"JavaUsage.md" \
|
||||
"Benchmarks.md" \
|
||||
"WhitePaper.md" \
|
||||
|
||||
635
go/builder.go
Normal file
635
go/builder.go
Normal file
@@ -0,0 +1,635 @@
|
||||
package flatbuffers
|
||||
|
||||
// Builder is a state machine for creating FlatBuffer objects.
|
||||
// Use a Builder to construct object(s) starting from leaf nodes.
|
||||
//
|
||||
// A Builder constructs byte buffers in a last-first manner for simplicity and
|
||||
// performance.
|
||||
type Builder struct {
|
||||
Bytes []byte
|
||||
|
||||
minalign int
|
||||
vtable []UOffsetT
|
||||
objectEnd UOffsetT
|
||||
vtables []UOffsetT
|
||||
head UOffsetT
|
||||
}
|
||||
|
||||
// NewBuilder initializes a Builder of size `initial_size`.
|
||||
// The internal buffer is grown as needed.
|
||||
func NewBuilder(initialSize int) *Builder {
|
||||
if initialSize <= 0 {
|
||||
initialSize = 0
|
||||
}
|
||||
|
||||
b := &Builder{}
|
||||
b.Bytes = make([]byte, initialSize)
|
||||
b.head = UOffsetT(initialSize)
|
||||
b.minalign = 1
|
||||
b.vtables = make([]UOffsetT, 0, 16) // sensible default capacity
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// StartObject initializes bookkeeping for writing a new object.
|
||||
func (b *Builder) StartObject(numfields int) {
|
||||
b.notNested()
|
||||
// use 32-bit offsets so that arithmetic doesn't overflow.
|
||||
b.vtable = make([]UOffsetT, numfields)
|
||||
b.objectEnd = b.Offset()
|
||||
b.minalign = 1
|
||||
}
|
||||
|
||||
// WriteVtable serializes the vtable for the current object, if applicable.
|
||||
//
|
||||
// Before writing out the vtable, this checks pre-existing vtables for equality
|
||||
// to this one. If an equal vtable is found, point the object to the existing
|
||||
// vtable and return.
|
||||
//
|
||||
// Because vtable values are sensitive to alignment of object data, not all
|
||||
// logically-equal vtables will be deduplicated.
|
||||
//
|
||||
// A vtable has the following format:
|
||||
// <VOffsetT: size of the vtable in bytes, including this value>
|
||||
// <VOffsetT: size of the object in bytes, including the vtable offset>
|
||||
// <VOffsetT: offset for a field> * N, where N is the number of fields in
|
||||
// the schema for this type. Includes deprecated fields.
|
||||
// Thus, a vtable is made of 2 + N elements, each SizeVOffsetT bytes wide.
|
||||
//
|
||||
// An object has the following format:
|
||||
// <SOffsetT: offset to this object's vtable (may be negative)>
|
||||
// <byte: data>+
|
||||
func (b *Builder) WriteVtable() (n UOffsetT) {
|
||||
// Prepend a zero scalar to the object. Later in this function we'll
|
||||
// write an offset here that points to the object's vtable:
|
||||
b.PrependSOffsetT(0)
|
||||
|
||||
objectOffset := b.Offset()
|
||||
existingVtable := UOffsetT(0)
|
||||
|
||||
// Search backwards through existing vtables, because similar vtables
|
||||
// are likely to have been recently appended. See
|
||||
// BenchmarkVtableDeduplication for a case in which this heuristic
|
||||
// saves about 30% of the time used in writing objects with duplicate
|
||||
// tables.
|
||||
for i := len(b.vtables) - 1; i >= 0; i-- {
|
||||
// Find the other vtable, which is associated with `i`:
|
||||
vt2Offset := b.vtables[i]
|
||||
vt2Start := len(b.Bytes) - int(vt2Offset)
|
||||
vt2Len := GetVOffsetT(b.Bytes[vt2Start:])
|
||||
|
||||
metadata := VtableMetadataFields * SizeVOffsetT
|
||||
vt2End := vt2Start + int(vt2Len)
|
||||
vt2 := b.Bytes[vt2Start+metadata : vt2End]
|
||||
|
||||
// Compare the other vtable to the one under consideration.
|
||||
// If they are equal, store the offset and break:
|
||||
if vtableEqual(b.vtable, objectOffset, vt2) {
|
||||
existingVtable = vt2Offset
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if existingVtable == 0 {
|
||||
// Did not find a vtable, so write this one to the buffer.
|
||||
|
||||
// Write out the current vtable in reverse , because
|
||||
// serialization occurs in last-first order:
|
||||
for i := len(b.vtable) - 1; i >= 0; i-- {
|
||||
var off UOffsetT
|
||||
if b.vtable[i] != 0 {
|
||||
// Forward reference to field;
|
||||
// use 32bit number to ensure no overflow:
|
||||
off = objectOffset - b.vtable[i]
|
||||
}
|
||||
|
||||
b.PrependVOffsetT(VOffsetT(off))
|
||||
}
|
||||
|
||||
// The two metadata fields are written last.
|
||||
|
||||
// First, store the object bytesize:
|
||||
objectSize := objectOffset - b.objectEnd
|
||||
b.PrependVOffsetT(VOffsetT(objectSize))
|
||||
|
||||
// Second, store the vtable bytesize:
|
||||
vBytes := (len(b.vtable) + VtableMetadataFields) * SizeVOffsetT
|
||||
b.PrependVOffsetT(VOffsetT(vBytes))
|
||||
|
||||
// Next, write the offset to the new vtable in the
|
||||
// already-allocated SOffsetT at the beginning of this object:
|
||||
objectStart := SOffsetT(len(b.Bytes)) - SOffsetT(objectOffset)
|
||||
WriteSOffsetT(b.Bytes[objectStart:],
|
||||
SOffsetT(b.Offset())-SOffsetT(objectOffset))
|
||||
|
||||
// Finally, store this vtable in memory for future
|
||||
// deduplication:
|
||||
b.vtables = append(b.vtables, b.Offset())
|
||||
} else {
|
||||
// Found a duplicate vtable.
|
||||
|
||||
objectStart := SOffsetT(len(b.Bytes)) - SOffsetT(objectOffset)
|
||||
b.head = UOffsetT(objectStart)
|
||||
|
||||
// Write the offset to the found vtable in the
|
||||
// already-allocated SOffsetT at the beginning of this object:
|
||||
WriteSOffsetT(b.Bytes[b.head:],
|
||||
SOffsetT(existingVtable)-SOffsetT(objectOffset))
|
||||
}
|
||||
|
||||
b.vtable = nil
|
||||
return objectOffset
|
||||
}
|
||||
|
||||
// EndObject writes data necessary to finish object construction.
|
||||
func (b *Builder) EndObject() UOffsetT {
|
||||
if b.vtable == nil {
|
||||
panic("not in object")
|
||||
}
|
||||
return b.WriteVtable()
|
||||
}
|
||||
|
||||
// Doubles the size of the byteslice, and copies the old data towards the
|
||||
// end of the new byteslice (since we build the buffer backwards).
|
||||
func (b *Builder) growByteBuffer() {
|
||||
if (len(b.Bytes) & 0xC0000000) != 0 {
|
||||
panic("cannot grow buffer beyond 2 gigabytes")
|
||||
}
|
||||
newSize := len(b.Bytes) * 2
|
||||
if newSize == 0 {
|
||||
newSize = 1
|
||||
}
|
||||
bytes2 := make([]byte, newSize)
|
||||
copy(bytes2[newSize-len(b.Bytes):], b.Bytes)
|
||||
b.Bytes = bytes2
|
||||
}
|
||||
|
||||
// Head gives the start of useful data in the underlying byte buffer.
|
||||
// Note: unlike other functions, this value is interpreted as from the left.
|
||||
func (b *Builder) Head() UOffsetT {
|
||||
return b.head
|
||||
}
|
||||
|
||||
// Offset relative to the end of the buffer.
|
||||
func (b *Builder) Offset() UOffsetT {
|
||||
return UOffsetT(len(b.Bytes)) - b.head
|
||||
}
|
||||
|
||||
// Pad places zeros at the current offset.
|
||||
func (b *Builder) Pad(n int) {
|
||||
for i := 0; i < n; i++ {
|
||||
b.PlaceByte(0)
|
||||
}
|
||||
}
|
||||
|
||||
// Prep prepares 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 SizeInt32, and the string data follows it
|
||||
// directly.
|
||||
// If all you need to do is align, `additionalBytes` will be 0.
|
||||
func (b *Builder) Prep(size, additionalBytes int) {
|
||||
// Track the biggest thing we've ever aligned to.
|
||||
if size > b.minalign {
|
||||
b.minalign = size
|
||||
}
|
||||
// Find the amount of alignment needed such that `size` is properly
|
||||
// aligned after `additionalBytes`:
|
||||
alignSize := (^(len(b.Bytes) - int(b.Head()) + additionalBytes)) + 1
|
||||
alignSize &= (size - 1)
|
||||
|
||||
// Reallocate the buffer if needed:
|
||||
for int(b.head) < alignSize+size+additionalBytes {
|
||||
oldBufSize := len(b.Bytes)
|
||||
b.growByteBuffer()
|
||||
b.head += UOffsetT(len(b.Bytes) - oldBufSize)
|
||||
}
|
||||
b.Pad(alignSize)
|
||||
}
|
||||
|
||||
// PrependSOffsetT prepends an SOffsetT, relative to where it will be written.
|
||||
func (b *Builder) PrependSOffsetT(off SOffsetT) {
|
||||
b.Prep(SizeSOffsetT, 0) // Ensure alignment is already done.
|
||||
if !(UOffsetT(off) <= b.Offset()) {
|
||||
panic("unreachable: off <= b.Offset()")
|
||||
}
|
||||
off2 := SOffsetT(b.Offset()) - off + SOffsetT(SizeSOffsetT)
|
||||
b.PlaceSOffsetT(off2)
|
||||
}
|
||||
|
||||
// PrependUOffsetT prepends an UOffsetT, relative to where it will be written.
|
||||
func (b *Builder) PrependUOffsetT(off UOffsetT) {
|
||||
b.Prep(SizeUOffsetT, 0) // Ensure alignment is already done.
|
||||
if !(off <= b.Offset()) {
|
||||
panic("unreachable: off <= b.Offset()")
|
||||
}
|
||||
off2 := b.Offset() - off + UOffsetT(SizeUOffsetT)
|
||||
b.PlaceUOffsetT(off2)
|
||||
}
|
||||
|
||||
// StartVector initializes bookkeeping for writing a new vector.
|
||||
//
|
||||
// A vector has the following format:
|
||||
// <UOffsetT: number of elements in this vector>
|
||||
// <T: data>+, where T is the type of elements of this vector.
|
||||
func (b *Builder) StartVector(elemSize, numElems int) UOffsetT {
|
||||
b.notNested()
|
||||
b.Prep(SizeUint32, elemSize*numElems)
|
||||
return b.Offset()
|
||||
}
|
||||
|
||||
// EndVector writes data necessary to finish vector construction.
|
||||
func (b *Builder) EndVector(vectorNumElems int) UOffsetT {
|
||||
// we already made space for this, so write without PrependUint32
|
||||
b.PlaceUOffsetT(UOffsetT(vectorNumElems))
|
||||
return b.Offset()
|
||||
}
|
||||
|
||||
// CreateString writes a null-terminated string as a vector.
|
||||
func (b *Builder) CreateString(s string) UOffsetT {
|
||||
b.Prep(int(SizeUOffsetT), (len(s)+1)*SizeByte)
|
||||
b.PlaceByte(0)
|
||||
|
||||
x := []byte(s)
|
||||
l := UOffsetT(len(x))
|
||||
|
||||
b.head -= l
|
||||
copy(b.Bytes[b.head:b.head+l], x)
|
||||
|
||||
return b.EndVector(len(x))
|
||||
}
|
||||
|
||||
func (b *Builder) notNested() {
|
||||
// Check that no other objects are being built while making this
|
||||
// object. If not, panic:
|
||||
if b.vtable != nil {
|
||||
panic("non-inline data write inside of object")
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Builder) nested(obj UOffsetT) {
|
||||
// Structs are always stored inline, so need to be created right
|
||||
// where they are used. You'll get this panic if you created it
|
||||
// elsewhere:
|
||||
if obj != b.Offset() {
|
||||
panic("inline data write outside of object")
|
||||
}
|
||||
}
|
||||
|
||||
// PrependBoolSlot prepends a bool onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependBoolSlot(o int, x, d bool) {
|
||||
val := byte(0)
|
||||
if x {
|
||||
val = 1
|
||||
}
|
||||
def := byte(0)
|
||||
if d {
|
||||
def = 1
|
||||
}
|
||||
b.PrependByteSlot(o, val, def)
|
||||
}
|
||||
|
||||
// PrependByteSlot prepends a byte onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependByteSlot(o int, x, d byte) {
|
||||
if x != d {
|
||||
b.PrependByte(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependUint8Slot prepends a uint8 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependUint8Slot(o int, x, d uint8) {
|
||||
if x != d {
|
||||
b.PrependUint8(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependUint16Slot prepends a uint16 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependUint16Slot(o int, x, d uint16) {
|
||||
if x != d {
|
||||
b.PrependUint16(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependUint32Slot prepends a uint32 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependUint32Slot(o int, x, d uint32) {
|
||||
if x != d {
|
||||
b.PrependUint32(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependUint64Slot prepends a uint64 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependUint64Slot(o int, x, d uint64) {
|
||||
if x != d {
|
||||
b.PrependUint64(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependInt8Slot prepends a int8 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependInt8Slot(o int, x, d int8) {
|
||||
if x != d {
|
||||
b.PrependInt8(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependInt16Slot prepends a int16 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependInt16Slot(o int, x, d int16) {
|
||||
if x != d {
|
||||
b.PrependInt16(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependInt32Slot prepends a int32 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependInt32Slot(o int, x, d int32) {
|
||||
if x != d {
|
||||
b.PrependInt32(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependInt64Slot prepends a int64 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependInt64Slot(o int, x, d int64) {
|
||||
if x != d {
|
||||
b.PrependInt64(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependFloat32Slot prepends a float32 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependFloat32Slot(o int, x, d float32) {
|
||||
if x != d {
|
||||
b.PrependFloat32(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependFloat64Slot prepends a float64 onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependFloat64Slot(o int, x, d float64) {
|
||||
if x != d {
|
||||
b.PrependFloat64(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependUOffsetTSlot prepends an UOffsetT onto the object at vtable slot `o`.
|
||||
// If value `x` equals default `d`, then the slot will be set to zero and no
|
||||
// other data will be written.
|
||||
func (b *Builder) PrependUOffsetTSlot(o int, x, d UOffsetT) {
|
||||
if x != d {
|
||||
b.PrependUOffsetT(x)
|
||||
b.Slot(o)
|
||||
}
|
||||
}
|
||||
|
||||
// PrependStructSlot prepends a struct onto the object at vtable slot `o`.
|
||||
// Structs are stored inline, so nothing additional is being added.
|
||||
// In generated code, `d` is always 0.
|
||||
func (b *Builder) PrependStructSlot(voffset int, x, d UOffsetT) {
|
||||
if x != d {
|
||||
b.nested(x)
|
||||
b.Slot(voffset)
|
||||
}
|
||||
}
|
||||
|
||||
// Slot sets the vtable key `voffset` to the current location in the buffer.
|
||||
func (b *Builder) Slot(slotnum int) {
|
||||
b.vtable[slotnum] = UOffsetT(b.Offset())
|
||||
}
|
||||
|
||||
// Finish finalizes a buffer, pointing to the given `rootTable`.
|
||||
func (b *Builder) Finish(rootTable UOffsetT) {
|
||||
b.Prep(b.minalign, SizeUOffsetT)
|
||||
b.PrependUOffsetT(rootTable)
|
||||
}
|
||||
|
||||
// vtableEqual compares an unwritten vtable to a written vtable.
|
||||
func vtableEqual(a []UOffsetT, objectStart UOffsetT, b []byte) bool {
|
||||
if len(a)*SizeVOffsetT != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 0; i < len(a); i++ {
|
||||
x := GetVOffsetT(b[i*SizeVOffsetT : (i+1)*SizeVOffsetT])
|
||||
|
||||
// Skip vtable entries that indicate a default value.
|
||||
if x == 0 && a[i] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
y := SOffsetT(objectStart) - SOffsetT(a[i])
|
||||
if SOffsetT(x) != y {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// PrependBool prepends a bool to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependBool(x bool) {
|
||||
b.Prep(SizeBool, 0)
|
||||
b.PlaceBool(x)
|
||||
}
|
||||
|
||||
// PrependUint8 prepends a uint8 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependUint8(x uint8) {
|
||||
b.Prep(SizeUint8, 0)
|
||||
b.PlaceUint8(x)
|
||||
}
|
||||
|
||||
// PrependUint16 prepends a uint16 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependUint16(x uint16) {
|
||||
b.Prep(SizeUint16, 0)
|
||||
b.PlaceUint16(x)
|
||||
}
|
||||
|
||||
// PrependUint32 prepends a uint32 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependUint32(x uint32) {
|
||||
b.Prep(SizeUint32, 0)
|
||||
b.PlaceUint32(x)
|
||||
}
|
||||
|
||||
// PrependUint64 prepends a uint64 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependUint64(x uint64) {
|
||||
b.Prep(SizeUint64, 0)
|
||||
b.PlaceUint64(x)
|
||||
}
|
||||
|
||||
// PrependInt8 prepends a int8 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependInt8(x int8) {
|
||||
b.Prep(SizeInt8, 0)
|
||||
b.PlaceInt8(x)
|
||||
}
|
||||
|
||||
// PrependInt16 prepends a int16 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependInt16(x int16) {
|
||||
b.Prep(SizeInt16, 0)
|
||||
b.PlaceInt16(x)
|
||||
}
|
||||
|
||||
// PrependInt32 prepends a int32 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependInt32(x int32) {
|
||||
b.Prep(SizeInt32, 0)
|
||||
b.PlaceInt32(x)
|
||||
}
|
||||
|
||||
// PrependInt64 prepends a int64 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependInt64(x int64) {
|
||||
b.Prep(SizeInt64, 0)
|
||||
b.PlaceInt64(x)
|
||||
}
|
||||
|
||||
// PrependFloat32 prepends a float32 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependFloat32(x float32) {
|
||||
b.Prep(SizeFloat32, 0)
|
||||
b.PlaceFloat32(x)
|
||||
}
|
||||
|
||||
// PrependFloat64 prepends a float64 to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependFloat64(x float64) {
|
||||
b.Prep(SizeFloat64, 0)
|
||||
b.PlaceFloat64(x)
|
||||
}
|
||||
|
||||
// PrependByte prepends a byte to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependByte(x byte) {
|
||||
b.Prep(SizeByte, 0)
|
||||
b.PlaceByte(x)
|
||||
}
|
||||
|
||||
// PrependVOffsetT prepends a VOffsetT to the Builder buffer.
|
||||
// Aligns and checks for space.
|
||||
func (b *Builder) PrependVOffsetT(x VOffsetT) {
|
||||
b.Prep(SizeVOffsetT, 0)
|
||||
b.PlaceVOffsetT(x)
|
||||
}
|
||||
|
||||
// PlaceBool prepends a bool to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceBool(x bool) {
|
||||
b.head -= UOffsetT(SizeBool)
|
||||
WriteBool(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceUint8 prepends a uint8 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceUint8(x uint8) {
|
||||
b.head -= UOffsetT(SizeUint8)
|
||||
WriteUint8(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceUint16 prepends a uint16 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceUint16(x uint16) {
|
||||
b.head -= UOffsetT(SizeUint16)
|
||||
WriteUint16(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceUint32 prepends a uint32 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceUint32(x uint32) {
|
||||
b.head -= UOffsetT(SizeUint32)
|
||||
WriteUint32(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceUint64 prepends a uint64 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceUint64(x uint64) {
|
||||
b.head -= UOffsetT(SizeUint64)
|
||||
WriteUint64(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceInt8 prepends a int8 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceInt8(x int8) {
|
||||
b.head -= UOffsetT(SizeInt8)
|
||||
WriteInt8(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceInt16 prepends a int16 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceInt16(x int16) {
|
||||
b.head -= UOffsetT(SizeInt16)
|
||||
WriteInt16(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceInt32 prepends a int32 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceInt32(x int32) {
|
||||
b.head -= UOffsetT(SizeInt32)
|
||||
WriteInt32(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceInt64 prepends a int64 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceInt64(x int64) {
|
||||
b.head -= UOffsetT(SizeInt64)
|
||||
WriteInt64(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceFloat32 prepends a float32 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceFloat32(x float32) {
|
||||
b.head -= UOffsetT(SizeFloat32)
|
||||
WriteFloat32(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceFloat64 prepends a float64 to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceFloat64(x float64) {
|
||||
b.head -= UOffsetT(SizeFloat64)
|
||||
WriteFloat64(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceByte prepends a byte to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceByte(x byte) {
|
||||
b.head -= UOffsetT(SizeByte)
|
||||
WriteByte(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceVOffsetT prepends a VOffsetT to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceVOffsetT(x VOffsetT) {
|
||||
b.head -= UOffsetT(SizeVOffsetT)
|
||||
WriteVOffsetT(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceSOffsetT prepends a SOffsetT to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceSOffsetT(x SOffsetT) {
|
||||
b.head -= UOffsetT(SizeSOffsetT)
|
||||
WriteSOffsetT(b.Bytes[b.head:], x)
|
||||
}
|
||||
|
||||
// PlaceUOffsetT prepends a UOffsetT to the Builder, without checking for space.
|
||||
func (b *Builder) PlaceUOffsetT(x UOffsetT) {
|
||||
b.head -= UOffsetT(SizeUOffsetT)
|
||||
WriteUOffsetT(b.Bytes[b.head:], x)
|
||||
}
|
||||
3
go/doc.go
Normal file
3
go/doc.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package flatbuffers provides facilities to read and write flatbuffers
|
||||
// objects.
|
||||
package flatbuffers
|
||||
216
go/encode.go
Normal file
216
go/encode.go
Normal file
@@ -0,0 +1,216 @@
|
||||
package flatbuffers
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
type (
|
||||
// A SOffsetT stores a signed offset into arbitrary data.
|
||||
SOffsetT int32
|
||||
// A UOffsetT stores an unsigned offset into vector data.
|
||||
UOffsetT uint32
|
||||
// A VOffsetT stores an unsigned offset in a vtable.
|
||||
VOffsetT uint16
|
||||
)
|
||||
|
||||
const (
|
||||
// VtableMetadataFields is the count of metadata fields in each vtable.
|
||||
VtableMetadataFields = 2
|
||||
)
|
||||
|
||||
// GetByte decodes a little-endian byte from a byte slice.
|
||||
func GetByte(buf []byte) byte {
|
||||
return byte(GetUint8(buf))
|
||||
}
|
||||
|
||||
// GetBool decodes a little-endian bool from a byte slice.
|
||||
func GetBool(buf []byte) bool {
|
||||
return buf[0] == 1
|
||||
}
|
||||
|
||||
// GetUint8 decodes a little-endian uint8 from a byte slice.
|
||||
func GetUint8(buf []byte) (n uint8) {
|
||||
n = uint8(buf[0])
|
||||
return
|
||||
}
|
||||
|
||||
// GetUint16 decodes a little-endian uint16 from a byte slice.
|
||||
func GetUint16(buf []byte) (n uint16) {
|
||||
n |= uint16(buf[0])
|
||||
n |= uint16(buf[1]) << 8
|
||||
return
|
||||
}
|
||||
|
||||
// GetUint32 decodes a little-endian uint32 from a byte slice.
|
||||
func GetUint32(buf []byte) (n uint32) {
|
||||
n |= uint32(buf[0])
|
||||
n |= uint32(buf[1]) << 8
|
||||
n |= uint32(buf[2]) << 16
|
||||
n |= uint32(buf[3]) << 24
|
||||
return
|
||||
}
|
||||
|
||||
// GetUint64 decodes a little-endian uint64 from a byte slice.
|
||||
func GetUint64(buf []byte) (n uint64) {
|
||||
n |= uint64(buf[0])
|
||||
n |= uint64(buf[1]) << 8
|
||||
n |= uint64(buf[2]) << 16
|
||||
n |= uint64(buf[3]) << 24
|
||||
n |= uint64(buf[4]) << 32
|
||||
n |= uint64(buf[5]) << 40
|
||||
n |= uint64(buf[6]) << 48
|
||||
n |= uint64(buf[7]) << 56
|
||||
return
|
||||
}
|
||||
|
||||
// GetInt8 decodes a little-endian int8 from a byte slice.
|
||||
func GetInt8(buf []byte) (n int8) {
|
||||
n = int8(buf[0])
|
||||
return
|
||||
}
|
||||
|
||||
// GetInt16 decodes a little-endian int16 from a byte slice.
|
||||
func GetInt16(buf []byte) (n int16) {
|
||||
n |= int16(buf[0])
|
||||
n |= int16(buf[1]) << 8
|
||||
return
|
||||
}
|
||||
|
||||
// GetInt32 decodes a little-endian int32 from a byte slice.
|
||||
func GetInt32(buf []byte) (n int32) {
|
||||
n |= int32(buf[0])
|
||||
n |= int32(buf[1]) << 8
|
||||
n |= int32(buf[2]) << 16
|
||||
n |= int32(buf[3]) << 24
|
||||
return
|
||||
}
|
||||
|
||||
// GetInt64 decodes a little-endian int64 from a byte slice.
|
||||
func GetInt64(buf []byte) (n int64) {
|
||||
n |= int64(buf[0])
|
||||
n |= int64(buf[1]) << 8
|
||||
n |= int64(buf[2]) << 16
|
||||
n |= int64(buf[3]) << 24
|
||||
n |= int64(buf[4]) << 32
|
||||
n |= int64(buf[5]) << 40
|
||||
n |= int64(buf[6]) << 48
|
||||
n |= int64(buf[7]) << 56
|
||||
return
|
||||
}
|
||||
|
||||
// GetFloat32 decodes a little-endian float32 from a byte slice.
|
||||
func GetFloat32(buf []byte) float32 {
|
||||
x := GetUint32(buf)
|
||||
return math.Float32frombits(x)
|
||||
}
|
||||
|
||||
// GetFloat64 decodes a little-endian float64 from a byte slice.
|
||||
func GetFloat64(buf []byte) float64 {
|
||||
x := GetUint64(buf)
|
||||
return math.Float64frombits(x)
|
||||
}
|
||||
|
||||
// GetUOffsetT decodes a little-endian UOffsetT from a byte slice.
|
||||
func GetUOffsetT(buf []byte) UOffsetT {
|
||||
return UOffsetT(GetInt32(buf))
|
||||
}
|
||||
|
||||
// GetSOffsetT decodes a little-endian SOffsetT from a byte slice.
|
||||
func GetSOffsetT(buf []byte) SOffsetT {
|
||||
return SOffsetT(GetInt32(buf))
|
||||
}
|
||||
|
||||
// GetVOffsetT decodes a little-endian VOffsetT from a byte slice.
|
||||
func GetVOffsetT(buf []byte) VOffsetT {
|
||||
return VOffsetT(GetUint16(buf))
|
||||
}
|
||||
|
||||
// WriteByte encodes a little-endian uint8 into a byte slice.
|
||||
func WriteByte(buf []byte, n byte) {
|
||||
WriteUint8(buf, uint8(n))
|
||||
}
|
||||
|
||||
// WriteBool encodes a little-endian bool into a byte slice.
|
||||
func WriteBool(buf []byte, b bool) {
|
||||
buf[0] = 0
|
||||
if b {
|
||||
buf[0] = 1
|
||||
}
|
||||
}
|
||||
|
||||
// WriteUint8 encodes a little-endian uint8 into a byte slice.
|
||||
func WriteUint8(buf []byte, n uint8) {
|
||||
buf[0] = byte(n)
|
||||
}
|
||||
|
||||
// WriteUint16 encodes a little-endian uint16 into a byte slice.
|
||||
func WriteUint16(buf []byte, n uint16) {
|
||||
buf[0] = byte(n)
|
||||
buf[1] = byte(n >> 8)
|
||||
}
|
||||
|
||||
// WriteUint32 encodes a little-endian uint32 into a byte slice.
|
||||
func WriteUint32(buf []byte, n uint32) {
|
||||
buf[0] = byte(n)
|
||||
buf[1] = byte(n >> 8)
|
||||
buf[2] = byte(n >> 16)
|
||||
buf[3] = byte(n >> 24)
|
||||
}
|
||||
|
||||
// WriteUint64 encodes a little-endian uint64 into a byte slice.
|
||||
func WriteUint64(buf []byte, n uint64) {
|
||||
for i := uint(0); i < uint(SizeUint64); i++ {
|
||||
buf[i] = byte(n >> (i * 8))
|
||||
}
|
||||
}
|
||||
|
||||
// WriteInt8 encodes a little-endian int8 into a byte slice.
|
||||
func WriteInt8(buf []byte, n int8) {
|
||||
buf[0] = byte(n)
|
||||
}
|
||||
|
||||
// WriteInt16 encodes a little-endian int16 into a byte slice.
|
||||
func WriteInt16(buf []byte, n int16) {
|
||||
buf[0] = byte(n)
|
||||
buf[1] = byte(n >> 8)
|
||||
}
|
||||
|
||||
// WriteInt32 encodes a little-endian int32 into a byte slice.
|
||||
func WriteInt32(buf []byte, n int32) {
|
||||
buf[0] = byte(n)
|
||||
buf[1] = byte(n >> 8)
|
||||
buf[2] = byte(n >> 16)
|
||||
buf[3] = byte(n >> 24)
|
||||
}
|
||||
|
||||
// WriteInt64 encodes a little-endian int64 into a byte slice.
|
||||
func WriteInt64(buf []byte, n int64) {
|
||||
for i := uint(0); i < uint(SizeInt64); i++ {
|
||||
buf[i] = byte(n >> (i * 8))
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFloat32 encodes a little-endian float32 into a byte slice.
|
||||
func WriteFloat32(buf []byte, n float32) {
|
||||
WriteUint32(buf, math.Float32bits(n))
|
||||
}
|
||||
|
||||
// WriteFloat64 encodes a little-endian float64 into a byte slice.
|
||||
func WriteFloat64(buf []byte, n float64) {
|
||||
WriteUint64(buf, math.Float64bits(n))
|
||||
}
|
||||
|
||||
// WriteVOffsetT encodes a little-endian VOffsetT into a byte slice.
|
||||
func WriteVOffsetT(buf []byte, n VOffsetT) {
|
||||
WriteUint16(buf, uint16(n))
|
||||
}
|
||||
|
||||
// WriteSOffsetT encodes a little-endian SOffsetT into a byte slice.
|
||||
func WriteSOffsetT(buf []byte, n SOffsetT) {
|
||||
WriteInt32(buf, int32(n))
|
||||
}
|
||||
|
||||
// WriteUOffsetT encodes a little-endian UOffsetT into a byte slice.
|
||||
func WriteUOffsetT(buf []byte, n UOffsetT) {
|
||||
WriteUint32(buf, uint32(n))
|
||||
}
|
||||
8
go/struct.go
Normal file
8
go/struct.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package flatbuffers
|
||||
|
||||
// Struct wraps a byte slice and provides read access to its data.
|
||||
//
|
||||
// Structs do not have a vtable.
|
||||
type Struct struct {
|
||||
Table
|
||||
}
|
||||
290
go/table.go
Normal file
290
go/table.go
Normal file
@@ -0,0 +1,290 @@
|
||||
package flatbuffers
|
||||
|
||||
// Table wraps a byte slice and provides read access to its data.
|
||||
//
|
||||
// The variable `Pos` indicates the root of the FlatBuffers object therein.
|
||||
type Table struct {
|
||||
Bytes []byte
|
||||
Pos UOffsetT // Always < 1<<31.
|
||||
}
|
||||
|
||||
// Offset provides access into the Table's vtable.
|
||||
//
|
||||
// Deprecated fields are ignored by checking against the vtable's length.
|
||||
func (t *Table) Offset(vtableOffset VOffsetT) VOffsetT {
|
||||
vtable := UOffsetT(SOffsetT(t.Pos) - t.GetSOffsetT(t.Pos))
|
||||
if vtableOffset < t.GetVOffsetT(vtable) {
|
||||
return t.GetVOffsetT(vtable + UOffsetT(vtableOffset))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Indirect retrieves the relative offset stored at `offset`.
|
||||
func (t *Table) Indirect(off UOffsetT) UOffsetT {
|
||||
return off + GetUOffsetT(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// String gets a string from data stored inside the flatbuffer.
|
||||
func (t *Table) String(off UOffsetT) string {
|
||||
off += t.Pos
|
||||
off += GetUOffsetT(t.Bytes[off:])
|
||||
start := off + UOffsetT(SizeUOffsetT)
|
||||
length := GetUOffsetT(t.Bytes[off:])
|
||||
return string(t.Bytes[start : start+length])
|
||||
}
|
||||
|
||||
// VectorLen retrieves the length of the vector whose offset is stored at
|
||||
// "off" in this object.
|
||||
func (t *Table) VectorLen(off UOffsetT) int {
|
||||
off += t.Pos
|
||||
off += GetUOffsetT(t.Bytes[off:])
|
||||
return int(GetUOffsetT(t.Bytes[off:]))
|
||||
}
|
||||
|
||||
// Vector retrieves the start of data of the vector whose offset is stored
|
||||
// at "off" in this object.
|
||||
func (t *Table) Vector(off UOffsetT) UOffsetT {
|
||||
off += t.Pos
|
||||
x := off + GetUOffsetT(t.Bytes[off:])
|
||||
// data starts after metadata containing the vector length
|
||||
x += UOffsetT(SizeUOffsetT)
|
||||
return x
|
||||
}
|
||||
|
||||
// Union initializes any Table-derived type to point to the union at the given
|
||||
// offset.
|
||||
func (t *Table) Union(t2 *Table, off UOffsetT) {
|
||||
off += t.Pos
|
||||
t2.Pos = off + t.GetUOffsetT(off)
|
||||
t2.Bytes = t.Bytes
|
||||
}
|
||||
|
||||
// GetBool retrieves a bool at the given offset.
|
||||
func (t *Table) GetBool(off UOffsetT) bool {
|
||||
return GetBool(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetByte retrieves a byte at the given offset.
|
||||
func (t *Table) GetByte(off UOffsetT) byte {
|
||||
return GetByte(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetUint8 retrieves a uint8 at the given offset.
|
||||
func (t *Table) GetUint8(off UOffsetT) uint8 {
|
||||
return GetUint8(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetUint16 retrieves a uint16 at the given offset.
|
||||
func (t *Table) GetUint16(off UOffsetT) uint16 {
|
||||
return GetUint16(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetUint32 retrieves a uint32 at the given offset.
|
||||
func (t *Table) GetUint32(off UOffsetT) uint32 {
|
||||
return GetUint32(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetUint64 retrieves a uint64 at the given offset.
|
||||
func (t *Table) GetUint64(off UOffsetT) uint64 {
|
||||
return GetUint64(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetInt8 retrieves a int8 at the given offset.
|
||||
func (t *Table) GetInt8(off UOffsetT) int8 {
|
||||
return GetInt8(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetInt16 retrieves a int16 at the given offset.
|
||||
func (t *Table) GetInt16(off UOffsetT) int16 {
|
||||
return GetInt16(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetInt32 retrieves a int32 at the given offset.
|
||||
func (t *Table) GetInt32(off UOffsetT) int32 {
|
||||
return GetInt32(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetInt64 retrieves a int64 at the given offset.
|
||||
func (t *Table) GetInt64(off UOffsetT) int64 {
|
||||
return GetInt64(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetFloat32 retrieves a float32 at the given offset.
|
||||
func (t *Table) GetFloat32(off UOffsetT) float32 {
|
||||
return GetFloat32(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetFloat64 retrieves a float64 at the given offset.
|
||||
func (t *Table) GetFloat64(off UOffsetT) float64 {
|
||||
return GetFloat64(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetUOffsetT retrieves a UOffsetT at the given offset.
|
||||
func (t *Table) GetUOffsetT(off UOffsetT) UOffsetT {
|
||||
return GetUOffsetT(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetVOffsetT retrieves a VOffsetT at the given offset.
|
||||
func (t *Table) GetVOffsetT(off UOffsetT) VOffsetT {
|
||||
return GetVOffsetT(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetSOffsetT retrieves a SOffsetT at the given offset.
|
||||
func (t *Table) GetSOffsetT(off UOffsetT) SOffsetT {
|
||||
return GetSOffsetT(t.Bytes[off:])
|
||||
}
|
||||
|
||||
// GetBoolSlot retrieves the bool that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetBoolSlot(slot VOffsetT, d bool) bool {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetBool(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetByteSlot retrieves the byte that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetByteSlot(slot VOffsetT, d byte) byte {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetByte(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetInt8Slot retrieves the int8 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetInt8Slot(slot VOffsetT, d int8) int8 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetInt8(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetUint8Slot retrieves the uint8 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetUint8Slot(slot VOffsetT, d uint8) uint8 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetUint8(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetInt16Slot retrieves the int16 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetInt16Slot(slot VOffsetT, d int16) int16 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetInt16(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetUint16Slot retrieves the uint16 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetUint16Slot(slot VOffsetT, d uint16) uint16 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetUint16(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetInt32Slot retrieves the int32 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetInt32Slot(slot VOffsetT, d int32) int32 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetInt32(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetUint32Slot retrieves the uint32 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetUint32Slot(slot VOffsetT, d uint32) uint32 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetUint32(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetInt64Slot retrieves the int64 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetInt64Slot(slot VOffsetT, d int64) int64 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetInt64(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetUint64Slot retrieves the uint64 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetUint64Slot(slot VOffsetT, d uint64) uint64 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetUint64(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetFloat32Slot retrieves the float32 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetFloat32Slot(slot VOffsetT, d float32) float32 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetFloat32(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetFloat64Slot retrieves the float64 that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetFloat64Slot(slot VOffsetT, d float64) float64 {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
|
||||
return t.GetFloat64(t.Pos + UOffsetT(off))
|
||||
}
|
||||
|
||||
// GetVOffsetTSlot retrieves the VOffsetT that the given vtable location
|
||||
// points to. If the vtable value is zero, the default value `d`
|
||||
// will be returned.
|
||||
func (t *Table) GetVOffsetTSlot(slot VOffsetT, d VOffsetT) VOffsetT {
|
||||
off := t.Offset(slot)
|
||||
if off == 0 {
|
||||
return d
|
||||
}
|
||||
return VOffsetT(off)
|
||||
}
|
||||
45
go/unsafe.go
Normal file
45
go/unsafe.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package flatbuffers
|
||||
|
||||
import "unsafe"
|
||||
|
||||
var (
|
||||
// See http://golang.org/ref/spec#Numeric_types
|
||||
|
||||
// SizeUint8 is the byte size of a uint8.
|
||||
SizeUint8 = int(unsafe.Sizeof(uint8(0)))
|
||||
// SizeUint16 is the byte size of a uint16.
|
||||
SizeUint16 = int(unsafe.Sizeof(uint16(0)))
|
||||
// SizeUint32 is the byte size of a uint32.
|
||||
SizeUint32 = int(unsafe.Sizeof(uint32(0)))
|
||||
// SizeUint64 is the byte size of a uint64.
|
||||
SizeUint64 = int(unsafe.Sizeof(uint64(0)))
|
||||
|
||||
// SizeInt8 is the byte size of a int8.
|
||||
SizeInt8 = int(unsafe.Sizeof(int8(0)))
|
||||
// SizeInt16 is the byte size of a int16.
|
||||
SizeInt16 = int(unsafe.Sizeof(int16(0)))
|
||||
// SizeInt32 is the byte size of a int32.
|
||||
SizeInt32 = int(unsafe.Sizeof(int32(0)))
|
||||
// SizeInt64 is the byte size of a int64.
|
||||
SizeInt64 = int(unsafe.Sizeof(int64(0)))
|
||||
|
||||
// SizeFloat32 is the byte size of a float32.
|
||||
SizeFloat32 = int(unsafe.Sizeof(float32(0)))
|
||||
// SizeFloat64 is the byte size of a float64.
|
||||
SizeFloat64 = int(unsafe.Sizeof(float64(0)))
|
||||
|
||||
// SizeByte is the byte size of a byte.
|
||||
// The `byte` type is aliased (by Go definition) to uint8.
|
||||
SizeByte = SizeUint8
|
||||
|
||||
// SizeBool is the byte size of a bool.
|
||||
// The `bool` type is aliased (by flatbuffers convention) to uint8.
|
||||
SizeBool = SizeUint8
|
||||
|
||||
// SizeSOffsetT is the byte size of an SOffsetT.
|
||||
SizeSOffsetT = int(unsafe.Sizeof(SOffsetT(0)))
|
||||
// SizeUOffsetT is the byte size of an UOffsetT.
|
||||
SizeUOffsetT = int(unsafe.Sizeof(UOffsetT(0)))
|
||||
// SizeVOffsetT is the byte size of an VOffsetT.
|
||||
SizeVOffsetT = int(unsafe.Sizeof(VOffsetT(0)))
|
||||
)
|
||||
@@ -20,10 +20,12 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#if __cplusplus <= 199711L && \
|
||||
(!defined(_MSC_VER) || _MSC_VER < 1600) && \
|
||||
@@ -43,7 +45,11 @@
|
||||
#define FLATBUFFERS_LITTLEENDIAN 1
|
||||
#endif // __BIG_ENDIAN__
|
||||
#elif defined(_MSC_VER)
|
||||
#define FLATBUFFERS_LITTLEENDIAN 1
|
||||
#if defined(_M_PPC)
|
||||
#define FLATBUFFERS_LITTLEENDIAN 0
|
||||
#else
|
||||
#define FLATBUFFERS_LITTLEENDIAN 1
|
||||
#endif
|
||||
#else
|
||||
#error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
|
||||
#endif
|
||||
@@ -75,7 +81,7 @@ typedef uintmax_t largest_scalar_t;
|
||||
template<typename T> struct Offset {
|
||||
uoffset_t o;
|
||||
Offset() : o(0) {}
|
||||
explicit Offset(uoffset_t _o) : o(_o) {}
|
||||
Offset(uoffset_t _o) : o(_o) {}
|
||||
Offset<void> Union() const { return Offset<void>(o); }
|
||||
};
|
||||
|
||||
@@ -90,6 +96,14 @@ template<typename T> T EndianScalar(T t) {
|
||||
#if FLATBUFFERS_LITTLEENDIAN
|
||||
return t;
|
||||
#else
|
||||
#if defined(_MSC_VER)
|
||||
#pragma push_macro("__builtin_bswap16")
|
||||
#pragma push_macro("__builtin_bswap32")
|
||||
#pragma push_macro("__builtin_bswap64")
|
||||
#define __builtin_bswap16 _byteswap_ushort
|
||||
#define __builtin_bswap32 _byteswap_ulong
|
||||
#define __builtin_bswap64 _byteswap_uint64
|
||||
#endif
|
||||
// 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.
|
||||
@@ -106,6 +120,11 @@ template<typename T> T EndianScalar(T t) {
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
#if defined(_MSC_VER)
|
||||
#pragma pop_macro("__builtin_bswap16")
|
||||
#pragma pop_macro("__builtin_bswap32")
|
||||
#pragma pop_macro("__builtin_bswap64")
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -135,29 +154,99 @@ template<typename T> size_t AlignOf() {
|
||||
// (avoiding the need for a trailing return decltype)
|
||||
template<typename T> struct IndirectHelper {
|
||||
typedef T return_type;
|
||||
static const size_t element_stride = sizeof(T);
|
||||
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 const size_t element_stride = sizeof(uoffset_t);
|
||||
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)));
|
||||
return reinterpret_cast<return_type>(p + ReadScalar<uoffset_t>(p));
|
||||
}
|
||||
};
|
||||
template<typename T> struct IndirectHelper<const T *> {
|
||||
typedef const T &return_type;
|
||||
typedef const T *return_type;
|
||||
static const size_t element_stride = sizeof(T);
|
||||
static return_type Read(const uint8_t *p, uoffset_t i) {
|
||||
return *reinterpret_cast<const T *>(p + i * sizeof(T));
|
||||
return reinterpret_cast<const T *>(p + i * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
// An STL compatible iterator implementation for Vector below, effectively
|
||||
// calling Get() for every element.
|
||||
template<typename T, bool bConst>
|
||||
struct VectorIterator : public
|
||||
std::iterator < std::input_iterator_tag,
|
||||
typename std::conditional < bConst,
|
||||
const typename IndirectHelper<T>::return_type,
|
||||
typename IndirectHelper<T>::return_type > ::type, uoffset_t > {
|
||||
|
||||
typedef std::iterator<std::input_iterator_tag,
|
||||
typename std::conditional<bConst,
|
||||
const typename IndirectHelper<T>::return_type,
|
||||
typename IndirectHelper<T>::return_type>::type, uoffset_t> super_type;
|
||||
|
||||
public:
|
||||
VectorIterator(const uint8_t *data, uoffset_t i) :
|
||||
data_(data + IndirectHelper<T>::element_stride * i) {};
|
||||
VectorIterator(const VectorIterator &other) : data_(other.data_) {}
|
||||
VectorIterator(VectorIterator &&other) : data_(std::move(other.data_)) {}
|
||||
|
||||
VectorIterator &operator=(const VectorIterator &other) {
|
||||
data_ = other.data_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VectorIterator &operator=(VectorIterator &&other) {
|
||||
data_ = other.data_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const VectorIterator& other) const {
|
||||
return data_ == other.data_;
|
||||
}
|
||||
|
||||
bool operator!=(const VectorIterator& other) const {
|
||||
return data_ != other.data_;
|
||||
}
|
||||
|
||||
ptrdiff_t operator-(const VectorIterator& other) const {
|
||||
return (data_ - other.data_) / IndirectHelper<T>::element_stride;
|
||||
}
|
||||
|
||||
typename super_type::value_type operator *() const {
|
||||
return IndirectHelper<T>::Read(data_, 0);
|
||||
}
|
||||
|
||||
typename super_type::value_type operator->() const {
|
||||
return IndirectHelper<T>::Read(data_, 0);
|
||||
}
|
||||
|
||||
VectorIterator &operator++() {
|
||||
data_ += IndirectHelper<T>::element_stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VectorIterator operator++(int) {
|
||||
VectorIterator temp(data_);
|
||||
data_ += IndirectHelper<T>::element_stride;
|
||||
return temp;
|
||||
}
|
||||
|
||||
private:
|
||||
const uint8_t *data_;
|
||||
};
|
||||
|
||||
// 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:
|
||||
public:
|
||||
typedef VectorIterator<T, false> iterator;
|
||||
typedef VectorIterator<T, true> const_iterator;
|
||||
|
||||
uoffset_t Length() const { return EndianScalar(length_); }
|
||||
|
||||
typedef typename IndirectHelper<T>::return_type return_type;
|
||||
@@ -171,15 +260,22 @@ template<typename T> class Vector {
|
||||
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();
|
||||
iterator begin() { return iterator(Data(), 0); }
|
||||
const_iterator begin() const { return const_iterator(Data(), 0); }
|
||||
|
||||
iterator end() { return iterator(Data(), length_); }
|
||||
const_iterator end() const { return const_iterator(Data(), length_); }
|
||||
|
||||
// The raw data in little endian format. Use with care.
|
||||
const uint8_t *Data() const {
|
||||
return reinterpret_cast<const uint8_t *>(&length_ + 1);
|
||||
}
|
||||
|
||||
protected:
|
||||
// This class is only used to access pre-existing data. Don't ever
|
||||
// try to construct these manually.
|
||||
Vector();
|
||||
|
||||
uoffset_t length_;
|
||||
};
|
||||
|
||||
@@ -192,7 +288,7 @@ struct String : public Vector<char> {
|
||||
// in the lowest address in the vector.
|
||||
class vector_downward {
|
||||
public:
|
||||
explicit vector_downward(uoffset_t initial_size)
|
||||
explicit vector_downward(size_t initial_size)
|
||||
: reserved_(initial_size),
|
||||
buf_(new uint8_t[reserved_]),
|
||||
cur_(buf_ + reserved_) {
|
||||
@@ -203,11 +299,11 @@ class vector_downward {
|
||||
|
||||
void clear() { cur_ = buf_ + reserved_; }
|
||||
|
||||
uoffset_t growth_policy(uoffset_t size) {
|
||||
return (size / 2) & ~(sizeof(largest_scalar_t) - 1);
|
||||
size_t growth_policy(size_t bytes) {
|
||||
return (bytes / 2) & ~(sizeof(largest_scalar_t) - 1);
|
||||
}
|
||||
|
||||
uint8_t *make_space(uoffset_t len) {
|
||||
uint8_t *make_space(size_t len) {
|
||||
if (buf_ > cur_ - len) {
|
||||
auto old_size = size();
|
||||
reserved_ += std::max(len, growth_policy(reserved_));
|
||||
@@ -231,13 +327,13 @@ class vector_downward {
|
||||
|
||||
uint8_t *data() const { return cur_; }
|
||||
|
||||
uint8_t *data_at(uoffset_t offset) { return buf_ + reserved_ - offset; }
|
||||
uint8_t *data_at(size_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 push(const uint8_t *bytes, size_t num) {
|
||||
auto dest = make_space(num);
|
||||
for (size_t i = 0; i < num; i++) dest[i] = bytes[i];
|
||||
}
|
||||
|
||||
void fill(size_t zero_pad_bytes) {
|
||||
@@ -248,7 +344,11 @@ class vector_downward {
|
||||
void pop(size_t bytes_to_remove) { cur_ += bytes_to_remove; }
|
||||
|
||||
private:
|
||||
uoffset_t reserved_;
|
||||
// You shouldn't really be copying instances of this class.
|
||||
vector_downward(const vector_downward &);
|
||||
vector_downward &operator=(const vector_downward &);
|
||||
|
||||
size_t reserved_;
|
||||
uint8_t *buf_;
|
||||
uint8_t *cur_; // Points at location between empty (below) and used (above).
|
||||
};
|
||||
@@ -280,11 +380,6 @@ class FlatBufferBuilder {
|
||||
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
|
||||
@@ -301,8 +396,6 @@ class FlatBufferBuilder {
|
||||
// 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); }
|
||||
@@ -406,13 +499,13 @@ class FlatBufferBuilder {
|
||||
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>(static_cast<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);
|
||||
auto pos = static_cast<voffset_t>(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);
|
||||
@@ -497,38 +590,52 @@ class FlatBufferBuilder {
|
||||
template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
|
||||
NotNested();
|
||||
StartVector(len, sizeof(T));
|
||||
auto i = len;
|
||||
do {
|
||||
for (auto i = len; i > 0; ) {
|
||||
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());
|
||||
return CreateVector(v.data(), v.size());
|
||||
}
|
||||
|
||||
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||
const T *v, size_t len) {
|
||||
NotNested();
|
||||
StartVector(len, AlignOf<T>());
|
||||
StartVector(len * sizeof(T) / AlignOf<T>(), 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());
|
||||
return CreateVectorOfStructs(v.data(), v.size());
|
||||
}
|
||||
|
||||
static const size_t kFileIdentifierLength = 4;
|
||||
|
||||
// Finish serializing a buffer by writing the root offset.
|
||||
template<typename T> void Finish(Offset<T> root) {
|
||||
// If a file_identifier is given, the buffer will be prefix with a standard
|
||||
// FlatBuffers file header.
|
||||
template<typename T> void Finish(Offset<T> root,
|
||||
const char *file_identifier = nullptr) {
|
||||
// This will cause the whole buffer to be aligned.
|
||||
PreAlign(sizeof(uoffset_t), minalign_);
|
||||
PreAlign(sizeof(uoffset_t) + (file_identifier ? kFileIdentifierLength : 0),
|
||||
minalign_);
|
||||
if (file_identifier) {
|
||||
assert(strlen(file_identifier) == kFileIdentifierLength);
|
||||
buf_.push(reinterpret_cast<const uint8_t *>(file_identifier),
|
||||
kFileIdentifierLength);
|
||||
}
|
||||
PushElement(ReferTo(root.o)); // Location of root.
|
||||
}
|
||||
|
||||
private:
|
||||
// You shouldn't really be copying instances of this class.
|
||||
FlatBufferBuilder(const FlatBufferBuilder &);
|
||||
FlatBufferBuilder &operator=(const FlatBufferBuilder &);
|
||||
|
||||
struct FieldLoc {
|
||||
uoffset_t off;
|
||||
voffset_t id;
|
||||
@@ -544,16 +651,6 @@ class FlatBufferBuilder {
|
||||
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.
|
||||
@@ -563,7 +660,103 @@ template<typename T> const T *GetRoot(const void *buf) {
|
||||
EndianScalar(*reinterpret_cast<const uoffset_t *>(buf)));
|
||||
}
|
||||
|
||||
// "structs_" are flat structures that do not have an offset table, thus
|
||||
// Helper to see if the identifier in a buffer has the expected value.
|
||||
inline bool BufferHasIdentifier(const void *buf, const char *identifier) {
|
||||
return strncmp(reinterpret_cast<const char *>(buf) + 4, identifier,
|
||||
FlatBufferBuilder::kFileIdentifierLength) == 0;
|
||||
}
|
||||
|
||||
// Helper class to verify the integrity of a FlatBuffer
|
||||
class Verifier {
|
||||
public:
|
||||
Verifier(const uint8_t *buf, size_t buf_len)
|
||||
: buf_(buf), end_(buf + buf_len)
|
||||
{}
|
||||
|
||||
// Verify any range within the buffer.
|
||||
bool Verify(const void *elem, size_t elem_len) const {
|
||||
bool ok = elem >= buf_ && elem <= end_ - elem_len;
|
||||
#ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
|
||||
assert(ok);
|
||||
#endif
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Verify a range indicated by sizeof(T).
|
||||
template<typename T> bool Verify(const void *elem) const {
|
||||
return Verify(elem, sizeof(T));
|
||||
}
|
||||
|
||||
// Verify a pointer (may be NULL) of a table type.
|
||||
template<typename T> bool VerifyTable(const T *table) const {
|
||||
return !table || table->Verify(*this);
|
||||
}
|
||||
|
||||
// Verify a pointer (may be NULL) of any vector type.
|
||||
template<typename T> bool Verify(const Vector<T> *vec) const {
|
||||
const uint8_t *end;
|
||||
return !vec ||
|
||||
VerifyVector(reinterpret_cast<const uint8_t *>(vec), sizeof(T),
|
||||
&end);
|
||||
}
|
||||
|
||||
// Verify a pointer (may be NULL) to string.
|
||||
bool Verify(const String *str) const {
|
||||
const uint8_t *end;
|
||||
return !str ||
|
||||
(VerifyVector(reinterpret_cast<const uint8_t *>(str), 1, &end) &&
|
||||
Verify(end, 1) && // Must have terminator
|
||||
*end == '\0'); // Terminating byte must be 0.
|
||||
}
|
||||
|
||||
// Common code between vectors and strings.
|
||||
bool VerifyVector(const uint8_t *vec, size_t elem_size,
|
||||
const uint8_t **end) const {
|
||||
// Check we can read the size field.
|
||||
if (!Verify<uoffset_t>(vec)) return false;
|
||||
// Check the whole array. If this is a string, the byte past the array
|
||||
// must be 0.
|
||||
auto size = ReadScalar<uoffset_t>(vec);
|
||||
auto byte_size = sizeof(size) + elem_size * size;
|
||||
*end = vec + byte_size;
|
||||
return Verify(vec, byte_size);
|
||||
}
|
||||
|
||||
// Special case for string contents, after the above has been called.
|
||||
bool VerifyVectorOfStrings(const Vector<Offset<String>> *vec) const {
|
||||
if (vec) {
|
||||
for (uoffset_t i = 0; i < vec->Length(); i++) {
|
||||
if (!Verify(vec->Get(i))) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Special case for table contents, after the above has been called.
|
||||
template<typename T> bool VerifyVectorOfTables(const Vector<Offset<T>> *vec)
|
||||
const {
|
||||
if (vec) {
|
||||
for (uoffset_t i = 0; i < vec->Length(); i++) {
|
||||
if (!vec->Get(i)->Verify(*this)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Verify this whole buffer, starting with root type T.
|
||||
template<typename T> bool VerifyBuffer() const {
|
||||
// Call T::Verify, which must be in the generated code for this type.
|
||||
return Verify<uoffset_t>(buf_) &&
|
||||
reinterpret_cast<const T *>(buf_ + ReadScalar<uoffset_t>(buf_))->
|
||||
Verify(*this);
|
||||
}
|
||||
|
||||
private:
|
||||
const uint8_t *buf_;
|
||||
const uint8_t *end_;
|
||||
};
|
||||
|
||||
// "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.
|
||||
|
||||
@@ -594,7 +787,7 @@ class Table {
|
||||
// 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_);
|
||||
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
|
||||
@@ -604,12 +797,12 @@ class Table {
|
||||
|
||||
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;
|
||||
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];
|
||||
auto p = data_ + field_offset;
|
||||
return field_offset
|
||||
? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p))
|
||||
: nullptr;
|
||||
@@ -617,7 +810,7 @@ class Table {
|
||||
|
||||
template<typename P> P GetStruct(voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return field_offset ? reinterpret_cast<P>(&data_[field_offset]) : nullptr;
|
||||
return field_offset ? reinterpret_cast<P>(data_ + field_offset) : nullptr;
|
||||
}
|
||||
|
||||
template<typename T> void SetField(voffset_t field, T val) {
|
||||
@@ -626,18 +819,39 @@ class Table {
|
||||
// (or should we return a bool instead?).
|
||||
// check if it exists first using CheckField()
|
||||
assert(field_offset);
|
||||
WriteScalar(&data_[field_offset], val);
|
||||
WriteScalar(data_ + field_offset, val);
|
||||
}
|
||||
|
||||
bool CheckField(voffset_t field) const {
|
||||
return GetOptionalFieldOffset(field) != 0;
|
||||
}
|
||||
|
||||
// Verify the vtable of this table.
|
||||
// Call this once per table, followed by VerifyField once per field.
|
||||
bool VerifyTable(const Verifier &verifier) const {
|
||||
// Check the vtable offset.
|
||||
if (!verifier.Verify<soffset_t>(data_)) return false;
|
||||
auto vtable = data_ - ReadScalar<soffset_t>(data_);
|
||||
// Check the vtable size field, then check vtable fits in its entirety.
|
||||
return verifier.Verify<voffset_t>(vtable) &&
|
||||
verifier.Verify(vtable, ReadScalar<voffset_t>(vtable));
|
||||
}
|
||||
|
||||
// Verify a particular field.
|
||||
template<typename T> bool VerifyField(const Verifier &verifier,
|
||||
voffset_t field) const {
|
||||
// Calling GetOptionalFieldOffset should be safe now thanks to
|
||||
// VerifyTable().
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
// Check the actual field.
|
||||
return !field_offset || verifier.Verify<T>(data_ + field_offset);
|
||||
}
|
||||
|
||||
private:
|
||||
// private constructor & copy constructor: you obtain instances of this
|
||||
// class by pointing to existing data only
|
||||
Table() {};
|
||||
Table(const Table &other) {};
|
||||
Table();
|
||||
Table(const Table &other);
|
||||
|
||||
uint8_t data_[1];
|
||||
};
|
||||
@@ -645,10 +859,10 @@ class Table {
|
||||
// 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) {
|
||||
inline int LookupEnum(const char **names, const char *name) {
|
||||
for (const char **p = names; *p; p++)
|
||||
if (!strcmp(*p, name))
|
||||
return p - names;
|
||||
return static_cast<int>(p - names);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -668,18 +882,39 @@ inline size_t LookupEnum(const char **names, const char *name) {
|
||||
struct __declspec(align(alignment))
|
||||
#define STRUCT_END(name, size) \
|
||||
__pragma(pack()); \
|
||||
static_assert(sizeof(name) == size, "compiler breaks packing rules");
|
||||
static_assert(sizeof(name) == size, "compiler breaks packing rules")
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#define MANUALLY_ALIGNED_STRUCT(alignment) \
|
||||
_Pragma("pack(1)"); \
|
||||
_Pragma("pack(1)") \
|
||||
struct __attribute__((aligned(alignment)))
|
||||
#define STRUCT_END(name, size) \
|
||||
_Pragma("pack()"); \
|
||||
static_assert(sizeof(name) == size, "compiler breaks packing rules");
|
||||
_Pragma("pack()") \
|
||||
static_assert(sizeof(name) == size, "compiler breaks packing rules")
|
||||
#else
|
||||
#error Unknown compiler, please define structure alignment macros
|
||||
#endif
|
||||
|
||||
// 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.
|
||||
|
||||
// Weak linkage is culled by VS & doesn't work on cygwin.
|
||||
#if !defined(_WIN32) && !defined(__CYGWIN__)
|
||||
|
||||
extern volatile __attribute__((weak)) const char *flatbuffer_version_string;
|
||||
volatile __attribute__((weak)) const char *flatbuffer_version_string =
|
||||
"FlatBuffers "
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
|
||||
|
||||
#endif // !defined(_WIN32) && !defined(__CYGWIN__)
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_H_
|
||||
|
||||
@@ -31,31 +31,31 @@ namespace flatbuffers {
|
||||
// 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 */
|
||||
TD(NONE, "", uint8_t, byte, byte) \
|
||||
TD(UTYPE, "", uint8_t, byte, byte) /* begin scalars, ints */ \
|
||||
TD(BOOL, "bool", uint8_t, byte, byte) \
|
||||
TD(CHAR, "byte", int8_t, byte, int8) \
|
||||
TD(UCHAR, "ubyte", uint8_t, byte, byte) \
|
||||
TD(SHORT, "short", int16_t, short, int16) \
|
||||
TD(USHORT, "ushort", uint16_t, short, uint16) \
|
||||
TD(INT, "int", int32_t, int, int32) \
|
||||
TD(UINT, "uint", uint32_t, int, uint32) \
|
||||
TD(LONG, "long", int64_t, long, int64) \
|
||||
TD(ULONG, "ulong", uint64_t, long, uint64) /* end ints */ \
|
||||
TD(FLOAT, "float", float, float, float32) /* begin floats */ \
|
||||
TD(DOUBLE, "double", double, double, float64) /* 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)
|
||||
TD(STRING, "string", Offset<void>, int, int) \
|
||||
TD(VECTOR, "", Offset<void>, int, int) \
|
||||
TD(STRUCT, "", Offset<void>, int, int) \
|
||||
TD(UNION, "", Offset<void>, int, 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) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
// do something specific to CTYPE here
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
@@ -67,14 +67,17 @@ switch (type) {
|
||||
FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
|
||||
FLATBUFFERS_GEN_TYPES_POINTER(TD)
|
||||
|
||||
// Create an enum for all the types above
|
||||
// Create an enum for all the types above.
|
||||
#ifdef __GNUC__
|
||||
__extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
|
||||
#endif
|
||||
enum BaseType {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) BASE_TYPE_ ## ENUM,
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) BASE_TYPE_ ## ENUM,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
static_assert(sizeof(CTYPE) <= sizeof(largest_scalar_t), \
|
||||
"define largest_scalar_t as " #CTYPE);
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
@@ -100,28 +103,30 @@ 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)
|
||||
explicit Type(BaseType _base_type = BASE_TYPE_NONE,
|
||||
StructDef *_sd = nullptr, EnumDef *_ed = nullptr)
|
||||
: base_type(_base_type),
|
||||
element(BASE_TYPE_NONE),
|
||||
struct_def(_sd),
|
||||
enum_def(nullptr)
|
||||
enum_def(_ed)
|
||||
{}
|
||||
|
||||
Type VectorType() const { return Type(element, struct_def); }
|
||||
Type VectorType() const { return Type(element, struct_def, enum_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
|
||||
EnumDef *enum_def; // set if t == BASE_TYPE_UNION / BASE_TYPE_UTYPE,
|
||||
// or for an integral type derived from an enum.
|
||||
};
|
||||
|
||||
// Represents a parsed scalar value, it's type, and field offset.
|
||||
struct Value {
|
||||
Value() : constant("0"), offset(-1) {}
|
||||
|
||||
Value() : constant("0"), offset(static_cast<voffset_t>(
|
||||
~(static_cast<voffset_t>(0U)))) {}
|
||||
Type type;
|
||||
std::string constant;
|
||||
int offset;
|
||||
voffset_t offset;
|
||||
};
|
||||
|
||||
// Helper class that retains the original order of a set of identifiers and
|
||||
@@ -208,23 +213,22 @@ inline size_t InlineAlignment(const Type &type) {
|
||||
}
|
||||
|
||||
struct EnumVal {
|
||||
EnumVal(const std::string &_name, int _val)
|
||||
EnumVal(const std::string &_name, int64_t _val)
|
||||
: name(_name), value(_val), struct_def(nullptr) {}
|
||||
|
||||
std::string name;
|
||||
std::string doc_comment;
|
||||
int value;
|
||||
int64_t 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);
|
||||
EnumVal *ReverseLookup(int enum_idx) {
|
||||
for (auto it = vals.vec.begin() + 1; it != vals.vec.end(); ++it) {
|
||||
if ((*it)->value == enum_idx) {
|
||||
return (*it)->struct_def;
|
||||
return *it;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
@@ -267,6 +271,7 @@ class Parser {
|
||||
void ParseMetaData(Definition &def);
|
||||
bool TryTypedValue(int dtoken, bool check, Value &e, BaseType req);
|
||||
void ParseSingleValue(Value &e);
|
||||
int64_t ParseIntegerFromString(Type &type);
|
||||
StructDef *LookupCreateStruct(const std::string &name);
|
||||
void ParseEnum(bool is_union);
|
||||
void ParseDecl();
|
||||
@@ -279,6 +284,8 @@ class Parser {
|
||||
|
||||
FlatBufferBuilder builder_; // any data contained in the file
|
||||
StructDef *root_struct_def;
|
||||
std::string file_identifier_;
|
||||
std::string file_extension_;
|
||||
|
||||
private:
|
||||
const char *source_, *cursor_;
|
||||
@@ -290,26 +297,66 @@ class Parser {
|
||||
std::vector<uint8_t> struct_stack_;
|
||||
};
|
||||
|
||||
// Utility functions for generators:
|
||||
|
||||
// Convert an underscore_based_indentifier in to camelCase.
|
||||
// Also uppercases the first character if first is true.
|
||||
inline 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 += static_cast<char>(toupper(in[0]));
|
||||
else if (in[i] == '_' && i + 1 < in.length())
|
||||
s += static_cast<char>(toupper(in[++i]));
|
||||
else
|
||||
s += in[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// Container of options that may apply to any of the source/text generators.
|
||||
struct GeneratorOptions {
|
||||
bool strict_json;
|
||||
int indent_step;
|
||||
bool output_enum_identifiers;
|
||||
|
||||
GeneratorOptions() : strict_json(false), indent_step(2),
|
||||
output_enum_identifiers(true) {}
|
||||
};
|
||||
|
||||
// Generate text (JSON) from a given FlatBuffer, and a given Parser
|
||||
// object that has been populated with the corresponding schema.
|
||||
// If ident_step is 0, no indentation will be generated. Additionally,
|
||||
// if it is less than 0, no linefeeds will be generated either.
|
||||
// See idl_gen_text.cpp.
|
||||
// strict_json adds "quotes" around field names if true.
|
||||
extern void GenerateText(const Parser &parser,
|
||||
const void *flatbuffer,
|
||||
int indent_step,
|
||||
const GeneratorOptions &opts,
|
||||
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 std::string GenerateCPP(const Parser &parser,
|
||||
const std::string &include_guard_ident);
|
||||
extern bool GenerateCPP(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// Generate Go files from the definitions in the Parser object.
|
||||
// See idl_gen_go.cpp.
|
||||
extern bool GenerateGo(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
// 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);
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
||||
@@ -25,6 +25,13 @@
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
static const char kPosixPathSeparator = '/';
|
||||
#ifdef _WIN32
|
||||
static const char kPathSeparator = '\\';
|
||||
#else
|
||||
static const char kPathSeparator = kPosixPathSeparator;
|
||||
#endif // _WIN32
|
||||
|
||||
// Convert an integer or floating point value to a string.
|
||||
// In contrast to std::stringstream, "char" values are
|
||||
// converted to a string of digits.
|
||||
@@ -32,10 +39,16 @@ 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.
|
||||
ss << t;
|
||||
return ss.str();
|
||||
}
|
||||
// Avoid char types used as character data.
|
||||
template<> inline std::string NumToString<signed char>(signed char t) {
|
||||
return NumToString(static_cast<int>(t));
|
||||
}
|
||||
template<> inline std::string NumToString<unsigned char>(unsigned char t) {
|
||||
return NumToString(static_cast<int>(t));
|
||||
}
|
||||
|
||||
// Convert an integer value to a hexadecimal string.
|
||||
// The returned string length is the number of nibbles in
|
||||
|
||||
@@ -28,7 +28,7 @@ import java.nio.charset.Charset;
|
||||
public class FlatBufferBuilder {
|
||||
ByteBuffer bb; // Where we construct the FlatBuffer.
|
||||
int space; // Remaining space in the ByteBuffer.
|
||||
final Charset utf8charset = Charset.forName("UTF-8");
|
||||
static 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.
|
||||
@@ -42,6 +42,7 @@ public class FlatBufferBuilder {
|
||||
|
||||
// Start with a buffer of size `initial_size`, then grow as required.
|
||||
public FlatBufferBuilder(int initial_size) {
|
||||
if (initial_size <= 0) initial_size = 1;
|
||||
space = initial_size;
|
||||
bb = newByteBuffer(new byte[initial_size]);
|
||||
}
|
||||
@@ -57,7 +58,9 @@ public class FlatBufferBuilder {
|
||||
ByteBuffer growByteBuffer(ByteBuffer bb) {
|
||||
byte[] old_buf = bb.array();
|
||||
int old_buf_size = old_buf.length;
|
||||
int new_buf_size = old_buf_size * 2;
|
||||
if ((old_buf_size & 0xC0000000) != 0) // Ensure we don't grow beyond what fits in an int.
|
||||
throw new AssertionError("FlatBuffers: cannot grow buffer beyond 2 gigabytes.");
|
||||
int new_buf_size = old_buf_size << 1;
|
||||
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);
|
||||
@@ -120,10 +123,11 @@ public class FlatBufferBuilder {
|
||||
putInt(off);
|
||||
}
|
||||
|
||||
public void startVector(int elem_size, int num_elems) {
|
||||
public void startVector(int elem_size, int num_elems, int alignment) {
|
||||
notNested();
|
||||
vector_num_elems = num_elems;
|
||||
prep(SIZEOF_INT, elem_size * num_elems);
|
||||
prep(alignment, elem_size * num_elems); // Just in case alignment > int.
|
||||
}
|
||||
|
||||
public int endVector() {
|
||||
@@ -133,8 +137,8 @@ public class FlatBufferBuilder {
|
||||
|
||||
public int createString(String s) {
|
||||
byte[] utf8 = s.getBytes(utf8charset);
|
||||
bb.put(--space, (byte)0);
|
||||
startVector(1, utf8.length);
|
||||
addByte((byte)0);
|
||||
startVector(1, utf8.length, 1);
|
||||
System.arraycopy(utf8, 0, bb.array(), space -= utf8.length, utf8.length);
|
||||
return endVector();
|
||||
}
|
||||
@@ -190,12 +194,12 @@ public class FlatBufferBuilder {
|
||||
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);
|
||||
addShort(off);
|
||||
}
|
||||
|
||||
final int standard_fields = 2; // The fields below:
|
||||
putShort((short)(vtableloc - object_start));
|
||||
putShort((short)((vtable.length + standard_fields) * SIZEOF_SHORT));
|
||||
addShort((short)(vtableloc - object_start));
|
||||
addShort((short)((vtable.length + standard_fields) * SIZEOF_SHORT));
|
||||
|
||||
// Search for an existing vtable that matches the current one.
|
||||
int existing_vtable = 0;
|
||||
@@ -243,6 +247,11 @@ public class FlatBufferBuilder {
|
||||
|
||||
// The FlatBuffer data doesn't start at offset 0 in the ByteBuffer:
|
||||
public int dataStart() {
|
||||
return bb.array().length - offset();
|
||||
return space;
|
||||
}
|
||||
|
||||
// Utility function for copying a byte array that starts at 0.
|
||||
public byte[] sizedByteArray() {
|
||||
return Arrays.copyOfRange(bb.array(), dataStart(), bb.array().length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ public class Table {
|
||||
|
||||
// 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"));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
// automatically generated, do not modify
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
#ifndef FLATBUFFERS_GENERATED_MONSTER_MYGAME_SAMPLE_H_
|
||||
#define FLATBUFFERS_GENERATED_MONSTER_MYGAME_SAMPLE_H_
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
@@ -8,7 +11,7 @@ namespace Sample {
|
||||
enum {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2,
|
||||
Color_Blue = 2
|
||||
};
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
@@ -20,7 +23,7 @@ inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
|
||||
enum {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1,
|
||||
Any_Monster = 1
|
||||
};
|
||||
|
||||
inline const char **EnumNamesAny() {
|
||||
@@ -30,6 +33,8 @@ inline const char **EnumNamesAny() {
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
|
||||
bool VerifyAny(const flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type);
|
||||
|
||||
struct Vec3;
|
||||
struct Monster;
|
||||
|
||||
@@ -56,6 +61,17 @@ struct Monster : private flatbuffers::Table {
|
||||
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); }
|
||||
bool Verify(const flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTable(verifier) &&
|
||||
VerifyField<Vec3>(verifier, 4 /* pos */) &&
|
||||
VerifyField<int16_t>(verifier, 6 /* mana */) &&
|
||||
VerifyField<int16_t>(verifier, 8 /* hp */) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 10 /* name */) &&
|
||||
verifier.Verify(name()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 14 /* inventory */) &&
|
||||
verifier.Verify(inventory()) &&
|
||||
VerifyField<int8_t>(verifier, 16 /* color */);
|
||||
}
|
||||
};
|
||||
|
||||
struct MonsterBuilder {
|
||||
@@ -68,10 +84,17 @@ struct MonsterBuilder {
|
||||
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(); }
|
||||
MonsterBuilder &operator=(const MonsterBuilder &);
|
||||
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) {
|
||||
inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb,
|
||||
const Vec3 *pos = 0,
|
||||
int16_t mana = 150,
|
||||
int16_t hp = 100,
|
||||
flatbuffers::Offset<flatbuffers::String> name = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory = 0,
|
||||
int8_t color = 2) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_inventory(inventory);
|
||||
builder_.add_name(name);
|
||||
@@ -82,7 +105,19 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
bool VerifyAny(const flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type) {
|
||||
switch (type) {
|
||||
case Any_NONE: return true;
|
||||
case Any_Monster: return verifier.VerifyTable(reinterpret_cast<const Monster *>(union_obj));
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
|
||||
|
||||
}; // namespace MyGame
|
||||
}; // namespace Sample
|
||||
inline bool VerifyMonsterBuffer(const flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer<Monster>(); }
|
||||
|
||||
} // namespace Sample
|
||||
} // namespace MyGame
|
||||
|
||||
#endif // FLATBUFFERS_GENERATED_MONSTER_MYGAME_SAMPLE_H_
|
||||
|
||||
@@ -22,7 +22,7 @@ using namespace MyGame::Sample;
|
||||
|
||||
// Example how to use FlatBuffers to create and read binary buffers.
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
// Build up a serialized buffer algorithmically:
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ 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[]) {
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
@@ -46,7 +46,8 @@ int main(int argc, const char *argv[]) {
|
||||
// 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);
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(),
|
||||
flatbuffers::GeneratorOptions(), &jsongen);
|
||||
|
||||
if (jsongen != jsonfile) {
|
||||
printf("%s----------------\n%s", jsongen.c_str(), jsonfile.c_str());
|
||||
|
||||
@@ -18,16 +18,19 @@
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
void Error(const char *err, const char *obj = nullptr, bool usage = false);
|
||||
static 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) {
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin";
|
||||
return !parser.builder_.GetSize() ||
|
||||
flatbuffers::SaveFile(
|
||||
(path + file_name + "_wire.bin").c_str(),
|
||||
(path + file_name + "." + ext).c_str(),
|
||||
reinterpret_cast<char *>(parser.builder_.GetBufferPointer()),
|
||||
parser.builder_.GetSize(),
|
||||
true);
|
||||
@@ -35,12 +38,14 @@ bool GenerateBinary(const Parser &parser,
|
||||
|
||||
bool GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts) {
|
||||
if (!parser.builder_.GetSize()) return true;
|
||||
if (!parser.root_struct_def) Error("root_type not set");
|
||||
std::string text;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), 2, &text);
|
||||
return flatbuffers::SaveFile((path + file_name + "_wire.txt").c_str(),
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), opts,
|
||||
&text);
|
||||
return flatbuffers::SaveFile((path + file_name + ".json").c_str(),
|
||||
text,
|
||||
false);
|
||||
|
||||
@@ -53,7 +58,8 @@ bool GenerateTextFile(const Parser &parser,
|
||||
struct Generator {
|
||||
bool (*generate)(const flatbuffers::Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
const std::string &file_name,
|
||||
const flatbuffers::GeneratorOptions &opts);
|
||||
const char *extension;
|
||||
const char *name;
|
||||
const char *help;
|
||||
@@ -66,22 +72,26 @@ const Generator generators[] = {
|
||||
"Generate text output for any data definitions" },
|
||||
{ flatbuffers::GenerateCPP, "c", "C++",
|
||||
"Generate C++ headers for tables/structs" },
|
||||
{ flatbuffers::GenerateGo, "g", "Go",
|
||||
"Generate Go files 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);
|
||||
static void Error(const char *err, const char *obj, bool usage) {
|
||||
printf("%s: %s", program_name, err);
|
||||
if (obj) printf(": %s", obj);
|
||||
printf("\n");
|
||||
if (usage) {
|
||||
printf("usage: %s [OPTION]... FILE...\n", program_name);
|
||||
printf("usage: %s [OPTION]... FILE... [-- FILE...]\n", program_name);
|
||||
for (size_t i = 0; i < sizeof(generators) / sizeof(generators[0]); ++i)
|
||||
printf(" -%s %s.\n", generators[i].extension, generators[i].help);
|
||||
printf(" -o PATH Prefix PATH to all generated files.\n"
|
||||
" -S Strict JSON: add quotes to field names.\n"
|
||||
"FILEs may depend on declarations in earlier files.\n"
|
||||
"FILEs after the -- must be binary flatbuffer format files.\n"
|
||||
"Output files are named using the base file name of the input,"
|
||||
"and written to the current directory or the path given by -o.\n"
|
||||
"example: %s -c -b schema1.fbs schema2.fbs data.json\n",
|
||||
@@ -95,18 +105,31 @@ std::string StripExtension(const std::string &filename) {
|
||||
return i != std::string::npos ? filename.substr(0, i) : filename;
|
||||
}
|
||||
|
||||
std::string StripPath(const std::string &filename) {
|
||||
size_t i = filename.find_last_of(
|
||||
#ifdef _WIN32
|
||||
"\\:"
|
||||
#else
|
||||
"/"
|
||||
#endif
|
||||
);
|
||||
return i != std::string::npos ? filename.substr(i + 1) : filename;
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
program_name = argv[0];
|
||||
flatbuffers::Parser parser;
|
||||
flatbuffers::GeneratorOptions opts;
|
||||
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;
|
||||
size_t binary_files_from = std::numeric_limits<size_t>::max();
|
||||
for (int i = 1; i < argc; i++) {
|
||||
const char *arg = argv[i];
|
||||
if (arg[0] == '-') {
|
||||
if (filenames.size())
|
||||
if (filenames.size() && arg[1] != '-')
|
||||
Error("invalid option location", arg, true);
|
||||
if (strlen(arg) != 2)
|
||||
Error("invalid commandline argument", arg, true);
|
||||
@@ -114,6 +137,16 @@ int main(int argc, const char *argv[]) {
|
||||
case 'o':
|
||||
if (++i >= argc) Error("missing path following", arg, true);
|
||||
output_path = argv[i];
|
||||
if (!(output_path.back() == flatbuffers::kPathSeparator ||
|
||||
output_path.back() == flatbuffers::kPosixPathSeparator)) {
|
||||
output_path += flatbuffers::kPathSeparator;
|
||||
}
|
||||
break;
|
||||
case 'S':
|
||||
opts.strict_json = true;
|
||||
break;
|
||||
case '-': // Separator between text and binary input files.
|
||||
binary_files_from = filenames.size();
|
||||
break;
|
||||
default:
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
@@ -136,7 +169,7 @@ int main(int argc, const char *argv[]) {
|
||||
|
||||
if (!any_generator)
|
||||
Error("no options: no output files generated.",
|
||||
"specify one of -c -j -t -b etc.", true);
|
||||
"specify one of -c -g -j -t -b etc.", true);
|
||||
|
||||
// Now process the files:
|
||||
for (auto file_it = filenames.begin();
|
||||
@@ -146,14 +179,23 @@ int main(int argc, const char *argv[]) {
|
||||
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());
|
||||
bool is_binary = static_cast<size_t>(file_it - filenames.begin()) >=
|
||||
binary_files_from;
|
||||
if (is_binary) {
|
||||
parser.builder_.Clear();
|
||||
parser.builder_.PushBytes(
|
||||
reinterpret_cast<const uint8_t *>(contents.c_str()),
|
||||
contents.length());
|
||||
} else {
|
||||
if (!parser.Parse(contents.c_str()))
|
||||
Error(parser.error_.c_str());
|
||||
}
|
||||
|
||||
std::string filebase = StripExtension(*file_it);
|
||||
std::string filebase = StripPath(StripExtension(*file_it));
|
||||
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
if (generator_enabled[i]) {
|
||||
if (!generators[i].generate(parser, output_path, filebase)) {
|
||||
if (!generators[i].generate(parser, output_path, filebase, opts)) {
|
||||
Error((std::string("Unable to generate ") +
|
||||
generators[i].name +
|
||||
" for " +
|
||||
@@ -177,4 +219,3 @@ int main(int argc, const char *argv[]) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ 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,
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) #CTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
@@ -62,6 +62,16 @@ static std::string GenTypeWire(const Type &type, const char *postfix) {
|
||||
: "flatbuffers::Offset<" + GenTypePointer(type) + ">" + postfix;
|
||||
}
|
||||
|
||||
// Return a C++ type for any type (scalar/pointer) that reflects its
|
||||
// serialized size.
|
||||
static std::string GenTypeSize(const Type &type) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
: IsStruct(type)
|
||||
? GenTypePointer(type)
|
||||
: "flatbuffers::uoffset_t";
|
||||
}
|
||||
|
||||
// Return a C++ type for any type (scalar/pointer) specifically for
|
||||
// using a flatbuffer.
|
||||
static std::string GenTypeGet(const Type &type, const char *afterbasic,
|
||||
@@ -82,9 +92,11 @@ static void GenComment(const std::string &dc,
|
||||
}
|
||||
|
||||
// Generate an enum declaration and an enum string lookup table.
|
||||
static void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
|
||||
static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
|
||||
std::string *code_ptr_post) {
|
||||
if (enum_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
std::string &code_post = *code_ptr_post;
|
||||
GenComment(enum_def.doc_comment, code_ptr);
|
||||
code += "enum {\n";
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
@@ -93,7 +105,8 @@ static void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
|
||||
auto &ev = **it;
|
||||
GenComment(ev.doc_comment, code_ptr, " ");
|
||||
code += " " + enum_def.name + "_" + ev.name + " = ";
|
||||
code += NumToString(ev.value) + ",\n";
|
||||
code += NumToString(ev.value);
|
||||
code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n";
|
||||
}
|
||||
code += "};\n\n";
|
||||
|
||||
@@ -101,15 +114,15 @@ static void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
|
||||
// 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;
|
||||
auto 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) {
|
||||
if (range / static_cast<int64_t>(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;
|
||||
auto val = enum_def.vals.vec.front()->value;
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
@@ -123,10 +136,37 @@ static void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
|
||||
code += " - " + enum_def.name + "_" + enum_def.vals.vec.front()->name;
|
||||
code += "]; }\n\n";
|
||||
}
|
||||
|
||||
if (enum_def.is_union) {
|
||||
// Generate a verifier function for this union that can be called by the
|
||||
// table verifier functions. It uses a switch case to select a specific
|
||||
// verifier function to call, this should be safe even if the union type
|
||||
// has been corrupted, since the verifiers will simply fail when called
|
||||
// on the wrong type.
|
||||
auto signature = "bool Verify" + enum_def.name +
|
||||
"(const flatbuffers::Verifier &verifier, " +
|
||||
"const void *union_obj, uint8_t type)";
|
||||
code += signature + ";\n\n";
|
||||
code_post += signature + " {\n switch (type) {\n";
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
code_post += " case " + enum_def.name + "_" + ev.name;
|
||||
if (!ev.value) {
|
||||
code_post += ": return true;\n"; // "NONE" enum value.
|
||||
} else {
|
||||
code_post += ": return verifier.VerifyTable(reinterpret_cast<const ";
|
||||
code_post += ev.struct_def->name + " *>(union_obj));\n";
|
||||
}
|
||||
}
|
||||
code_post += " default: return false;\n }\n}\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Generate an accessor struct, builder structs & function for a table.
|
||||
static void GenTable(StructDef &struct_def, std::string *code_ptr) {
|
||||
static void GenTable(const Parser &parser, StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
if (struct_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
@@ -153,8 +193,68 @@ static void GenTable(StructDef &struct_def, std::string *code_ptr) {
|
||||
if (IsScalar(field.value.type.base_type))
|
||||
code += ", " + field.value.constant;
|
||||
code += "); }\n";
|
||||
auto nested = field.attributes.Lookup("nested_flatbuffer");
|
||||
if (nested) {
|
||||
auto nested_root = parser.structs_.Lookup(nested->constant);
|
||||
assert(nested_root); // Guaranteed to exist by parser.
|
||||
code += " const " + nested_root->name + " *" + field.name;
|
||||
code += "_nested_root() { return flatbuffers::GetRoot<";
|
||||
code += nested_root->name + ">(" + field.name + "()->Data()); }\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
// Generate a verifier function that can check a buffer from an untrusted
|
||||
// source will never cause reads outside the buffer.
|
||||
code += " bool Verify(const flatbuffers::Verifier &verifier) const {\n";
|
||||
code += " return VerifyTable(verifier)";
|
||||
std::string prefix = " &&\n ";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) {
|
||||
code += prefix + "VerifyField<" + GenTypeSize(field.value.type);
|
||||
code += ">(verifier, " + NumToString(field.value.offset);
|
||||
code += " /* " + field.name + " */)";
|
||||
switch (field.value.type.base_type) {
|
||||
case BASE_TYPE_UNION:
|
||||
code += prefix + "Verify" + field.value.type.enum_def->name;
|
||||
code += "(verifier, " + field.name + "(), " + field.name + "_type())";
|
||||
break;
|
||||
case BASE_TYPE_STRUCT:
|
||||
if (!field.value.type.struct_def->fixed) {
|
||||
code += prefix + "verifier.VerifyTable(" + field.name;
|
||||
code += "())";
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
code += prefix + "verifier.Verify(" + field.name + "())";
|
||||
break;
|
||||
case BASE_TYPE_VECTOR:
|
||||
code += prefix + "verifier.Verify(" + field.name + "())";
|
||||
switch (field.value.type.element) {
|
||||
case BASE_TYPE_STRING: {
|
||||
code += prefix + "verifier.VerifyVectorOfStrings(" + field.name;
|
||||
code += "())";
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_STRUCT: {
|
||||
if (!field.value.type.struct_def->fixed) {
|
||||
code += prefix + "verifier.VerifyVectorOfTables(" + field.name;
|
||||
code += "())";
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
code += ";\n }\n";
|
||||
code += "};\n\n";
|
||||
|
||||
// Generate a builder struct, with methods of the form:
|
||||
@@ -184,6 +284,8 @@ static void GenTable(StructDef &struct_def, std::string *code_ptr) {
|
||||
code += " " + struct_def.name;
|
||||
code += "Builder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) ";
|
||||
code += "{ start_ = fbb_.StartTable(); }\n";
|
||||
code += " " + struct_def.name + "Builder &operator=(const ";
|
||||
code += struct_def.name + "Builder &);\n";
|
||||
code += " flatbuffers::Offset<" + struct_def.name;
|
||||
code += "> Finish() { return flatbuffers::Offset<" + struct_def.name;
|
||||
code += ">(fbb_.EndTable(start_, ";
|
||||
@@ -199,7 +301,8 @@ static void GenTable(StructDef &struct_def, std::string *code_ptr) {
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (!field.deprecated) {
|
||||
code += ", " + GenTypeWire(field.value.type, " ") + field.name;
|
||||
code += ",\n " + GenTypeWire(field.value.type, " ") + field.name;
|
||||
code += " = " + field.value.constant;
|
||||
}
|
||||
}
|
||||
code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n";
|
||||
@@ -298,14 +401,14 @@ static void GenStruct(StructDef &struct_def, std::string *code_ptr) {
|
||||
|
||||
// Iterate through all definitions we haven't generate code for (enums, structs,
|
||||
// and tables) and output them to a single file.
|
||||
std::string GenerateCPP(const Parser &parser) {
|
||||
std::string GenerateCPP(const Parser &parser, const std::string &include_guard_ident) {
|
||||
using namespace cpp;
|
||||
|
||||
// Generate code for all the enum declarations.
|
||||
std::string enum_code;
|
||||
std::string enum_code, enum_code_post;
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
GenEnum(**it, &enum_code);
|
||||
GenEnum(**it, &enum_code, &enum_code_post);
|
||||
}
|
||||
|
||||
// Generate forward declarations for all structs/tables, since they may
|
||||
@@ -325,34 +428,85 @@ std::string GenerateCPP(const Parser &parser) {
|
||||
}
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
if (!(**it).fixed) GenTable(**it, &decl_code);
|
||||
if (!(**it).fixed) GenTable(parser, **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 = "// automatically generated by the FlatBuffers compiler,"
|
||||
" do not modify\n\n";
|
||||
|
||||
// Generate include guard.
|
||||
std::string include_guard = "FLATBUFFERS_GENERATED_" + include_guard_ident;
|
||||
include_guard += "_";
|
||||
for (auto it = parser.name_space_.begin();
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
include_guard += *it + "_";
|
||||
}
|
||||
include_guard += "H_";
|
||||
std::transform(include_guard.begin(), include_guard.end(),
|
||||
include_guard.begin(), ::toupper);
|
||||
code += "#ifndef " + include_guard + "\n";
|
||||
code += "#define " + include_guard + "\n\n";
|
||||
|
||||
code += "#include \"flatbuffers/flatbuffers.h\"\n\n";
|
||||
|
||||
// Generate nested namespaces.
|
||||
for (auto it = parser.name_space_.begin();
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
code += "namespace " + *it + " {\n";
|
||||
}
|
||||
|
||||
// Output the main declaration code from above.
|
||||
code += "\n";
|
||||
code += enum_code;
|
||||
code += forward_decl_code;
|
||||
code += "\n";
|
||||
code += decl_code;
|
||||
code += enum_code_post;
|
||||
|
||||
// Generate convenient global helper functions:
|
||||
if (parser.root_struct_def) {
|
||||
// The root datatype accessor:
|
||||
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";
|
||||
|
||||
// The root verifier:
|
||||
code += "inline bool Verify";
|
||||
code += parser.root_struct_def->name;
|
||||
code += "Buffer(const flatbuffers::Verifier &verifier) { "
|
||||
"return verifier.VerifyBuffer<";
|
||||
code += parser.root_struct_def->name + ">(); }\n\n";
|
||||
|
||||
// Finish a buffer with a given root object:
|
||||
code += "inline void Finish" + parser.root_struct_def->name;
|
||||
code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<";
|
||||
code += parser.root_struct_def->name + "> root) { fbb.Finish(root";
|
||||
if (parser.file_identifier_.length())
|
||||
code += ", \"" + parser.file_identifier_ + "\"";
|
||||
code += "); }\n\n";
|
||||
|
||||
if (parser.file_identifier_.length()) {
|
||||
// Check if a buffer has the identifier.
|
||||
code += "inline bool " + parser.root_struct_def->name;
|
||||
code += "BufferHasIdentifier(const void *buf) { return flatbuffers::";
|
||||
code += "BufferHasIdentifier(buf, \"" + parser.file_identifier_;
|
||||
code += "\"); }\n\n";
|
||||
}
|
||||
}
|
||||
for (auto it = parser.name_space_.begin();
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
code += "}; // namespace " + *it + "\n";
|
||||
|
||||
// Close the namespaces.
|
||||
for (auto it = parser.name_space_.rbegin();
|
||||
it != parser.name_space_.rend(); ++it) {
|
||||
code += "} // namespace " + *it + "\n";
|
||||
}
|
||||
|
||||
// Close the include guard.
|
||||
code += "\n#endif // " + include_guard + "\n";
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
@@ -361,8 +515,9 @@ std::string GenerateCPP(const Parser &parser) {
|
||||
|
||||
bool GenerateCPP(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
auto code = GenerateCPP(parser);
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
auto code = GenerateCPP(parser, file_name);
|
||||
return !code.length() ||
|
||||
SaveFile((path + file_name + "_generated.h").c_str(), code, false);
|
||||
}
|
||||
|
||||
680
src/idl_gen_go.cpp
Executable file
680
src/idl_gen_go.cpp
Executable file
@@ -0,0 +1,680 @@
|
||||
/*
|
||||
* 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 <string>
|
||||
|
||||
#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 go {
|
||||
|
||||
static std::string GenGetter(const Type &type);
|
||||
static std::string GenMethod(const FieldDef &field);
|
||||
static void GenStructBuilder(const StructDef &struct_def,
|
||||
std::string *code_ptr);
|
||||
static void GenReceiver(const StructDef &struct_def, std::string *code_ptr);
|
||||
static std::string GenTypeBasic(const Type &type);
|
||||
static std::string GenTypeGet(const Type &type);
|
||||
static std::string TypeName(const FieldDef &field);
|
||||
|
||||
|
||||
// Write a comment.
|
||||
static void Comment(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";
|
||||
}
|
||||
}
|
||||
|
||||
// Most field accessors need to retrieve and test the field offset first,
|
||||
// this is the prefix code for that.
|
||||
std::string OffsetPrefix(const FieldDef &field) {
|
||||
return "{\n\to := flatbuffers.UOffsetT(rcv._tab.Offset(" +
|
||||
NumToString(field.value.offset) +
|
||||
"))\n\tif o != 0 {\n";
|
||||
}
|
||||
|
||||
// Begin by declaring namespace and imports.
|
||||
static void BeginFile(const std::string name_space_name,
|
||||
const bool needs_imports,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "// automatically generated, do not modify\n\n";
|
||||
code += "package " + name_space_name + "\n\n";
|
||||
if (needs_imports) {
|
||||
code += "import (\n";
|
||||
code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n";
|
||||
code += ")\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Begin a class declaration.
|
||||
static void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
code += "type " + struct_def.name + " struct {\n\t";
|
||||
|
||||
// _ is reserved in flatbuffers field names, so no chance of name conflict:
|
||||
code += "_tab ";
|
||||
code += struct_def.fixed ? "flatbuffers.Struct" : "flatbuffers.Table";
|
||||
code += "\n}\n\n";
|
||||
}
|
||||
|
||||
// Begin enum code with a class declaration.
|
||||
static void BeginEnum(std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "const (\n";
|
||||
}
|
||||
|
||||
// A single enum member.
|
||||
static void EnumMember(const EnumDef &enum_def, const EnumVal ev,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "\t";
|
||||
code += enum_def.name;
|
||||
code += ev.name;
|
||||
code += " = ";
|
||||
code += NumToString(ev.value) + "\n";
|
||||
}
|
||||
|
||||
// End enum code.
|
||||
static void EndEnum(std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += ")\n";
|
||||
}
|
||||
|
||||
// Initialize a new struct or table from existing data.
|
||||
static void NewRootTypeFromBuffer(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
code += "func GetRootAs";
|
||||
code += struct_def.name;
|
||||
code += "(buf []byte, offset flatbuffers.UOffsetT) ";
|
||||
code += "*" + struct_def.name + "";
|
||||
code += " {\n";
|
||||
code += "\tn := flatbuffers.GetUOffsetT(buf[offset:])\n";
|
||||
code += "\tx := &" + struct_def.name + "{}\n";
|
||||
code += "\tx.Init(buf, n + offset)\n";
|
||||
code += "\treturn x\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Initialize an existing object with other data, to avoid an allocation.
|
||||
static void InitializeExisting(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " Init(buf []byte, i flatbuffers.UOffsetT) ";
|
||||
code += "{\n";
|
||||
code += "\trcv._tab.Bytes = buf\n";
|
||||
code += "\trcv._tab.Pos = i\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get the length of a vector.
|
||||
static void GetVectorLen(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name) + "Length(";
|
||||
code += ") int " + OffsetPrefix(field);
|
||||
code += "\t\treturn rcv._tab.VectorLen(o)\n\t}\n";
|
||||
code += "\treturn 0\n}\n\n";
|
||||
}
|
||||
|
||||
// Get the value of a struct's scalar.
|
||||
static void GetScalarFieldOfStruct(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
std::string getter = GenGetter(field.value.type);
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "() " + TypeName(field) + " { return " + getter;
|
||||
code += "(rcv._tab.Pos + flatbuffers.UOffsetT(";
|
||||
code += NumToString(field.value.offset) + ")) }\n";
|
||||
}
|
||||
|
||||
// Get the value of a table's scalar.
|
||||
static void GetScalarFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
std::string getter = GenGetter(field.value.type);
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "() " + TypeName(field) + " ";
|
||||
code += OffsetPrefix(field) + "\t\treturn " + getter;
|
||||
code += "(o + rcv._tab.Pos)\n\t}\n";
|
||||
code += "\treturn " + field.value.constant + "\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get a struct by initializing an existing struct.
|
||||
// Specific to Struct.
|
||||
static void GetStructFieldOfStruct(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "(obj *" + TypeName(field);
|
||||
code += ") *" + TypeName(field);
|
||||
code += " {\n";
|
||||
code += "\tif obj == nil {\n";
|
||||
code += "\t\tobj = new(" + TypeName(field) + ")\n";
|
||||
code += "\t}\n";
|
||||
code += "\tobj.Init(rcv._tab.Bytes, rcv._tab.Pos + ";
|
||||
code += NumToString(field.value.offset) + ")";
|
||||
code += "\n\treturn obj\n";
|
||||
code += "}\n";
|
||||
}
|
||||
|
||||
// Get a struct by initializing an existing struct.
|
||||
// Specific to Table.
|
||||
static void GetStructFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "(obj *";
|
||||
code += TypeName(field);
|
||||
code += ") *" + TypeName(field) + " " + OffsetPrefix(field);
|
||||
if (field.value.type.struct_def->fixed) {
|
||||
code += "\t\tx := o + rcv._tab.Pos\n";
|
||||
} else {
|
||||
code += "\t\tx := rcv._tab.Indirect(o + rcv._tab.Pos)\n";
|
||||
}
|
||||
code += "\t\tif obj == nil {\n";
|
||||
code += "\t\t\tobj = new(" + TypeName(field) + ")\n";
|
||||
code += "\t\t}\n";
|
||||
code += "\t\tobj.Init(rcv._tab.Bytes, x)\n";
|
||||
code += "\t\treturn obj\n\t}\n\treturn nil\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get the value of a string.
|
||||
static void GetStringField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "() " + TypeName(field) + " ";
|
||||
code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type);
|
||||
code += "(o)\n\t}\n\treturn \"\"\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get the value of a union from an object.
|
||||
static void GetUnionField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name) + "(";
|
||||
code += "obj " + TypeName(field) + ") bool ";
|
||||
code += OffsetPrefix(field);
|
||||
code += "\t\t" + GenGetter(field.value.type);
|
||||
code += "(obj, o)\n\t\treturn true\n\t}\n";
|
||||
code += "\treturn false\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get the value of a vector's struct member.
|
||||
static void GetMemberOfVectorOfStruct(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "(obj *" + TypeName(field);
|
||||
code += ", j int) bool " + OffsetPrefix(field);
|
||||
code += "\t\tx := rcv._tab.Vector(o)\n";
|
||||
code += "\t\tx += flatbuffers.UOffsetT(j) * ";
|
||||
code += NumToString(InlineSize(vectortype)) + "\n";
|
||||
if (!(vectortype.struct_def->fixed)) {
|
||||
code += "\t\tx = rcv._tab.Indirect(x)\n";
|
||||
}
|
||||
code += "\tif obj == nil {\n";
|
||||
code += "\t\tobj = new(" + TypeName(field) + ")\n";
|
||||
code += "\t}\n";
|
||||
code += "\t\tobj.Init(rcv._tab.Bytes, x)\n";
|
||||
code += "\t\treturn true\n\t}\n";
|
||||
code += "\treturn false\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get the value of a vector's non-struct member. Uses a named return
|
||||
// argument to conveniently set the zero value for the result.
|
||||
static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "(j int) " + TypeName(field) + " ";
|
||||
code += OffsetPrefix(field);
|
||||
code += "\t\ta := rcv._tab.Vector(o)\n";
|
||||
code += "\t\treturn " + GenGetter(field.value.type) + "(";
|
||||
code += "a + flatbuffers.UOffsetT(j * ";
|
||||
code += NumToString(InlineSize(vectortype)) + "))\n";
|
||||
code += "\t}\n";
|
||||
if (vectortype.base_type == BASE_TYPE_STRING) {
|
||||
code += "\treturn \"\"\n";
|
||||
} else {
|
||||
code += "\treturn 0\n";
|
||||
}
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Begin the creator function signature.
|
||||
static void BeginBuilderArgs(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
code += "\n";
|
||||
code += "func Create" + struct_def.name;
|
||||
code += "(builder *flatbuffers.Builder";
|
||||
}
|
||||
|
||||
// Recursively generate arguments for a constructor, to deal with nested
|
||||
// structs.
|
||||
static void StructBuilderArgs(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *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.
|
||||
StructBuilderArgs(*field.value.type.struct_def,
|
||||
(field.value.type.struct_def->name + "_").c_str(),
|
||||
code_ptr);
|
||||
} else {
|
||||
std::string &code = *code_ptr;
|
||||
code += (std::string)", " + nameprefix;
|
||||
code += MakeCamel(field.name, false);
|
||||
code += " " + GenTypeBasic(field.value.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// End the creator function signature.
|
||||
static void EndBuilderArgs(std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += ") flatbuffers.UOffsetT {\n";
|
||||
}
|
||||
|
||||
// Recursively generate struct construction statements and instert manual
|
||||
// padding.
|
||||
static void StructBuilderBody(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += " builder.Prep(" + NumToString(struct_def.minalign) + ", ";
|
||||
code += NumToString(struct_def.bytesize) + ")\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)) {
|
||||
StructBuilderBody(*field.value.type.struct_def,
|
||||
(field.value.type.struct_def->name + "_").c_str(),
|
||||
code_ptr);
|
||||
} else {
|
||||
code += " builder.Prepend" + GenMethod(field) + "(";
|
||||
code += nameprefix + MakeCamel(field.name, false) + ")\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void EndBuilderBody(std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += " return builder.Offset()\n";
|
||||
code += "}\n";
|
||||
}
|
||||
|
||||
// Get the value of a table's starting offset.
|
||||
static void GetStartOfTable(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + struct_def.name + "Start";
|
||||
code += "(builder *flatbuffers.Builder) { ";
|
||||
code += "builder.StartObject(";
|
||||
code += NumToString(struct_def.fields.vec.size());
|
||||
code += ") }\n";
|
||||
}
|
||||
|
||||
// Set the value of a table's field.
|
||||
static void BuildFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
const size_t offset,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + struct_def.name + "Add" + MakeCamel(field.name);
|
||||
code += "(builder *flatbuffers.Builder, ";
|
||||
code += MakeCamel(field.name, false) + " ";
|
||||
if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
|
||||
code += "flatbuffers.UOffsetT";
|
||||
} else {
|
||||
code += GenTypeBasic(field.value.type);
|
||||
}
|
||||
code += ") ";
|
||||
code += "{ builder.Prepend";
|
||||
code += GenMethod(field) + "Slot(";
|
||||
code += NumToString(offset) + ", ";
|
||||
if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
|
||||
code += "flatbuffers.UOffsetT";
|
||||
code += "(";
|
||||
code += MakeCamel(field.name, false) + ")";
|
||||
} else {
|
||||
code += MakeCamel(field.name, false);
|
||||
}
|
||||
code += ", " + field.value.constant;
|
||||
code += ") }\n";
|
||||
}
|
||||
|
||||
// Set the value of one of the members of a table's vector.
|
||||
static void BuildVectorOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + struct_def.name + "Start";
|
||||
code += MakeCamel(field.name);
|
||||
code += "Vector(builder *flatbuffers.Builder, numElems int) ";
|
||||
code += "flatbuffers.UOffsetT { return builder.StartVector(";
|
||||
code += NumToString(InlineSize(field.value.type.VectorType()));
|
||||
code += ", numElems) }\n";
|
||||
}
|
||||
|
||||
// Get the offset of the end of a table.
|
||||
static void GetEndOffsetOnTable(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + struct_def.name + "End";
|
||||
code += "(builder *flatbuffers.Builder) flatbuffers.UOffsetT ";
|
||||
code += "{ return builder.EndObject() }\n";
|
||||
}
|
||||
|
||||
// Generate the receiver for function signatures.
|
||||
static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func (rcv *" + struct_def.name + ")";
|
||||
}
|
||||
|
||||
// Generate a struct field, conditioned on its child type(s).
|
||||
static void GenStructAccessor(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
Comment(field.doc_comment, code_ptr, "");
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
GetScalarFieldOfStruct(struct_def, field, code_ptr);
|
||||
} else {
|
||||
GetScalarFieldOfTable(struct_def, field, code_ptr);
|
||||
}
|
||||
} else {
|
||||
switch (field.value.type.base_type) {
|
||||
case BASE_TYPE_STRUCT:
|
||||
if (struct_def.fixed) {
|
||||
GetStructFieldOfStruct(struct_def, field, code_ptr);
|
||||
} else {
|
||||
GetStructFieldOfTable(struct_def, field, code_ptr);
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
GetStringField(struct_def, field, code_ptr);
|
||||
break;
|
||||
case BASE_TYPE_VECTOR: {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
if (vectortype.base_type == BASE_TYPE_STRUCT) {
|
||||
GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
|
||||
} else {
|
||||
GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
GetUnionField(struct_def, field, code_ptr);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
GetVectorLen(struct_def, field, code_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate table constructors, conditioned on its members' types.
|
||||
static void GenTableBuilders(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
GetStartOfTable(struct_def, code_ptr);
|
||||
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
auto offset = it - struct_def.fields.vec.begin();
|
||||
BuildFieldOfTable(struct_def, field, offset, code_ptr);
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
BuildVectorOfTable(struct_def, field, code_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
GetEndOffsetOnTable(struct_def, code_ptr);
|
||||
}
|
||||
|
||||
// Generate struct or table methods.
|
||||
static void GenStruct(const StructDef &struct_def,
|
||||
std::string *code_ptr,
|
||||
StructDef *root_struct_def) {
|
||||
if (struct_def.generated) return;
|
||||
|
||||
Comment(struct_def.doc_comment, code_ptr);
|
||||
BeginClass(struct_def, code_ptr);
|
||||
if (&struct_def == root_struct_def) {
|
||||
// Generate a special accessor for the table that has been declared as
|
||||
// the root type.
|
||||
NewRootTypeFromBuffer(struct_def, code_ptr);
|
||||
}
|
||||
// Generate the Init method that sets the field in a pre-existing
|
||||
// accessor object. This is to allow object reuse.
|
||||
InitializeExisting(struct_def, code_ptr);
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
GenStructAccessor(struct_def, field, code_ptr);
|
||||
}
|
||||
|
||||
if (struct_def.fixed) {
|
||||
// create a struct constructor function
|
||||
GenStructBuilder(struct_def, code_ptr);
|
||||
} else {
|
||||
// Create a set of functions that allow table construction.
|
||||
GenTableBuilders(struct_def, code_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate enum declarations.
|
||||
static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
|
||||
if (enum_def.generated) return;
|
||||
|
||||
Comment(enum_def.doc_comment, code_ptr);
|
||||
BeginEnum(code_ptr);
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
Comment(ev.doc_comment, code_ptr, " ");
|
||||
EnumMember(enum_def, ev, code_ptr);
|
||||
}
|
||||
EndEnum(code_ptr);
|
||||
}
|
||||
|
||||
// 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 "rcv._tab.String";
|
||||
case BASE_TYPE_UNION: return "rcv._tab.Union";
|
||||
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
|
||||
default:
|
||||
return "rcv._tab.Get" + 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" : "UOffsetT");
|
||||
}
|
||||
|
||||
|
||||
// Save out the generated code for a Go Table type.
|
||||
static bool SaveType(const Parser &parser, const Definition &def,
|
||||
const std::string &classcode, const std::string &path,
|
||||
bool needs_imports) {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string name_space_name;
|
||||
std::string name_space_dir = path;
|
||||
for (auto it = parser.name_space_.begin();
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
if (name_space_name.length()) {
|
||||
name_space_name += ".";
|
||||
name_space_dir += PATH_SEPARATOR;
|
||||
}
|
||||
name_space_name = *it;
|
||||
name_space_dir += *it;
|
||||
mkdir(name_space_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
|
||||
}
|
||||
|
||||
|
||||
std::string code = "";
|
||||
BeginFile(name_space_name, needs_imports, &code);
|
||||
code += classcode;
|
||||
std::string filename = name_space_dir + PATH_SEPARATOR + def.name + ".go";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) #GTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
return ctypename[type.base_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 "*flatbuffers.Table";
|
||||
}
|
||||
}
|
||||
|
||||
static std::string GenTypeGet(const Type &type) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
: GenTypePointer(type);
|
||||
}
|
||||
|
||||
static std::string TypeName(const FieldDef &field) {
|
||||
return GenTypeGet(field.value.type);
|
||||
}
|
||||
|
||||
// Create a struct with a builder and the struct's arguments.
|
||||
static void GenStructBuilder(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
BeginBuilderArgs(struct_def, code_ptr);
|
||||
StructBuilderArgs(struct_def, "", code_ptr);
|
||||
EndBuilderArgs(code_ptr);
|
||||
|
||||
StructBuilderBody(struct_def, "", code_ptr);
|
||||
EndBuilderBody(code_ptr);
|
||||
}
|
||||
|
||||
} // namespace go
|
||||
|
||||
bool GenerateGo(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string & /*file_name*/,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
std::string enumcode;
|
||||
go::GenEnum(**it, &enumcode);
|
||||
if (!go::SaveType(parser, **it, enumcode, path, false))
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
std::string declcode;
|
||||
go::GenStruct(**it, &declcode, parser.root_struct_def);
|
||||
if (!go::SaveType(parser, **it, declcode, path, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -22,11 +22,9 @@
|
||||
|
||||
#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 {
|
||||
@@ -34,7 +32,7 @@ namespace java {
|
||||
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) #JTYPE,
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) #JTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
@@ -73,18 +71,6 @@ static void GenComment(const std::string &dc,
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
@@ -157,7 +143,8 @@ static void GenStructArgs(const StructDef &struct_def, std::string *code_ptr,
|
||||
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";
|
||||
code += " builder.prep(" + NumToString(struct_def.minalign) + ", ";
|
||||
code += NumToString(struct_def.bytesize) + ");\n";
|
||||
for (auto it = struct_def.fields.vec.rbegin();
|
||||
it != struct_def.fields.vec.rend();
|
||||
++it) {
|
||||
@@ -255,12 +242,12 @@ static void GenStruct(StructDef &struct_def,
|
||||
code += "obj.__init(";
|
||||
code += field.value.type.struct_def->fixed
|
||||
? "o + bb_pos"
|
||||
: "__indirect(o + i)";
|
||||
: "__indirect(o + bb_pos)";
|
||||
code += ", bb) : null";
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
code += offset_prefix + getter +"(o) : null";
|
||||
code += offset_prefix + getter +"(o + bb_pos) : null";
|
||||
break;
|
||||
case BASE_TYPE_VECTOR: {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
@@ -322,17 +309,23 @@ static void GenStruct(StructDef &struct_def,
|
||||
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";
|
||||
auto argname = MakeCamel(field.name, false);
|
||||
if (!IsScalar(field.value.type.base_type)) argname += "Offset";
|
||||
code += " " + argname + ") { builder.add";
|
||||
code += GenMethod(field) + "(";
|
||||
code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
|
||||
code += MakeCamel(field.name, false) + ", " + field.value.constant;
|
||||
code += argname + ", " + 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";
|
||||
auto vector_type = field.value.type.VectorType();
|
||||
auto alignment = InlineAlignment(vector_type);
|
||||
auto elem_size = InlineSize(vector_type);
|
||||
code += NumToString(elem_size);
|
||||
code += ", numElems, " + NumToString(alignment);
|
||||
code += "); }\n";
|
||||
}
|
||||
}
|
||||
code += " public static int end" + struct_def.name;
|
||||
@@ -344,7 +337,8 @@ static void GenStruct(StructDef &struct_def,
|
||||
// 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) {
|
||||
const std::string &classcode, const std::string &path,
|
||||
bool needs_imports) {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string name_space_java;
|
||||
@@ -353,7 +347,7 @@ static bool SaveClass(const Parser &parser, const Definition &def,
|
||||
it != parser.name_space_.end(); ++it) {
|
||||
if (name_space_java.length()) {
|
||||
name_space_java += ".";
|
||||
name_space_dir += PATH_SEPARATOR;
|
||||
name_space_dir += kPathSeparator;
|
||||
}
|
||||
name_space_java += *it;
|
||||
name_space_dir += *it;
|
||||
@@ -362,10 +356,12 @@ static bool SaveClass(const Parser &parser, const Definition &def,
|
||||
|
||||
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";
|
||||
if (needs_imports) {
|
||||
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";
|
||||
auto filename = name_space_dir + kPathSeparator + def.name + ".java";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
@@ -373,14 +369,15 @@ static bool SaveClass(const Parser &parser, const Definition &def,
|
||||
|
||||
bool GenerateJava(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string & /*file_name*/,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
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))
|
||||
if (!SaveClass(parser, **it, enumcode, path, false))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -388,7 +385,7 @@ bool GenerateJava(const Parser &parser,
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
std::string declcode;
|
||||
GenStruct(**it, &declcode, parser.root_struct_def);
|
||||
if (!SaveClass(parser, **it, declcode, path))
|
||||
if (!SaveClass(parser, **it, declcode, path, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -396,4 +393,3 @@ bool GenerateJava(const Parser &parser,
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
||||
@@ -23,33 +23,63 @@
|
||||
namespace flatbuffers {
|
||||
|
||||
static void GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, int indent_step, std::string *_text);
|
||||
int indent, const GeneratorOptions &opts,
|
||||
std::string *_text);
|
||||
|
||||
// If indentation is less than 0, that indicates we don't want any newlines
|
||||
// either.
|
||||
const char *NewLine(int indent_step) {
|
||||
return indent_step >= 0 ? "\n" : "";
|
||||
}
|
||||
|
||||
// Output an identifier with or without quotes depending on strictness.
|
||||
void OutputIdentifier(const std::string &name, const GeneratorOptions &opts,
|
||||
std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
if (opts.strict_json) text += "\"";
|
||||
text += name;
|
||||
if (opts.strict_json) 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) {
|
||||
template<typename T> void Print(T val, Type type, int /*indent*/,
|
||||
StructDef * /*union_sd*/,
|
||||
const GeneratorOptions &opts,
|
||||
std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
if (type.enum_def && opts.output_enum_identifiers) {
|
||||
auto enum_val = type.enum_def->ReverseLookup(static_cast<int>(val));
|
||||
if (enum_val) {
|
||||
OutputIdentifier(enum_val->name, opts, _text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
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,
|
||||
int indent, const GeneratorOptions &opts,
|
||||
std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "[\n";
|
||||
text += "[";
|
||||
text += NewLine(opts.indent_step);
|
||||
for (uoffset_t i = 0; i < v.Length(); i++) {
|
||||
if (i) text += ",\n";
|
||||
text.append(indent + indent_step, ' ');
|
||||
if (i) {
|
||||
text += ",";
|
||||
text += NewLine(opts.indent_step);
|
||||
}
|
||||
text.append(indent + opts.indent_step, ' ');
|
||||
if (IsStruct(type))
|
||||
Print(v.GetStructFromOffset(i * type.struct_def->bytesize), type,
|
||||
indent + indent_step, indent_step, nullptr, _text);
|
||||
indent + opts.indent_step, nullptr, opts, _text);
|
||||
else
|
||||
Print(v.Get(i), type, indent + indent_step, indent_step, nullptr, _text);
|
||||
Print(v.Get(i), type, indent + opts.indent_step, nullptr,
|
||||
opts, _text);
|
||||
}
|
||||
text += "\n";
|
||||
text += NewLine(opts.indent_step);
|
||||
text.append(indent, ' ');
|
||||
text += "]";
|
||||
}
|
||||
@@ -70,7 +100,8 @@ static void EscapeString(const String &s, std::string *_text) {
|
||||
text += c;
|
||||
} else {
|
||||
auto u = static_cast<unsigned char>(c);
|
||||
text += "\\x" + IntToStringHex(u);
|
||||
text += "\\x";
|
||||
text += IntToStringHex(u);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -80,8 +111,10 @@ static void EscapeString(const String &s, std::string *_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) {
|
||||
Type type, int indent,
|
||||
StructDef *union_sd,
|
||||
const GeneratorOptions &opts,
|
||||
std::string *_text) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_UNION:
|
||||
// If this assert hits, you have an corrupt buffer, a union type field
|
||||
@@ -90,14 +123,14 @@ template<> void Print<const void *>(const void *val,
|
||||
GenStruct(*union_sd,
|
||||
reinterpret_cast<const Table *>(val),
|
||||
indent,
|
||||
indent_step,
|
||||
opts,
|
||||
_text);
|
||||
break;
|
||||
case BASE_TYPE_STRUCT:
|
||||
GenStruct(*type.struct_def,
|
||||
reinterpret_cast<const Table *>(val),
|
||||
indent,
|
||||
indent_step,
|
||||
opts,
|
||||
_text);
|
||||
break;
|
||||
case BASE_TYPE_STRING: {
|
||||
@@ -108,11 +141,11 @@ template<> void Print<const void *>(const void *val,
|
||||
type = type.VectorType();
|
||||
// Call PrintVector above specifically for each element type:
|
||||
switch (type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
PrintVector<CTYPE>( \
|
||||
*reinterpret_cast<const Vector<CTYPE> *>(val), \
|
||||
type, indent, indent_step, _text); break;
|
||||
type, indent, opts, _text); break;
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
}
|
||||
@@ -124,18 +157,19 @@ template<> void Print<const void *>(const void *val,
|
||||
// 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,
|
||||
const GeneratorOptions &opts,
|
||||
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);
|
||||
table->GetField<T>(fd.value.offset, 0), fd.value.type, indent, nullptr,
|
||||
opts, _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) {
|
||||
int indent, StructDef *union_sd,
|
||||
const GeneratorOptions &opts, std::string *_text) {
|
||||
const void *val = nullptr;
|
||||
if (fixed) {
|
||||
// The only non-scalar fields in structs are structs.
|
||||
@@ -147,15 +181,16 @@ static void GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
||||
? 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);
|
||||
Print(val, fd.value.type, indent, union_sd, opts, _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) {
|
||||
int indent, const GeneratorOptions &opts,
|
||||
std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "{\n";
|
||||
text += "{";
|
||||
int fieldout = 0;
|
||||
StructDef *union_sd = nullptr;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
@@ -164,50 +199,55 @@ static void GenStruct(const StructDef &struct_def, const Table *table,
|
||||
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;
|
||||
if (fieldout++) {
|
||||
text += ",";
|
||||
}
|
||||
text += NewLine(opts.indent_step);
|
||||
text.append(indent + opts.indent_step, ' ');
|
||||
OutputIdentifier(fd.name, opts, _text);
|
||||
text += ": ";
|
||||
switch (fd.value.type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
GenField<CTYPE>(fd, table, struct_def.fixed, \
|
||||
indent + indent_step, indent_step, _text); \
|
||||
opts, indent + opts.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) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
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);
|
||||
GenFieldOffset(fd, table, struct_def.fixed, indent + opts.indent_step,
|
||||
union_sd, opts, _text);
|
||||
break;
|
||||
}
|
||||
if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
|
||||
union_sd = fd.value.type.enum_def->ReverseLookup(
|
||||
auto enum_val = fd.value.type.enum_def->ReverseLookup(
|
||||
table->GetField<uint8_t>(fd.value.offset, 0));
|
||||
assert(enum_val);
|
||||
union_sd = enum_val->struct_def;
|
||||
}
|
||||
}
|
||||
}
|
||||
text += "\n";
|
||||
text += NewLine(opts.indent_step);
|
||||
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) {
|
||||
const GeneratorOptions &opts, 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,
|
||||
opts,
|
||||
_text);
|
||||
text += "\n";
|
||||
text += NewLine(opts.indent_step);
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -23,14 +23,14 @@
|
||||
namespace flatbuffers {
|
||||
|
||||
const char *const kTypeNames[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) IDLTYPE,
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) IDLTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
nullptr
|
||||
};
|
||||
|
||||
const char kTypeSizes[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) sizeof(CTYPE),
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) sizeof(CTYPE),
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
@@ -81,12 +81,17 @@ template<> inline Offset<void> atot<Offset<void>>(const char *s) {
|
||||
TD(Enum, 263, "enum") \
|
||||
TD(Union, 264, "union") \
|
||||
TD(NameSpace, 265, "namespace") \
|
||||
TD(RootType, 266, "root_type")
|
||||
TD(RootType, 266, "root_type") \
|
||||
TD(FileIdentifier, 267, "file_identifier") \
|
||||
TD(FileExtension, 268, "file_extension")
|
||||
#ifdef __GNUC__
|
||||
__extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
|
||||
#endif
|
||||
enum {
|
||||
#define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME,
|
||||
#define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
|
||||
FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
|
||||
#undef FLATBUFFERS_TOKEN
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) kToken ## ENUM,
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) kToken ## ENUM,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
@@ -96,7 +101,7 @@ static std::string TokenToString(int t) {
|
||||
#define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
|
||||
FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
|
||||
#undef FLATBUFFERS_TOKEN
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) IDLTYPE,
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) IDLTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
@@ -170,7 +175,7 @@ void Parser::Next() {
|
||||
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) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
if (attribute_ == IDLTYPE) { \
|
||||
token_ = kToken ## ENUM; \
|
||||
return; \
|
||||
@@ -191,6 +196,14 @@ void Parser::Next() {
|
||||
if (attribute_ == "union") { token_ = kTokenUnion; return; }
|
||||
if (attribute_ == "namespace") { token_ = kTokenNameSpace; return; }
|
||||
if (attribute_ == "root_type") { token_ = kTokenRootType; return; }
|
||||
if (attribute_ == "file_identifier") {
|
||||
token_ = kTokenFileIdentifier;
|
||||
return;
|
||||
}
|
||||
if (attribute_ == "file_extension") {
|
||||
token_ = kTokenFileExtension;
|
||||
return;
|
||||
}
|
||||
// If not, it is a user-defined identifier:
|
||||
token_ = kTokenIdentifier;
|
||||
return;
|
||||
@@ -200,6 +213,13 @@ void Parser::Next() {
|
||||
if (*cursor_ == '.') {
|
||||
cursor_++;
|
||||
while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
|
||||
// See if this float has a scientific notation suffix. Both JSON
|
||||
// and C++ (through strtod() we use) have the same format:
|
||||
if (*cursor_ == 'e' || *cursor_ == 'E') {
|
||||
cursor_++;
|
||||
if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
|
||||
while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
|
||||
}
|
||||
token_ = kTokenFloatConstant;
|
||||
} else {
|
||||
token_ = kTokenIntegerConstant;
|
||||
@@ -261,7 +281,7 @@ void Parser::ParseType(Type &type) {
|
||||
// union element.
|
||||
Error("vector of union types not supported (wrap in table first).");
|
||||
}
|
||||
type = Type(BASE_TYPE_VECTOR, subtype.struct_def);
|
||||
type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
|
||||
type.element = subtype.base_type;
|
||||
Expect(']');
|
||||
return;
|
||||
@@ -287,7 +307,7 @@ FieldDef &Parser::AddField(StructDef &struct_def,
|
||||
// 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);
|
||||
field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
|
||||
struct_def.bytesize += size;
|
||||
}
|
||||
if (struct_def.fields.Add(name, &field))
|
||||
@@ -306,10 +326,12 @@ void Parser::ParseField(StructDef &struct_def) {
|
||||
if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type))
|
||||
Error("structs_ may contain only scalar or struct fields");
|
||||
|
||||
FieldDef *typefield = nullptr;
|
||||
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);
|
||||
typefield = &AddField(struct_def, name + "_type",
|
||||
type.enum_def->underlying_type);
|
||||
}
|
||||
|
||||
auto &field = AddField(struct_def, name, type);
|
||||
@@ -324,6 +346,30 @@ void Parser::ParseField(StructDef &struct_def) {
|
||||
field.deprecated = field.attributes.Lookup("deprecated") != nullptr;
|
||||
if (field.deprecated && struct_def.fixed)
|
||||
Error("can't deprecate fields in a struct");
|
||||
auto nested = field.attributes.Lookup("nested_flatbuffer");
|
||||
if (nested) {
|
||||
if (nested->type.base_type != BASE_TYPE_STRING)
|
||||
Error("nested_flatbuffer attribute must be a string (the root type)");
|
||||
if (field.value.type.base_type != BASE_TYPE_VECTOR ||
|
||||
field.value.type.element != BASE_TYPE_UCHAR)
|
||||
Error("nested_flatbuffer attribute may only apply to a vector of ubyte");
|
||||
// This will cause an error if the root type of the nested flatbuffer
|
||||
// wasn't defined elsewhere.
|
||||
LookupCreateStruct(nested->constant);
|
||||
}
|
||||
|
||||
if (typefield) {
|
||||
// If this field is a union, and it has a manually assigned id,
|
||||
// the automatically added type field should have an id as well (of N - 1).
|
||||
auto attr = field.attributes.Lookup("id");
|
||||
if (attr) {
|
||||
auto id = atoi(attr->constant.c_str());
|
||||
auto val = new Value();
|
||||
val->type = attr->type;
|
||||
val->constant = NumToString(id - 1);
|
||||
typefield->attributes.Add("id", val);
|
||||
}
|
||||
}
|
||||
|
||||
Expect(';');
|
||||
}
|
||||
@@ -337,9 +383,9 @@ void Parser::ParseAnyValue(Value &val, FieldDef *field) {
|
||||
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));
|
||||
auto enum_val = val.type.enum_def->ReverseLookup(enum_idx);
|
||||
if (!enum_val) Error("illegal type id for: " + field->name);
|
||||
val.constant = NumToString(ParseTable(*enum_val->struct_def));
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_STRUCT:
|
||||
@@ -374,7 +420,7 @@ void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
|
||||
uoffset_t Parser::ParseTable(const StructDef &struct_def) {
|
||||
Expect('{');
|
||||
size_t fieldn = 0;
|
||||
for (;;) {
|
||||
if (!IsNext('}')) for (;;) {
|
||||
std::string name = attribute_;
|
||||
if (!IsNext(kTokenStringConstant)) Expect(kTokenIdentifier);
|
||||
auto field = struct_def.fields.Lookup(name);
|
||||
@@ -407,16 +453,20 @@ uoffset_t Parser::ParseTable(const StructDef &struct_def) {
|
||||
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) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
builder_.Pad(field->padding); \
|
||||
builder_.AddElement(value.offset, \
|
||||
if (struct_def.fixed) { \
|
||||
builder_.PushElement(atot<CTYPE>(value.constant.c_str())); \
|
||||
} else { \
|
||||
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) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
builder_.Pad(field->padding); \
|
||||
if (IsStruct(field->value.type)) { \
|
||||
@@ -465,12 +515,13 @@ uoffset_t Parser::ParseVector(const Type &type) {
|
||||
}
|
||||
Next();
|
||||
|
||||
builder_.StartVector(count * InlineSize(type), InlineAlignment((type)));
|
||||
builder_.StartVector(count * InlineSize(type) / InlineAlignment(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) \
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
|
||||
else builder_.PushElement(atot<CTYPE>(val.constant.c_str())); \
|
||||
@@ -523,8 +574,52 @@ bool Parser::TryTypedValue(int dtoken,
|
||||
return match;
|
||||
}
|
||||
|
||||
int64_t Parser::ParseIntegerFromString(Type &type) {
|
||||
int64_t result = 0;
|
||||
// Parse one or more enum identifiers, separated by spaces.
|
||||
const char *next = attribute_.c_str();
|
||||
do {
|
||||
const char *divider = strchr(next, ' ');
|
||||
std::string word;
|
||||
if (divider) {
|
||||
word = std::string(next, divider);
|
||||
next = divider + strspn(divider, " ");
|
||||
} else {
|
||||
word = next;
|
||||
next += word.length();
|
||||
}
|
||||
if (type.enum_def) { // The field has an enum type
|
||||
auto enum_val = type.enum_def->vals.Lookup(word);
|
||||
if (!enum_val)
|
||||
Error("unknown enum value: " + word +
|
||||
", for enum: " + type.enum_def->name);
|
||||
result |= enum_val->value;
|
||||
} else { // No enum type, probably integral field.
|
||||
if (!IsInteger(type.base_type))
|
||||
Error("not a valid value for this field: " + word);
|
||||
// TODO: could check if its a valid number constant here.
|
||||
const char *dot = strchr(word.c_str(), '.');
|
||||
if (!dot) Error("enum values need to be qualified by an enum type");
|
||||
std::string enum_def_str(word.c_str(), dot);
|
||||
std::string enum_val_str(dot + 1, word.c_str() + word.length());
|
||||
auto enum_def = enums_.Lookup(enum_def_str);
|
||||
if (!enum_def) Error("unknown enum: " + enum_def_str);
|
||||
auto enum_val = enum_def->vals.Lookup(enum_val_str);
|
||||
if (!enum_val) Error("unknown enum value: " + enum_val_str);
|
||||
result |= enum_val->value;
|
||||
}
|
||||
} while(*next);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Parser::ParseSingleValue(Value &e) {
|
||||
if (TryTypedValue(kTokenIntegerConstant,
|
||||
// First check if this could be a string/identifier enum value:
|
||||
if (e.type.base_type != BASE_TYPE_STRING &&
|
||||
e.type.base_type != BASE_TYPE_NONE &&
|
||||
(token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
|
||||
e.constant = NumToString(ParseIntegerFromString(e.type));
|
||||
Next();
|
||||
} else if (TryTypedValue(kTokenIntegerConstant,
|
||||
IsScalar(e.type.base_type),
|
||||
e,
|
||||
BASE_TYPE_INT) ||
|
||||
@@ -536,19 +631,6 @@ void Parser::ParseSingleValue(Value &e) {
|
||||
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_));
|
||||
}
|
||||
@@ -580,14 +662,17 @@ void Parser::ParseEnum(bool is_union) {
|
||||
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.
|
||||
} else {
|
||||
// Give specialized error message, since this type spec used to
|
||||
// be optional in the first FlatBuffers release.
|
||||
if (!IsNext(':')) Error("must specify the underlying integer type for this"
|
||||
" enum (e.g. \': short\', which was the default).");
|
||||
// Specify the integer type underlying this enum.
|
||||
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;
|
||||
// Make this type refer back to the enum it was derived from.
|
||||
enum_def.underlying_type.enum_def = &enum_def;
|
||||
}
|
||||
ParseMetaData(enum_def);
|
||||
Expect('{');
|
||||
@@ -597,10 +682,10 @@ void Parser::ParseEnum(bool is_union) {
|
||||
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));
|
||||
auto value = enum_def.vals.vec.size()
|
||||
? enum_def.vals.vec.back()->value + 1
|
||||
: 0;
|
||||
auto &ev = *new EnumVal(name, value);
|
||||
if (enum_def.vals.Add(name, &ev))
|
||||
Error("enum value already exists: " + name);
|
||||
ev.doc_comment = dc;
|
||||
@@ -615,6 +700,15 @@ void Parser::ParseEnum(bool is_union) {
|
||||
}
|
||||
} while (IsNext(','));
|
||||
Expect('}');
|
||||
if (enum_def.attributes.Lookup("bit_flags")) {
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
if (static_cast<size_t>((*it)->value) >=
|
||||
SizeOf(enum_def.underlying_type.base_type) * 8)
|
||||
Error("bit flag out of range of underlying integral type");
|
||||
(*it)->value = 1LL << (*it)->value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::ParseDecl() {
|
||||
@@ -638,8 +732,6 @@ void Parser::ParseDecl() {
|
||||
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()));
|
||||
@@ -651,6 +743,37 @@ void Parser::ParseDecl() {
|
||||
"struct\'s natural alignment to 256");
|
||||
struct_def.minalign = align;
|
||||
}
|
||||
struct_def.PadLastField(struct_def.minalign);
|
||||
// Check if this is a table that has manual id assignments
|
||||
auto &fields = struct_def.fields.vec;
|
||||
if (!struct_def.fixed && fields.size()) {
|
||||
size_t num_id_fields = 0;
|
||||
for (auto it = fields.begin(); it != fields.end(); ++it) {
|
||||
if ((*it)->attributes.Lookup("id")) num_id_fields++;
|
||||
}
|
||||
// If any fields have ids..
|
||||
if (num_id_fields) {
|
||||
// Then all fields must have them.
|
||||
if (num_id_fields != fields.size())
|
||||
Error("either all fields or no fields must have an 'id' attribute");
|
||||
// Simply sort by id, then the fields are the same as if no ids had
|
||||
// been specified.
|
||||
std::sort(fields.begin(), fields.end(),
|
||||
[](const FieldDef *a, const FieldDef *b) -> bool {
|
||||
auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
|
||||
auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
|
||||
return a_id < b_id;
|
||||
});
|
||||
// Verify we have a contiguous set, and reassign vtable offsets.
|
||||
for (int i = 0; i < static_cast<int>(fields.size()); i++) {
|
||||
if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str()))
|
||||
Error("field id\'s must be consecutive from 0, id " +
|
||||
NumToString(i) + " missing or set twice");
|
||||
fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
Expect('}');
|
||||
}
|
||||
|
||||
bool Parser::SetRootType(const char *name) {
|
||||
@@ -668,6 +791,7 @@ bool Parser::Parse(const char *source) {
|
||||
while (token_ != kTokenEof) {
|
||||
if (token_ == kTokenNameSpace) {
|
||||
Next();
|
||||
name_space_.clear();
|
||||
for (;;) {
|
||||
name_space_.push_back(attribute_);
|
||||
Expect(kTokenIdentifier);
|
||||
@@ -688,11 +812,26 @@ bool Parser::Parse(const char *source) {
|
||||
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");
|
||||
Expect(';');
|
||||
} else if (token_ == kTokenFileIdentifier) {
|
||||
Next();
|
||||
file_identifier_ = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
if (file_identifier_.length() !=
|
||||
FlatBufferBuilder::kFileIdentifierLength)
|
||||
Error("file_identifier must be exactly " +
|
||||
NumToString(FlatBufferBuilder::kFileIdentifierLength) +
|
||||
" characters");
|
||||
Expect(';');
|
||||
} else if (token_ == kTokenFileExtension) {
|
||||
Next();
|
||||
file_extension_ = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
Expect(';');
|
||||
} else {
|
||||
ParseDecl();
|
||||
}
|
||||
|
||||
52
tests/GoTest.sh
Executable file
52
tests/GoTest.sh
Executable file
@@ -0,0 +1,52 @@
|
||||
#!/bin/bash -eu
|
||||
# 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.
|
||||
|
||||
pushd "$(dirname $0)" >/dev/null
|
||||
test_dir="$(pwd)"
|
||||
go_path=${test_dir}/go_gen
|
||||
go_src=${go_path}/src
|
||||
|
||||
# Emit Go code for the example schema in the test dir:
|
||||
../flatc -g monster_test.fbs
|
||||
|
||||
# Go requires a particular layout of files in order to link multiple packages.
|
||||
# Copy flatbuffer Go files to their own package directories to compile the
|
||||
# test binary:
|
||||
mkdir -p ${go_src}/MyGame/Example
|
||||
mkdir -p ${go_src}/github.com/google/flatbuffers/go
|
||||
mkdir -p ${go_src}/flatbuffers_test
|
||||
|
||||
cp -u MyGame/Example/*.go ./go_gen/src/MyGame/Example/
|
||||
cp -u ../go/* ./go_gen/src/github.com/google/flatbuffers/go
|
||||
cp -u ./go_test.go ./go_gen/src/flatbuffers_test/
|
||||
|
||||
# Run tests with necessary flags.
|
||||
# Developers may wish to see more detail by appending the verbosity flag
|
||||
# -test.v to arguments for this command, as in:
|
||||
# go -test -test.v ...
|
||||
# Developers may also wish to run benchmarks, which may be achieved with the
|
||||
# flag -test.bench and the wildcard regexp ".":
|
||||
# go -test -test.bench=. ...
|
||||
GOPATH=${go_path} go test flatbuffers_test \
|
||||
--test.coverpkg=github.com/google/flatbuffers/go \
|
||||
--cpp_data=${test_dir}/monsterdata_test.bin \
|
||||
--out_data=${test_dir}/monsterdata_go_wire.bin \
|
||||
--fuzz=true \
|
||||
--fuzz_fields=4 \
|
||||
--fuzz_objects=10000
|
||||
|
||||
rm -rf ${go_path}/{pkg,src}
|
||||
|
||||
echo "OK: Go tests passed."
|
||||
@@ -17,5 +17,5 @@ rem Compile then run the Java test.
|
||||
|
||||
set batch_file_dir=%~d0%~p0
|
||||
|
||||
javac -classpath %batch_file_dir%\..\java;%batch_file_dir% JavaTest.java
|
||||
javac -g -classpath %batch_file_dir%\..\java;%batch_file_dir% JavaTest.java
|
||||
java -classpath %batch_file_dir%\..\java;%batch_file_dir% JavaTest
|
||||
|
||||
@@ -26,7 +26,7 @@ class JavaTest {
|
||||
// This file was generated from monsterdata_test.json
|
||||
|
||||
byte[] data = null;
|
||||
File file = new File("monsterdata_test_wire.bin");
|
||||
File file = new File("monsterdata_test.bin");
|
||||
RandomAccessFile f = null;
|
||||
try {
|
||||
f = new RandomAccessFile(file, "r");
|
||||
@@ -44,11 +44,16 @@ class JavaTest {
|
||||
TestBuffer(bb, 0);
|
||||
|
||||
// Second, let's create a FlatBuffer from scratch in Java, and test it also.
|
||||
// We use an initial size of 1 to exercise the reallocation algorithm,
|
||||
// normally a size larger than the typical FlatBuffer you generate would be
|
||||
// better for performance.
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
|
||||
|
||||
// We set up the same values as monsterdata.json:
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(1024);
|
||||
|
||||
int str = fbb.createString("MyMonster");
|
||||
int test1 = fbb.createString("test1");
|
||||
int test2 = fbb.createString("test2");
|
||||
|
||||
Monster.startInventoryVector(fbb, 5);
|
||||
for (byte i = 4; i >=0; i--) fbb.addByte(i);
|
||||
@@ -63,6 +68,11 @@ class JavaTest {
|
||||
Test.createTest(fbb, (short)30, (byte)40);
|
||||
int test4 = fbb.endVector();
|
||||
|
||||
Monster.startTestarrayofstringVector(fbb, 2);
|
||||
fbb.addOffset(test2);
|
||||
fbb.addOffset(test1);
|
||||
int testArrayOfString = 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));
|
||||
@@ -72,6 +82,7 @@ class JavaTest {
|
||||
Monster.addTestType(fbb, (byte)1);
|
||||
Monster.addTest(fbb, mon2);
|
||||
Monster.addTest4(fbb, test4);
|
||||
Monster.addTestarrayofstring(fbb, testArrayOfString);
|
||||
int mon = Monster.endMonster(fbb);
|
||||
|
||||
fbb.finish(mon);
|
||||
@@ -132,6 +143,10 @@ class JavaTest {
|
||||
Test test_1 = monster.test4(1);
|
||||
TestEq(monster.test4Length(), 2);
|
||||
TestEq(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100);
|
||||
|
||||
TestEq(monster.testarrayofstringLength(), 2);
|
||||
TestEq(monster.testarrayofstring(0),"test1");
|
||||
TestEq(monster.testarrayofstring(1),"test2");
|
||||
}
|
||||
|
||||
static <T> void TestEq(T a, T b) {
|
||||
|
||||
29
tests/JavaTest.sh
Executable file
29
tests/JavaTest.sh
Executable file
@@ -0,0 +1,29 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Copyright 2014 Google Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
echo Compile then run the Java test.
|
||||
|
||||
testdir=$(readlink -fn `dirname $0`)
|
||||
thisdir=$(readlink -fn `pwd`)
|
||||
|
||||
if [[ "$testdir" != "$thisdir" ]]; then
|
||||
echo error: must be run from inside the ${testdir} directory
|
||||
echo you ran it from ${thisdir}
|
||||
exit 1
|
||||
fi
|
||||
|
||||
javac -classpath ${testdir}/../java:${testdir} JavaTest.java
|
||||
java -classpath ${testdir}/../java:${testdir} JavaTest
|
||||
8
tests/MyGame/Example/Any.go
Normal file
8
tests/MyGame/Example/Any.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package Example
|
||||
|
||||
const (
|
||||
AnyNONE = 0
|
||||
AnyMonster = 1
|
||||
)
|
||||
@@ -2,11 +2,6 @@
|
||||
|
||||
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;
|
||||
|
||||
9
tests/MyGame/Example/Color.go
Normal file
9
tests/MyGame/Example/Color.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package Example
|
||||
|
||||
const (
|
||||
ColorRed = 1
|
||||
ColorGreen = 2
|
||||
ColorBlue = 8
|
||||
)
|
||||
@@ -2,14 +2,9 @@
|
||||
|
||||
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;
|
||||
public static final byte Red = 1;
|
||||
public static final byte Green = 2;
|
||||
public static final byte Blue = 8;
|
||||
};
|
||||
|
||||
|
||||
215
tests/MyGame/Example/Monster.go
Normal file
215
tests/MyGame/Example/Monster.go
Normal file
@@ -0,0 +1,215 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package Example
|
||||
|
||||
import (
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
)
|
||||
type Monster struct {
|
||||
_tab flatbuffers.Table
|
||||
}
|
||||
|
||||
func GetRootAsMonster(buf []byte, offset flatbuffers.UOffsetT) *Monster {
|
||||
n := flatbuffers.GetUOffsetT(buf[offset:])
|
||||
x := &Monster{}
|
||||
x.Init(buf, n + offset)
|
||||
return x
|
||||
}
|
||||
|
||||
func (rcv *Monster) Init(buf []byte, i flatbuffers.UOffsetT) {
|
||||
rcv._tab.Bytes = buf
|
||||
rcv._tab.Pos = i
|
||||
}
|
||||
|
||||
func (rcv *Monster) Pos(obj *Vec3) *Vec3 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
|
||||
if o != 0 {
|
||||
x := o + rcv._tab.Pos
|
||||
if obj == nil {
|
||||
obj = new(Vec3)
|
||||
}
|
||||
obj.Init(rcv._tab.Bytes, x)
|
||||
return obj
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rcv *Monster) Mana() int16 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(6))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetInt16(o + rcv._tab.Pos)
|
||||
}
|
||||
return 150
|
||||
}
|
||||
|
||||
func (rcv *Monster) Hp() int16 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(8))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetInt16(o + rcv._tab.Pos)
|
||||
}
|
||||
return 100
|
||||
}
|
||||
|
||||
func (rcv *Monster) Name() string {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(10))
|
||||
if o != 0 {
|
||||
return rcv._tab.String(o)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (rcv *Monster) Inventory(j int) byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(14))
|
||||
if o != 0 {
|
||||
a := rcv._tab.Vector(o)
|
||||
return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j * 1))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) InventoryLength() int {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(14))
|
||||
if o != 0 {
|
||||
return rcv._tab.VectorLen(o)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Color() int8 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(16))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetInt8(o + rcv._tab.Pos)
|
||||
}
|
||||
return 8
|
||||
}
|
||||
|
||||
func (rcv *Monster) TestType() byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(18))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetByte(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Test(obj *flatbuffers.Table) bool {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(20))
|
||||
if o != 0 {
|
||||
rcv._tab.Union(obj, o)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (rcv *Monster) Test4(obj *Test, j int) bool {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(22))
|
||||
if o != 0 {
|
||||
x := rcv._tab.Vector(o)
|
||||
x += flatbuffers.UOffsetT(j) * 4
|
||||
if obj == nil {
|
||||
obj = new(Test)
|
||||
}
|
||||
obj.Init(rcv._tab.Bytes, x)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (rcv *Monster) Test4Length() int {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(22))
|
||||
if o != 0 {
|
||||
return rcv._tab.VectorLen(o)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testarrayofstring(j int) string {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(24))
|
||||
if o != 0 {
|
||||
a := rcv._tab.Vector(o)
|
||||
return rcv._tab.String(a + flatbuffers.UOffsetT(j * 4))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (rcv *Monster) TestarrayofstringLength() int {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(24))
|
||||
if o != 0 {
|
||||
return rcv._tab.VectorLen(o)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
/// an example documentation comment: this will end up in the generated code multiline too
|
||||
func (rcv *Monster) Testarrayoftables(obj *Monster, j int) bool {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(26))
|
||||
if o != 0 {
|
||||
x := rcv._tab.Vector(o)
|
||||
x += flatbuffers.UOffsetT(j) * 4
|
||||
x = rcv._tab.Indirect(x)
|
||||
if obj == nil {
|
||||
obj = new(Monster)
|
||||
}
|
||||
obj.Init(rcv._tab.Bytes, x)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (rcv *Monster) TestarrayoftablesLength() int {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(26))
|
||||
if o != 0 {
|
||||
return rcv._tab.VectorLen(o)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) Enemy(obj *Monster) *Monster {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(28))
|
||||
if o != 0 {
|
||||
x := rcv._tab.Indirect(o + rcv._tab.Pos)
|
||||
if obj == nil {
|
||||
obj = new(Monster)
|
||||
}
|
||||
obj.Init(rcv._tab.Bytes, x)
|
||||
return obj
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testnestedflatbuffer(j int) byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(30))
|
||||
if o != 0 {
|
||||
a := rcv._tab.Vector(o)
|
||||
return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j * 1))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) TestnestedflatbufferLength() int {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(30))
|
||||
if o != 0 {
|
||||
return rcv._tab.VectorLen(o)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(14) }
|
||||
func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) }
|
||||
func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { builder.PrependInt16Slot(1, mana, 150) }
|
||||
func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { builder.PrependInt16Slot(2, hp, 100) }
|
||||
func MonsterAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(name), 0) }
|
||||
func MonsterAddInventory(builder *flatbuffers.Builder, inventory flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(5, flatbuffers.UOffsetT(inventory), 0) }
|
||||
func MonsterStartInventoryVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems) }
|
||||
func MonsterAddColor(builder *flatbuffers.Builder, color int8) { builder.PrependInt8Slot(6, color, 8) }
|
||||
func MonsterAddTestType(builder *flatbuffers.Builder, testType byte) { builder.PrependByteSlot(7, testType, 0) }
|
||||
func MonsterAddTest(builder *flatbuffers.Builder, test flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(8, flatbuffers.UOffsetT(test), 0) }
|
||||
func MonsterAddTest4(builder *flatbuffers.Builder, test4 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(9, flatbuffers.UOffsetT(test4), 0) }
|
||||
func MonsterStartTest4Vector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems) }
|
||||
func MonsterAddTestarrayofstring(builder *flatbuffers.Builder, testarrayofstring flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(10, flatbuffers.UOffsetT(testarrayofstring), 0) }
|
||||
func MonsterStartTestarrayofstringVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems) }
|
||||
func MonsterAddTestarrayoftables(builder *flatbuffers.Builder, testarrayoftables flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(11, flatbuffers.UOffsetT(testarrayoftables), 0) }
|
||||
func MonsterStartTestarrayoftablesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems) }
|
||||
func MonsterAddEnemy(builder *flatbuffers.Builder, enemy flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(12, flatbuffers.UOffsetT(enemy), 0) }
|
||||
func MonsterAddTestnestedflatbuffer(builder *flatbuffers.Builder, testnestedflatbuffer flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(13, flatbuffers.UOffsetT(testnestedflatbuffer), 0) }
|
||||
func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems) }
|
||||
func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() }
|
||||
@@ -14,29 +14,48 @@ public class Monster extends Table {
|
||||
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 String name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : 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 color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 8; }
|
||||
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 String testarrayofstring(int j) { int o = __offset(24); return o != 0 ? __string(__vector(o) + j * 4) : null; }
|
||||
public int testarrayofstringLength() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; }
|
||||
/// an example documentation comment: this will end up in the generated code multiline too
|
||||
public Monster testarrayoftables(int j) { return testarrayoftables(new Monster(), j); }
|
||||
public Monster testarrayoftables(Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__init(__indirect(__vector(o) + j * 4), bb) : null; }
|
||||
public int testarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; }
|
||||
public Monster enemy() { return enemy(new Monster()); }
|
||||
public Monster enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
public byte testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
|
||||
public Monster testempty() { return testempty(new Monster()); }
|
||||
public Monster testempty(Monster obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
|
||||
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 startMonster(FlatBufferBuilder builder) { builder.startObject(15); }
|
||||
public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); }
|
||||
public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); }
|
||||
public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); }
|
||||
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 addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(3, nameOffset, 0); }
|
||||
public static void addInventory(FlatBufferBuilder builder, int inventoryOffset) { builder.addOffset(5, inventoryOffset, 0); }
|
||||
public static void startInventoryVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
|
||||
public static void addColor(FlatBufferBuilder builder, byte color) { builder.addByte(6, color, 8); }
|
||||
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 void addTest(FlatBufferBuilder builder, int testOffset) { builder.addOffset(8, testOffset, 0); }
|
||||
public static void addTest4(FlatBufferBuilder builder, int test4Offset) { builder.addOffset(9, test4Offset, 0); }
|
||||
public static void startTest4Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 2); }
|
||||
public static void addTestarrayofstring(FlatBufferBuilder builder, int testarrayofstringOffset) { builder.addOffset(10, testarrayofstringOffset, 0); }
|
||||
public static void startTestarrayofstringVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
|
||||
public static void addTestarrayoftables(FlatBufferBuilder builder, int testarrayoftablesOffset) { builder.addOffset(11, testarrayoftablesOffset, 0); }
|
||||
public static void startTestarrayoftablesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
|
||||
public static void addEnemy(FlatBufferBuilder builder, int enemyOffset) { builder.addOffset(12, enemyOffset, 0); }
|
||||
public static void addTestnestedflatbuffer(FlatBufferBuilder builder, int testnestedflatbufferOffset) { builder.addOffset(13, testnestedflatbufferOffset, 0); }
|
||||
public static void startTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
|
||||
public static void addTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.addOffset(14, testemptyOffset, 0); }
|
||||
public static int endMonster(FlatBufferBuilder builder) { return builder.endObject(); }
|
||||
};
|
||||
|
||||
|
||||
26
tests/MyGame/Example/Test.go
Normal file
26
tests/MyGame/Example/Test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package Example
|
||||
|
||||
import (
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
)
|
||||
type Test struct {
|
||||
_tab flatbuffers.Struct
|
||||
}
|
||||
|
||||
func (rcv *Test) Init(buf []byte, i flatbuffers.UOffsetT) {
|
||||
rcv._tab.Bytes = buf
|
||||
rcv._tab.Pos = i
|
||||
}
|
||||
|
||||
func (rcv *Test) A() int16 { return rcv._tab.GetInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0)) }
|
||||
func (rcv *Test) B() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2)) }
|
||||
|
||||
func CreateTest(builder *flatbuffers.Builder, a int16, b int8) flatbuffers.UOffsetT {
|
||||
builder.Prep(2, 4)
|
||||
builder.Pad(1)
|
||||
builder.PrependInt8(b)
|
||||
builder.PrependInt16(a)
|
||||
return builder.Offset()
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public class Test extends Struct {
|
||||
public byte b() { return bb.get(bb_pos + 2); }
|
||||
|
||||
public static int createTest(FlatBufferBuilder builder, short a, byte b) {
|
||||
builder.prep(2, 0);
|
||||
builder.prep(2, 4);
|
||||
builder.pad(1);
|
||||
builder.putByte(b);
|
||||
builder.putShort(a);
|
||||
|
||||
45
tests/MyGame/Example/Vec3.go
Normal file
45
tests/MyGame/Example/Vec3.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// automatically generated, do not modify
|
||||
|
||||
package Example
|
||||
|
||||
import (
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
)
|
||||
type Vec3 struct {
|
||||
_tab flatbuffers.Struct
|
||||
}
|
||||
|
||||
func (rcv *Vec3) Init(buf []byte, i flatbuffers.UOffsetT) {
|
||||
rcv._tab.Bytes = buf
|
||||
rcv._tab.Pos = i
|
||||
}
|
||||
|
||||
func (rcv *Vec3) X() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) }
|
||||
func (rcv *Vec3) Y() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) }
|
||||
func (rcv *Vec3) Z() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8)) }
|
||||
func (rcv *Vec3) Test1() float64 { return rcv._tab.GetFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16)) }
|
||||
func (rcv *Vec3) Test2() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24)) }
|
||||
func (rcv *Vec3) Test3(obj *Test) *Test {
|
||||
if obj == nil {
|
||||
obj = new(Test)
|
||||
}
|
||||
obj.Init(rcv._tab.Bytes, rcv._tab.Pos + 26)
|
||||
return obj
|
||||
}
|
||||
|
||||
func CreateVec3(builder *flatbuffers.Builder, x float32, y float32, z float32, test1 float64, test2 int8, Test_a int16, Test_b int8) flatbuffers.UOffsetT {
|
||||
builder.Prep(16, 32)
|
||||
builder.Pad(2)
|
||||
builder.Prep(2, 4)
|
||||
builder.Pad(1)
|
||||
builder.PrependInt8(Test_b)
|
||||
builder.PrependInt16(Test_a)
|
||||
builder.Pad(1)
|
||||
builder.PrependInt8(test2)
|
||||
builder.PrependFloat64(test1)
|
||||
builder.Pad(4)
|
||||
builder.PrependFloat32(z)
|
||||
builder.PrependFloat32(y)
|
||||
builder.PrependFloat32(x)
|
||||
return builder.Offset()
|
||||
}
|
||||
@@ -18,9 +18,9 @@ public class Vec3 extends Struct {
|
||||
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.prep(16, 32);
|
||||
builder.pad(2);
|
||||
builder.prep(2, 0);
|
||||
builder.prep(2, 4);
|
||||
builder.pad(1);
|
||||
builder.putByte(Test_b);
|
||||
builder.putShort(Test_a);
|
||||
|
||||
1104
tests/go_test.go
Normal file
1104
tests/go_test.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace MyGame.Example;
|
||||
|
||||
enum Color:byte { Red = 0, Green, Blue = 2 }
|
||||
enum Color:byte (bit_flags) { Red = 0, Green, Blue = 3 }
|
||||
|
||||
union Any { Monster } // TODO: add more elements
|
||||
|
||||
@@ -18,17 +18,25 @@ struct Vec3 (force_align: 16) {
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3;
|
||||
mana:short = 150;
|
||||
hp:short = 100;
|
||||
name:string;
|
||||
friendly:bool = false (deprecated, priority: 1);
|
||||
inventory:[ubyte];
|
||||
pos:Vec3 (id: 0);
|
||||
hp:short = 100 (id: 2);
|
||||
mana:short = 150 (id: 1);
|
||||
name:string (id: 3);
|
||||
color:Color = Blue (id: 6);
|
||||
inventory:[ubyte] (id: 5);
|
||||
friendly:bool = false (deprecated, priority: 1, id: 4);
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
color:Color = Blue;
|
||||
test:Any;
|
||||
test4:[Test];
|
||||
testarrayoftables:[Monster] (id: 11);
|
||||
testarrayofstring:[string] (id: 10);
|
||||
enemy:Monster (id:12);
|
||||
test:Any (id: 8);
|
||||
test4:[Test] (id: 9);
|
||||
testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster");
|
||||
testempty:Monster (id:14);
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
|
||||
file_identifier "MONS";
|
||||
file_extension "mon";
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
// automatically generated, do not modify
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
#ifndef FLATBUFFERS_GENERATED_MONSTER_TEST_MYGAME_EXAMPLE_H_
|
||||
#define FLATBUFFERS_GENERATED_MONSTER_TEST_MYGAME_EXAMPLE_H_
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
@@ -6,21 +9,21 @@ namespace MyGame {
|
||||
namespace Example {
|
||||
|
||||
enum {
|
||||
Color_Red = 0,
|
||||
Color_Green = 1,
|
||||
Color_Blue = 2,
|
||||
Color_Red = 1,
|
||||
Color_Green = 2,
|
||||
Color_Blue = 8
|
||||
};
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
static const char *names[] = { "Red", "Green", "Blue", nullptr };
|
||||
static const char *names[] = { "Red", "Green", "", "", "", "", "", "Blue", nullptr };
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e]; }
|
||||
inline const char *EnumNameColor(int e) { return EnumNamesColor()[e - Color_Red]; }
|
||||
|
||||
enum {
|
||||
Any_NONE = 0,
|
||||
Any_Monster = 1,
|
||||
Any_Monster = 1
|
||||
};
|
||||
|
||||
inline const char **EnumNamesAny() {
|
||||
@@ -30,6 +33,8 @@ inline const char **EnumNamesAny() {
|
||||
|
||||
inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
|
||||
|
||||
bool VerifyAny(const flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type);
|
||||
|
||||
struct Test;
|
||||
struct Vec3;
|
||||
struct Monster;
|
||||
@@ -80,11 +85,45 @@ struct Monster : private flatbuffers::Table {
|
||||
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); }
|
||||
int8_t color() const { return GetField<int8_t>(16, 8); }
|
||||
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); }
|
||||
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *testarrayofstring() const { return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *>(24); }
|
||||
/// an example documentation comment: this will end up in the generated code multiline too
|
||||
const flatbuffers::Vector<flatbuffers::Offset<Monster>> *testarrayoftables() const { return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<Monster>> *>(26); }
|
||||
const Monster *enemy() const { return GetPointer<const Monster *>(28); }
|
||||
const flatbuffers::Vector<uint8_t> *testnestedflatbuffer() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(30); }
|
||||
const Monster *testnestedflatbuffer_nested_root() { return flatbuffers::GetRoot<Monster>(testnestedflatbuffer()->Data()); }
|
||||
const Monster *testempty() const { return GetPointer<const Monster *>(32); }
|
||||
bool Verify(const flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTable(verifier) &&
|
||||
VerifyField<Vec3>(verifier, 4 /* pos */) &&
|
||||
VerifyField<int16_t>(verifier, 6 /* mana */) &&
|
||||
VerifyField<int16_t>(verifier, 8 /* hp */) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 10 /* name */) &&
|
||||
verifier.Verify(name()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 14 /* inventory */) &&
|
||||
verifier.Verify(inventory()) &&
|
||||
VerifyField<int8_t>(verifier, 16 /* color */) &&
|
||||
VerifyField<uint8_t>(verifier, 18 /* test_type */) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 20 /* test */) &&
|
||||
VerifyAny(verifier, test(), test_type()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 22 /* test4 */) &&
|
||||
verifier.Verify(test4()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 24 /* testarrayofstring */) &&
|
||||
verifier.Verify(testarrayofstring()) &&
|
||||
verifier.VerifyVectorOfStrings(testarrayofstring()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 26 /* testarrayoftables */) &&
|
||||
verifier.Verify(testarrayoftables()) &&
|
||||
verifier.VerifyVectorOfTables(testarrayoftables()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 28 /* enemy */) &&
|
||||
verifier.VerifyTable(enemy()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 30 /* testnestedflatbuffer */) &&
|
||||
verifier.Verify(testnestedflatbuffer()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 32 /* testempty */) &&
|
||||
verifier.VerifyTable(testempty());
|
||||
}
|
||||
};
|
||||
|
||||
struct MonsterBuilder {
|
||||
@@ -95,16 +134,41 @@ struct MonsterBuilder {
|
||||
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_color(int8_t color) { fbb_.AddElement<int8_t>(16, color, 8); }
|
||||
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); }
|
||||
void add_testarrayofstring(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> testarrayofstring) { fbb_.AddOffset(24, testarrayofstring); }
|
||||
void add_testarrayoftables(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Monster>>> testarrayoftables) { fbb_.AddOffset(26, testarrayoftables); }
|
||||
void add_enemy(flatbuffers::Offset<Monster> enemy) { fbb_.AddOffset(28, enemy); }
|
||||
void add_testnestedflatbuffer(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer) { fbb_.AddOffset(30, testnestedflatbuffer); }
|
||||
void add_testempty(flatbuffers::Offset<Monster> testempty) { fbb_.AddOffset(32, testempty); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
flatbuffers::Offset<Monster> Finish() { return flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 10)); }
|
||||
MonsterBuilder &operator=(const MonsterBuilder &);
|
||||
flatbuffers::Offset<Monster> Finish() { return flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 15)); }
|
||||
};
|
||||
|
||||
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) {
|
||||
inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb,
|
||||
const Vec3 *pos = 0,
|
||||
int16_t mana = 150,
|
||||
int16_t hp = 100,
|
||||
flatbuffers::Offset<flatbuffers::String> name = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> inventory = 0,
|
||||
int8_t color = 8,
|
||||
uint8_t test_type = 0,
|
||||
flatbuffers::Offset<void> test = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<const Test *>> test4 = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> testarrayofstring = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Monster>>> testarrayoftables = 0,
|
||||
flatbuffers::Offset<Monster> enemy = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer = 0,
|
||||
flatbuffers::Offset<Monster> testempty = 0) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_testempty(testempty);
|
||||
builder_.add_testnestedflatbuffer(testnestedflatbuffer);
|
||||
builder_.add_enemy(enemy);
|
||||
builder_.add_testarrayoftables(testarrayoftables);
|
||||
builder_.add_testarrayofstring(testarrayofstring);
|
||||
builder_.add_test4(test4);
|
||||
builder_.add_test(test);
|
||||
builder_.add_inventory(inventory);
|
||||
@@ -117,7 +181,23 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
bool VerifyAny(const flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type) {
|
||||
switch (type) {
|
||||
case Any_NONE: return true;
|
||||
case Any_Monster: return verifier.VerifyTable(reinterpret_cast<const Monster *>(union_obj));
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
|
||||
|
||||
}; // namespace MyGame
|
||||
}; // namespace Example
|
||||
inline bool VerifyMonsterBuffer(const flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer<Monster>(); }
|
||||
|
||||
inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<Monster> root) { fbb.Finish(root, "MONS"); }
|
||||
|
||||
inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, "MONS"); }
|
||||
|
||||
} // namespace Example
|
||||
} // namespace MyGame
|
||||
|
||||
#endif // FLATBUFFERS_GENERATED_MONSTER_TEST_MYGAME_EXAMPLE_H_
|
||||
|
||||
BIN
tests/monsterdata_test.bin
Normal file
BIN
tests/monsterdata_test.bin
Normal file
Binary file not shown.
42
tests/monsterdata_test.golden
Normal file
42
tests/monsterdata_test.golden
Normal file
@@ -0,0 +1,42 @@
|
||||
{
|
||||
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: Monster,
|
||||
test: {
|
||||
hp: 20
|
||||
},
|
||||
test4: [
|
||||
{
|
||||
a: 10,
|
||||
b: 20
|
||||
},
|
||||
{
|
||||
a: 30,
|
||||
b: 40
|
||||
}
|
||||
],
|
||||
testarrayofstring: [
|
||||
"test1",
|
||||
"test2"
|
||||
],
|
||||
testempty: {
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@
|
||||
3,
|
||||
4
|
||||
],
|
||||
test_type: 1,
|
||||
test_type: Monster,
|
||||
test: {
|
||||
hp: 20
|
||||
},
|
||||
|
||||
Binary file not shown.
123
tests/test.cpp
123
tests/test.cpp
@@ -47,8 +47,8 @@ void TestEq(T expval, U val, const char *exp, const char *file, int line) {
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST_EQ(exp, val) TestEq( exp, val, #exp, __FILE__, __LINE__)
|
||||
#define TEST_NOTNULL(exp) TestEq(!exp, false, #exp, __FILE__, __LINE__)
|
||||
#define TEST_EQ(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__)
|
||||
#define TEST_NOTNULL(exp) TestEq(exp == NULL, false, #exp, __FILE__, __LINE__)
|
||||
|
||||
// Include simple random number generator to ensure results will be the
|
||||
// same cross platform.
|
||||
@@ -79,12 +79,21 @@ std::string CreateFlatBufferTest() {
|
||||
mb.add_hp(20);
|
||||
auto mloc2 = mb.Finish();
|
||||
|
||||
// Create an array of strings:
|
||||
flatbuffers::Offset<flatbuffers::String> strings[2];
|
||||
strings[0] = builder.CreateString("bob");
|
||||
strings[1] = builder.CreateString("fred");
|
||||
auto vecofstrings = builder.CreateVector(strings, 2);
|
||||
|
||||
// Create an array of tables:
|
||||
auto vecoftables = builder.CreateVector(&mloc2, 1);
|
||||
|
||||
// shortcut for creating monster with all fields set:
|
||||
auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
|
||||
Any_Monster, mloc2.Union(), // Store a union.
|
||||
testv);
|
||||
testv, vecofstrings, vecoftables, 0);
|
||||
|
||||
builder.Finish(mloc);
|
||||
FinishMonsterBuffer(builder, mloc);
|
||||
|
||||
#ifdef FLATBUFFERS_TEST_VERBOSE
|
||||
// print byte data for debugging:
|
||||
@@ -101,6 +110,15 @@ std::string CreateFlatBufferTest() {
|
||||
// example of accessing a buffer loaded in memory:
|
||||
void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
|
||||
// First, verify the buffers integrity (optional)
|
||||
flatbuffers::Verifier verifier(
|
||||
reinterpret_cast<const uint8_t *>(flatbuf.c_str()),
|
||||
flatbuf.length());
|
||||
TEST_EQ(VerifyMonsterBuffer(verifier), true);
|
||||
|
||||
TEST_EQ(MonsterBufferHasIdentifier(flatbuf.c_str()), true);
|
||||
|
||||
// Access the buffer from the root.
|
||||
auto monster = GetMonster(flatbuf.c_str());
|
||||
|
||||
TEST_EQ(monster->hp(), 80);
|
||||
@@ -119,8 +137,8 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
auto inventory = monster->inventory();
|
||||
TEST_NOTNULL(inventory);
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
for (flatbuffers::uoffset_t i = 0; i < inventory->Length(); i++)
|
||||
TEST_EQ(inventory->Get(i), inv_data[i]);
|
||||
for (auto it = inventory->begin(); it != inventory->end(); ++it)
|
||||
TEST_EQ(*it, inv_data[it - inventory->begin()]);
|
||||
|
||||
// Example of accessing a union:
|
||||
TEST_EQ(monster->test_type(), Any_Monster); // First make sure which it is.
|
||||
@@ -128,20 +146,36 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
TEST_NOTNULL(monster2);
|
||||
TEST_EQ(monster2->hp(), 20);
|
||||
|
||||
// Example of accessing a vector of strings:
|
||||
auto vecofstrings = monster->testarrayofstring();
|
||||
TEST_EQ(vecofstrings->Length(), 2U);
|
||||
TEST_EQ(strcmp(vecofstrings->Get(0)->c_str(), "bob"), 0);
|
||||
TEST_EQ(strcmp(vecofstrings->Get(1)->c_str(), "fred"), 0);
|
||||
|
||||
// Example of accessing a vector of tables:
|
||||
auto vecoftables = monster->testarrayoftables();
|
||||
TEST_EQ(vecoftables->Length(), 1U);
|
||||
for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
|
||||
TEST_EQ(it->hp(), 20);
|
||||
|
||||
// Since Flatbuffers uses explicit mechanisms to override the default
|
||||
// compiler alignment, double check that the compiler indeed obeys them:
|
||||
// (Test consists of a short and byte):
|
||||
TEST_EQ(flatbuffers::AlignOf<Test>(), static_cast<size_t>(2));
|
||||
TEST_EQ(sizeof(Test), static_cast<size_t>(4));
|
||||
TEST_EQ(flatbuffers::AlignOf<Test>(), 2UL);
|
||||
TEST_EQ(sizeof(Test), 4UL);
|
||||
|
||||
auto tests = monster->test4();
|
||||
TEST_NOTNULL(tests);
|
||||
auto &test_0 = tests->Get(0);
|
||||
auto &test_1 = tests->Get(1);
|
||||
TEST_EQ(test_0.a(), 10);
|
||||
TEST_EQ(test_0.b(), 20);
|
||||
TEST_EQ(test_1.a(), 30);
|
||||
TEST_EQ(test_1.b(), 40);
|
||||
auto test_0 = tests->Get(0);
|
||||
auto test_1 = tests->Get(1);
|
||||
TEST_EQ(test_0->a(), 10);
|
||||
TEST_EQ(test_0->b(), 20);
|
||||
TEST_EQ(test_1->a(), 30);
|
||||
TEST_EQ(test_1->b(), 40);
|
||||
for (auto it = tests->begin(); it != tests->end(); ++it) {
|
||||
TEST_EQ(it->a() == 10 || it->a() == 30, true); // Just testing iterators.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// example of parsing text straight into a buffer, and generating
|
||||
@@ -154,7 +188,7 @@ void ParseAndGenerateTextTest() {
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
"tests/monster_test.fbs", false, &schemafile), true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
"tests/monsterdata_test.json", false, &jsonfile), true);
|
||||
"tests/monsterdata_test.golden", false, &jsonfile), true);
|
||||
|
||||
// parse schema first, so we can use it to parse the data after
|
||||
flatbuffers::Parser parser;
|
||||
@@ -163,10 +197,16 @@ void ParseAndGenerateTextTest() {
|
||||
|
||||
// here, parser.builder_ contains a binary buffer that is the parsed data.
|
||||
|
||||
// First, verify it, just in case:
|
||||
flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
|
||||
parser.builder_.GetSize());
|
||||
TEST_EQ(VerifyMonsterBuffer(verifier), true);
|
||||
|
||||
// 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);
|
||||
flatbuffers::GeneratorOptions opts;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), opts, &jsongen);
|
||||
|
||||
if (jsongen != jsonfile) {
|
||||
printf("%s----------------\n%s", jsongen.c_str(), jsonfile.c_str());
|
||||
@@ -175,7 +215,8 @@ void ParseAndGenerateTextTest() {
|
||||
}
|
||||
|
||||
template<typename T> void CompareTableFieldValue(flatbuffers::Table *table,
|
||||
int voffset, T val) {
|
||||
flatbuffers::voffset_t voffset,
|
||||
T val) {
|
||||
T read = table->GetField(voffset, static_cast<T>(0));
|
||||
TEST_EQ(read, val);
|
||||
}
|
||||
@@ -199,7 +240,7 @@ void FuzzTest1() {
|
||||
const double double_val = 3.14159265359;
|
||||
|
||||
const int test_values_max = 11;
|
||||
const int fields_per_object = 4;
|
||||
const flatbuffers::voffset_t fields_per_object = 4;
|
||||
const int num_fuzz_objects = 10000; // The higher, the more thorough :)
|
||||
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
@@ -212,9 +253,9 @@ void FuzzTest1() {
|
||||
// fields_per_object fields, each of a random type.
|
||||
for (int i = 0; i < num_fuzz_objects; i++) {
|
||||
auto start = builder.StartTable();
|
||||
for (int f = 0; f < fields_per_object; f++) {
|
||||
for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
|
||||
int choice = lcg_rand() % test_values_max;
|
||||
flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
|
||||
auto off = flatbuffers::FieldIndexToOffset(f);
|
||||
switch (choice) {
|
||||
case 0: builder.AddElement<uint8_t >(off, bool_val, 0); break;
|
||||
case 1: builder.AddElement<int8_t >(off, char_val, 0); break;
|
||||
@@ -242,7 +283,7 @@ void FuzzTest1() {
|
||||
// so this is deterministic.
|
||||
for (int i = 0; i < num_fuzz_objects; i++) {
|
||||
auto table = reinterpret_cast<flatbuffers::Table *>(eob - objects[i]);
|
||||
for (int f = 0; f < fields_per_object; f++) {
|
||||
for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
|
||||
int choice = lcg_rand() % test_values_max;
|
||||
flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
|
||||
switch (choice) {
|
||||
@@ -373,7 +414,9 @@ void FuzzTest2() {
|
||||
TEST_EQ(parser.Parse(json.c_str()), true);
|
||||
|
||||
std::string jsongen;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), 0, &jsongen);
|
||||
flatbuffers::GeneratorOptions opts;
|
||||
opts.indent_step = 0;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), opts, &jsongen);
|
||||
|
||||
if (jsongen != json) {
|
||||
// These strings are larger than a megabyte, so we show the bytes around
|
||||
@@ -429,12 +472,14 @@ void ErrorTest() {
|
||||
TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
|
||||
TestError("struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
|
||||
"{ V:{ Y:1 } }", "incomplete");
|
||||
TestError("table X { Y:byte; } root_type X; { Y:U }", "valid enum");
|
||||
TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
|
||||
"unknown enum value");
|
||||
TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
|
||||
TestError("enum X:byte { Y } enum X {", "enum already");
|
||||
TestError("enum X:float {}", "underlying");
|
||||
TestError("enum X:byte { Y, Y }", "value already");
|
||||
TestError("enum X:byte { Y=2, Z=1 }", "ascending");
|
||||
TestError("enum X:byte (bit_flags) { Y=8 }", "bit flag out");
|
||||
TestError("table X { Y:int; } table X {", "datatype already");
|
||||
TestError("struct X (force_align: 7) { Y:int; }", "force_align");
|
||||
TestError("{}", "no root");
|
||||
@@ -445,7 +490,35 @@ void ErrorTest() {
|
||||
TestError("union Z { X } struct X { Y:int; }", "only tables");
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
// Additional parser testing not covered elsewhere.
|
||||
void ScientificTest() {
|
||||
flatbuffers::Parser parser;
|
||||
|
||||
// Simple schema.
|
||||
TEST_EQ(parser.Parse("table X { Y:float; } root_type X;"), true);
|
||||
|
||||
// Test scientific notation numbers.
|
||||
TEST_EQ(parser.Parse("{ Y:0.0314159e+2 }"), true);
|
||||
auto root = flatbuffers::GetRoot<float>(parser.builder_.GetBufferPointer());
|
||||
// root will point to the table, which is a 32bit vtable offset followed
|
||||
// by a float:
|
||||
TEST_EQ(fabs(root[1] - 3.14159) < 0.001, true);
|
||||
}
|
||||
|
||||
void EnumStringsTest() {
|
||||
flatbuffers::Parser parser1;
|
||||
TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }"
|
||||
"root_type T;"
|
||||
"{ F:[ A, B, \"C\", \"A B C\" ] }"), true);
|
||||
flatbuffers::Parser parser2;
|
||||
TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }"
|
||||
"root_type T;"
|
||||
"{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"), true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
// Run our various test suites:
|
||||
|
||||
auto flatbuf = CreateFlatBufferTest();
|
||||
@@ -459,6 +532,8 @@ int main(int argc, const char *argv[]) {
|
||||
FuzzTest2();
|
||||
|
||||
ErrorTest();
|
||||
ScientificTest();
|
||||
EnumStringsTest();
|
||||
|
||||
if (!testing_fails) {
|
||||
TEST_OUTPUT_LINE("ALL TESTS PASSED");
|
||||
|
||||
Reference in New Issue
Block a user