Compare commits

...

80 Commits

Author SHA1 Message Date
Wouter van Oortmerssen
20a400e940 Update version number to 1.9
Change-Id: I0b0ed43129c4d8fc88a177792df50adfeb52a1e3
2018-04-05 15:11:52 -07:00
Wouter van Oortmerssen
676f0248aa Added missing generated code files.
Change-Id: I59af6ca1825870461c0badc7e7a045b97befdc1a
2018-04-05 15:01:35 -07:00
Kamil Rojewski
34b8b80f15 Missing TS links in docs (#4693)
* Eclipse ignore

* TypeScript support

* Prefixing enums

* Test results

* Merged JS and TS generators

* Fixed AppVeyor build problems

* Fixed more AppVeyor build problems

* Fixed more AppVeyor build problems

* Changed TS flag to options struct

* Storing options by value

* Removed unneeded const

* Re-export support for unions

* Uint support

* Casting bools to numbers for mutation

* TS shell tests

* Reverted generates js test file to original version

* Backing up js tests and properly generating test data

* Not importing flatbuffers for TS test generation

* Not overwriting generated js for tests

* AppVeyor test fixes

* Generating the most strict TS code possible

* Not returning null when creating vectors

* Not returning null from struct contructors

* Vector of unions for ts/js

* Sanity check for languages

* Indentation fix + output test files

* Vectors of unions for php

* Fixes to union vector handling + tests

* Fix for strictPropertyInitialization

* Fix for new aligned operator new for gcc >= 7.1

* Not generating imports/ns prefixes with --gen-all

* TypeScript docs

* Missing imports of enums

* Missing TS links
2018-04-05 09:54:37 -07:00
Juyeon Yoon
0998861e0f Remove line break of CMakeLists.txt (#4691)
Fix #4650
2018-04-05 09:17:33 -07:00
David Stephan
2e3d3cbcb5 Fix typos in C++ tutorial (#4685)
CreateMonster and MonsterBuilder examples fixed for C++
2018-04-02 09:25:05 -07:00
Kamil Rojewski
d3a00f7730 Fix for missing enum imports (#4683)
* Eclipse ignore

* TypeScript support

* Prefixing enums

* Test results

* Merged JS and TS generators

* Fixed AppVeyor build problems

* Fixed more AppVeyor build problems

* Fixed more AppVeyor build problems

* Changed TS flag to options struct

* Storing options by value

* Removed unneeded const

* Re-export support for unions

* Uint support

* Casting bools to numbers for mutation

* TS shell tests

* Reverted generates js test file to original version

* Backing up js tests and properly generating test data

* Not importing flatbuffers for TS test generation

* Not overwriting generated js for tests

* AppVeyor test fixes

* Generating the most strict TS code possible

* Not returning null when creating vectors

* Not returning null from struct contructors

* Vector of unions for ts/js

* Sanity check for languages

* Indentation fix + output test files

* Vectors of unions for php

* Fixes to union vector handling + tests

* Fix for strictPropertyInitialization

* Fix for new aligned operator new for gcc >= 7.1

* Not generating imports/ns prefixes with --gen-all

* TypeScript docs

* Missing imports of enums
2018-03-26 10:29:45 -07:00
Kamil Rojewski
cc54963830 TypeScript docs (#4680)
* Eclipse ignore

* TypeScript support

* Prefixing enums

* Test results

* Merged JS and TS generators

* Fixed AppVeyor build problems

* Fixed more AppVeyor build problems

* Fixed more AppVeyor build problems

* Changed TS flag to options struct

* Storing options by value

* Removed unneeded const

* Re-export support for unions

* Uint support

* Casting bools to numbers for mutation

* TS shell tests

* Reverted generates js test file to original version

* Backing up js tests and properly generating test data

* Not importing flatbuffers for TS test generation

* Not overwriting generated js for tests

* AppVeyor test fixes

* Generating the most strict TS code possible

* Not returning null when creating vectors

* Not returning null from struct contructors

* Vector of unions for ts/js

* Sanity check for languages

* Indentation fix + output test files

* Vectors of unions for php

* Fixes to union vector handling + tests

* Fix for strictPropertyInitialization

* Fix for new aligned operator new for gcc >= 7.1

* Not generating imports/ns prefixes with --gen-all

* TypeScript docs
2018-03-23 09:01:39 -07:00
Flaviu
79f62ee353 Const correctness in generated code and in code generators. Added missing \reflection\generate_code.bat file. (#4679) 2018-03-23 08:58:07 -07:00
Andreas Bergmeier
c0a6e5120d [C++, Go, Bazel]: Fix up Bazel support (#4672)
* Add Bazel support for Go library

* Make flatc compile again.

Was missing java_generator, which is now referenced from source.
2018-03-15 12:31:16 -07:00
Robert Schmidtke
08cf50c54a Java/C#/Python prefixed size support (#4445)
* initial changes to support size prefixed buffers in Java

* add slice equivalent to CSharp ByteBuffer

* resolve TODO for slicing in CSharp code generation

* add newly generated Java and CSharp test sources

* fix typo in comment

* add FinishSizePrefixed methods to CSharp FlatBufferBuilder as well

* add option to allow writing the prefix as well

* generate size-prefixed monster binary as well

* extend JavaTest to test the size prefixed binary as well

* use constants for size prefix length

* fuse common code for getRootAs and getSizePrefixedRootAs

* pulled file identifier out of if

* add FinishSizePrefixed, GetSizePrefixedRootAs support for Python

* Revert "extend JavaTest to test the size prefixed binary as well"

This reverts commit 68be4420dd.

* Revert "generate size-prefixed monster binary as well"

This reverts commit 2939516fdf.

* fix ByteBuffer.cs Slice() method; add proper CSharp and Java tests

* fix unused parameter

* increment version number

* pulled out generated methods into separate utility class

* pulled out generated methods into separate utility class for Python

* fix indentation

* remove unnecessary comment

* fix newline and copyright

* add ByteBufferUtil to csproj compilation

* hide ByteBuffer's internal data; track offset into parent's array

* test unsafe versions as well; compile and run in debug mode

* clarify help text for size prefix

* move ByteBuffer slicing behavior to subclass

* fix protection levels

* add size prefix support for text generation

* add ByteBufferSlice to csproj compilation

* revert size prefix handling for nested buffers

* use duplicate instead of slice for removing size prefix

* remove slice subclass and use duplicate for removing size prefix

* remove slice specific tests

* remove superfluous command line option
2018-03-12 11:30:46 -07:00
Sergey Avseyev
6b3f057bdc Remove unnecessary const qualifier from cast (#4666)
Const does not make sense here, and compiler actually throws warning
(error with -Werror) when you would try to compile it.

    In file included from include/flatbuffers/flexbuffers.h:24,
                     from include/flatbuffers/idl.h:26,
                     from include/flatbuffers/code_generators.h:22,
                     from src/code_generators.cpp:17:
    include/flatbuffers/util.h: In function ‘int flatbuffers::FromUTF8(const char**)’:
    include/flatbuffers/util.h:325:44: error: type qualifiers ignored on cast result type [-Werror=ignored-qualifiers]
       if ((static_cast<const unsigned char>(tmp) << len) & 0x80) return -1;  // Bit after leading 1's must be 0.
                                            ^
    cc1plus: all warnings being treated as errors

This warning caught by gcc8:

    $ g++ --version
    g++ (GCC) 8.0.1 20180222 (Red Hat 8.0.1-0.16)
2018-03-12 08:22:17 -07:00
ianXian
60de374486 Convert to unsigned char before applying bit shift operator (#4664)
* Convert to unsigned char before applying bit shift operator

* convert to const unsigned char inline

* convert to unsigned char inline
2018-03-09 09:45:47 -08:00
desqaz
e78825e7a0 Json : Add --size-prefixed option to flatc (#4645)
to be able to convert to json size prefixed buffers.
2018-03-09 08:21:28 -08:00
Montoli
cc158e7009 Swapped the order of two conditions in an assert. (#4663)
An assert in flexbuffers was bit-shifting a 64-bit number by
64 bits, which throws up warnings in some automated tools.

The same assert also checks to see if the number of bytes
being shifted is 8.  Swapped the order, so that the bitshift
only occurs if the number of bits being shifted is not 64.

Should be the same behavior, but plays nicer with diagnostic
tools.
2018-03-08 14:18:41 -08:00
desqaz
5377957b14 Update .gitignore with Ninja stuff (#4659) 2018-03-08 10:36:27 -08:00
Wouter van Oortmerssen
4bc6de9a88 Fix broken Java/C# codegen 2018-03-05 16:37:04 -08:00
Wouter van Oortmerssen
3a62813f0e Added missing generated code files.
Change-Id: Ie1cab284cb6e0fe5bd2b2c293c3136c148927ac3
2018-03-05 15:47:16 -08:00
Nik Hemmings
fb94af8899 Protobufs: Added '--oneof-union' option. (#4647)
* Added '--oneof-union' option.

Used with the .proto -> .fbs converter, will translate protobuff oneofs to flatbuffer unions.
Updated proto test to check both methods of converting oneofs.

* Added '--oneof-union' option.

Used with the .proto -> .fbs converter, will translate protobuff oneofs to flatbuffer unions.
Updated proto test to check both methods of converting oneofs.

* FlatBuffers: Moved MakeCamel() into idl_parser.cpp

Removes library dependency on Java/C# generator code.
2018-03-05 08:45:25 -08:00
smillius
77b458bee5 Make nested flatbuffer lookup consistent. (#4656)
Lookup type of nested flatbuffer field with either raw name or fully qualified name as already done in the parser.
LookupCreateStruct tries both the raw name and the fully qualified one.
Without this, we cannot reference types outside of the current namespace, e.g. in a different module.
2018-03-05 08:44:14 -08:00
Christian Helmich
9ce98dd77d Support attribute "cpp_ptr_type" on table elements marked as "hash" (#4643)
* added intended use-cases to monster_test.fbs

* added check for `cpp_ptr_type` on hashed fields
added default value 'naked' to `cpp_ptr_type` on hashed fields

* added C++ generation of cpp_type vectors
removed ctor call for vector fields
added condition !vector for cpp_type check
added Pack() and UnPack() code generation for vector of hashes
added generation of correct resolve/rehash for cpp_type elements

* added attribute 'cpp_ptr_type_get' to hold accessor for pointer types possible where '.get()' does not work
use case: cpp_ptr_type:"std::weak_ptr", cpp_ptr_type_get:".lock().get()"

* run flatc to re-generate headers

* added bool param is_ctor to GetDefaultScalarValue() to differentiate between usage places

* modified monster_test.fbs to remove usage of shared_ptr/weak_ptr
reason: STLport does not support std::shared_ptr and std::weak_ptr

* run flatc again to re-generate headers

* fixed symbol unique_ptr not in namespace std when building with STLport
2018-03-05 08:40:55 -08:00
Mitchel
4ea1be53d4 Java + C#, reuse object in lookup_by_key (#4648)
* Java + C#, reuse object in lookup_by_key

* Java + C#, reuse object in lookup_by_key
2018-03-02 15:22:46 -08:00
Mitchel
0068b25132 Fix for Java infinite loop encoding into 0-sized buffer (#4654) 2018-03-02 15:21:08 -08:00
Christian Helmich
ec74f58b94 made HashFnv functions constexpr (#4640)
* added FLATBUFFERS_CONSTEXPR_CPP14 define for C++14 and above constexpr

* made HashFnv functions constexpr (depending on FLATBUFFERS_CONSTEXPR)
2018-03-02 09:13:55 -08:00
Kamil Rojewski
48d8232584 Not generating imports/ns prefixes with --gen-all (#4644)
* Eclipse ignore

* TypeScript support

* Prefixing enums

* Test results

* Merged JS and TS generators

* Fixed AppVeyor build problems

* Fixed more AppVeyor build problems

* Fixed more AppVeyor build problems

* Changed TS flag to options struct

* Storing options by value

* Removed unneeded const

* Re-export support for unions

* Uint support

* Casting bools to numbers for mutation

* TS shell tests

* Reverted generates js test file to original version

* Backing up js tests and properly generating test data

* Not importing flatbuffers for TS test generation

* Not overwriting generated js for tests

* AppVeyor test fixes

* Generating the most strict TS code possible

* Not returning null when creating vectors

* Not returning null from struct contructors

* Vector of unions for ts/js

* Sanity check for languages

* Indentation fix + output test files

* Vectors of unions for php

* Fixes to union vector handling + tests

* Fix for strictPropertyInitialization

* Fix for new aligned operator new for gcc >= 7.1

* Not generating imports/ns prefixes with --gen-all
2018-02-26 08:56:04 -08:00
Mitchel
55ddb84eb2 In Java, allow reusing ByteBuffer in getters (#4633)
* In Java, allow reusing ByteBuffer in getters

* In Java, allow reusing ByteBuffer in getters

* In Java, allow reusing ByteBuffer in getters
2018-02-23 14:01:05 -08:00
Christian Helmich
6e2e909b5c template specialized Hash functions for 16-bit hash types (#4631)
added access to HashFunction for 16 bit
2018-02-23 10:29:37 -08:00
Philip S Doctor
b24c0b07a3 When Java raises a CharacterCodingException, the catch block rethrows this exception as a java.lang.Error. Per the docs https://docs.oracle.com/javase/8/docs/api/java/lang/Error.html an Error is a serious problem that applications should not attempt to catch such as ThreadDeath. In this case, it is reasonable for a consumer to try to catch this error as it likely indicates a problem with the underlying data. (#4630)
As Error does not inherit from Exception, a generic `catch(Exception ex)` will not catch this error.

A simple change from `Error` to `Exception` is not sufficient as an `Exception` is checked by the compiler, so in order to keep this issue unchecked, I am proposing raising a `RuntimeException` which is not checked, but is still a subclass of `Exception`.

The only possible breaking change would be if a consumer was explicitly catching `Error` already for this library.

https://github.com/google/flatbuffers/issues/4629
2018-02-22 14:54:35 -08:00
Christian Helmich
59e26017cb added support for parsing hash on vector elements (#4502)
* added support for parsing hash on vector elements

reversed check for scalar to check for vector

added C++ generation of cpp_type vectors

removed ctor call for vector fields
added condition !vector for cpp_type check

added Pack() and UnPack() code generation for vector of hashes

* schema change:
added table Referrable and weak references towards it from Monster

added single_weak_reference to Monster table
changed order with vector_of_weak_references

* re-generated monster schema dependent code

added Referrable.cs to FlatBuffers.Test.csproj
2018-02-22 11:04:26 -08:00
desqaz
132e6a8220 Add new c++ helpers to ease usage of size prefixed FlatBuffers (#4626)
It was missing some helpers when we choose to use
size prefixed FlatBuffers.

* Add general helper : GetPrefixedSize
* Add generated helpers :
 * GetSizePrefixedXXX
 * VerfifySizePrefixedXXXBuffer
 * FinishSizePrefixedXXXBuffer
2018-02-22 09:19:08 -08:00
Masato Nagai
19c81b11b3 [C++] better type mismatch error (#4623)
* better parse error

* pass str as a pointer instead of a reference for more efficient performance
2018-02-16 08:32:50 -08:00
Alexander Gallego
36f8564846 C++: mini_reflect: Add DefaultTypeTable (#4614)
* mini_reflect: Add DefaultTypeTable

Currently it's very easy to make a mistake when it comes to
instantiating the TypeTable to print a buffer because it is not type
safe.

This will allow us to write safer cpp code:

flatbuffers::FlatBufferToString(reinterpret_cast<const uint8_t *>(&t),
                                decltype(t)::DefaultTypeTable());

* c++: mini_reflect: update generated code

* Ensure types and names are set for mini_reflect

* c++: mini_refelct: update unit tests with new typed TypeTable

* Adding PR feedback of sylte and naming convention
2018-02-15 14:58:06 -08:00
Dmitry Volosnykh
daf0a420be Support version compatibility checks via find_package command (#4625) 2018-02-15 13:39:15 -08:00
Kamil Rojewski
474ba68bba Fix for aligned new for gcc >= 7.1 (#4621)
* Eclipse ignore

* TypeScript support

* Prefixing enums

* Test results

* Merged JS and TS generators

* Fixed AppVeyor build problems

* Fixed more AppVeyor build problems

* Fixed more AppVeyor build problems

* Changed TS flag to options struct

* Storing options by value

* Removed unneeded const

* Re-export support for unions

* Uint support

* Casting bools to numbers for mutation

* TS shell tests

* Reverted generates js test file to original version

* Backing up js tests and properly generating test data

* Not importing flatbuffers for TS test generation

* Not overwriting generated js for tests

* AppVeyor test fixes

* Generating the most strict TS code possible

* Not returning null when creating vectors

* Not returning null from struct contructors

* Vector of unions for ts/js

* Sanity check for languages

* Indentation fix + output test files

* Vectors of unions for php

* Fixes to union vector handling + tests

* Fix for strictPropertyInitialization

* Fix for new aligned operator new for gcc >= 7.1
2018-02-12 08:37:51 -08:00
vglavnyy
9de0861104 Fix vector_data failure under debug (#4606) 2018-02-08 15:15:17 -08:00
Wouter van Oortmerssen
b513db86c7 Misc fixes from internal integration / clang tidy.
Change-Id: Ic5e8f6a423b426abb9f8b90d39db0f85f28b94be
Tested: on Linux.
2018-02-08 14:58:16 -08:00
Sergey Avseyev
6a1acdc23b Fix compiler errors in parser (#4612)
src/idl_parser.cpp: In member function 'flatbuffers::CheckedError flatbuffers::Parser::ParseHexNum(int, uint64_t*)':
src/idl_parser.cpp:220:62: error: type qualifiers ignored on cast result type [-Werror=ignored-qualifiers]
     if (!isxdigit(static_cast<const unsigned char>(cursor_[i])))
                                                              ^
src/idl_parser.cpp: In member function 'flatbuffers::CheckedError flatbuffers::Parser::Next()':
src/idl_parser.cpp:260:62: error: type qualifiers ignored on cast result type [-Werror=ignored-qualifiers]
         if(!isdigit(static_cast<const unsigned char>(*cursor_))) return NoError();
                                                              ^
cc1plus: all warnings being treated as errors
2018-02-05 12:04:55 -08:00
Joe Schafer
c696422558 Add initial Bazel WORKSPACE and BUILD (#4608)
For #4574.

To test:

    bazel test --verbose_failures //:flatbuffers_test
2018-02-05 10:23:15 -08:00
Wouter van Oortmerssen
e93d2bda07 Cleaned up & fixed buffer alignment handling.
- Fixed ForceVectorAlignment (and possibly other call-sites) not
  setting minalign_.
- Fixed flipped alignment parameters in CopyTable (reflection).
- Made aligment for FlatBufferBuilder internal buffer configurable
  (useful when reading a constructed buffer directly).
- Ensured Alignment rounding is always up.

Change-Id: I33ca4887d92a09cb11a369c14a109f4b07ae707a
Tested: on Linux.
2018-01-29 11:46:30 -08:00
Wouter van Oortmerssen
bbf4dac6a3 Made doubles output with higher precision in JSON and elsewhere.
Change-Id: I5dd8f3a7c315c40b86a67aba5c2cca4d36363523
Tested: on Linux.
2018-01-22 10:24:45 -08:00
Wouter van Oortmerssen
8df2d9a3ef Removed truncating of enum vals to int in ReverseLookup.
Some implementations (e.g. C++98) won't support 64-bit enum values,
but there is no reason to silently truncate them.

Change-Id: I8629563523a96e887068f9c0efcd53741f60e0d6
Tested: on Linux.
2018-01-22 09:51:17 -08:00
Li-Deng
462ce03ebe Fix the spell mistake (#4599) 2018-01-22 09:08:13 -08:00
Wouter van Oortmerssen
020012e69c Fixed native_include not requiring a ; terminator.
Change-Id: Ic4f49e94c67ba23f07e5ba7e87ca94e8db5d5906
Tested: on Linux.
2018-01-22 09:06:13 -08:00
Wouter van Oortmerssen
f431a96523 New error: declaring an enum field without a default in range of the enum.
Change-Id: I4db7f032440c00d31c7434975b8a3f17c29d74b9
Tested: on Linux.
2018-01-18 12:06:01 -08:00
Wouter van Oortmerssen
3694ae0817 Disallow defaults on structs.
Change-Id: Ia098126c92ea6f8abe94561bd9c09b29ac8f61f5
Context: https://github.com/google/flatbuffers/issues/4591
Tested: on Linux.
2018-01-18 11:41:43 -08:00
Wouter van Oortmerssen
2265129e14 Removed date/time from generated code, causing commit polution.
Change-Id: I79724416110a3d2241d7eeecf0e12aa6d5b760a0
2018-01-18 11:21:10 -08:00
Wouter van Oortmerssen
4bddc6cc0c Added missing generated code.
Change-Id: I9420bf01d2bdb6ccfe3703c835297be73457c0d7
2018-01-18 11:20:58 -08:00
vglavnyy
e162366b3f Removed internal variable with name "data" inside a generated method _nested_root(). Local variable with name "data" violates [-Werror=shadow]. (#4587) 2018-01-18 11:15:31 -08:00
Wouter van Oortmerssen
fee9afd80b Use CreateVectorOfStructs when creating a vector of structs.
(mirrored from cl/180944741)

Change-Id: Ib9f1613ccc355528c5c495fd953a9b3944ecb4ce
2018-01-18 11:07:37 -08:00
Daniel Lin
98f681deb0 Author: Daniel Lin <danielin@umich.edi> (#4595)
Date:   Mon Jan 15 11:38:20 2018 -0200

Compilation failure with grpc.h

If cmake run with flag FLATBUFFERS_BUILD_GRPCTEST=ON
compilation fails.

Fix :
	-Fix argument list for overriden function in grpc.
	-Fix member function name called by FlatBufferBuilder from
	buf() to scratch_data()
2018-01-18 08:51:57 -08:00
Wouter van Oortmerssen
5cee340ad3 Cleaned up FlatBufferBuilder clearing.
Change-Id: I81cf4d268670bdd11d1d56ca9f2de78c120df842
Tested: on Linux.
2018-01-16 10:17:20 -08:00
Uilian Ries
f0769b60ab #4522 Conan package support (#4590)
- Added Conan recipe to build Flatbuffers
- Added Travis job to create Conan package

Signed-off-by: Uilian Ries <uilianries@gmail.com>
2018-01-12 08:18:20 -08:00
Wouter van Oortmerssen
79b80f84df Reduced FlatBufferBuilder from 3 buffers to 1
Previously, FlatBufferBuilder used 3 resizable buffers:
- serialization (vector_downward)
- field offsets (std::vector)
- vtable offsets (std::vector)

Since the serialization buffer grows downwards, the bottom part of
it can be used as a "scratchpad" storage for the other two. Since
field offsets are only accumulated during table construction, and
vtable offsets only after table construction, the two can trivially
share the same storage.

Not only does this reduce the amount of allocation, it also removes
the bulk of std::vector usage from FlatBufferBuilder which was
the #1 cause of slow-down in debug mode, see e.g.:
https://stackoverflow.com/questions/36085285/any-way-to-improve-flatbuffer-performance-in-debug-c-msvc

Change-Id: I0224cf2f2a863d2d7ef762bc9163b52fdc149522
Tested: on Linux.
2018-01-11 11:13:23 -08:00
Andrew Gunnerson
dfe68566e4 Add char * overload for FlatBufferBuilder::CreateString() (#4583)
Without this change, the compiler tries to select the following overload
when CreateString is passed a `char *`:

    template<typename T>
    Offset<String> CreateString(const T &str) {
      return CreateString(str.c_str(), str.length());
    }

which is not valid since char pointers don't have methods.

(Fixes #4579)

Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-08 08:26:19 -08:00
brianhall77
0aa36101f4 Portable range check for *cursor_ value. (#4582)
Avoids the following compile error when char is unsigned:

error: comparison of unsigned expression >= 0 is always true [-Werror,-Wtautological-unsigned-zero-compare]
2018-01-04 16:03:03 -08:00
vglavnyy
70f345012d GenerateText generate a text string for default identifier of enum and float scalar. An enum identifier printed as the strictly quoted string. (#4527) 2018-01-04 10:12:18 -08:00
Andrei Polushin
a056402f56 Remove unused declarations (#4578) 2018-01-04 10:01:41 -08:00
Darío Hereñú
d7b1d418ee Update URL (#4576) 2018-01-04 09:10:03 -08:00
Bastien Brunnenstein
9dae3eac60 Fix error in ConCatPathFileName (#4575) 2018-01-04 09:09:09 -08:00
Yuri Finkelstein
99a8a68a80 new maven jar called flatbuffers-java-grpc which contains utility … (#4558)
* new maven jar called flatbuffers-java-grpc which contains utility class supporting generated code for grpc over flatbuffers; grpc java unit test based on maven and junit (maven is used only for simplicity of testing); removed straneous namespace_test/NamespaceA/TableInC.java which is not longer used in the test and no longer generated but contains complilation errors if java compiler picks it up

* moved java grpc tests files according to review request

* Added missing generated code for Java gRPC.

Change-Id: Iada090fe2e99b80a4b7c2f8e39c838a992a1feae

* added missing name and url
2017-12-28 15:26:29 -08:00
Wouter van Oortmerssen
0c86929e39 Added missing nullptr check in vector of union verifiers.
Change-Id: Iec720991e68ad27580537135bfd8eb4159333921
Tested: on Linux.
2017-12-28 10:09:48 -08:00
Wouter van Oortmerssen
b24f2016a1 Disabled python in travis.yml pending fix.
Change-Id: I6d2a9cc8b32c531d4a1e272ddf29974a0a1a2ee3
2017-12-28 09:56:57 -08:00
Josh Ventura
1d73b3b9fc Various build fixes; update checked-in codegen. (#4572) 2017-12-28 09:10:55 -08:00
Yuri Finkelstein
a4dbe13486 fixed java codegen bug documented in #4563 (#4565)
* grpc bindings generator for Java and a few minor supporting changes in improvements

* restored formatting before my previous changes for ease of review

* Fixed grpc java code generation bug resulting in duplicate extractor declarations in case the same is used in more than a single RPC method

* fixed previous merge issue

* removed extra space

* restored extra space

* restored extra space

* fixed java codegen bug documented in https://github.com/google/flatbuffers/issues/4563
2017-12-21 13:32:34 -08:00
Wouter van Oortmerssen
89711c9c47 Made all C++ files clang-formatted.
Also added missing generated files.

Change-Id: Ifd22a643a08e3f2edfce92812ed57b87fc0e1875
2017-12-21 11:45:02 -08:00
Yuri Finkelstein
5d9930aa0d added config files for popular formatting tools (#4554)
* added config files for popular formatting tools

* edited and moved formatting files to root per review suggestion
2017-12-21 10:18:20 -08:00
Yuri Finkelstein
8518b3fb4e grpc bindings generator for Java and a few minor supporting changes i… (#4553)
* grpc bindings generator for Java and a few minor supporting changes in improvements

* restored formatting before my previous changes for ease of review

* Fixed grpc java code generation bug resulting in duplicate extractor declarations in case the same is used in more than a single RPC method
2017-12-21 09:55:57 -08:00
Yuri Finkelstein
61f4a46c43 synchronized grpc cpp_generator with latest version as of today: grpc-1.8.1. (#4556)
* updateed cpp_generator.cc to be compatible with the latest grpc version

* preserved the original license

* synchronized grpc cpp_generator with latest version as of today: GRPC 1.8.1. Regenerated test/monster_test.grpc.fb.* files and verified that grpctest is nicely passing

* fixed merge glitch
2017-12-21 08:49:00 -08:00
Yuri Finkelstein
dd73b53e28 updated cpp_generator.cc to be compatible with the latest grpc version (#4551)
* updateed cpp_generator.cc to be compatible with the latest grpc version

* preserved the original license
2017-12-18 14:17:59 -08:00
xgdgsc
c1901f2c01 update mutation status in doc (#4549) 2017-12-18 09:14:15 -08:00
Robert
4071b6f68b Merge pull request #4516 from mikeholler/travis
Add support for Python lib continuous deployment.
2017-12-14 13:20:18 -08:00
Kamil Rojewski
142401f50a Fix for strictPropertyInitialization for TS (#4540)
* Eclipse ignore

* TypeScript support

* Prefixing enums

* Test results

* Merged JS and TS generators

* Fixed AppVeyor build problems

* Fixed more AppVeyor build problems

* Fixed more AppVeyor build problems

* Changed TS flag to options struct

* Storing options by value

* Removed unneeded const

* Re-export support for unions

* Uint support

* Casting bools to numbers for mutation

* TS shell tests

* Reverted generates js test file to original version

* Backing up js tests and properly generating test data

* Not importing flatbuffers for TS test generation

* Not overwriting generated js for tests

* AppVeyor test fixes

* Generating the most strict TS code possible

* Not returning null when creating vectors

* Not returning null from struct contructors

* Vector of unions for ts/js

* Sanity check for languages

* Indentation fix + output test files

* Vectors of unions for php

* Fixes to union vector handling + tests

* Fix for strictPropertyInitialization
2017-12-14 08:30:02 -08:00
Mike Holler
7799642270 Change mikeholler to PYPI_USERNAME 2017-12-13 15:14:01 -06:00
Wouter van Oortmerssen
67b29d4e43 Fixed CreateStruct not testing for nesting.
Change-Id: I16bf46682ad68c62b778ed1514b6b706c8cb8f6f
Tested: on Linux.
2017-12-07 11:16:19 -08:00
Bastien Brunnenstein
85b131a719 Replace the non standard M_PI with a constant (#4528) 2017-12-04 15:06:13 -08:00
Paul Taylor
0e8a21854c publish additional js/flatbuffers.mjs to npm to support ESModules in node (#4504) 2017-12-01 14:18:43 -08:00
rmawatson
53a897731e this is allow custom allocator for obj-api structs/tables. (#4520)
added "native_custom_alloc" attribute to tables/structs, eg.

table parent_table( native_custom_alloc:"custom_alloc_name" ) {
...
}

with a custom allocator defined as

template <typename T> class custom_alloc_name : public std::allocator<T> {
public:

 typedef T*       pointer;

 template <class U>
 struct rebind {
  typedef custom_alloc_name<U> other;
 };

 pointer allocate(const std::size_t n) {
      return ....;
 }

 void deallocate(T* ptr, std::size_t n) {
    ...
 }

 custom_alloc_name() throw() {}
 template <class U> custom_alloc_name(const custom_alloc_name<U>&) throw() {}
};
};
2017-12-01 09:15:41 -08:00
Bastien Brunnenstein
ba08b0ec02 Add include directory to CMake install targets (#4510) 2017-11-30 08:45:58 -08:00
Mike Holler
da0bda6be3 Fixed prod PyPI URL and deploy block list. 2017-11-27 16:10:43 -06:00
Mike Holler
617bbc9b0c Added all_branches to tags: true block 2017-11-27 13:59:01 -06:00
Michael Holler
0c8b4c7614 Add support for Python lib continuous deployment.
Use a combination of travis and twine to publish to PyPI. New
publications will be made:

* When `master` is updated. This will trigger the publication of a
  the Python artifact versioned an iso-compliant build datetime. In this
  way, the cutting edge version will always be available via PyPI.
* When a new git tag is pushed. Tag pushes trigger the publication of
  a python artifact with the same version as the git tag, with the
  leading `v` stripped if present (`v1.2.3` becomes `1.2.3`).

Publications rely on Travis having a PYPI_PASSWORD environment set in
the project settings. See the Travis CI documentation for information on
[setting environment variables which containing sensitive data][1]. Make
extra sure the "Display value in build log" switch is OFF.

In addition to setting the previously mentioned `PYPI_PASSWORD`
environment variable, the owner of the PyPI `flatbuffers` repository
should, after merging this commit into master, add his own commit to
change `mikeholler` in `.travis/deploy-python.sh` to his username. It's
also recommended that the owner of `flatbuffers` use a separate account
in the unlikely event that the environment variable somehow becomes
compromised. Again, this is very unlikely, since the environment
variable is only set for "safe" builds approved by maintainers (not on
random pull requests).

[1]: https://docs.travis-ci.com/user/environment-variables/#Defining-Variables-in-Repository-Settings
2017-11-27 13:14:33 -06:00
125 changed files with 13186 additions and 6991 deletions

13
.clang-format Normal file
View File

@@ -0,0 +1,13 @@
---
Language: Cpp
BasedOnStyle: Google
DerivePointerAlignment: false
PointerAlignment: Right
IndentPPDirectives: AfterHash
Cpp11BracedListStyle: false
AlwaysBreakTemplateDeclarations: false
AllowShortCaseLabelsOnASingleLine: true
SpaceAfterTemplateKeyword: false
AllowShortBlocksOnASingleLine: true
...

7
.editorconfig Normal file
View File

@@ -0,0 +1,7 @@
root = true
[*.{cpp,cc,h}]
end_of_line = LF
indent_style = space
indent_size = 2
insert_final_newline = true

12
.gitignore vendored
View File

@@ -47,6 +47,7 @@ snapshot.sh
tags
tests/go_gen
tests/monsterdata_java_wire.mon
tests/monsterdata_java_wire_sp.mon
tests/monsterdata_go_wire.mon
tests/monsterdata_javascript_wire.mon
tests/unicode_test.mon
@@ -55,6 +56,7 @@ tests/php/
CMakeLists.txt.user
CMakeScripts/**
CTestTestfile.cmake
FlatbuffersConfigVersion.cmake
FlatBuffers.cbp
build/Xcode/FlatBuffers.xcodeproj/project.xcworkspace/**
build/Xcode/FlatBuffers.xcodeproj/xcuserdata/**
@@ -81,3 +83,13 @@ android/build/
samples/android/.externalNativeBuild/
samples/android/.gradle/
samples/android/build/
js/flatbuffers.mjs
/bazel-bin
/bazel-flatbuffers
/bazel-genfiles
/bazel-out
/bazel-testlogs
.ninja_deps
.ninja_log
build.ninja
rules.ninja

View File

@@ -4,23 +4,47 @@ env:
- GCC_VERSION="4.9"
matrix:
include:
#- language: python
# python: "2.7"
# install:
# - "pip install wheel twine"
# script:
# - "cd python/"
# - 'VERSION="$TRAVIS_TAG" python setup.py sdist bdist_wheel'
# - "cd ../"
# deploy:
# # Checkpointed release builds.
# - provider: script
# script: .travis/deploy-python.sh
# skip_cleanup: true
# on:
# tags: true
# # all_branches must be set with tags: true. See below post:
# # https://stackoverflow.com/a/27775257/1076585
# all_branches: true
# # Produce a new build for the cutting edge when master changes.
# - provider: script
# script: .travis/deploy-python.sh
# skip_cleanup: true
# on:
# branch: master
- language: cpp
os:
- linux
- osx
compiler:
- gcc
#- clang
env:
matrix:
- BUILD_TYPE=Debug BIICODE=false
- BUILD_TYPE=Release BIICODE=false
- BUILD_TYPE=Release BIICODE=false CONAN=true
# biicode .deb files no longer available.
# - BUILD_TYPE=Release BIICODE=true
# - BUILD_TYPE=Debug BIICODE=true
before_install:
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq; fi
@@ -28,10 +52,11 @@ matrix:
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq gcc-$GCC_VERSION; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which g++-$GCC_VERSION) /usr/bin/g++; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which gcc-$GCC_VERSION) /usr/bin/gcc; fi
script:
- if [ "$BIICODE" == "false" ]; then cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE . && make && make test; fi
- if [ "$BIICODE" == "true" ] && [ "$TRAVIS_OS_NAME" == "linux" ]; then ./biicode/support/bii-travis.sh $BUILD_TYPE; fi
- if [ "$CONAN" == "true" ] && [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo pip install conan && conan create . flatbuffers/testing -s build_type=$BUILD_TYPE; fi
- language: android
sudo: true

12
.travis/deploy-python.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROD_REPOSITORY="https://upload.pypi.org/legacy/"
TEST_REPOSITORY="https://test.pypi.org/legacy/"
twine upload \
--username "$PYPI_USERNAME" \
--password "$PYPI_PASSWORD" \
--repository-url "$PROD_REPOSITORY" \
"$DIR/../python/dist/"*

145
BUILD Normal file
View File

@@ -0,0 +1,145 @@
package(
default_visibility = ["//visibility:public"],
features = [
"-layering_check",
"-parse_headers",
],
)
exports_files([
"LICENSE",
])
FLATBUFFERS_COPTS = [
"-Wno-implicit-fallthrough",
"-linclude",
]
# Public flatc library to compile flatbuffer files at runtime.
cc_library(
name = "flatbuffers",
srcs = [
"src/code_generators.cpp",
"src/idl_gen_fbs.cpp",
"src/idl_gen_general.cpp",
"src/idl_gen_text.cpp",
"src/idl_parser.cpp",
"src/reflection.cpp",
"src/util.cpp",
],
hdrs = [":public_headers"],
copts = FLATBUFFERS_COPTS,
includes = ["include/"],
linkstatic = 1,
)
# Public C++ headers for the Flatbuffers library.
filegroup(
name = "public_headers",
srcs = [
"include/flatbuffers/base.h",
"include/flatbuffers/code_generators.h",
"include/flatbuffers/flatbuffers.h",
"include/flatbuffers/flexbuffers.h",
"include/flatbuffers/hash.h",
"include/flatbuffers/idl.h",
"include/flatbuffers/reflection.h",
"include/flatbuffers/reflection_generated.h",
"include/flatbuffers/stl_emulation.h",
"include/flatbuffers/util.h",
],
)
# Public flatc compiler library.
cc_library(
name = "flatc_library",
srcs = [
"src/code_generators.cpp",
"src/flatc.cpp",
"src/idl_gen_fbs.cpp",
"src/idl_parser.cpp",
"src/reflection.cpp",
"src/util.cpp",
],
hdrs = [
"include/flatbuffers/flatc.h",
":public_headers",
],
copts = FLATBUFFERS_COPTS,
includes = [
"grpc/",
"include/",
],
)
# Public flatc compiler.
cc_binary(
name = "flatc",
srcs = [
"grpc/src/compiler/config.h",
"grpc/src/compiler/cpp_generator.cc",
"grpc/src/compiler/cpp_generator.h",
"grpc/src/compiler/go_generator.cc",
"grpc/src/compiler/go_generator.h",
"grpc/src/compiler/java_generator.cc",
"grpc/src/compiler/java_generator.h",
"grpc/src/compiler/schema_interface.h",
"src/flatc_main.cpp",
"src/idl_gen_cpp.cpp",
"src/idl_gen_general.cpp",
"src/idl_gen_go.cpp",
"src/idl_gen_grpc.cpp",
"src/idl_gen_js.cpp",
"src/idl_gen_json_schema.cpp",
"src/idl_gen_php.cpp",
"src/idl_gen_python.cpp",
"src/idl_gen_text.cpp",
],
copts = FLATBUFFERS_COPTS,
includes = [
"grpc/",
"include/",
],
deps = [
":flatc_library",
],
)
# Test binary.
cc_test(
name = "flatbuffers_test",
testonly = 1,
srcs = [
"include/flatbuffers/minireflect.h",
"include/flatbuffers/registry.h",
"src/code_generators.cpp",
"src/idl_gen_fbs.cpp",
"src/idl_gen_general.cpp",
"src/idl_gen_text.cpp",
"src/idl_parser.cpp",
"src/reflection.cpp",
"src/util.cpp",
"tests/monster_test_generated.h",
"tests/namespace_test/namespace_test1_generated.h",
"tests/namespace_test/namespace_test2_generated.h",
"tests/test.cpp",
"tests/union_vector/union_vector_generated.h",
":public_headers",
],
copts = FLATBUFFERS_COPTS + [
"-DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE",
],
data = [
":tests/include_test/include_test1.fbs",
":tests/include_test/sub/include_test2.fbs",
":tests/monster_test.bfbs",
":tests/monster_test.fbs",
":tests/monsterdata_test.golden",
":tests/prototest/imported.proto",
":tests/prototest/test.golden",
":tests/prototest/test.proto",
":tests/prototest/test_union.golden",
":tests/union_vector/union_vector.fbs",
],
includes = ["include/"],
)

View File

@@ -0,0 +1,11 @@
set(PACKAGE_VERSION "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@")
# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()

View File

@@ -11,22 +11,6 @@ if (UNIX)
SET(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/google/flatbuffers")
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Vitaly Isaev <vitalyisaev2@gmail.com>")
# Derive package version from git
EXECUTE_PROCESS(
COMMAND date +%Y%m%d
OUTPUT_VARIABLE DATE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
EXECUTE_PROCESS(
COMMAND git describe
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DESCRIBE_DIRTY
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${GIT_DESCRIBE_DIRTY}")
SET(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
@@ -49,7 +33,7 @@ if (UNIX)
# Package name
SET(CPACK_DEBIAN_PACKAGE_NAME "flatbuffers")
SET(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE.txt)
SET(CPACK_PACKAGE_FILE_NAME
SET(CPACK_PACKAGE_FILE_NAME
"${CPACK_DEBIAN_PACKAGE_NAME}_${CPACK_DEBIAN_PACKAGE_VERSION}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
endif(UNIX)

11
CMake/Version.cmake Normal file
View File

@@ -0,0 +1,11 @@
find_program(GIT git)
execute_process(
COMMAND ${GIT} describe
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DESCRIBE_DIRTY
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${GIT_DESCRIBE_DIRTY}")

View File

@@ -60,6 +60,8 @@ set(FlatBuffers_Compiler_SRCS
grpc/src/compiler/cpp_generator.cc
grpc/src/compiler/go_generator.h
grpc/src/compiler/go_generator.cc
grpc/src/compiler/java_generator.h
grpc/src/compiler/java_generator.cc
)
set(FlatHash_SRCS
@@ -118,10 +120,13 @@ elseif(CMAKE_COMPILER_IS_GNUCXX)
endif(CYGWIN)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Werror=shadow")
if (GCC_VERSION VERSION_GREATER 4.4)
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.4)
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -faligned-new")
endif()
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result \
-Wunused-parameter -Werror=unused-parameter")
"${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result -Wunused-parameter -Werror=unused-parameter")
endif()
# Certain platforms such as ARM do not use signed chars by default
@@ -195,7 +200,7 @@ if(FLATBUFFERS_BUILD_SHAREDLIB)
# - minor updated when there are additions in API/ABI
# - major (ABI number) updated when there are changes in ABI (or removals)
set(FlatBuffers_Library_SONAME_MAJOR "1")
set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.8.0")
set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.9.0")
set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers
SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}"
VERSION "${FlatBuffers_Library_SONAME_FULL}")
@@ -244,9 +249,11 @@ if(FLATBUFFERS_BUILD_GRPCTEST)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-shadow")
endif()
add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
target_link_libraries(grpctest grpc++_unsecure grpc_unsecure pthread dl)
target_link_libraries(grpctest grpc++_unsecure grpc_unsecure gpr pthread dl)
endif()
include(CMake/Version.cmake)
if(FLATBUFFERS_INSTALL)
include(GNUInstallDirs)
@@ -254,16 +261,25 @@ if(FLATBUFFERS_INSTALL)
set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers")
configure_file(CMake/FlatbuffersConfigVersion.cmake.in FlatbuffersConfigVersion.cmake @ONLY)
install(
FILES "CMake/FlatbuffersConfig.cmake"
FILES "CMake/FlatbuffersConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/FlatbuffersConfigVersion.cmake"
DESTINATION ${FB_CMAKE_DIR}
)
if(FLATBUFFERS_BUILD_FLATLIB)
install(
TARGETS flatbuffers EXPORT FlatbuffersTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
if(CMAKE_VERSION VERSION_LESS 3.0)
install(
TARGETS flatbuffers EXPORT FlatbuffersTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
else()
install(
TARGETS flatbuffers EXPORT FlatbuffersTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
install(EXPORT FlatbuffersTargets
FILE FlatbuffersTargets.cmake
@@ -289,12 +305,22 @@ if(FLATBUFFERS_INSTALL)
endif()
if(FLATBUFFERS_BUILD_SHAREDLIB)
install(
TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
if(CMAKE_VERSION VERSION_LESS 3.0)
install(
TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
else()
install(
TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
install(
EXPORT FlatbuffersSharedTargets

2
WORKSPACE Normal file
View File

@@ -0,0 +1,2 @@
workspace(name = "com_github_google_flatbuffers")

53
conanfile.py Normal file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Conan recipe package for Google FlatBuffers
"""
import os
from conans import ConanFile, CMake, tools
class FlatbuffersConan(ConanFile):
name = "flatbuffers"
version = "1.9.0"
license = "https://github.com/google/flatbuffers/blob/master/LICENSE.txt"
url = "https://github.com/google/flatbuffers"
description = "Memory Efficient Serialization Library"
settings = "os", "compiler", "build_type", "arch", "os_build", "arch_build"
options = {"shared": [True, False]}
default_options = "shared=False"
generators = "cmake"
exports = "LICENSE.txt"
exports_sources = ["CMake/*", "include/*", "src/*", "grpc/*", "CMakeLists.txt"]
def _inject_magic_lines(self):
"""Inject Conan setup in cmake file to solve exteral dependencies.
"""
conan_magic_lines = '''project(FlatBuffers)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
'''
tools.replace_in_file("CMakeLists.txt", "project(FlatBuffers)", conan_magic_lines)
def build(self):
"""Configure, build and install FlatBuffers using CMake.
"""
self._inject_magic_lines()
cmake = CMake(self)
cmake.definitions["FLATBUFFERS_BUILD_TESTS"] = False
cmake.definitions["FLATBUFFERS_BUILD_SHAREDLIB"] = self.options.shared
cmake.configure()
cmake.build()
cmake.install()
def package(self):
"""Copy Flatbuffers' artifacts to package folder
"""
self.copy(pattern="LICENSE.txt", dst="licenses")
self.copy(pattern="flathash*", dst="bin", src="bin")
def package_info(self):
"""Collect built libraries names and solve flatc path.
"""
self.cpp_info.libs = tools.collect_libs(self)
self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))

View File

@@ -5,7 +5,7 @@ Building {#flatbuffers_guide_building}
The distribution comes with a `cmake` file that should allow
you to build project/make files for any platform. For details on `cmake`, see
<http://www.cmake.org>. In brief, depending on your platform, use one of
<https://www.cmake.org>. In brief, depending on your platform, use one of
e.g.:
cmake -G "Unix Makefiles"

View File

@@ -126,6 +126,45 @@ The following attributes are specific to the object-based API code generation:
"native_inline", the value specified with this attribute will be included
verbatim in the class constructor initializer list for this member.
- `native_custom_alloc`:"custom_allocator" (on a table or struct): When using the
object-based API all generated NativeTables that are allocated when unpacking
your flatbuffer will use "custom allocator". The allocator is also used by
any std::vector that appears in a table defined with `native_custom_alloc`.
This can be used to provide allocation from a pool for example, for faster
unpacking when using the object-based API.
Minimal Example:
schema:
table mytable(native_custom_alloc:"custom_allocator") {
...
}
with custom_allocator defined before flatbuffers.h is included, as:
template <typename T> struct custom_allocator : public std::allocator<T> {
typedef T *pointer;
template <class U>
struct rebind {
typedef custom_allocator<U> other;
};
pointer allocate(const std::size_t n) {
return std::allocator<T>::allocate(n);
}
void deallocate(T* ptr, std::size_t n) {
return std::allocator<T>::deallocate(ptr,n);
}
custom_allocator() throw() {}
template <class U>
custom_allocator(const custom_allocator<U>&) throw() {}
};
- `native_type`' "type" (on a struct): In some cases, a more optimal C++ data
type exists for a given struct. For example, the following schema:
@@ -290,7 +329,7 @@ performs a swap operation on big endian machines), and also because
the layout of things is generally not known to the user.
For structs, layout is deterministic and guaranteed to be the same
accross platforms (scalars are aligned to their
across platforms (scalars are aligned to their
own size, and structs themselves to their largest member), and you
are allowed to access this memory directly by using `sizeof()` and
`memcpy` on the pointer to a struct, or even an array of structs.

View File

@@ -4,7 +4,7 @@ FlatBuffers {#flatbuffers_index}
# Overview {#flatbuffers_overview}
[FlatBuffers](@ref flatbuffers_overview) is an efficient cross platform
serialization library for C++, C#, C, Go, Java, JavaScript, PHP, and Python.
serialization library for C++, C#, C, Go, Java, JavaScript, TypeScript, PHP, and Python.
It was originally created at Google for game development and other
performance-critical applications.
@@ -134,6 +134,10 @@ sections provide a more in-depth usage guide.
in your own programs.
- How to [use the generated Go code](@ref flatbuffers_guide_use_go) in your
own programs.
- How to [use the generated JavaScript code](@ref flatbuffers_guide_use_javascript) in your
own programs.
- How to [use the generated TypeScript code](@ref flatbuffers_guide_use_typescript) in your
own programs.
- How to [use FlatBuffers in C with `flatcc`](@ref flatbuffers_guide_use_c) in your
own programs.
- [Support matrix](@ref flatbuffers_support) for platforms/languages/features.

View File

@@ -18,27 +18,28 @@ In general:
NOTE: this table is a start, it needs to be extended.
Feature | C++ | Java | C# | Go | Python | JS | C | PHP | Ruby
------------------------------ | ------ | ------ | ------ | ------ | ------ | --------- | ------ | --- | ----
Codegen for all basic features | Yes | Yes | Yes | Yes | Yes | Yes | Yes | WiP | WiP
JSON parsing | Yes | No | No | No | No | No | Yes | No | No
Simple mutation | Yes | WIP | WIP | No | No | No | No | No | No
Reflection | Yes | No | No | No | No | No | Basic | No | No
Buffer verifier | Yes | No | No | No | No | No | Yes | No | No
Testing: basic | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ?
Testing: fuzz | Yes | No | No | Yes | Yes | No | No | ? | ?
Performance: | Superb | Great | Great | Great | Ok | ? | Superb | ? | ?
Platform: Windows | VS2010 | Yes | Yes | ? | ? | ? | VS2010 | ? | ?
Platform: Linux | GCC282 | Yes | ? | Yes | Yes | ? | Yes | ? | ?
Platform: OS X | Xcode4 | ? | ? | ? | Yes | ? | Yes | ? | ?
Platform: Android | NDK10d | Yes | ? | ? | ? | ? | ? | ? | ?
Platform: iOS | ? | ? | ? | ? | ? | ? | ? | ? | ?
Engine: Unity | ? | ? | Yes | ? | ? | ? | ? | ? | ?
Primary authors (github) | gwvo | gwvo | ev*/js*| rw | rw | evanw/ev* | mik* | ch* | rw
Feature | C++ | Java | C# | Go | Python | JS | TS | C | PHP | Ruby
------------------------------ | ------ | ------ | ------ | ------ | ------ | --------- | --------- | ------ | --- | ----
Codegen for all basic features | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | WiP | WiP
JSON parsing | Yes | No | No | No | No | No | No | Yes | No | No
Simple mutation | Yes | Yes | Yes | Yes | No | No | No | No | No | No
Reflection | Yes | No | No | No | No | No | No | Basic | No | No
Buffer verifier | Yes | No | No | No | No | No | No | Yes | No | No
Testing: basic | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ?
Testing: fuzz | Yes | No | No | Yes | Yes | No | No | No | ? | ?
Performance: | Superb | Great | Great | Great | Ok | ? | ? | Superb | ? | ?
Platform: Windows | VS2010 | Yes | Yes | ? | ? | ? | Yes | VS2010 | ? | ?
Platform: Linux | GCC282 | Yes | ? | Yes | Yes | ? | Yes | Yes | ? | ?
Platform: OS X | Xcode4 | ? | ? | ? | Yes | ? | Yes | Yes | ? | ?
Platform: Android | NDK10d | Yes | ? | ? | ? | ? | ? | ? | ? | ?
Platform: iOS | ? | ? | ? | ? | ? | ? | ? | ? | ? | ?
Engine: Unity | ? | ? | Yes | ? | ? | ? | ? | ? | ? | ?
Primary authors (github) | gwvo | gwvo | ev*/js*| rw | rw | evanw/ev* | kr | mik* | ch* | rw
* ev = evolutional
* js = jonsimantov
* mik = mikkelfj
* ch = chobie
* kr = krojew
<br>

View File

@@ -27,6 +27,7 @@ Please select your desired language for our quest:
<input type="radio" name="language" value="go">Go</input>
<input type="radio" name="language" value="python">Python</input>
<input type="radio" name="language" value="javascript">JavaScript</input>
<input type="radio" name="language" value="typescript">TypeScript</input>
<input type="radio" name="language" value="php">PHP</input>
<input type="radio" name="language" value="c">C</input>
</form>
@@ -122,6 +123,9 @@ For your chosen language, please cross-reference with:
<div class="language-javascript">
[samplebinary.js](https://github.com/google/flatbuffers/blob/master/samples/samplebinary.js)
</div>
<div class="language-typescript">
<em>none yet</em>
</div>
<div class="language-php">
[SampleBinary.php](https://github.com/google/flatbuffers/blob/master/samples/SampleBinary.php)
</div>
@@ -284,7 +288,13 @@ Please be aware of the difference between `flatc` and `flatcc` tools.
<div class="language-javascript">
~~~{.sh}
cd flatbuffers/sample
./../flatc --javascript samples/monster.fbs
./../flatc --js samples/monster.fbs
~~~
</div>
<div class="language-typescript">
~~~{.sh}
cd flatbuffers/sample
./../flatc --ts samples/monster.fbs
~~~
</div>
<div class="language-php">
@@ -372,6 +382,11 @@ The first step is to import/include the library, generated files, etc.
<script src="monster_generated.js"></script> // Generated by `flatc`.
~~~
</div>
<div class="language-typescript">
// note: import flabuffers with your desired import method
import { MyGame } from './monster_generated';
</div>
<div class="language-php">
~~~{.php}
// It is recommended that your use PSR autoload when using FlatBuffers in PHP.
@@ -454,6 +469,13 @@ which will grow automatically if needed:
var builder = new flatbuffers.Builder(1024);
~~~
</div>
<div class="language-typescript">
~~~{.ts}
// Create a `flatbuffer.Builder`, which will be used to create our
// monsters' FlatBuffers.
let builder = new flatbuffers.Builder(1024);
~~~
</div>
<div class="language-php">
~~~{.php}
// Create a `FlatBufferBuilder`, which will be used to create our
@@ -566,6 +588,24 @@ our `orc` Monster, lets create some `Weapon`s: a `Sword` and an `Axe`.
var axe = MyGame.Sample.Weapon.endWeapon(builder);
~~~
</div>
<div class="language-typescript">
~~~{.js}
let weaponOne = builder.createString('Sword');
let weaponTwo = builder.createString('Axe');
// Create the first `Weapon` ('Sword').
MyGame.Sample.Weapon.startWeapon(builder);
MyGame.Sample.Weapon.addName(builder, weaponOne);
MyGame.Sample.Weapon.addDamage(builder, 3);
let sword = MyGame.Sample.Weapon.endWeapon(builder);
// Create the second `Weapon` ('Axe').
MyGame.Sample.Weapon.startWeapon(builder);
MyGame.Sample.Weapon.addName(builder, weaponTwo);
MyGame.Sample.Weapon.addDamage(builder, 5);
let axe = MyGame.Sample.Weapon.endWeapon(builder);
~~~
</div>
<div class="language-php">
~~~{.php}
// Create the `Weapon`s using the `createWeapon()` helper function.
@@ -684,6 +724,17 @@ traversal. This is generally easy to do on any tree structures.
var inv = MyGame.Sample.Monster.createInventoryVector(builder, treasure);
~~~
</div>
<div class="language-typescript">
~~~{.js}
// Serialize a name for our monster, called 'Orc'.
let name = builder.createString('Orc');
// Create a `vector` representing the inventory of the Orc. Each number
// could correspond to an item that can be claimed after he is slain.
let treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let inv = MyGame.Sample.Monster.createInventoryVector(builder, treasure);
~~~
</div>
<div class="language-php">
~~~{.php}
// Serialize a name for our monster, called "Orc".
@@ -787,6 +838,14 @@ offsets.
var weapons = MyGame.Sample.Monster.createWeaponsVector(builder, weaps);
~~~
</div>
<div class="language-typescript">
~~~{.ts}
// Create an array from the two `Weapon`s and pass it to the
// `createWeaponsVector()` method to create a FlatBuffer vector.
let weaps = [sword, axe];
let weapons = MyGame.Sample.Monster.createWeaponsVector(builder, weaps);
~~~
</div>
<div class="language-php">
~~~{.php}
// Create an array from the two `Weapon`s and pass it to the
@@ -863,6 +922,14 @@ for the `path` field above:
var path = builder.endVector();
~~~
</div>
<div class="language-typescript">
~~~{.ts}
MyGame.Sample.Monster.startPathVector(builder, 2);
MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0);
MyGame.Sample.Vec3.createVec3(builder, 4.0, 5.0, 6.0);
let path = builder.endVector();
~~~
</div>
<div class="language-php">
~~~{.php}
\MyGame\Example\Monster::StartPathVector($builder, 2);
@@ -882,15 +949,18 @@ can serialize the monster itself:
<div class="language-cpp">
~~~{.cpp}
// Create the position struct
auto position = Vec3(1.0f, 2.0f, 3.0f);
// Set his hit points to 300 and his mana to 150.
int hp = 300;
int mana = 150;
// Finally, create the monster using the `CreateMonster` helper function
// to set all fields.
auto orc = CreateMonster(builder, Vec3(1.0f, 2.0f, 3.0f), mana, hp, name,
inventory, Color_Red, weapons, Equipment_Weapon,
axe.Union(), path);
auto orc = CreateMonster(builder, &position, mana, hp, name, inventory,
Color_Red, weapons, Equipment_Weapon, axe.Union(),
path);
~~~
</div>
<div class="language-java">
@@ -977,6 +1047,23 @@ can serialize the monster itself:
var orc = MyGame.Sample.Monster.endMonster(builder);
~~~
</div>
<div class="language-typescript">
~~~{.ts}
// Create our monster by using `startMonster()` and `endMonster()`.
MyGame.Sample.Monster.startMonster(builder);
MyGame.Sample.Monster.addPos(builder,
MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0));
MyGame.Sample.Monster.addHp(builder, 300);
MyGame.Sample.Monster.addColor(builder, MyGame.Sample.Color.Red)
MyGame.Sample.Monster.addName(builder, name);
MyGame.Sample.Monster.addInventory(builder, inv);
MyGame.Sample.Monster.addWeapons(builder, weapons);
MyGame.Sample.Monster.addEquippedType(builder, MyGame.Sample.Equipment.Weapon);
MyGame.Sample.Monster.addEquipped(builder, axe);
MyGame.Sample.Monster.addPath(builder, path);
let orc = MyGame.Sample.Monster.endMonster(builder);
~~~
</div>
<div class="language-php">
~~~{.php}
// Create our monster by using `StartMonster()` and `EndMonster()`.
@@ -1036,15 +1123,14 @@ a bit more flexibility.
// You can use this code instead of `CreateMonster()`, to create our orc
// manually.
MonsterBuilder monster_builder(builder);
monster_builder.add_pos(&pos);
auto pos = Vec3(1.0f, 2.0f, 3.0f);
monster_builder.add_pos(&position);
monster_builder.add_hp(hp);
monster_builder.add_name(name);
monster_builder.add_inventory(inventory);
monster_builder.add_color(Color_Red);
monster_builder.add_weapons(weapons);
monster_builder.add_equipped_type(Equipment_Weapon);
monster_builder.add_equpped(axe.Union());
monster_builder.add_equipped(axe.Union());
auto orc = monster_builder.Finish();
~~~
</div>
@@ -1122,6 +1208,12 @@ Here is a repetition these lines, to help highlight them more clearly:
MyGame.Sample.Monster.addEquipped(builder, axe); // Union data
~~~
</div>
<div class="language-typescript">
~~~{.ts}
MyGame.Sample.Monster.addEquippedType(builder, MyGame.Sample.Equipment.Weapon); // Union type
MyGame.Sample.Monster.addEquipped(builder, axe); // Union data
~~~
</div>
<div class="language-php">
~~~{.php}
\MyGame\Sample\Monster::AddEquippedType($builder, \MyGame\Sample\Equipment::Weapon); // Union type
@@ -1180,6 +1272,13 @@ appropriate `finish` method.
// orc);`.
~~~
</div>
<div class="language-typescript">
~~~{.ts}
// Call `finish()` to instruct the builder that this monster is complete.
builder.finish(orc); // You could also call `MyGame.Sample.Monster.finishMonsterBuffer(builder,
// orc);`.
~~~
</div>
<div class="language-php">
~~~{.php}
// Call `finish()` to instruct the builder that this monster is complete.
@@ -1246,6 +1345,12 @@ like so:
var buf = builder.asUint8Array(); // Of type `Uint8Array`.
~~~
</div>
<div class="language-typescript">
~~~{.ts}
// This must be called after `finish()`.
let buf = builder.asUint8Array(); // Of type `Uint8Array`.
~~~
</div>
<div class="language-php">
~~~{.php}
// This must be called after `finish()`.
@@ -1347,6 +1452,13 @@ before:
<script src="monster_generated.js"></script> // Generated by `flatc`.
~~~
</div>
<div class="language-typescript">
~~~{.js}
// note: import flabuffers with your desired import method
import { MyGame } from './monster_generated';
~~~
</div>
<div class="language-php">
~~~{.php}
// It is recommended that your use PSR autoload when using FlatBuffers in PHP.
@@ -1451,6 +1563,15 @@ won't work**
var monster = MyGame.Sample.Monster.getRootAsMonster(buf);
~~~
</div>
<div class="language-typescript">
~~~{.ts}
let bytes = /* the data you just read, in an object of type "Uint8Array" */
let buf = new flatbuffers.ByteBuffer(bytes);
// Get an accessor to the root object inside the buffer.
let monster = MyGame.Sample.Monster.getRootAsMonster(buf);
~~~
</div>
<div class="language-php">
~~~{.php}
$bytes = /* the data you just read, in a string */
@@ -1518,6 +1639,13 @@ accessors for all non-`deprecated` fields. For example:
var name = $monster.name();
~~~
</div>
<div class="language-typescript">
~~~{.ts}
let hp = $monster.hp();
let mana = $monster.mana();
let name = $monster.name();
~~~
</div>
<div class="language-php">
~~~{.php}
$hp = $monster->getHp();
@@ -1593,6 +1721,14 @@ To access sub-objects, in the case of our `pos`, which is a `Vec3`:
var z = pos.z();
~~~
</div>
<div class="language-typescript">
~~~{.ts}
let pos = monster.pos();
let x = pos.x();
let y = pos.y();
let z = pos.z();
~~~
</div>
<div class="language-php">
~~~{.php}
$pos = $monster->getPos();
@@ -1655,6 +1791,12 @@ FlatBuffers `vector`.
var thirdItem = monster.inventory(2);
~~~
</div>
<div class="language-typescript">
~~~{.ts}
let invLength = monster.inventoryLength();
let thirdItem = monster.inventory(2);
~~~
</div>
<div class="language-php">
~~~{.php}
$inv_len = $monster->getInventoryLength();
@@ -1720,6 +1862,13 @@ except your need to handle the result as a FlatBuffer `table`:
var secondWeaponDamage = monster.weapons(1).damage();
~~~
</div>
<div class="language-typescript">
~~~{.ts}
let weaponsLength = monster.weaponsLength();
let secondWeaponName = monster.weapons(1).name();
let secondWeaponDamage = monster.weapons(1).damage();
~~~
</div>
<div class="language-php">
~~~{.php}
$weapons_len = $monster->getWeaponsLength();
@@ -1827,6 +1976,16 @@ We can access the type to dynamically cast the data as needed (since the
}
~~~
</div>
<div class="language-typescript">
~~~{.ts}
let unionType = monster.equippedType();
if (unionType == MyGame.Sample.Equipment.Weapon) {
let weapon_name = monster.equipped(new MyGame.Sample.Weapon()).name(); // 'Axe'
let weapon_damage = monster.equipped(new MyGame.Sample.Weapon()).damage(); // 5
}
~~~
</div>
<div class="language-php">
~~~{.php}
$union_type = $monster->getEquippedType();
@@ -1905,7 +2064,12 @@ mutators like so:
</div>
<div class="language-javascript">
~~~{.js}
<API for mutating FlatBuffers is not yet support in JavaScript.>
<API for mutating FlatBuffers is not yet supported in JavaScript.>
~~~
</div>
<div class="language-typescript">
~~~{.ts}
<API for mutating FlatBuffers is not yet supported in TypeScript.>
~~~
</div>
<div class="language-php">
@@ -2019,6 +2183,9 @@ For your chosen language, see:
<div class="language-javascript">
[Use in JavaScript](@ref flatbuffers_guide_use_javascript)
</div>
<div class="language-typescript">
[Use in TypeScript](@ref flatbuffers_guide_use_typescript)
</div>
<div class="language-php">
[Use in PHP](@ref flatbuffers_guide_use_php)
</div>

View File

@@ -0,0 +1,66 @@
Use in TypeScript {#flatbuffers_guide_use_typescript}
=================
## Before you get started
Before diving into the FlatBuffers usage in TypeScript, it should be noted that
the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to
general FlatBuffers usage in all of the supported languages
(including TypeScript). This page is specifically designed to cover the nuances
of FlatBuffers usage in TypeScript.
You should also have read the [Building](@ref flatbuffers_guide_building)
documentation to build `flatc` and should be familiar with
[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
[Writing a schema](@ref flatbuffers_guide_writing_schema).
## FlatBuffers TypeScript library code location
The code for the FlatBuffers TypeScript library can be found at
`flatbuffers/js` with typings available at @types/flatubffers.
## Testing the FlatBuffers TypeScript library
To run the tests, use the [TypeScriptTest.sh](https://github.com/google/
flatbuffers/blob/master/tests/TypeScriptTest.sh) shell script.
*Note: The TypeScript test file requires [Node.js](https://nodejs.org/en/).*
## Using the FlatBuffers TypeScript libary
*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
example of how to use FlatBuffers in TypeScript.*
FlatBuffers supports both reading and writing FlatBuffers in TypeScript.
To use FlatBuffers in your own code, first generate TypeScript classes from your
schema with the `--ts` option to `flatc`. Then you can include both FlatBuffers
and the generated code to read or write a FlatBuffer.
For example, here is how you would read a FlatBuffer binary file in TypeScript:
First, include the library and generated code. Then read the file into an
`Uint8Array`. Make a `flatbuffers.ByteBuffer` out of the `Uint8Array`, and pass
the ByteBuffer to the `getRootAsMonster` function.
~~~{.ts}
// note: import flabuffers with your desired import method
import { MyGame } from './monster_generated';
let data = new Uint8Array(fs.readFileSync('monster.dat'));
let buf = new flatbuffers.ByteBuffer(data);
let monster = MyGame.Example.Monster.getRootAsMonster(buf);
~~~
Now you can access values like this:
~~~{.ts}
let hp = monster.hp();
let pos = monster.pos();
~~~
## Text parsing FlatBuffers in TypeScript
There currently is no support for parsing text (Schema's and JSON) directly
from TypeScript.

View File

@@ -33,6 +33,8 @@
title="Use in Java/C#"/>
<tab type="user" url="@ref flatbuffers_guide_use_javascript"
title="Use in JavaScript"/>
<tab type="user" url="@ref flatbuffers_guide_use_typescript"
title="Use in TypeScript"/>
<tab type="user" url="@ref flatbuffers_guide_use_php"
title="Use in PHP"/>
<tab type="user" url="@ref flatbuffers_guide_use_python"

View File

@@ -13,6 +13,9 @@
/// @defgroup flatbuffers_javascript_api JavaScript API
/// @brief FlatBuffers API for JavaScript
/// @defgroup flatbuffers_typescript_api TypeScript API
/// @brief FlatBuffers API for TypeScript
/// @defgroup flatbuffers_php_api PHP API
/// @brief FlatBuffers API for PHP

17
go/BUILD.bazel Normal file
View File

@@ -0,0 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go",
srcs = [
"builder.go",
"doc.go",
"encode.go",
"grpc.go",
"lib.go",
"sizes.go",
"struct.go",
"table.go",
],
importpath = "github.com/google/flatbuffers/go",
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-parent</artifactId>
<version>1.8.0</version>
</parent>
<artifactId>flatbuffers-java-grpc</artifactId>
<name>${project.artifactId}</name>
<packaging>bundle</packaging>
<description>
Utilities supporting generated code for GRPC
</description>
<developers>
<developer>
<name>Wouter van Oortmerssen</name>
</developer>
<developer>
<name>Yuri Finkelstein</name>
<url>https://github.com/yfinkelstein</url>
</developer>
</developers>
<properties>
<gRPC.version>1.8.0</gRPC.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>${gRPC.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.flatbuffers.grpc;
import com.google.flatbuffers.Table;
import io.grpc.Drainable;
import io.grpc.KnownLength;
import io.grpc.MethodDescriptor;
import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
public class FlatbuffersUtils {
abstract public static class FBExtactor <T extends Table> {
T extract (InputStream stream) throws IOException {
if (stream instanceof KnownLength) {
int size = stream.available();
ByteBuffer buffer = ByteBuffer.allocate(size);
stream.read(buffer.array());
return extract(buffer);
} else
throw new RuntimeException("The class " + stream.getClass().getCanonicalName() + " does not extend from KnownLength ");
}
public abstract T extract(ByteBuffer buffer);
}
static class FBInputStream extends InputStream implements Drainable, KnownLength {
private final ByteBuffer buffer;
private final int size;
@Nullable private ByteArrayInputStream inputStream;
FBInputStream(ByteBuffer buffer) {
this.buffer = buffer;
this.size = buffer.remaining();
}
private void makeStreamIfNotAlready() {
if (inputStream == null)
inputStream = new ByteArrayInputStream(buffer.array(), buffer.position(), size);
}
@Override
public int drainTo(OutputStream target) throws IOException {
target.write(buffer.array(), buffer.position(), size);
return size;
}
@Override
public int read() throws IOException {
makeStreamIfNotAlready();
return inputStream.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
makeStreamIfNotAlready();
if (inputStream == null) {
if (len >= size) {
System.arraycopy(buffer.array(), buffer.position(), b, off, size);
return size;
} else {
makeStreamIfNotAlready();
return inputStream.read(b, off, len);
}
} else
return inputStream.read(b, off, len);
}
@Override
public int available() throws IOException {
return inputStream == null ? size : inputStream.available();
}
}
public static <T extends Table> MethodDescriptor.Marshaller<T> marshaller(final Class<T> clazz, final FBExtactor<T> extractor) {
return new MethodDescriptor.ReflectableMarshaller<T>() {
@Override
public Class<T> getMessageClass() {
return clazz;
}
@Override
public InputStream stream(T value) {
return new FBInputStream (value.getByteBuffer());
}
@Override
public T parse(InputStream stream) {
try {
return extractor.extract(stream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
}

213
grpc/pom.xml Normal file
View File

@@ -0,0 +1,213 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-parent</artifactId>
<packaging>pom</packaging>
<version>1.8.0</version>
<name>flatbuffers-parent</name>
<description>parent pom for flatbuffers java artifacts</description>
<properties>
<scm.url>https://github.com/google/flatbuffers</scm.url>
<scm.connection>scm:git:${scm.url}.git</scm.connection>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/google/flatbuffers/issues</url>
</issueManagement>
<developers>
<developer>
<name>Wouter van Oortmerssen</name>
</developer>
</developers>
<url>${scm.url}</url>
<scm>
<connection>${scm.connection}</connection>
<developerConnection>${scm.connection}</developerConnection>
<url>${scm.url}</url>
<tag>HEAD</tag>
</scm>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<!--
os-maven-plugin is a Maven extension/plugin that generates various useful platform-dependent
project properties normalized from ${os.detected.name} and ${os.detected.arch}.
-->
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<!-- consider the benefits of publishing all maven artifacts in this project
<module>flatbuffers-compiler</module>
<module>flatbuffers-java</module>
-->
<module>flatbuffers-java-grpc</module>
</modules>
</project>

View File

@@ -34,6 +34,7 @@
#include <map>
#include "src/compiler/cpp_generator.h"
#include "flatbuffers/util.h"
#include <sstream>
@@ -127,8 +128,8 @@ grpc::string GetHeaderPrologue(grpc_generator::File *file,
printer->Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n");
printer->Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n");
printer->Print(vars, "\n");
printer->Print(vars, file->additional_headers().c_str());
printer->Print(vars, "#include \"$filename_base$$message_header_ext$\"\n");
printer->Print(vars, file->additional_headers().c_str());
printer->Print(vars, "\n");
}
return output;
@@ -158,7 +159,6 @@ grpc::string GetHeaderIncludes(grpc_generator::File *file,
printer->Print(vars, "namespace grpc {\n");
printer->Print(vars, "class CompletionQueue;\n");
printer->Print(vars, "class Channel;\n");
printer->Print(vars, "class RpcService;\n");
printer->Print(vars, "class ServerCompletionQueue;\n");
printer->Print(vars, "class ServerContext;\n");
printer->Print(vars, "} // namespace grpc\n\n");
@@ -183,25 +183,38 @@ void PrintHeaderClientMethodInterfaces(
(*vars)["Request"] = method->input_type_name();
(*vars)["Response"] = method->output_type_name();
struct {
grpc::string prefix;
grpc::string method_params; // extra arguments to method
grpc::string raw_args; // extra arguments to raw version of method
} async_prefixes[] = {{"Async", ", void* tag", ", tag"},
{"PrepareAsync", "", ""}};
if (is_public) {
if (method->NoStreaming()) {
printer->Print(
*vars,
"virtual ::grpc::Status $Method$(::grpc::ClientContext* context, "
"const $Request$& request, $Response$* response) = 0;\n");
printer->Print(*vars,
"std::unique_ptr< "
"::grpc::ClientAsyncResponseReaderInterface< $Response$>> "
"Async$Method$(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
printer->Indent();
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncResponseReaderInterface< $Response$>>("
"Async$Method$Raw(context, request, cq));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
"std::unique_ptr< "
"::grpc::ClientAsyncResponseReaderInterface< $Response$>> "
"$AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncResponseReaderInterface< $Response$>>("
"$AsyncPrefix$$Method$Raw(context, request, cq));\n");
printer->Outdent();
printer->Print("}\n");
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
@@ -215,19 +228,27 @@ void PrintHeaderClientMethodInterfaces(
"($Method$Raw(context, response));\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncWriterInterface< $Request$>>"
" Async$Method$(::grpc::ClientContext* context, $Response$* "
"response, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Indent();
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncWriterInterface< $Request$>>("
"Async$Method$Raw(context, response, cq, tag));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncWriterInterface< $Request$>>"
" $AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"$Response$* "
"response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncWriterInterface< $Request$>>("
"$AsyncPrefix$$Method$Raw(context, response, "
"cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
@@ -241,19 +262,26 @@ void PrintHeaderClientMethodInterfaces(
"($Method$Raw(context, request));\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncReaderInterface< $Response$>> "
"Async$Method$("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Indent();
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderInterface< $Response$>>("
"Async$Method$Raw(context, request, cq, tag));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncReaderInterface< $Response$>> "
"$AsyncPrefix$$Method$("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderInterface< $Response$>>("
"$AsyncPrefix$$Method$Raw(context, request, cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
} else if (method->BidiStreaming()) {
printer->Print(*vars,
"std::unique_ptr< ::grpc::ClientReaderWriterInterface< "
@@ -267,61 +295,89 @@ void PrintHeaderClientMethodInterfaces(
"$Method$Raw(context));\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(
*vars,
"std::unique_ptr< "
"::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>> "
"Async$Method$(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>>("
"Async$Method$Raw(context, cq, tag));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"std::unique_ptr< "
"::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>> "
"$AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>>("
"$AsyncPrefix$$Method$Raw(context, cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
}
} else {
if (method->NoStreaming()) {
printer->Print(
*vars,
"virtual ::grpc::ClientAsyncResponseReaderInterface< $Response$>* "
"Async$Method$Raw(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) = 0;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
"virtual ::grpc::ClientAsyncResponseReaderInterface< $Response$>* "
"$AsyncPrefix$$Method$Raw(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) = 0;\n");
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
"virtual ::grpc::ClientWriterInterface< $Request$>*"
" $Method$Raw("
"::grpc::ClientContext* context, $Response$* response) = 0;\n");
printer->Print(*vars,
"virtual ::grpc::ClientAsyncWriterInterface< $Request$>*"
" Async$Method$Raw(::grpc::ClientContext* context, "
"$Response$* response, "
"::grpc::CompletionQueue* cq, void* tag) = 0;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
printer->Print(
*vars,
"virtual ::grpc::ClientAsyncWriterInterface< $Request$>*"
" $AsyncPrefix$$Method$Raw(::grpc::ClientContext* context, "
"$Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) = 0;\n");
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"virtual ::grpc::ClientReaderInterface< $Response$>* $Method$Raw("
"virtual ::grpc::ClientReaderInterface< $Response$>* "
"$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request) = 0;\n");
printer->Print(
*vars,
"virtual ::grpc::ClientAsyncReaderInterface< $Response$>* "
"Async$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq, void* tag) = 0;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
printer->Print(
*vars,
"virtual ::grpc::ClientAsyncReaderInterface< $Response$>* "
"$AsyncPrefix$$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) = 0;\n");
}
} else if (method->BidiStreaming()) {
printer->Print(*vars,
"virtual ::grpc::ClientReaderWriterInterface< $Request$, "
"$Response$>* "
"$Method$Raw(::grpc::ClientContext* context) = 0;\n");
printer->Print(*vars,
"virtual ::grpc::ClientAsyncReaderWriterInterface< "
"$Request$, $Response$>* "
"Async$Method$Raw(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq, void* tag) = 0;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
printer->Print(
*vars,
"virtual ::grpc::ClientAsyncReaderWriterInterface< "
"$Request$, $Response$>* "
"$AsyncPrefix$$Method$Raw(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) = 0;\n");
}
}
}
}
@@ -333,25 +389,36 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type_name();
(*vars)["Response"] = method->output_type_name();
struct {
grpc::string prefix;
grpc::string method_params; // extra arguments to method
grpc::string raw_args; // extra arguments to raw version of method
} async_prefixes[] = {{"Async", ", void* tag", ", tag"},
{"PrepareAsync", "", ""}};
if (is_public) {
if (method->NoStreaming()) {
printer->Print(
*vars,
"::grpc::Status $Method$(::grpc::ClientContext* context, "
"const $Request$& request, $Response$* response) override;\n");
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
"Async$Method$(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
printer->Indent();
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncResponseReader< $Response$>>("
"Async$Method$Raw(context, request, cq));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
"$AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
printer->Indent();
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncResponseReader< $Response$>>("
"$AsyncPrefix$$Method$Raw(context, request, cq));\n");
printer->Outdent();
printer->Print("}\n");
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
@@ -364,18 +431,25 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
"($Method$Raw(context, response));\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(*vars,
"std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>"
" Async$Method$(::grpc::ClientContext* context, "
"$Response$* response, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>("
"Async$Method$Raw(context, response, cq, tag));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(*vars,
"std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>"
" $AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"$Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>("
"$AsyncPrefix$$Method$Raw(context, response, "
"cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
@@ -389,19 +463,25 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
"($Method$Raw(context, request));\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> "
"Async$Method$("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>>("
"Async$Method$Raw(context, request, cq, tag));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> "
"$AsyncPrefix$$Method$("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>>("
"$AsyncPrefix$$Method$Raw(context, request, cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
} else if (method->BidiStreaming()) {
printer->Print(
*vars,
@@ -414,53 +494,85 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
"$Method$Raw(context));\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(*vars,
"std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
"$Request$, $Response$>> "
"Async$Method$(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Indent();
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>("
"Async$Method$Raw(context, cq, tag));\n");
printer->Outdent();
printer->Print("}\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(*vars,
"std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
"$Request$, $Response$>> "
"$AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>("
"$AsyncPrefix$$Method$Raw(context, cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
}
} else {
if (method->NoStreaming()) {
printer->Print(*vars,
"::grpc::ClientAsyncResponseReader< $Response$>* "
"Async$Method$Raw(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) override;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
"::grpc::ClientAsyncResponseReader< $Response$>* "
"$AsyncPrefix$$Method$Raw(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) override;\n");
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(*vars,
"::grpc::ClientWriter< $Request$>* $Method$Raw("
"::grpc::ClientContext* context, $Response$* response) "
"override;\n");
printer->Print(*vars,
"::grpc::ClientAsyncWriter< $Request$>* Async$Method$Raw("
"::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq, void* tag) override;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"::grpc::ClientAsyncWriter< $Request$>* $AsyncPrefix$$Method$Raw("
"::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) override;\n");
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(*vars,
"::grpc::ClientReader< $Response$>* $Method$Raw("
"::grpc::ClientContext* context, const $Request$& request)"
" override;\n");
printer->Print(
*vars,
"::grpc::ClientAsyncReader< $Response$>* Async$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq, void* tag) override;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"::grpc::ClientAsyncReader< $Response$>* $AsyncPrefix$$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) override;\n");
}
} else if (method->BidiStreaming()) {
printer->Print(*vars,
"::grpc::ClientReaderWriter< $Request$, $Response$>* "
"$Method$Raw(::grpc::ClientContext* context) override;\n");
printer->Print(*vars,
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
"Async$Method$Raw(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq, void* tag) override;\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
"$AsyncPrefix$$Method$Raw(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) override;\n");
}
}
}
}
@@ -469,7 +581,8 @@ void PrintHeaderClientMethodData(grpc_generator::Printer *printer,
const grpc_generator::Method *method,
std::map<grpc::string, grpc::string> *vars) {
(*vars)["Method"] = method->name();
printer->Print(*vars, "const ::grpc::RpcMethod rpcmethod_$Method$_;\n");
printer->Print(*vars,
"const ::grpc::internal::RpcMethod rpcmethod_$Method$_;\n");
}
void PrintHeaderServerMethodSync(grpc_generator::Printer *printer,
@@ -641,7 +754,7 @@ void PrintHeaderServerMethodStreamedUnary(
printer->Print(*vars,
"WithStreamedUnaryMethod_$Method$() {\n"
" ::grpc::Service::MarkMethodStreamed($Idx$,\n"
" new ::grpc::StreamedUnaryHandler< $Request$, "
" new ::grpc::internal::StreamedUnaryHandler< $Request$, "
"$Response$>(std::bind"
"(&WithStreamedUnaryMethod_$Method$<BaseClass>::"
"Streamed$Method$, this, std::placeholders::_1, "
@@ -689,15 +802,16 @@ void PrintHeaderServerMethodSplitStreaming(
"{}\n");
printer->Print(" public:\n");
printer->Indent();
printer->Print(*vars,
"WithSplitStreamingMethod_$Method$() {\n"
" ::grpc::Service::MarkMethodStreamed($Idx$,\n"
" new ::grpc::SplitServerStreamingHandler< $Request$, "
"$Response$>(std::bind"
"(&WithSplitStreamingMethod_$Method$<BaseClass>::"
"Streamed$Method$, this, std::placeholders::_1, "
"std::placeholders::_2)));\n"
"}\n");
printer->Print(
*vars,
"WithSplitStreamingMethod_$Method$() {\n"
" ::grpc::Service::MarkMethodStreamed($Idx$,\n"
" new ::grpc::internal::SplitServerStreamingHandler< $Request$, "
"$Response$>(std::bind"
"(&WithSplitStreamingMethod_$Method$<BaseClass>::"
"Streamed$Method$, this, std::placeholders::_1, "
"std::placeholders::_2)));\n"
"}\n");
printer->Print(*vars,
"~WithSplitStreamingMethod_$Method$() override {\n"
" BaseClassMustBeDerivedFromService(this);\n"
@@ -837,7 +951,8 @@ void PrintHeaderService(grpc_generator::Printer *printer,
" {\n public:\n");
printer->Indent();
printer->Print(
"Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel);\n");
"Stub(const std::shared_ptr< ::grpc::ChannelInterface>& "
"channel);\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintHeaderClientMethod(printer, service->method(i).get(), vars, true);
}
@@ -1095,99 +1210,137 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer,
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type_name();
(*vars)["Response"] = method->output_type_name();
struct {
grpc::string prefix;
grpc::string start; // bool literal expressed as string
grpc::string method_params; // extra arguments to method
grpc::string create_args; // extra arguments to creator
} async_prefixes[] = {{"Async", "true", ", void* tag", ", tag"},
{"PrepareAsync", "false", "", ", nullptr"}};
if (method->NoStreaming()) {
printer->Print(*vars,
"::grpc::Status $ns$$Service$::Stub::$Method$("
"::grpc::ClientContext* context, "
"const $Request$& request, $Response$* response) {\n");
printer->Print(*vars,
" return ::grpc::BlockingUnaryCall(channel_.get(), "
"rpcmethod_$Method$_, "
"context, request, response);\n"
"}\n\n");
printer->Print(
*vars,
"::grpc::ClientAsyncResponseReader< $Response$>* "
"$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
printer->Print(*vars,
" return "
"::grpc::ClientAsyncResponseReader< $Response$>::Create("
"channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, request);\n"
"}\n\n");
" return ::grpc::internal::BlockingUnaryCall"
"(channel_.get(), rpcmethod_$Method$_, "
"context, request, response);\n}\n\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncStart"] = async_prefix.start;
printer->Print(*vars,
"::grpc::ClientAsyncResponseReader< $Response$>* "
"$ns$$Service$::Stub::$AsyncPrefix$$Method$Raw(::grpc::"
"ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
printer->Print(
*vars,
" return "
"::grpc::internal::ClientAsyncResponseReaderFactory< $Response$>"
"::Create(channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, request, $AsyncStart$);\n"
"}\n\n");
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(*vars,
"::grpc::ClientWriter< $Request$>* "
"$ns$$Service$::Stub::$Method$Raw("
"::grpc::ClientContext* context, $Response$* response) {\n");
printer->Print(*vars,
" return new ::grpc::ClientWriter< $Request$>("
"channel_.get(), "
"rpcmethod_$Method$_, "
"context, response);\n"
"}\n\n");
printer->Print(*vars,
"::grpc::ClientAsyncWriter< $Request$>* "
"$ns$$Service$::Stub::Async$Method$Raw("
"::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Print(*vars,
" return ::grpc::ClientAsyncWriter< $Request$>::Create("
"channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, response, tag);\n"
"}\n\n");
printer->Print(
*vars,
" return ::grpc::internal::ClientWriterFactory< $Request$>::Create("
"channel_.get(), "
"rpcmethod_$Method$_, "
"context, response);\n"
"}\n\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncStart"] = async_prefix.start;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncCreateArgs"] = async_prefix.create_args;
printer->Print(*vars,
"::grpc::ClientAsyncWriter< $Request$>* "
"$ns$$Service$::Stub::$AsyncPrefix$$Method$Raw("
"::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Print(
*vars,
" return ::grpc::internal::ClientAsyncWriterFactory< $Request$>"
"::Create(channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, response, $AsyncStart$$AsyncCreateArgs$);\n"
"}\n\n");
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"::grpc::ClientReader< $Response$>* "
"$ns$$Service$::Stub::$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request) {\n");
printer->Print(*vars,
" return new ::grpc::ClientReader< $Response$>("
"channel_.get(), "
"rpcmethod_$Method$_, "
"context, request);\n"
"}\n\n");
printer->Print(*vars,
"::grpc::ClientAsyncReader< $Response$>* "
"$ns$$Service$::Stub::Async$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Print(*vars,
" return ::grpc::ClientAsyncReader< $Response$>::Create("
"channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, request, tag);\n"
"}\n\n");
printer->Print(
*vars,
" return ::grpc::internal::ClientReaderFactory< $Response$>::Create("
"channel_.get(), "
"rpcmethod_$Method$_, "
"context, request);\n"
"}\n\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncStart"] = async_prefix.start;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncCreateArgs"] = async_prefix.create_args;
printer->Print(
*vars,
"::grpc::ClientAsyncReader< $Response$>* "
"$ns$$Service$::Stub::$AsyncPrefix$$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Print(
*vars,
" return ::grpc::internal::ClientAsyncReaderFactory< $Response$>"
"::Create(channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, request, $AsyncStart$$AsyncCreateArgs$);\n"
"}\n\n");
}
} else if (method->BidiStreaming()) {
printer->Print(
*vars,
"::grpc::ClientReaderWriter< $Request$, $Response$>* "
"$ns$$Service$::Stub::$Method$Raw(::grpc::ClientContext* context) {\n");
printer->Print(*vars,
" return new ::grpc::ClientReaderWriter< "
"$Request$, $Response$>("
" return ::grpc::internal::ClientReaderWriterFactory< "
"$Request$, $Response$>::Create("
"channel_.get(), "
"rpcmethod_$Method$_, "
"context);\n"
"}\n\n");
printer->Print(
*vars,
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
"$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq, void* tag) {\n");
printer->Print(
*vars,
" return "
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>::Create("
"channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, tag);\n"
"}\n\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncStart"] = async_prefix.start;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncCreateArgs"] = async_prefix.create_args;
printer->Print(*vars,
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
"$ns$$Service$::Stub::$AsyncPrefix$$Method$Raw(::grpc::"
"ClientContext* context, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Print(*vars,
" return "
"::grpc::internal::ClientAsyncReaderWriterFactory< "
"$Request$, $Response$>::Create("
"channel_.get(), cq, "
"rpcmethod_$Method$_, "
"context, $AsyncStart$$AsyncCreateArgs$);\n"
"}\n\n");
}
}
}
@@ -1297,7 +1450,7 @@ void PrintSourceService(grpc_generator::Printer *printer,
printer->Print(*vars,
", rpcmethod_$Method$_("
"$prefix$$Service$_method_names[$Idx$], "
"::grpc::RpcMethod::$StreamingType$, "
"::grpc::internal::RpcMethod::$StreamingType$, "
"channel"
")\n");
}
@@ -1320,38 +1473,38 @@ void PrintSourceService(grpc_generator::Printer *printer,
if (method->NoStreaming()) {
printer->Print(
*vars,
"AddMethod(new ::grpc::RpcServiceMethod(\n"
"AddMethod(new ::grpc::internal::RpcServiceMethod(\n"
" $prefix$$Service$_method_names[$Idx$],\n"
" ::grpc::RpcMethod::NORMAL_RPC,\n"
" new ::grpc::RpcMethodHandler< $ns$$Service$::Service, "
" ::grpc::internal::RpcMethod::NORMAL_RPC,\n"
" new ::grpc::internal::RpcMethodHandler< $ns$$Service$::Service, "
"$Request$, "
"$Response$>(\n"
" std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
} else if (ClientOnlyStreaming(method.get())) {
printer->Print(
*vars,
"AddMethod(new ::grpc::RpcServiceMethod(\n"
"AddMethod(new ::grpc::internal::RpcServiceMethod(\n"
" $prefix$$Service$_method_names[$Idx$],\n"
" ::grpc::RpcMethod::CLIENT_STREAMING,\n"
" new ::grpc::ClientStreamingHandler< "
" ::grpc::internal::RpcMethod::CLIENT_STREAMING,\n"
" new ::grpc::internal::ClientStreamingHandler< "
"$ns$$Service$::Service, $Request$, $Response$>(\n"
" std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
} else if (ServerOnlyStreaming(method.get())) {
printer->Print(
*vars,
"AddMethod(new ::grpc::RpcServiceMethod(\n"
"AddMethod(new ::grpc::internal::RpcServiceMethod(\n"
" $prefix$$Service$_method_names[$Idx$],\n"
" ::grpc::RpcMethod::SERVER_STREAMING,\n"
" new ::grpc::ServerStreamingHandler< "
" ::grpc::internal::RpcMethod::SERVER_STREAMING,\n"
" new ::grpc::internal::ServerStreamingHandler< "
"$ns$$Service$::Service, $Request$, $Response$>(\n"
" std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
} else if (method->BidiStreaming()) {
printer->Print(
*vars,
"AddMethod(new ::grpc::RpcServiceMethod(\n"
"AddMethod(new ::grpc::internal::RpcServiceMethod(\n"
" $prefix$$Service$_method_names[$Idx$],\n"
" ::grpc::RpcMethod::BIDI_STREAMING,\n"
" new ::grpc::BidiStreamingHandler< "
" ::grpc::internal::RpcMethod::BIDI_STREAMING,\n"
" new ::grpc::internal::BidiStreamingHandler< "
"$ns$$Service$::Service, $Request$, $Response$>(\n"
" std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
}
@@ -1452,7 +1605,8 @@ grpc::string GetMockIncludes(grpc_generator::File *file,
static const char *headers_strs[] = {
"grpc++/impl/codegen/async_stream.h",
"grpc++/impl/codegen/sync_stream.h", "gmock/gmock.h",
"grpc++/impl/codegen/sync_stream.h",
"gmock/gmock.h",
};
std::vector<grpc::string> headers(headers_strs, array_end(headers_strs));
PrintIncludes(printer.get(), headers, params);
@@ -1478,50 +1632,83 @@ void PrintMockClientMethods(grpc_generator::Printer *printer,
(*vars)["Request"] = method->input_type_name();
(*vars)["Response"] = method->output_type_name();
struct {
grpc::string prefix;
grpc::string method_params; // extra arguments to method
int extra_method_param_count;
} async_prefixes[] = {{"Async", ", void* tag", 1}, {"PrepareAsync", "", 0}};
if (method->NoStreaming()) {
printer->Print(
*vars,
"MOCK_METHOD3($Method$, ::grpc::Status(::grpc::ClientContext* context, "
"const $Request$& request, $Response$* response));\n");
printer->Print(*vars,
"MOCK_METHOD3(Async$Method$Raw, "
"::grpc::ClientAsyncResponseReaderInterface< $Response$>*"
"(::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq));\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
"MOCK_METHOD3($AsyncPrefix$$Method$Raw, "
"::grpc::ClientAsyncResponseReaderInterface< $Response$>*"
"(::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq));\n");
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
"MOCK_METHOD2($Method$Raw, "
"::grpc::ClientWriterInterface< $Request$>*"
"(::grpc::ClientContext* context, $Response$* response));\n");
printer->Print(*vars,
"MOCK_METHOD4(Async$Method$Raw, "
"::grpc::ClientAsyncWriterInterface< $Request$>*"
"(::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq, void* tag));\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["MockArgs"] =
flatbuffers::NumToString(3 + async_prefix.extra_method_param_count);
printer->Print(*vars,
"MOCK_METHOD$MockArgs$($AsyncPrefix$$Method$Raw, "
"::grpc::ClientAsyncWriterInterface< $Request$>*"
"(::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$));\n");
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"MOCK_METHOD2($Method$Raw, "
"::grpc::ClientReaderInterface< $Response$>*"
"(::grpc::ClientContext* context, const $Request$& request));\n");
printer->Print(*vars,
"MOCK_METHOD4(Async$Method$Raw, "
"::grpc::ClientAsyncReaderInterface< $Response$>*"
"(::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq, void* tag));\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["MockArgs"] =
flatbuffers::NumToString(3 + async_prefix.extra_method_param_count);
printer->Print(
*vars,
"MOCK_METHOD$MockArgs$($AsyncPrefix$$Method$Raw, "
"::grpc::ClientAsyncReaderInterface< $Response$>*"
"(::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$));\n");
}
} else if (method->BidiStreaming()) {
printer->Print(
*vars,
"MOCK_METHOD1($Method$Raw, "
"::grpc::ClientReaderWriterInterface< $Request$, $Response$>*"
"(::grpc::ClientContext* context));\n");
printer->Print(
*vars,
"MOCK_METHOD3(Async$Method$Raw, "
"::grpc::ClientAsyncReaderWriterInterface<$Request$, $Response$>*"
"(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq, "
"void* tag));\n");
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
auto& async_prefix = async_prefixes[i];
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["MockArgs"] =
flatbuffers::NumToString(2 + async_prefix.extra_method_param_count);
printer->Print(
*vars,
"MOCK_METHOD$MockArgs$($AsyncPrefix$$Method$Raw, "
"::grpc::ClientAsyncReaderWriterInterface<$Request$, $Response$>*"
"(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq"
"$AsyncMethodParams$));\n");
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NET_GRPC_COMPILER_JAVA_GENERATOR_H_
#define NET_GRPC_COMPILER_JAVA_GENERATOR_H_
#include <stdlib.h> // for abort()
#include <iostream>
#include <map>
#include <string>
#include "src/compiler/schema_interface.h"
class LogMessageVoidify {
public:
LogMessageVoidify() {}
// This has to be an operator with a precedence lower than << but
// higher than ?:
void operator&(std::ostream&) {}
};
class LogHelper {
std::ostream* os_;
public:
LogHelper(std::ostream* os) : os_(os) {}
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning( \
disable : 4722) // the flow of control terminates in a destructor
// (needed to compile ~LogHelper where destructor emits abort intentionally -
// inherited from grpc/java code generator).
#endif
~LogHelper() {
*os_ << std::endl;
::abort();
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
std::ostream& get_os() const { return *os_; }
};
// Abort the program after logging the mesage if the given condition is not
// true. Otherwise, do nothing.
#define GRPC_CODEGEN_CHECK(x) \
(x) ? (void)0 \
: LogMessageVoidify() & LogHelper(&std::cerr).get_os() \
<< "CHECK FAILED: " << __FILE__ << ":" \
<< __LINE__ << ": "
// Abort the program after logging the mesage.
#define GRPC_CODEGEN_FAIL GRPC_CODEGEN_CHECK(false)
using namespace std;
namespace grpc_java_generator {
struct Parameters {
// //Defines the custom parameter types for methods
// //eg: flatbuffers uses flatbuffers.Builder as input for the client
// and output for the server grpc::string custom_method_io_type;
// Package name for the service
grpc::string package_name;
};
// Return the source of the generated service file.
grpc::string GenerateServiceSource(grpc_generator::File* file,
const grpc_generator::Service* service,
grpc_java_generator::Parameters* parameters);
} // namespace grpc_java_generator
#endif // NET_GRPC_COMPILER_JAVA_GENERATOR_H_

View File

@@ -40,8 +40,8 @@
#include <vector>
#ifndef GRPC_CUSTOM_STRING
#include <string>
#define GRPC_CUSTOM_STRING std::string
# include <string>
# define GRPC_CUSTOM_STRING std::string
#endif
namespace grpc {

View File

@@ -0,0 +1,119 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import MyGame.Example.Monster;
import MyGame.Example.MonsterStorageGrpc;
import MyGame.Example.Stat;
import com.google.flatbuffers.FlatBufferBuilder;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import org.junit.Assert;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
/**
* Demonstrates basic client-server interaction using grpc-java over netty.
*/
public class JavaGrpcTest {
static final String BIG_MONSTER_NAME = "big-monster";
static final short nestedMonsterHp = 600;
static final short nestedMonsterMana = 1024;
static final int numStreamedMsgs = 10;
static class MyService extends MonsterStorageGrpc.MonsterStorageImplBase {
@Override
public void store(Monster request, io.grpc.stub.StreamObserver<Stat> responseObserver) {
Assert.assertEquals(request.name(), BIG_MONSTER_NAME);
Assert.assertEquals(request.hp(), nestedMonsterHp);
Assert.assertEquals(request.mana(), nestedMonsterMana);
System.out.println("Received store request from " + request.name());
// Create a response from the incoming request name.
FlatBufferBuilder builder = new FlatBufferBuilder();
int statOffset = Stat.createStat(builder, builder.createString("Hello " + request.name()), 100, 10);
builder.finish(statOffset);
Stat stat = Stat.getRootAsStat(builder.dataBuffer());
responseObserver.onNext(stat);
responseObserver.onCompleted();
}
@Override
public void retrieve(Stat request, io.grpc.stub.StreamObserver<Monster> responseObserver) {
// Create 10 monsters for streaming response.
for (int i=0; i<numStreamedMsgs; i++) {
FlatBufferBuilder builder = new FlatBufferBuilder();
int i1 = builder.createString(request.id() + " No." + i);
Monster.startMonster(builder);
Monster.addName(builder, i1);
int i2 = Monster.endMonster(builder);
Monster.finishMonsterBuffer(builder, i2);
Monster monster = Monster.getRootAsMonster(builder.dataBuffer());
responseObserver.onNext(monster);
}
responseObserver.onCompleted();
}
}
private static int startServer() throws IOException {
Server server = ServerBuilder.forPort(0).addService(new MyService()).build().start();
return server.getPort();
}
@org.junit.Test
public void testMonster() throws IOException {
int port = startServer();
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext(true)
.directExecutor()
.build();
MonsterStorageGrpc.MonsterStorageBlockingStub stub = MonsterStorageGrpc.newBlockingStub(channel);
FlatBufferBuilder builder = new FlatBufferBuilder();
int o_string = builder.createString(BIG_MONSTER_NAME);
Monster.startMonster(builder);
Monster.addName(builder, o_string);
Monster.addHp(builder, nestedMonsterHp);
Monster.addMana(builder, nestedMonsterMana);
int monster1 = Monster.endMonster(builder);
Monster.finishMonsterBuffer(builder, monster1);
ByteBuffer buffer = builder.dataBuffer();
Monster monsterRequest = Monster.getRootAsMonster(buffer);
Stat stat = stub.store(monsterRequest);
Assert.assertEquals(stat.id(), "Hello " + BIG_MONSTER_NAME);
System.out.println("Received stat response from service: " + stat.id());
Iterator<Monster> iterator = stub.retrieve(stat);
int counter = 0;
while(iterator.hasNext()) {
Monster m = iterator.next();
System.out.println("Received monster " + m.name());
counter ++;
}
Assert.assertEquals(counter, numStreamedMsgs);
System.out.println("FlatBuffers GRPC client/server test: completed successfully");
}
}

View File

@@ -18,46 +18,48 @@
#include <grpc++/grpc++.h>
#include "monster_test_generated.h"
#include "monster_test.grpc.fb.h"
#include "monster_test_generated.h"
using namespace MyGame::Example;
// The callback implementation of our server, that derives from the generated
// code. It implements all rpcs specified in the FlatBuffers schema.
class ServiceImpl final : public MyGame::Example::MonsterStorage::Service {
virtual ::grpc::Status Store(::grpc::ServerContext* context,
const flatbuffers::grpc::Message<Monster> *request,
flatbuffers::grpc::Message<Stat> *response)
override {
virtual ::grpc::Status Store(
::grpc::ServerContext *context,
const flatbuffers::grpc::Message<Monster> *request,
flatbuffers::grpc::Message<Stat> *response) override {
// Create a response from the incoming request name.
fbb_.Clear();
auto stat_offset = CreateStat(fbb_, fbb_.CreateString("Hello, " +
request->GetRoot()->name()->str()));
auto stat_offset = CreateStat(
fbb_, fbb_.CreateString("Hello, " + request->GetRoot()->name()->str()));
fbb_.Finish(stat_offset);
// Transfer ownership of the message to gRPC
*response = fbb_.ReleaseMessage<Stat>();
return grpc::Status::OK;
}
virtual ::grpc::Status Retrieve(::grpc::ServerContext *context,
const flatbuffers::grpc::Message<Stat> *request,
::grpc::ServerWriter< flatbuffers::grpc::Message<Monster>>* writer)
override {
virtual ::grpc::Status Retrieve(
::grpc::ServerContext *context,
const flatbuffers::grpc::Message<Stat> *request,
::grpc::ServerWriter<flatbuffers::grpc::Message<Monster>> *writer)
override {
for (int i = 0; i < 10; i++) {
fbb_.Clear();
// Create 10 monsters for resposne.
auto monster_offset =
CreateMonster(fbb_, 0, 0, 0,
fbb_.CreateString(request->GetRoot()->id()->str() +
" No." + std::to_string(i)));
fbb_.Finish(monster_offset);
for (int i=0; i<10; i++) {
fbb_.Clear();
// Create 10 monsters for resposne.
auto monster_offset =
CreateMonster(fbb_, 0, 0, 0, fbb_.CreateString(
request->GetRoot()->id()->str() + " No." + std::to_string(i)));
fbb_.Finish(monster_offset);
flatbuffers::grpc::Message<Monster> monster =
fbb_.ReleaseMessage<Monster>();
flatbuffers::grpc::Message<Monster> monster = fbb_.ReleaseMessage<Monster>();
// Send monster to client using streaming.
writer->Write(monster);
}
return grpc::Status::OK;
// Send monster to client using streaming.
writer->Write(monster);
}
return grpc::Status::OK;
}
private:
@@ -90,7 +92,7 @@ void RunServer() {
server_instance->Wait();
}
int main(int /*argc*/, const char * /*argv*/[]) {
int main(int /*argc*/, const char * /*argv*/ []) {
// Launch server.
std::thread server_thread(RunServer);
@@ -103,7 +105,6 @@ int main(int /*argc*/, const char * /*argv*/[]) {
grpc::InsecureChannelCredentials());
auto stub = MyGame::Example::MonsterStorage::NewStub(channel);
flatbuffers::grpc::MessageBuilder fbb;
{
grpc::ClientContext context;
@@ -138,7 +139,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
}
}
#if !FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
#if !FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
{
// Test that an invalid request errors out correctly
grpc::ClientContext context;
@@ -149,9 +150,10 @@ int main(int /*argc*/, const char * /*argv*/[]) {
// matches the protobuf gRPC status code for an unparseable message.
assert(!status.ok());
assert(status.error_code() == ::grpc::StatusCode::INTERNAL);
assert(strcmp(status.error_message().c_str(), "Message verification failed") == 0);
assert(strcmp(status.error_message().c_str(),
"Message verification failed") == 0);
}
#endif
#endif
server_instance->Shutdown();

4
grpc/tests/java-grpc-test.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/sh
# NOTE: make sure `mvn install` in /gprc is executed before running this test
mvn test

73
grpc/tests/pom.xml Normal file
View File

@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-parent</artifactId>
<version>1.8.0</version>
</parent>
<artifactId>grpc-test</artifactId>
<description>Example/Test project demonstrating usage of flatbuffers with GRPC-Java instead of protobufs
</description>
<properties>
<gRPC.version>1.8.0</gRPC.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java-grpc</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${gRPC.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${gRPC.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}</source>
<source>${project.basedir}/../../tests</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!--<testSourceDirectory>${project.basedir}</testSourceDirectory>-->
</build>
</project>

View File

@@ -1,6 +1,7 @@
#ifndef FLATBUFFERS_BASE_H_
#define FLATBUFFERS_BASE_H_
// clang-format off
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
defined(_MSC_VER) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC
@@ -99,7 +100,7 @@
#endif // !defined(FLATBUFFERS_LITTLEENDIAN)
#define FLATBUFFERS_VERSION_MAJOR 1
#define FLATBUFFERS_VERSION_MINOR 8
#define FLATBUFFERS_VERSION_MINOR 9
#define FLATBUFFERS_VERSION_REVISION 0
#define FLATBUFFERS_STRING_EXPAND(X) #X
#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
@@ -120,6 +121,13 @@
#define FLATBUFFERS_CONSTEXPR
#endif
#if (defined(__cplusplus) && __cplusplus >= 201402L) || \
(defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
#define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR
#else
#define FLATBUFFERS_CONSTEXPR_CPP14
#endif
#if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \
defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026
#define FLATBUFFERS_NOEXCEPT noexcept

View File

@@ -49,7 +49,7 @@ class CodeWriter {
// Associates a key with a value. All subsequent calls to operator+=, where
// the specified key is contained in {{ and }} delimiters will be replaced by
// the given value.
void SetValue(const std::string& key, const std::string& value) {
void SetValue(const std::string &key, const std::string &value) {
value_map_[key] = value;
}
@@ -71,8 +71,7 @@ class BaseGenerator {
public:
virtual bool generate() = 0;
static std::string NamespaceDir(const Parser &parser,
const std::string &path,
static std::string NamespaceDir(const Parser &parser, const std::string &path,
const Namespace &ns);
protected:
@@ -128,8 +127,7 @@ struct CommentConfig {
};
extern void GenComment(const std::vector<std::string> &dc,
std::string *code_ptr,
const CommentConfig *config,
std::string *code_ptr, const CommentConfig *config,
const char *prefix = "");
} // namespace flatbuffers

File diff suppressed because it is too large Load Diff

View File

@@ -14,15 +14,15 @@
* limitations under the License.
*/
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include <functional>
#include <limits>
#include <string>
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#ifndef FLATC_H_
#define FLATC_H_
# define FLATC_H_
namespace flatbuffers {
@@ -49,12 +49,10 @@ class FlatCompiler {
MakeRuleFn make_rule;
};
typedef void (*WarnFn)(const FlatCompiler *flatc,
const std::string &warn,
typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn,
bool show_exe_name);
typedef void (*ErrorFn)(const FlatCompiler *flatc,
const std::string &err,
typedef void (*ErrorFn)(const FlatCompiler *flatc, const std::string &err,
bool usage, bool show_exe_name);
// Parameters required to initialize the FlatCompiler.
@@ -65,21 +63,20 @@ class FlatCompiler {
warn_fn(nullptr),
error_fn(nullptr) {}
const Generator* generators;
const Generator *generators;
size_t num_generators;
WarnFn warn_fn;
ErrorFn error_fn;
};
explicit FlatCompiler(const InitParams& params) : params_(params) {}
explicit FlatCompiler(const InitParams &params) : params_(params) {}
int Compile(int argc, const char** argv);
int Compile(int argc, const char **argv);
std::string GetUsageString(const char* program_name) const;
std::string GetUsageString(const char *program_name) const;
private:
void ParseFile(flatbuffers::Parser &parser,
const std::string &filename,
void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
const std::string &contents,
std::vector<const char *> &include_directories) const;
@@ -91,7 +88,6 @@ class FlatCompiler {
InitParams params_;
};
} // namespace flatbuffers
#endif // FLATC_H_

View File

@@ -24,12 +24,12 @@
#include "flatbuffers/util.h"
#ifdef _MSC_VER
#include <intrin.h>
# include <intrin.h>
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4127) // C4127: conditional expression is constant
# pragma warning(push)
# pragma warning(disable : 4127) // C4127: conditional expression is constant
#endif
namespace flexbuffers {
@@ -60,24 +60,25 @@ enum Type {
TYPE_INDIRECT_UINT = 7,
TYPE_INDIRECT_FLOAT = 8,
TYPE_MAP = 9,
TYPE_VECTOR = 10, // Untyped.
TYPE_VECTOR_INT = 11, // Typed any size (stores no type table).
TYPE_VECTOR = 10, // Untyped.
TYPE_VECTOR_INT = 11, // Typed any size (stores no type table).
TYPE_VECTOR_UINT = 12,
TYPE_VECTOR_FLOAT = 13,
TYPE_VECTOR_KEY = 14,
TYPE_VECTOR_STRING = 15,
TYPE_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
TYPE_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
TYPE_VECTOR_UINT2 = 17,
TYPE_VECTOR_FLOAT2 = 18,
TYPE_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
TYPE_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
TYPE_VECTOR_UINT3 = 20,
TYPE_VECTOR_FLOAT3 = 21,
TYPE_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
TYPE_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
TYPE_VECTOR_UINT4 = 23,
TYPE_VECTOR_FLOAT4 = 24,
TYPE_BLOB = 25,
TYPE_BOOL = 26,
TYPE_VECTOR_BOOL = 36, // To Allow the same type of conversion of type to vector type
TYPE_VECTOR_BOOL =
36, // To Allow the same type of conversion of type to vector type
};
inline bool IsInline(Type t) { return t <= TYPE_FLOAT || t == TYPE_BOOL; }
@@ -87,7 +88,8 @@ inline bool IsTypedVectorElementType(Type t) {
}
inline bool IsTypedVector(Type t) {
return (t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING) || t == TYPE_VECTOR_BOOL;
return (t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING) ||
t == TYPE_VECTOR_BOOL;
}
inline bool IsFixedTypedVector(Type t) {
@@ -113,7 +115,8 @@ inline Type ToTypedVectorElementType(Type t) {
inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) {
assert(IsFixedTypedVector(t));
auto fixed_type = t - TYPE_VECTOR_INT2;
*len = static_cast<uint8_t>(fixed_type / 3 + 2); // 3 types each, starting from length 2.
*len = static_cast<uint8_t>(fixed_type / 3 +
2); // 3 types each, starting from length 2.
return static_cast<Type>(fixed_type % 3 + TYPE_INT);
}
@@ -127,19 +130,20 @@ typedef int8_t quarter;
// decently quick, but it is the most frequently executed function.
// We could do an (unaligned) 64-bit read if we ifdef out the platforms for
// which that doesn't work (or where we'd read into un-owned memory).
template <typename R, typename T1, typename T2, typename T4, typename T8>
template<typename R, typename T1, typename T2, typename T4, typename T8>
R ReadSizedScalar(const uint8_t *data, uint8_t byte_width) {
return byte_width < 4
? (byte_width < 2 ? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
: static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
: (byte_width < 8 ? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
: static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
? (byte_width < 2
? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
: static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
: (byte_width < 8
? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
: static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
}
inline int64_t ReadInt64(const uint8_t *data, uint8_t byte_width) {
return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(data,
byte_width);
return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(
data, byte_width);
}
inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
@@ -148,6 +152,7 @@ inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
// TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a
// constant, which here it isn't. Test if memcpy is still faster than
// the conditionals in ReadSizedScalar. Can also use inline asm.
// clang-format off
#ifdef _MSC_VER
uint64_t u = 0;
__movsb(reinterpret_cast<uint8_t *>(&u),
@@ -157,11 +162,12 @@ inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
return ReadSizedScalar<uint64_t, uint8_t, uint16_t, uint32_t, uint64_t>(
data, byte_width);
#endif
// clang-format on
}
inline double ReadDouble(const uint8_t *data, uint8_t byte_width) {
return ReadSizedScalar<double, quarter, half, float, double>(data,
byte_width);
byte_width);
}
inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) {
@@ -173,13 +179,14 @@ template<typename T> const uint8_t *Indirect(const uint8_t *offset) {
}
inline BitWidth WidthU(uint64_t u) {
#define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) { \
#define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) \
{ \
if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \
}
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 8);
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 16);
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 32);
#undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
#undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
return BIT_WIDTH_64;
}
@@ -198,7 +205,7 @@ inline BitWidth WidthF(double f) {
class Object {
public:
Object(const uint8_t *data, uint8_t byte_width)
: data_(data), byte_width_(byte_width) {}
: data_(data), byte_width_(byte_width) {}
protected:
const uint8_t *data_;
@@ -216,15 +223,14 @@ class Sized : public Object {
class String : public Sized {
public:
String(const uint8_t *data, uint8_t byte_width)
: Sized(data, byte_width) {}
String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
size_t length() const { return size(); }
const char *c_str() const { return reinterpret_cast<const char *>(data_); }
std::string str() const { return std::string(c_str(), length()); }
static String EmptyString() {
static const uint8_t empty_string[] = { 0/*len*/, 0/*terminator*/ };
static const uint8_t empty_string[] = { 0 /*len*/, 0 /*terminator*/ };
return String(empty_string + 1, 1);
}
bool IsTheEmptyString() const { return data_ == EmptyString().data_; }
@@ -233,10 +239,10 @@ class String : public Sized {
class Blob : public Sized {
public:
Blob(const uint8_t *data_buf, uint8_t byte_width)
: Sized(data_buf, byte_width) {}
: Sized(data_buf, byte_width) {}
static Blob EmptyBlob() {
static const uint8_t empty_blob[] = { 0/*len*/ };
static const uint8_t empty_blob[] = { 0 /*len*/ };
return Blob(empty_blob + 1, 1);
}
bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; }
@@ -245,13 +251,12 @@ class Blob : public Sized {
class Vector : public Sized {
public:
Vector(const uint8_t *data, uint8_t byte_width)
: Sized(data, byte_width) {}
Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
Reference operator[](size_t i) const;
static Vector EmptyVector() {
static const uint8_t empty_vector[] = { 0/*len*/ };
static const uint8_t empty_vector[] = { 0 /*len*/ };
return Vector(empty_vector + 1, 1);
}
bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; }
@@ -260,12 +265,12 @@ class Vector : public Sized {
class TypedVector : public Sized {
public:
TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type)
: Sized(data, byte_width), type_(element_type) {}
: Sized(data, byte_width), type_(element_type) {}
Reference operator[](size_t i) const;
static TypedVector EmptyTypedVector() {
static const uint8_t empty_typed_vector[] = { 0/*len*/ };
static const uint8_t empty_typed_vector[] = { 0 /*len*/ };
return TypedVector(empty_typed_vector + 1, 1, TYPE_INT);
}
bool IsTheEmptyVector() const {
@@ -284,12 +289,12 @@ class FixedTypedVector : public Object {
public:
FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type,
uint8_t len)
: Object(data, byte_width), type_(element_type), len_(len) {}
: Object(data, byte_width), type_(element_type), len_(len) {}
Reference operator[](size_t i) const;
static FixedTypedVector EmptyFixedTypedVector() {
static const uint8_t fixed_empty_vector[] = { 0/* unused */ };
static const uint8_t fixed_empty_vector[] = { 0 /* unused */ };
return FixedTypedVector(fixed_empty_vector, 1, TYPE_INT, 0);
}
bool IsTheEmptyFixedTypedVector() const {
@@ -306,8 +311,7 @@ class FixedTypedVector : public Object {
class Map : public Vector {
public:
Map(const uint8_t *data, uint8_t byte_width)
: Vector(data, byte_width) {}
Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {}
Reference operator[](const char *key) const;
Reference operator[](const std::string &key) const;
@@ -319,31 +323,31 @@ class Map : public Vector {
auto keys_offset = data_ - byte_width_ * num_prefixed_fields;
return TypedVector(Indirect(keys_offset, byte_width_),
static_cast<uint8_t>(
ReadUInt64(keys_offset + byte_width_, byte_width_)),
ReadUInt64(keys_offset + byte_width_, byte_width_)),
TYPE_KEY);
}
static Map EmptyMap() {
static const uint8_t empty_map[] = {
0/*keys_len*/, 0/*keys_offset*/, 1/*keys_width*/, 0/*len*/
0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/
};
return Map(empty_map + 4, 1);
}
bool IsTheEmptyMap() const {
return data_ == EmptyMap().data_;
}
bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; }
};
class Reference {
public:
Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
Type type)
: data_(data), parent_width_(parent_width), byte_width_(byte_width),
type_(type) {}
: data_(data),
parent_width_(parent_width),
byte_width_(byte_width),
type_(type) {}
Reference(const uint8_t *data, uint8_t parent_width, uint8_t packed_type)
: data_(data), parent_width_(parent_width) {
: data_(data), parent_width_(parent_width) {
byte_width_ = 1U << static_cast<BitWidth>(packed_type & 3);
type_ = static_cast<Type>(packed_type >> 2);
}
@@ -352,13 +356,14 @@ class Reference {
bool IsNull() const { return type_ == TYPE_NULL; }
bool IsBool() const { return type_ == TYPE_BOOL; }
bool IsInt() const { return type_ == TYPE_INT ||
type_ == TYPE_INDIRECT_INT; }
bool IsUInt() const { return type_ == TYPE_UINT||
type_ == TYPE_INDIRECT_UINT;; }
bool IsInt() const { return type_ == TYPE_INT || type_ == TYPE_INDIRECT_INT; }
bool IsUInt() const {
return type_ == TYPE_UINT || type_ == TYPE_INDIRECT_UINT;
}
bool IsIntOrUint() const { return IsInt() || IsUInt(); }
bool IsFloat() const { return type_ == TYPE_FLOAT ||
type_ == TYPE_INDIRECT_FLOAT; }
bool IsFloat() const {
return type_ == TYPE_FLOAT || type_ == TYPE_INDIRECT_FLOAT;
}
bool IsNumeric() const { return IsIntOrUint() || IsFloat(); }
bool IsString() const { return type_ == TYPE_STRING; }
bool IsKey() const { return type_ == TYPE_KEY; }
@@ -367,7 +372,8 @@ class Reference {
bool IsBlob() const { return type_ == TYPE_BLOB; }
bool AsBool() const {
return (type_ == TYPE_BOOL ? ReadUInt64(data_, parent_width_) : AsUInt64()) != 0;
return (type_ == TYPE_BOOL ? ReadUInt64(data_, parent_width_)
: AsUInt64()) != 0;
}
// Reads any type as a int64_t. Never fails, does most sensible conversion.
@@ -377,79 +383,82 @@ class Reference {
if (type_ == TYPE_INT) {
// A fast path for the common case.
return ReadInt64(data_, parent_width_);
} else switch (type_) {
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_UINT: return ReadUInt64(data_, parent_width_);
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_FLOAT: return static_cast<int64_t>(
ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT: return static_cast<int64_t>(
ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<int64_t>(AsVector().size());
case TYPE_BOOL: return ReadInt64(data_, parent_width_);
default:
// Convert other things to int.
return 0;
}
} else
switch (type_) {
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_UINT: return ReadUInt64(data_, parent_width_);
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_FLOAT:
return static_cast<int64_t>(ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT:
return static_cast<int64_t>(ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<int64_t>(AsVector().size());
case TYPE_BOOL: return ReadInt64(data_, parent_width_);
default:
// Convert other things to int.
return 0;
}
}
// TODO: could specialize these to not use AsInt64() if that saves
// extension ops in generated code, and use a faster op than ReadInt64.
int32_t AsInt32() const { return static_cast<int32_t>(AsInt64()); }
int16_t AsInt16() const { return static_cast<int16_t>(AsInt64()); }
int8_t AsInt8() const { return static_cast<int8_t> (AsInt64()); }
int8_t AsInt8() const { return static_cast<int8_t>(AsInt64()); }
uint64_t AsUInt64() const {
if (type_ == TYPE_UINT) {
// A fast path for the common case.
return ReadUInt64(data_, parent_width_);
} else switch (type_) {
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_INT: return ReadInt64(data_, parent_width_);
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_FLOAT: return static_cast<uint64_t>(
ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT: return static_cast<uint64_t>(
ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToUInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<uint64_t>(AsVector().size());
case TYPE_BOOL: return ReadUInt64(data_, parent_width_);
default:
// Convert other things to uint.
return 0;
}
} else
switch (type_) {
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
case TYPE_INT: return ReadInt64(data_, parent_width_);
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
case TYPE_FLOAT:
return static_cast<uint64_t>(ReadDouble(data_, parent_width_));
case TYPE_INDIRECT_FLOAT:
return static_cast<uint64_t>(ReadDouble(Indirect(), byte_width_));
case TYPE_NULL: return 0;
case TYPE_STRING: return flatbuffers::StringToUInt(AsString().c_str());
case TYPE_VECTOR: return static_cast<uint64_t>(AsVector().size());
case TYPE_BOOL: return ReadUInt64(data_, parent_width_);
default:
// Convert other things to uint.
return 0;
}
}
uint32_t AsUInt32() const { return static_cast<uint32_t>(AsUInt64()); }
uint16_t AsUInt16() const { return static_cast<uint16_t>(AsUInt64()); }
uint8_t AsUInt8() const { return static_cast<uint8_t> (AsUInt64()); }
uint8_t AsUInt8() const { return static_cast<uint8_t>(AsUInt64()); }
double AsDouble() const {
if (type_ == TYPE_FLOAT) {
// A fast path for the common case.
return ReadDouble(data_, parent_width_);
} else switch (type_) {
case TYPE_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
case TYPE_INT: return static_cast<double>(
ReadInt64(data_, parent_width_));
case TYPE_UINT: return static_cast<double>(
ReadUInt64(data_, parent_width_));
case TYPE_INDIRECT_INT: return static_cast<double>(
ReadInt64(Indirect(), byte_width_));
case TYPE_INDIRECT_UINT: return static_cast<double>(
ReadUInt64(Indirect(), byte_width_));
case TYPE_NULL: return 0.0;
case TYPE_STRING: return strtod(AsString().c_str(), nullptr);
case TYPE_VECTOR: return static_cast<double>(AsVector().size());
case TYPE_BOOL: return static_cast<double>(
ReadUInt64(data_, parent_width_));
default:
// Convert strings and other things to float.
return 0;
}
} else
switch (type_) {
case TYPE_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
case TYPE_INT:
return static_cast<double>(ReadInt64(data_, parent_width_));
case TYPE_UINT:
return static_cast<double>(ReadUInt64(data_, parent_width_));
case TYPE_INDIRECT_INT:
return static_cast<double>(ReadInt64(Indirect(), byte_width_));
case TYPE_INDIRECT_UINT:
return static_cast<double>(ReadUInt64(Indirect(), byte_width_));
case TYPE_NULL: return 0.0;
case TYPE_STRING: return strtod(AsString().c_str(), nullptr);
case TYPE_VECTOR: return static_cast<double>(AsVector().size());
case TYPE_BOOL:
return static_cast<double>(ReadUInt64(data_, parent_width_));
default:
// Convert strings and other things to float.
return 0;
}
}
float AsFloat() const { return static_cast<float>(AsDouble()); }
@@ -652,9 +661,7 @@ class Reference {
memcpy(const_cast<char *>(s.c_str()), str, len);
return true;
}
bool MutateString(const char *str) {
return MutateString(str, strlen(str));
}
bool MutateString(const char *str) { return MutateString(str, strlen(str)); }
bool MutateString(const std::string &str) {
return MutateString(str.data(), str.length());
}
@@ -664,9 +671,11 @@ class Reference {
return flexbuffers::Indirect(data_, parent_width_);
}
template<typename T> bool Mutate(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <= byte_width;
template<typename T>
bool Mutate(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <=
byte_width;
if (fits) {
t = flatbuffers::EndianScalar(t);
memcpy(const_cast<uint8_t *>(dest), &t, byte_width);
@@ -674,8 +683,9 @@ class Reference {
return fits;
}
template<typename T> bool MutateF(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
template<typename T>
bool MutateF(const uint8_t *dest, T t, size_t byte_width,
BitWidth value_width) {
if (byte_width == sizeof(double))
return Mutate(dest, static_cast<double>(t), byte_width, value_width);
if (byte_width == sizeof(float))
@@ -707,21 +717,25 @@ template<> inline double Reference::As<double>() { return AsDouble(); }
template<> inline float Reference::As<float>() { return AsFloat(); }
template<> inline String Reference::As<String>() { return AsString(); }
template<> inline std::string Reference::As<std::string>() { return AsString().str(); }
template<> inline std::string Reference::As<std::string>() {
return AsString().str();
}
template<> inline Blob Reference::As<Blob>() { return AsBlob(); }
template<> inline Vector Reference::As<Vector>() { return AsVector(); }
template<> inline TypedVector Reference::As<TypedVector>() { return AsTypedVector(); }
template<> inline FixedTypedVector Reference::As<FixedTypedVector>() { return AsFixedTypedVector(); }
template<> inline TypedVector Reference::As<TypedVector>() {
return AsTypedVector();
}
template<> inline FixedTypedVector Reference::As<FixedTypedVector>() {
return AsFixedTypedVector();
}
template<> inline Map Reference::As<Map>() { return AsMap(); }
inline uint8_t PackedType(BitWidth bit_width, Type type) {
return static_cast<uint8_t>(bit_width | (type << 2));
}
inline uint8_t NullPackedType() {
return PackedType(BIT_WIDTH_8, TYPE_NULL);
}
inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, TYPE_NULL); }
// Vector accessors.
// Note: if you try to access outside of bounds, you get a Null value back
@@ -730,7 +744,7 @@ inline uint8_t NullPackedType() {
// wanted 3d).
// The Null converts seamlessly into a default value for any other type.
// TODO(wvo): Could introduce an #ifdef that makes this into an assert?
inline Reference Vector::operator[](size_t i) const {
inline Reference Vector::operator[](size_t i) const {
auto len = size();
if (i >= len) return Reference(nullptr, 1, NullPackedType());
auto packed_type = (data_ + len * byte_width_)[i];
@@ -738,14 +752,14 @@ inline Reference Vector::operator[](size_t i) const {
return Reference(elem, byte_width_, packed_type);
}
inline Reference TypedVector::operator[](size_t i) const {
inline Reference TypedVector::operator[](size_t i) const {
auto len = size();
if (i >= len) return Reference(nullptr, 1, NullPackedType());
auto elem = data_ + i * byte_width_;
return Reference(elem, byte_width_, 1, type_);
}
inline Reference FixedTypedVector::operator[](size_t i) const {
inline Reference FixedTypedVector::operator[](size_t i) const {
if (i >= len_) return Reference(nullptr, 1, NullPackedType());
auto elem = data_ + i * byte_width_;
return Reference(elem, byte_width_, 1, type_);
@@ -753,7 +767,7 @@ inline Reference FixedTypedVector::operator[](size_t i) const {
template<typename T> int KeyCompare(const void *key, const void *elem) {
auto str_elem = reinterpret_cast<const char *>(
Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
auto skey = reinterpret_cast<const char *>(key);
return strcmp(skey, str_elem);
}
@@ -770,8 +784,7 @@ inline Reference Map::operator[](const char *key) const {
case 8: comp = KeyCompare<uint64_t>; break;
}
auto res = std::bsearch(key, keys.data_, keys.size(), keys.byte_width_, comp);
if (!res)
return Reference(nullptr, 1, NullPackedType());
if (!res) return Reference(nullptr, 1, NullPackedType());
auto i = (reinterpret_cast<uint8_t *>(res) - keys.data_) / keys.byte_width_;
return (*static_cast<const Vector *>(this))[i];
}
@@ -817,8 +830,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
public:
Builder(size_t initial_size = 256,
BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS)
: buf_(initial_size), finished_(false), flags_(flags),
force_min_bit_width_(BIT_WIDTH_8), key_pool(KeyOffsetCompare(buf_)),
: buf_(initial_size),
finished_(false),
flags_(flags),
force_min_bit_width_(BIT_WIDTH_8),
key_pool(KeyOffsetCompare(buf_)),
string_pool(StringOffsetCompare(buf_)) {
buf_.clear();
}
@@ -831,9 +847,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
// Size of the buffer. Does not include unfinished values.
size_t GetSize() const {
return buf_.size();
}
size_t GetSize() const { return buf_.size(); }
// Reset all state so we can re-use the buffer.
void Clear() {
@@ -851,26 +865,42 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// vectors and elsewhere).
void Null() { stack_.push_back(Value()); }
void Null(const char *key) { Key(key); Null(); }
void Null(const char *key) {
Key(key);
Null();
}
void Int(int64_t i) { stack_.push_back(Value(i, TYPE_INT, WidthI(i))); }
void Int(const char *key, int64_t i) { Key(key); Int(i); }
void Int(const char *key, int64_t i) {
Key(key);
Int(i);
}
void UInt(uint64_t u) { stack_.push_back(Value(u, TYPE_UINT, WidthU(u))); }
void UInt(const char *key, uint64_t u) { Key(key); Int(u); }
void UInt(const char *key, uint64_t u) {
Key(key);
Int(u);
}
void Float(float f) { stack_.push_back(Value(f)); }
void Float(const char *key, float f) { Key(key); Float(f); }
void Float(const char *key, float f) {
Key(key);
Float(f);
}
void Double(double f) { stack_.push_back(Value(f)); }
void Double(const char *key, double d) { Key(key); Double(d); }
void Double(const char *key, double d) {
Key(key);
Double(d);
}
void Bool(bool b) { stack_.push_back(Value(b)); }
void Bool(const char *key, bool b) { Key(key); Bool(b); }
void IndirectInt(int64_t i) {
PushIndirect(i, TYPE_INDIRECT_INT, WidthI(i));
void Bool(const char *key, bool b) {
Key(key);
Bool(b);
}
void IndirectInt(int64_t i) { PushIndirect(i, TYPE_INDIRECT_INT, WidthI(i)); }
void IndirectInt(const char *key, int64_t i) {
Key(key);
IndirectInt(i);
@@ -939,9 +969,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
return sloc;
}
size_t String(const char *str) {
return String(str, strlen(str));
}
size_t String(const char *str) { return String(str, strlen(str)); }
size_t String(const std::string &str) {
return String(str.c_str(), str.size());
}
@@ -974,9 +1002,15 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// Also some FlatBuffers types?
size_t StartVector() { return stack_.size(); }
size_t StartVector(const char *key) { Key(key); return stack_.size(); }
size_t StartVector(const char *key) {
Key(key);
return stack_.size();
}
size_t StartMap() { return stack_.size(); }
size_t StartMap(const char *key) { Key(key); return stack_.size(); }
size_t StartMap(const char *key) {
Key(key);
return stack_.size();
}
// TODO(wvo): allow this to specify an aligment greater than the natural
// alignment.
@@ -1000,7 +1034,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
// Now sort values, so later we can do a binary seach lookup.
// We want to sort 2 array elements at a time.
struct TwoValue { Value key; Value val; };
struct TwoValue {
Value key;
Value val;
};
// TODO(wvo): strict aliasing?
// TODO(wvo): allow the caller to indicate the data is already sorted
// for maximum efficiency? With an assert to check sortedness to make sure
@@ -1011,22 +1048,22 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// sorted fashion.
// std::sort is typically already a lot faster on sorted data though.
auto dict =
reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) +
start);
reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) + start);
std::sort(dict, dict + len,
[&](const TwoValue &a, const TwoValue &b) -> bool {
auto as = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + a.key.u_);
auto bs = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + b.key.u_);
auto comp = strcmp(as, bs);
// If this assertion hits, you've added two keys with the same value to
// this map.
// TODO: Have to check for pointer equality, as some sort implementation
// apparently call this function with the same element?? Why?
assert(comp || &a == &b);
return comp < 0;
});
auto as = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + a.key.u_);
auto bs = reinterpret_cast<const char *>(
flatbuffers::vector_data(buf_) + b.key.u_);
auto comp = strcmp(as, bs);
// If this assertion hits, you've added two keys with the same
// value to this map.
// TODO: Have to check for pointer equality, as some sort
// implementation apparently call this function with the same
// element?? Why?
assert(comp || &a == &b);
return comp < 0;
});
// First create a vector out of all keys.
// TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share
// the first vector.
@@ -1043,7 +1080,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, false, false);
}
template <typename F, typename T> size_t Vector(F f, T &state) {
template<typename F, typename T> size_t Vector(F f, T &state) {
auto start = StartVector();
f(state);
return EndVector(start, false, false);
@@ -1053,8 +1090,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, false, false);
}
template <typename F, typename T> size_t Vector(const char *key, F f,
T &state) {
template<typename F, typename T>
size_t Vector(const char *key, F f, T &state) {
auto start = StartVector(key);
f(state);
return EndVector(start, false, false);
@@ -1070,8 +1107,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
EndVector(start, false, false);
}
}
template<typename T> void Vector(const char *key, const T *elems,
size_t len) {
template<typename T>
void Vector(const char *key, const T *elems, size_t len) {
Key(key);
Vector(elems, len);
}
@@ -1084,7 +1121,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, true, false);
}
template <typename F, typename T> size_t TypedVector(F f, T &state) {
template<typename F, typename T> size_t TypedVector(F f, T &state) {
auto start = StartVector();
f(state);
return EndVector(start, true, false);
@@ -1094,8 +1131,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndVector(start, true, false);
}
template <typename F, typename T> size_t TypedVector(const char *key, F f,
T &state) {
template<typename F, typename T>
size_t TypedVector(const char *key, F f, T &state) {
auto start = StartVector(key);
f(state);
return EndVector(start, true, false);
@@ -1110,8 +1147,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
return ScalarVector(elems, len, true);
}
template<typename T> size_t FixedTypedVector(const char *key, const T *elems,
size_t len) {
template<typename T>
size_t FixedTypedVector(const char *key, const T *elems, size_t len) {
Key(key);
return FixedTypedVector(elems, len);
}
@@ -1121,7 +1158,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndMap(start);
}
template <typename F, typename T> size_t Map(F f, T &state) {
template<typename F, typename T> size_t Map(F f, T &state) {
auto start = StartMap();
f(state);
return EndMap(start);
@@ -1131,8 +1168,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
f();
return EndMap(start);
}
template <typename F, typename T> size_t Map(const char *key, F f,
T &state) {
template<typename F, typename T> size_t Map(const char *key, F f, T &state) {
auto start = StartMap(key);
f(state);
return EndMap(start);
@@ -1160,9 +1196,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
void Add(const std::string &str) { String(str); }
void Add(const flexbuffers::String &str) { String(str); }
template<typename T> void Add(const std::vector<T> &vec) {
Vector(vec);
}
template<typename T> void Add(const std::vector<T> &vec) { Vector(vec); }
template<typename T> void Add(const char *key, const T &t) {
Key(key);
@@ -1173,9 +1207,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
Map(map);
}
template<typename T> void operator+=(const T &t) {
Add(t);
}
template<typename T> void operator+=(const T &t) { Add(t); }
// This function is useful in combination with the Mutate* functions above.
// It forces elements of vectors and maps to have a minimum size, such that
@@ -1220,8 +1252,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
void WriteBytes(const void *val, size_t size) {
buf_.insert(buf_.end(),
reinterpret_cast<const uint8_t *>(val),
buf_.insert(buf_.end(), reinterpret_cast<const uint8_t *>(val),
reinterpret_cast<const uint8_t *>(val) + size);
}
@@ -1235,15 +1266,15 @@ class Builder FLATBUFFERS_FINAL_CLASS {
switch (byte_width) {
case 8: Write(f, byte_width); break;
case 4: Write(static_cast<float>(f), byte_width); break;
//case 2: Write(static_cast<half>(f), byte_width); break;
//case 1: Write(static_cast<quarter>(f), byte_width); break;
// case 2: Write(static_cast<half>(f), byte_width); break;
// case 1: Write(static_cast<quarter>(f), byte_width); break;
default: assert(0);
}
}
void WriteOffset(uint64_t o, uint8_t byte_width) {
auto reloff = buf_.size() - o;
assert(reloff < 1ULL << (byte_width * 8) || byte_width == 8);
assert(byte_width == 8 || reloff < 1ULL << (byte_width * 8));
Write(reloff, byte_width);
}
@@ -1267,9 +1298,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
template<typename T> static Type GetScalarType() {
assert(flatbuffers::is_scalar<T>::value);
return flatbuffers::is_floating_point<T>::value
? TYPE_FLOAT
: flatbuffers::is_same<T, bool>::value ? TYPE_BOOL
: (flatbuffers::is_unsigned<T>::value ? TYPE_UINT : TYPE_INT);
? TYPE_FLOAT
: flatbuffers::is_same<T, bool>::value
? TYPE_BOOL
: (flatbuffers::is_unsigned<T>::value ? TYPE_UINT
: TYPE_INT);
}
struct Value {
@@ -1286,19 +1319,20 @@ class Builder FLATBUFFERS_FINAL_CLASS {
Value() : i_(0), type_(TYPE_NULL), min_bit_width_(BIT_WIDTH_8) {}
Value(bool b) : u_(static_cast<uint64_t>(b)), type_(TYPE_BOOL), min_bit_width_(BIT_WIDTH_8) {}
Value(bool b)
: u_(static_cast<uint64_t>(b)),
type_(TYPE_BOOL),
min_bit_width_(BIT_WIDTH_8) {}
Value(int64_t i, Type t, BitWidth bw)
: i_(i), type_(t), min_bit_width_(bw) {}
: i_(i), type_(t), min_bit_width_(bw) {}
Value(uint64_t u, Type t, BitWidth bw)
: u_(u), type_(t), min_bit_width_(bw) {}
: u_(u), type_(t), min_bit_width_(bw) {}
Value(float f)
: f_(f), type_(TYPE_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
Value(double f)
: f_(f), type_(TYPE_FLOAT), min_bit_width_(WidthF(f)) {}
Value(float f) : f_(f), type_(TYPE_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
Value(double f) : f_(f), type_(TYPE_FLOAT), min_bit_width_(WidthF(f)) {}
uint8_t StoredPackedType(BitWidth parent_bit_width_= BIT_WIDTH_8) const {
uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
return PackedType(StoredWidth(parent_bit_width_), type_);
}
@@ -1315,15 +1349,15 @@ class Builder FLATBUFFERS_FINAL_CLASS {
byte_width <= sizeof(flatbuffers::largest_scalar_t);
byte_width *= 2) {
// Where are we going to write this offset?
auto offset_loc =
buf_size +
flatbuffers::PaddingBytes(buf_size, byte_width) +
elem_index * byte_width;
auto offset_loc = buf_size +
flatbuffers::PaddingBytes(buf_size, byte_width) +
elem_index * byte_width;
// Compute relative offset.
auto offset = offset_loc - u_;
// Does it fit?
auto bit_width = WidthU(offset);
if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) == byte_width)
if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) ==
byte_width)
return bit_width;
}
assert(false); // Must match one of the sizes above.
@@ -1333,9 +1367,9 @@ class Builder FLATBUFFERS_FINAL_CLASS {
BitWidth StoredWidth(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
if (IsInline(type_)) {
return (std::max)(min_bit_width_, parent_bit_width_);
return (std::max)(min_bit_width_, parent_bit_width_);
} else {
return min_bit_width_;
return min_bit_width_;
}
}
};
@@ -1343,19 +1377,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
void WriteAny(const Value &val, uint8_t byte_width) {
switch (val.type_) {
case TYPE_NULL:
case TYPE_INT:
Write(val.i_, byte_width);
break;
case TYPE_INT: Write(val.i_, byte_width); break;
case TYPE_BOOL:
case TYPE_UINT:
Write(val.u_, byte_width);
break;
case TYPE_FLOAT:
WriteDouble(val.f_, byte_width);
break;
default:
WriteOffset(val.u_, byte_width);
break;
case TYPE_UINT: Write(val.u_, byte_width); break;
case TYPE_FLOAT: WriteDouble(val.f_, byte_width); break;
default: WriteOffset(val.u_, byte_width); break;
}
}
@@ -1369,8 +1395,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
return sloc;
}
template<typename T> size_t ScalarVector(const T *elems, size_t len,
bool fixed) {
template<typename T>
size_t ScalarVector(const T *elems, size_t len, bool fixed) {
auto vector_type = GetScalarType<T>();
auto byte_width = sizeof(T);
auto bit_width = WidthB(byte_width);
@@ -1436,12 +1462,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
buf_.push_back(stack_[i].StoredPackedType(bit_width));
}
}
return Value(static_cast<uint64_t>(vloc), keys
? TYPE_MAP
: (typed
? ToTypedVector(vector_type, fixed ? vec_len : 0)
: TYPE_VECTOR),
bit_width);
return Value(static_cast<uint64_t>(vloc),
keys ? TYPE_MAP
: (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0)
: TYPE_VECTOR),
bit_width);
}
// You shouldn't really be copying instances of this class.
@@ -1473,10 +1498,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
struct StringOffsetCompare {
StringOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
bool operator()(const StringOffset &a, const StringOffset &b) const {
auto stra = reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) +
a.first);
auto strb = reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) +
b.first);
auto stra = reinterpret_cast<const char *>(
flatbuffers::vector_data(*buf_) + a.first);
auto strb = reinterpret_cast<const char *>(
flatbuffers::vector_data(*buf_) + b.first);
return strncmp(stra, strb, (std::min)(a.second, b.second) + 1) < 0;
}
const std::vector<uint8_t> *buf_;
@@ -1491,8 +1516,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
} // namespace flexbuffers
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
# if defined(_MSC_VER)
# pragma warning(pop)
# endif
#endif // FLATBUFFERS_FLEXBUFFERS_H_

View File

@@ -30,13 +30,12 @@ namespace grpc {
// `grpc_slice` and also provides flatbuffers-specific helpers such as `Verify`
// and `GetRoot`. Since it is backed by a `grpc_slice`, the underlying buffer
// is refcounted and ownership is be managed automatically.
template <class T>
class Message {
template<class T> class Message {
public:
Message() : slice_(grpc_empty_slice()) {}
Message(grpc_slice slice, bool add_ref)
: slice_(add_ref ? grpc_slice_ref(slice) : slice) {}
: slice_(add_ref ? grpc_slice_ref(slice) : slice) {}
Message &operator=(const Message &other) = delete;
@@ -105,14 +104,16 @@ class SliceAllocator : public Allocator {
}
virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
size_t new_size) override {
size_t new_size, size_t in_use_back,
size_t in_use_front) override {
assert(old_p == GRPC_SLICE_START_PTR(slice_));
assert(old_size == GRPC_SLICE_LENGTH(slice_));
assert(new_size > old_size);
grpc_slice old_slice = slice_;
grpc_slice new_slice = grpc_slice_malloc(new_size);
uint8_t *new_p = GRPC_SLICE_START_PTR(new_slice);
memcpy(new_p + (new_size - old_size), old_p, old_size);
memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
in_use_front);
slice_ = new_slice;
grpc_slice_unref(old_slice);
return new_p;
@@ -137,7 +138,7 @@ namespace detail {
struct SliceAllocatorMember {
SliceAllocator slice_allocator_;
};
}
} // namespace detail
// MessageBuilder is a gRPC-specific FlatBufferBuilder that uses SliceAllocator
// to allocate gRPC buffers.
@@ -145,7 +146,7 @@ class MessageBuilder : private detail::SliceAllocatorMember,
public FlatBufferBuilder {
public:
explicit MessageBuilder(uoffset_t initial_size = 1024)
: FlatBufferBuilder(initial_size, &slice_allocator_, false) {}
: FlatBufferBuilder(initial_size, &slice_allocator_, false) {}
MessageBuilder(const MessageBuilder &other) = delete;
MessageBuilder &operator=(const MessageBuilder &other) = delete;
@@ -155,9 +156,8 @@ class MessageBuilder : private detail::SliceAllocatorMember,
// GetMessage extracts the subslice of the buffer corresponding to the
// flatbuffers-encoded region and wraps it in a `Message<T>` to handle buffer
// ownership.
template <class T>
Message<T> GetMessage() {
auto buf_data = buf_.buf(); // pointer to memory
template<class T> Message<T> GetMessage() {
auto buf_data = buf_.scratch_data(); // pointer to memory
auto buf_size = buf_.capacity(); // size of memory
auto msg_data = buf_.data(); // pointer to msg
auto msg_size = buf_.size(); // size of msg
@@ -178,8 +178,7 @@ class MessageBuilder : private detail::SliceAllocatorMember,
return msg;
}
template <class T>
Message<T> ReleaseMessage() {
template<class T> Message<T> ReleaseMessage() {
Message<T> msg = GetMessage<T>();
Reset();
return msg;
@@ -194,8 +193,7 @@ class MessageBuilder : private detail::SliceAllocatorMember,
namespace grpc {
template <class T>
class SerializationTraits<flatbuffers::grpc::Message<T>> {
template<class T> class SerializationTraits<flatbuffers::grpc::Message<T>> {
public:
static grpc::Status Serialize(const flatbuffers::grpc::Message<T> &msg,
grpc_byte_buffer **buffer, bool *own_buffer) {
@@ -237,19 +235,19 @@ class SerializationTraits<flatbuffers::grpc::Message<T>> {
*msg = flatbuffers::grpc::Message<T>(slice, false);
}
grpc_byte_buffer_destroy(buffer);
#if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
#if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
return ::grpc::Status::OK;
#else
#else
if (msg->Verify()) {
return ::grpc::Status::OK;
} else {
return ::grpc::Status(::grpc::StatusCode::INTERNAL,
"Message verification failed");
}
#endif
#endif
}
};
} // namespace grpc;
} // namespace grpc
#endif // FLATBUFFERS_GRPC_H_

View File

@@ -24,26 +24,22 @@
namespace flatbuffers {
template <typename T>
struct FnvTraits {
template<typename T> struct FnvTraits {
static const T kFnvPrime;
static const T kOffsetBasis;
};
template <>
struct FnvTraits<uint32_t> {
template<> struct FnvTraits<uint32_t> {
static const uint32_t kFnvPrime = 0x01000193;
static const uint32_t kOffsetBasis = 0x811C9DC5;
};
template <>
struct FnvTraits<uint64_t> {
template<> struct FnvTraits<uint64_t> {
static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
};
template <typename T>
T HashFnv1(const char *input) {
template<typename T> FLATBUFFERS_CONSTEXPR_CPP14 T HashFnv1(const char *input) {
T hash = FnvTraits<T>::kOffsetBasis;
for (const char *c = input; *c; ++c) {
hash *= FnvTraits<T>::kFnvPrime;
@@ -52,8 +48,7 @@ T HashFnv1(const char *input) {
return hash;
}
template <typename T>
T HashFnv1a(const char *input) {
template<typename T> FLATBUFFERS_CONSTEXPR_CPP14 T HashFnv1a(const char *input) {
T hash = FnvTraits<T>::kOffsetBasis;
for (const char *c = input; *c; ++c) {
hash ^= static_cast<unsigned char>(*c);
@@ -62,24 +57,49 @@ T HashFnv1a(const char *input) {
return hash;
}
template <typename T>
struct NamedHashFunction {
template <> FLATBUFFERS_CONSTEXPR_CPP14 inline uint16_t HashFnv1<uint16_t>(const char *input) {
uint32_t hash = HashFnv1<uint32_t>(input);
return (hash >> 16) ^ (hash & 0xffff);
}
template <> FLATBUFFERS_CONSTEXPR_CPP14 inline uint16_t HashFnv1a<uint16_t>(const char *input) {
uint32_t hash = HashFnv1a<uint32_t>(input);
return (hash >> 16) ^ (hash & 0xffff);
}
template <typename T> struct NamedHashFunction {
const char *name;
typedef T (*HashFunction)(const char*);
typedef T (*HashFunction)(const char *);
HashFunction function;
};
const NamedHashFunction<uint16_t> kHashFunctions16[] = {
{ "fnv1_16", HashFnv1<uint16_t> },
{ "fnv1a_16", HashFnv1a<uint16_t> },
};
const NamedHashFunction<uint32_t> kHashFunctions32[] = {
{ "fnv1_32", HashFnv1<uint32_t> },
{ "fnv1_32", HashFnv1<uint32_t> },
{ "fnv1a_32", HashFnv1a<uint32_t> },
};
const NamedHashFunction<uint64_t> kHashFunctions64[] = {
{ "fnv1_64", HashFnv1<uint64_t> },
{ "fnv1_64", HashFnv1<uint64_t> },
{ "fnv1a_64", HashFnv1a<uint64_t> },
};
inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(
const char *name) {
std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
for (std::size_t i = 0; i < size; ++i) {
if (std::strcmp(name, kHashFunctions16[i].name) == 0) {
return kHashFunctions16[i].function;
}
}
return nullptr;
}
inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
const char *name) {
std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);

View File

@@ -18,17 +18,17 @@
#define FLATBUFFERS_IDL_H_
#include <map>
#include <stack>
#include <memory>
#include <stack>
#include "flatbuffers/base.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/flexbuffers.h"
#include "flatbuffers/hash.h"
#include "flatbuffers/reflection.h"
#include "flatbuffers/flexbuffers.h"
#if !defined(FLATBUFFERS_CPP98_STL)
#include <functional>
# include <functional>
#endif // !defined(FLATBUFFERS_CPP98_STL)
// This file defines the data types representing a parsed IDL (Interface
@@ -39,6 +39,7 @@ namespace flatbuffers {
// The order of these matters for Is*() functions below.
// Additionally, Parser::ParseType assumes bool..string is a contiguous range
// of type tokens.
// clang-format off
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
TD(NONE, "", uint8_t, byte, byte, byte, uint8) \
TD(UTYPE, "", uint8_t, byte, byte, byte, uint8) /* begin scalar/int */ \
@@ -110,13 +111,12 @@ inline bool IsFloat (BaseType t) { return t == BASE_TYPE_FLOAT ||
inline bool IsLong (BaseType t) { return t == BASE_TYPE_LONG ||
t == BASE_TYPE_ULONG; }
inline bool IsBool (BaseType t) { return t == BASE_TYPE_BOOL; }
// clang-format on
extern const char *const kTypeNames[];
extern const char kTypeSizes[];
inline size_t SizeOf(BaseType t) {
return kTypeSizes[t];
}
inline size_t SizeOf(BaseType t) { return kTypeSizes[t]; }
struct StructDef;
struct EnumDef;
@@ -125,13 +125,12 @@ class Parser;
// 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, EnumDef *_ed = nullptr)
: base_type(_base_type),
element(BASE_TYPE_NONE),
struct_def(_sd),
enum_def(_ed)
{}
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(_ed) {}
bool operator==(const Type &o) {
return base_type == o.base_type && element == o.element &&
@@ -151,8 +150,9 @@ struct Type {
// Represents a parsed scalar value, it's type, and field offset.
struct Value {
Value() : constant("0"), offset(static_cast<voffset_t>(
~(static_cast<voffset_t>(0U)))) {}
Value()
: constant("0"),
offset(static_cast<voffset_t>(~(static_cast<voffset_t>(0U)))) {}
Type type;
std::string constant;
voffset_t offset;
@@ -163,9 +163,7 @@ struct Value {
template<typename T> class SymbolTable {
public:
~SymbolTable() {
for (auto it = vec.begin(); it != vec.end(); ++it) {
delete *it;
}
for (auto it = vec.begin(); it != vec.end(); ++it) { delete *it; }
}
bool Add(const std::string &name, T *e) {
@@ -193,15 +191,14 @@ template<typename T> class SymbolTable {
}
public:
std::map<std::string, T *> dict; // quick lookup
std::vector<T *> vec; // Used to iterate in order of insertion
std::map<std::string, T *> dict; // quick lookup
std::vector<T *> vec; // Used to iterate in order of insertion
};
// A name space, as set in the schema.
struct Namespace {
Namespace() : from_table(0) {}
// Given a (potentally unqualified) name, return the "fully qualified" name
// which has a full namespaced descriptor.
// With max_components you can request less than the number of components
@@ -215,13 +212,16 @@ struct Namespace {
// Base class for all definition types (fields, structs_, enums_).
struct Definition {
Definition() : generated(false), defined_namespace(nullptr),
serialized_location(0), index(-1), refcount(1) {}
Definition()
: generated(false),
defined_namespace(nullptr),
serialized_location(0),
index(-1),
refcount(1) {}
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<
reflection::KeyValue>>>
SerializeAttributes(FlatBufferBuilder *builder,
const Parser &parser) const;
flatbuffers::Offset<
flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
SerializeAttributes(FlatBufferBuilder *builder, const Parser &parser) const;
std::string name;
std::string file;
@@ -237,34 +237,38 @@ struct Definition {
};
struct FieldDef : public Definition {
FieldDef() : deprecated(false), required(false), key(false),
native_inline(false), flexbuffer(false), nested_flatbuffer(NULL),
padding(0) {}
FieldDef()
: deprecated(false),
required(false),
key(false),
native_inline(false),
flexbuffer(false),
nested_flatbuffer(NULL),
padding(0) {}
Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
const Parser &parser) const;
Value value;
bool deprecated; // Field is allowed to be present in old data, but can't be.
// written in new data nor accessed in new code.
bool required; // Field must always be present.
bool key; // Field functions as a key for creating sorted vectors.
bool deprecated; // Field is allowed to be present in old data, but can't be.
// written in new data nor accessed in new code.
bool required; // Field must always be present.
bool key; // Field functions as a key for creating sorted vectors.
bool native_inline; // Field will be defined inline (instead of as a pointer)
// for native tables if field is a struct.
bool flexbuffer; // This field contains FlexBuffer data.
StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
size_t padding; // Bytes to always pad after this field.
bool flexbuffer; // This field contains FlexBuffer data.
StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
size_t padding; // Bytes to always pad after this field.
};
struct StructDef : public Definition {
StructDef()
: fixed(false),
predecl(true),
sortbysize(true),
has_key(false),
minalign(1),
bytesize(0)
{}
: fixed(false),
predecl(true),
sortbysize(true),
has_key(false),
minalign(1),
bytesize(0) {}
void PadLastField(size_t min_align) {
auto padding = PaddingBytes(bytesize, min_align);
@@ -300,8 +304,7 @@ inline size_t InlineAlignment(const Type &type) {
}
struct EnumVal {
EnumVal(const std::string &_name, int64_t _val)
: name(_name), value(_val) {}
EnumVal(const std::string &_name, int64_t _val) : name(_name), value(_val) {}
Offset<reflection::EnumVal> Serialize(FlatBufferBuilder *builder) const;
@@ -314,13 +317,11 @@ struct EnumVal {
struct EnumDef : public Definition {
EnumDef() : is_union(false), uses_type_aliases(false) {}
EnumVal *ReverseLookup(int enum_idx, bool skip_union_default = true) {
for (auto it = vals.vec.begin() + static_cast<int>(is_union &&
skip_union_default);
it != vals.vec.end(); ++it) {
if ((*it)->value == enum_idx) {
return *it;
}
EnumVal *ReverseLookup(int64_t enum_idx, bool skip_union_default = true) {
for (auto it = vals.vec.begin() +
static_cast<int>(is_union && skip_union_default);
it != vals.vec.end(); ++it) {
if ((*it)->value == enum_idx) { return *it; }
}
return nullptr;
}
@@ -338,14 +339,14 @@ inline bool EqualByName(const Type &a, const Type &b) {
return a.base_type == b.base_type && a.element == b.element &&
(a.struct_def == b.struct_def ||
a.struct_def->name == b.struct_def->name) &&
(a.enum_def == b.enum_def ||
a.enum_def->name == b.enum_def->name);
(a.enum_def == b.enum_def || a.enum_def->name == b.enum_def->name);
}
struct RPCCall {
std::string name;
SymbolTable<Value> attributes;
StructDef *request, *response;
std::vector<std::string> rpc_comment;
};
struct ServiceDef : public Definition {
@@ -366,6 +367,7 @@ struct IDLOptions {
bool mutable_buffer;
bool one_file;
bool proto_mode;
bool proto_oneof_union;
bool generate_all;
bool skip_unexpected_fields_in_json;
bool generate_name_strings;
@@ -385,19 +387,20 @@ struct IDLOptions {
std::string go_namespace;
bool reexport_ts_modules;
bool protobuf_ascii_alike;
bool size_prefixed;
// Possible options for the more general generator below.
enum Language {
kJava = 1 << 0,
kJava = 1 << 0,
kCSharp = 1 << 1,
kGo = 1 << 2,
kCpp = 1 << 3,
kJs = 1 << 4,
kGo = 1 << 2,
kCpp = 1 << 3,
kJs = 1 << 4,
kPython = 1 << 5,
kPhp = 1 << 6,
kJson = 1 << 7,
kPhp = 1 << 6,
kJson = 1 << 7,
kBinary = 1 << 8,
kTs = 1 << 9,
kTs = 1 << 9,
kJsonSchema = 1 << 10,
kMAX
};
@@ -413,33 +416,37 @@ struct IDLOptions {
unsigned long lang_to_generate;
IDLOptions()
: strict_json(false),
skip_js_exports(false),
use_goog_js_export_format(false),
output_default_scalars_in_json(false),
indent_step(2),
output_enum_identifiers(true), prefixed_enums(true), scoped_enums(false),
include_dependence_headers(true),
mutable_buffer(false),
one_file(false),
proto_mode(false),
generate_all(false),
skip_unexpected_fields_in_json(false),
generate_name_strings(false),
generate_object_based_api(false),
cpp_object_api_pointer_type("std::unique_ptr"),
gen_nullable(false),
object_suffix("T"),
union_value_namespacing(true),
allow_non_utf8(false),
keep_include_path(false),
binary_schema_comments(false),
skip_flatbuffers_import(false),
reexport_ts_modules(true),
protobuf_ascii_alike(false),
lang(IDLOptions::kJava),
mini_reflect(IDLOptions::kNone),
lang_to_generate(0) {}
: strict_json(false),
skip_js_exports(false),
use_goog_js_export_format(false),
output_default_scalars_in_json(false),
indent_step(2),
output_enum_identifiers(true),
prefixed_enums(true),
scoped_enums(false),
include_dependence_headers(true),
mutable_buffer(false),
one_file(false),
proto_mode(false),
proto_oneof_union(false),
generate_all(false),
skip_unexpected_fields_in_json(false),
generate_name_strings(false),
generate_object_based_api(false),
cpp_object_api_pointer_type("std::unique_ptr"),
gen_nullable(false),
object_suffix("T"),
union_value_namespacing(true),
allow_non_utf8(false),
keep_include_path(false),
binary_schema_comments(false),
skip_flatbuffers_import(false),
reexport_ts_modules(true),
protobuf_ascii_alike(false),
size_prefixed(false),
lang(IDLOptions::kJava),
mini_reflect(IDLOptions::kNone),
lang_to_generate(0) {}
};
// This encapsulates where the parser is in the current source file.
@@ -465,7 +472,7 @@ struct ParserState {
class CheckedError {
public:
explicit CheckedError(bool error)
: is_error_(error), has_been_checked_(false) {}
: is_error_(error), has_been_checked_(false) {}
CheckedError &operator=(const CheckedError &other) {
is_error_ = other.is_error_;
@@ -480,7 +487,10 @@ class CheckedError {
~CheckedError() { assert(has_been_checked_); }
bool Check() { has_been_checked_ = true; return is_error_; }
bool Check() {
has_been_checked_ = true;
return is_error_;
}
private:
bool is_error_;
@@ -489,23 +499,25 @@ class CheckedError {
// Additionally, in GCC we can get these errors statically, for additional
// assurance:
// clang-format off
#ifdef __GNUC__
#define FLATBUFFERS_CHECKED_ERROR CheckedError \
__attribute__((warn_unused_result))
#else
#define FLATBUFFERS_CHECKED_ERROR CheckedError
#endif
// clang-format on
class Parser : public ParserState {
public:
explicit Parser(const IDLOptions &options = IDLOptions())
: current_namespace_(nullptr),
empty_namespace_(nullptr),
root_struct_def_(nullptr),
opts(options),
uses_flexbuffers_(false),
source_(nullptr),
anonymous_counter(0) {
: current_namespace_(nullptr),
empty_namespace_(nullptr),
root_struct_def_(nullptr),
opts(options),
uses_flexbuffers_(false),
source_(nullptr),
anonymous_counter(0) {
// Start out with the empty namespace being current.
empty_namespace_ = new Namespace();
namespaces_.push_back(empty_namespace_);
@@ -524,8 +536,10 @@ class Parser : public ParserState {
known_attributes_["idempotent"] = true;
known_attributes_["cpp_type"] = true;
known_attributes_["cpp_ptr_type"] = true;
known_attributes_["cpp_ptr_type_get"] = true;
known_attributes_["cpp_str_type"] = true;
known_attributes_["native_inline"] = true;
known_attributes_["native_custom_alloc"] = true;
known_attributes_["native_type"] = true;
known_attributes_["native_default"] = true;
known_attributes_["flexbuffer"] = true;
@@ -579,17 +593,17 @@ class Parser : public ParserState {
StructDef *LookupStruct(const std::string &id) const;
private:
private:
void Message(const std::string &msg);
void Warning(const std::string &msg);
FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t *val);
FLATBUFFERS_CHECKED_ERROR Next();
FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark();
bool Is(int t);
bool IsIdent(const char *id);
bool Is(int t) const;
bool IsIdent(const char *id) const;
FLATBUFFERS_CHECKED_ERROR Expect(int t);
std::string TokenToStringId(int t);
std::string TokenToStringId(int t) const;
EnumDef *LookupEnum(const std::string &id);
FLATBUFFERS_CHECKED_ERROR ParseNamespacing(std::string *id,
std::string *last);
@@ -604,6 +618,7 @@ private:
FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field,
size_t parent_fieldn,
const StructDef *parent_struct_def);
// clang-format off
#if defined(FLATBUFFERS_CPP98_STL)
typedef CheckedError (*ParseTableDelimitersBody)(
const std::string &name, size_t &fieldn, const StructDef *struct_def,
@@ -613,6 +628,7 @@ private:
const StructDef*, void*)>
ParseTableDelimitersBody;
#endif // defined(FLATBUFFERS_CPP98_STL)
// clang-format on
FLATBUFFERS_CHECKED_ERROR ParseTableDelimiters(size_t &fieldn,
const StructDef *struct_def,
ParseTableDelimitersBody body,
@@ -620,7 +636,7 @@ private:
FLATBUFFERS_CHECKED_ERROR ParseTable(const StructDef &struct_def,
std::string *value, uoffset_t *ovalue);
void SerializeStruct(const StructDef &struct_def, const Value &val);
void AddVector(bool sortbysize, int count);
// clang-format off
#if defined(FLATBUFFERS_CPP98_STL)
typedef CheckedError (*ParseVectorDelimitersBody)(size_t &count,
void *state);
@@ -628,6 +644,7 @@ private:
typedef std::function<CheckedError(size_t&, void*)>
ParseVectorDelimitersBody;
#endif // defined(FLATBUFFERS_CPP98_STL)
// clang-format on
FLATBUFFERS_CHECKED_ERROR ParseVectorDelimiters(
size_t &count, ParseVectorDelimitersBody body, void *state);
FLATBUFFERS_CHECKED_ERROR ParseVector(const Type &type, uoffset_t *ovalue);
@@ -635,11 +652,11 @@ private:
size_t fieldn,
const StructDef *parent_struct_def);
FLATBUFFERS_CHECKED_ERROR ParseMetaData(SymbolTable<Value> *attributes);
FLATBUFFERS_CHECKED_ERROR TryTypedValue(int dtoken, bool check, Value &e,
FLATBUFFERS_CHECKED_ERROR TryTypedValue(const std::string *name, int dtoken, bool check, Value &e,
BaseType req, bool *destmatch);
FLATBUFFERS_CHECKED_ERROR ParseHash(Value &e, FieldDef* field);
FLATBUFFERS_CHECKED_ERROR TokenError();
FLATBUFFERS_CHECKED_ERROR ParseSingleValue(Value &e);
FLATBUFFERS_CHECKED_ERROR ParseSingleValue(const std::string *name, Value &e);
FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(Type &type, int64_t *result);
StructDef *LookupCreateStruct(const std::string &name,
bool create_if_new = true,
@@ -648,6 +665,9 @@ private:
FLATBUFFERS_CHECKED_ERROR ParseNamespace();
FLATBUFFERS_CHECKED_ERROR StartStruct(const std::string &name,
StructDef **dest);
FLATBUFFERS_CHECKED_ERROR StartEnum(const std::string &name,
bool is_union,
EnumDef **dest);
FLATBUFFERS_CHECKED_ERROR ParseDecl();
FLATBUFFERS_CHECKED_ERROR ParseService();
FLATBUFFERS_CHECKED_ERROR ParseProtoFields(StructDef *struct_def,
@@ -738,15 +758,12 @@ extern bool GenerateBinary(const Parser &parser,
// Generate a C++ header from the definitions in the Parser object.
// See idl_gen_cpp.
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);
// Generate JavaScript or TypeScript code from the definitions in the Parser object.
// See idl_gen_js.
extern std::string GenerateJS(const Parser &parser);
extern bool GenerateJS(const Parser &parser,
const std::string &path,
const std::string &file_name);
@@ -757,12 +774,6 @@ extern bool GenerateGo(const Parser &parser,
const std::string &path,
const std::string &file_name);
// Generate Java files from the definitions in the Parser object.
// See idl_gen_java.cpp.
extern bool GenerateJava(const Parser &parser,
const std::string &path,
const std::string &file_name);
// Generate Php code from the definitions in the Parser object.
// See idl_gen_php.
extern bool GeneratePhp(const Parser &parser,
@@ -781,12 +792,6 @@ extern bool GenerateJsonSchema(const Parser &parser,
const std::string &path,
const std::string &file_name);
// Generate C# files from the definitions in the Parser object.
// See idl_gen_csharp.cpp.
extern bool GenerateCSharp(const Parser &parser,
const std::string &path,
const std::string &file_name);
// Generate Java/C#/.. files from the definitions in the Parser object.
// See idl_gen_general.cpp.
extern bool GenerateGeneral(const Parser &parser,
@@ -843,6 +848,12 @@ bool GenerateGoGRPC(const Parser &parser,
const std::string &path,
const std::string &file_name);
// Generate GRPC Java classes.
// See idl_gen_grpc.cpp
bool GenerateJavaGRPC(const Parser &parser,
const std::string &path,
const std::string &file_name);
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_H_

View File

@@ -65,8 +65,7 @@ struct IterationVisitor {
virtual void EndVector() {}
virtual void Element(size_t /*i*/, ElementaryType /*type*/,
const TypeTable * /*type_table*/,
const uint8_t * /*val*/)
{}
const uint8_t * /*val*/) {}
virtual ~IterationVisitor() {}
};
@@ -75,34 +74,24 @@ inline size_t InlineSize(ElementaryType type, const TypeTable *type_table) {
case ET_UTYPE:
case ET_BOOL:
case ET_CHAR:
case ET_UCHAR:
return 1;
case ET_UCHAR: return 1;
case ET_SHORT:
case ET_USHORT:
return 2;
case ET_USHORT: return 2;
case ET_INT:
case ET_UINT:
case ET_FLOAT:
case ET_STRING:
return 4;
case ET_STRING: return 4;
case ET_LONG:
case ET_ULONG:
case ET_DOUBLE:
return 8;
case ET_DOUBLE: return 8;
case ET_SEQUENCE:
switch (type_table->st) {
case ST_TABLE:
case ST_UNION:
return 4;
case ST_STRUCT:
return type_table->values[type_table->num_elems];
default:
assert(false);
return 1;
case ST_UNION: return 4;
case ST_STRUCT: return type_table->values[type_table->num_elems];
default: assert(false); return 1;
}
default:
assert(false);
return 1;
default: assert(false); return 1;
}
}
@@ -118,7 +107,7 @@ inline int32_t LookupEnum(int32_t enum_val, const int32_t *values,
template<typename T> const char *EnumName(T tval, const TypeTable *type_table) {
if (!type_table || !type_table->names) return nullptr;
auto i = LookupEnum(static_cast<int32_t>(tval), type_table->values,
type_table->num_elems);
type_table->num_elems);
if (i >= 0 && i < static_cast<int32_t>(type_table->num_elems)) {
return type_table->names[i];
}
@@ -126,13 +115,11 @@ template<typename T> const char *EnumName(T tval, const TypeTable *type_table) {
}
void IterateObject(const uint8_t *obj, const TypeTable *type_table,
IterationVisitor *visitor);
IterationVisitor *visitor);
inline void IterateValue(ElementaryType type, const uint8_t *val,
const TypeTable *type_table,
const uint8_t *prev_val,
soffset_t vector_index,
IterationVisitor *visitor) {
const TypeTable *type_table, const uint8_t *prev_val,
soffset_t vector_index, IterationVisitor *visitor) {
switch (type) {
case ET_UTYPE: {
auto tval = *reinterpret_cast<const uint8_t *>(val);
@@ -200,9 +187,7 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
val += ReadScalar<uoffset_t>(val);
IterateObject(val, type_table, visitor);
break;
case ST_STRUCT:
IterateObject(val, type_table, visitor);
break;
case ST_STRUCT: IterateObject(val, type_table, visitor); break;
case ST_UNION: {
val += ReadScalar<uoffset_t>(val);
assert(prev_val);
@@ -211,10 +196,10 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
auto type_vec = reinterpret_cast<const Vector<uint8_t> *>(prev_val);
union_type = type_vec->Get(static_cast<uoffset_t>(vector_index));
}
auto type_code_idx = LookupEnum(union_type, type_table->values,
type_table->num_elems);
if (type_code_idx >= 0 && type_code_idx <
static_cast<int32_t>(type_table->num_elems)) {
auto type_code_idx =
LookupEnum(union_type, type_table->values, type_table->num_elems);
if (type_code_idx >= 0 &&
type_code_idx < static_cast<int32_t>(type_table->num_elems)) {
auto type_code = type_table->type_codes[type_code_idx];
switch (type_code.base_type) {
case ET_SEQUENCE: {
@@ -225,17 +210,14 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
case ET_STRING:
visitor->String(reinterpret_cast<const String *>(val));
break;
default:
visitor->Unknown(val);
default: visitor->Unknown(val);
}
} else {
visitor->Unknown(val);
}
break;
}
case ST_ENUM:
assert(false);
break;
case ST_ENUM: assert(false); break;
}
break;
}
@@ -257,14 +239,12 @@ inline void IterateObject(const uint8_t *obj, const TypeTable *type_table,
auto is_vector = type_code.is_vector != 0;
auto ref_idx = type_code.sequence_ref;
const TypeTable *ref = nullptr;
if (ref_idx >= 0) {
ref = type_table->type_refs[ref_idx]();
}
if (ref_idx >= 0) { ref = type_table->type_refs[ref_idx](); }
auto name = type_table->names ? type_table->names[i] : nullptr;
const uint8_t *val = nullptr;
if (type_table->st == ST_TABLE) {
val = reinterpret_cast<const Table *>(obj)->GetAddressOf(
FieldIndexToOffset(static_cast<voffset_t>(i)));
FieldIndexToOffset(static_cast<voffset_t>(i)));
} else {
val = obj + type_table->values[i];
}
@@ -310,24 +290,29 @@ struct ToStringVisitor : public IterationVisitor {
const char *name, const uint8_t *val) {
if (!val) return;
if (set_idx) s += ", ";
if (name) { s += name; s += ": "; }
if (name) {
s += name;
s += ": ";
}
}
template<typename T> void Named(T x, const char *name) {
if (name) s+= name;
else s+= NumToString(x);
if (name)
s += name;
else
s += NumToString(x);
}
void UType(uint8_t x, const char *name) { Named(x, name); }
void Bool(bool x) { s+= x ? "true" : "false"; }
void Bool(bool x) { s += x ? "true" : "false"; }
void Char(int8_t x, const char *name) { Named(x, name); }
void UChar(uint8_t x, const char *name) { Named(x, name); }
void Short(int16_t x, const char *name) { Named(x, name); }
void UShort(uint16_t x, const char *name) { Named(x, name); }
void Int(int32_t x, const char *name) { Named(x, name); }
void UInt(uint32_t x, const char *name) { Named(x, name); }
void Long(int64_t x) { s+= NumToString(x); }
void ULong(uint64_t x) { s+= NumToString(x); }
void Float(float x) { s+= NumToString(x); }
void Double(double x) { s+= NumToString(x); }
void Long(int64_t x) { s += NumToString(x); }
void ULong(uint64_t x) { s += NumToString(x); }
void Float(float x) { s += NumToString(x); }
void Double(double x) { s += NumToString(x); }
void String(const struct String *str) {
EscapeString(str->c_str(), str->size(), &s, true);
}

View File

@@ -30,14 +30,18 @@ namespace flatbuffers {
// ------------------------- GETTERS -------------------------
inline bool IsScalar (reflection::BaseType t) { return t >= reflection::UType &&
t <= reflection::Double; }
inline bool IsInteger(reflection::BaseType t) { return t >= reflection::UType &&
t <= reflection::ULong; }
inline bool IsFloat (reflection::BaseType t) { return t == reflection::Float ||
t == reflection::Double; }
inline bool IsLong (reflection::BaseType t) { return t == reflection::Long ||
t == reflection::ULong; }
inline bool IsScalar(reflection::BaseType t) {
return t >= reflection::UType && t <= reflection::Double;
}
inline bool IsInteger(reflection::BaseType t) {
return t >= reflection::UType && t <= reflection::ULong;
}
inline bool IsFloat(reflection::BaseType t) {
return t == reflection::Float || t == reflection::Double;
}
inline bool IsLong(reflection::BaseType t) {
return t == reflection::Long || t == reflection::ULong;
}
// Size of a basic type, don't use with structs.
inline size_t GetTypeSize(reflection::BaseType base_type) {
@@ -48,8 +52,7 @@ inline size_t GetTypeSize(reflection::BaseType base_type) {
// Same as above, but now correctly returns the size of a struct if
// the field (or vector element) is a struct.
inline size_t GetTypeSizeInline(reflection::BaseType base_type,
int type_index,
inline size_t GetTypeSizeInline(reflection::BaseType base_type, int type_index,
const reflection::Schema &schema) {
if (base_type == reflection::Obj &&
schema.objects()->Get(type_index)->is_struct()) {
@@ -80,16 +83,16 @@ template<typename T> T GetFieldDefaultF(const reflection::Field &field) {
}
// Get a field, if you know it's an integer, and its exact type.
template<typename T> T GetFieldI(const Table &table,
const reflection::Field &field) {
template<typename T>
T GetFieldI(const Table &table, const reflection::Field &field) {
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
return table.GetField<T>(field.offset(),
static_cast<T>(field.default_integer()));
}
// Get a field, if you know it's floating point and its exact type.
template<typename T> T GetFieldF(const Table &table,
const reflection::Field &field) {
template<typename T>
T GetFieldF(const Table &table, const reflection::Field &field) {
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
return table.GetField<T>(field.offset(),
static_cast<T>(field.default_real()));
@@ -103,8 +106,8 @@ inline const String *GetFieldS(const Table &table,
}
// Get a field, if you know it's a vector.
template<typename T> Vector<T> *GetFieldV(const Table &table,
const reflection::Field &field) {
template<typename T>
Vector<T> *GetFieldV(const Table &table, const reflection::Field &field) {
assert(field.type()->base_type() == reflection::Vector &&
sizeof(T) == GetTypeSize(field.type()->element()));
return table.GetPointer<Vector<T> *>(field.offset());
@@ -119,8 +122,7 @@ inline VectorOfAny *GetFieldAnyV(const Table &table,
}
// Get a field, if you know it's a table.
inline Table *GetFieldT(const Table &table,
const reflection::Field &field) {
inline Table *GetFieldT(const Table &table, const reflection::Field &field) {
assert(field.type()->base_type() == reflection::Obj ||
field.type()->base_type() == reflection::Union);
return table.GetPointer<Table *>(field.offset());
@@ -153,8 +155,7 @@ double GetAnyValueF(reflection::BaseType type, const uint8_t *data);
// All scalars converted using stringstream, strings as-is, and all other
// data types provide some level of debug-pretty-printing.
std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
const reflection::Schema *schema,
int type_index);
const reflection::Schema *schema, int type_index);
// Get any table field as a 64bit int, regardless of what type it is.
inline int64_t GetAnyFieldI(const Table &table,
@@ -165,14 +166,12 @@ inline int64_t GetAnyFieldI(const Table &table,
}
// Get any table field as a double, regardless of what type it is.
inline double GetAnyFieldF(const Table &table,
const reflection::Field &field) {
inline double GetAnyFieldF(const Table &table, const reflection::Field &field) {
auto field_ptr = table.GetAddressOf(field.offset());
return field_ptr ? GetAnyValueF(field.type()->base_type(), field_ptr)
: field.default_real();
}
// Get any table field as a string, regardless of what type it is.
// You may pass nullptr for the schema if you don't care to have fields that
// are of table type pretty-printed.
@@ -186,15 +185,13 @@ inline std::string GetAnyFieldS(const Table &table,
}
// Get any struct field as a 64bit int, regardless of what type it is.
inline int64_t GetAnyFieldI(const Struct &st,
const reflection::Field &field) {
inline int64_t GetAnyFieldI(const Struct &st, const reflection::Field &field) {
return GetAnyValueI(field.type()->base_type(),
st.GetAddressOf(field.offset()));
}
// Get any struct field as a double, regardless of what type it is.
inline double GetAnyFieldF(const Struct &st,
const reflection::Field &field) {
inline double GetAnyFieldF(const Struct &st, const reflection::Field &field) {
return GetAnyValueF(field.type()->base_type(),
st.GetAddressOf(field.offset()));
}
@@ -228,8 +225,8 @@ inline std::string GetAnyVectorElemS(const VectorOfAny *vec,
// Get a vector element that's a table/string/vector from a generic vector.
// Pass Table/String/VectorOfAny as template parameter.
// Warning: does no typechecking.
template<typename T> T *GetAnyVectorElemPointer(const VectorOfAny *vec,
size_t i) {
template<typename T>
T *GetAnyVectorElemPointer(const VectorOfAny *vec, size_t i) {
auto elem_ptr = vec->Data() + sizeof(uoffset_t) * i;
return (T *)(elem_ptr + ReadScalar<uoffset_t>(elem_ptr));
}
@@ -239,34 +236,32 @@ template<typename T> T *GetAnyVectorElemPointer(const VectorOfAny *vec,
// Get elem_size from GetTypeSizeInline().
// Note: little-endian data on all platforms, use EndianScalar() instead of
// raw pointer access with scalars).
template<typename T> T *GetAnyVectorElemAddressOf(const VectorOfAny *vec,
size_t i,
size_t elem_size) {
template<typename T>
T *GetAnyVectorElemAddressOf(const VectorOfAny *vec, size_t i,
size_t elem_size) {
// C-cast to allow const conversion.
return (T *)(vec->Data() + elem_size * i);
}
// Similarly, for elements of tables.
template<typename T> T *GetAnyFieldAddressOf(const Table &table,
const reflection::Field &field) {
template<typename T>
T *GetAnyFieldAddressOf(const Table &table, const reflection::Field &field) {
return (T *)table.GetAddressOf(field.offset());
}
// Similarly, for elements of structs.
template<typename T> T *GetAnyFieldAddressOf(const Struct &st,
const reflection::Field &field) {
template<typename T>
T *GetAnyFieldAddressOf(const Struct &st, const reflection::Field &field) {
return (T *)st.GetAddressOf(field.offset());
}
// ------------------------- SETTERS -------------------------
// Set any scalar field, if you know its exact type.
template<typename T> bool SetField(Table *table, const reflection::Field &field,
T val) {
template<typename T>
bool SetField(Table *table, const reflection::Field &field, T val) {
reflection::BaseType type = field.type()->base_type();
if (!IsScalar(type)) {
return false;
}
if (!IsScalar(type)) { return false; }
assert(sizeof(T) == GetTypeSize(type));
T def;
if (IsInteger(type)) {
@@ -306,7 +301,7 @@ inline bool SetAnyFieldF(Table *table, const reflection::Field &field,
// Set any table field as a string, regardless of what type it is.
inline bool SetAnyFieldS(Table *table, const reflection::Field &field,
const char *val) {
const char *val) {
auto field_ptr = table->GetAddressOf(field.offset());
if (!field_ptr) return false;
SetAnyValueS(field.type()->base_type(), field_ptr, val);
@@ -352,7 +347,6 @@ inline void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type,
SetAnyValueS(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
}
// ------------------------- RESIZING SETTERS -------------------------
// "smart" pointer for use with resizing vectors: turns a pointer inside
@@ -360,27 +354,25 @@ inline void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type,
template<typename T, typename U> class pointer_inside_vector {
public:
pointer_inside_vector(T *ptr, std::vector<U> &vec)
: offset_(reinterpret_cast<uint8_t *>(ptr) -
reinterpret_cast<uint8_t *>(flatbuffers::vector_data(vec))),
vec_(vec) {}
: offset_(reinterpret_cast<uint8_t *>(ptr) -
reinterpret_cast<uint8_t *>(flatbuffers::vector_data(vec))),
vec_(vec) {}
T *operator*() const {
return reinterpret_cast<T *>(
reinterpret_cast<uint8_t *>(
flatbuffers::vector_data(vec_)) + offset_);
}
T *operator->() const {
return operator*();
reinterpret_cast<uint8_t *>(flatbuffers::vector_data(vec_)) + offset_);
}
T *operator->() const { return operator*(); }
void operator=(const pointer_inside_vector &piv);
private:
size_t offset_;
std::vector<U> &vec_;
};
// Helper to create the above easily without specifying template args.
template<typename T, typename U> pointer_inside_vector<T, U> piv(T *ptr,
std::vector<U> &vec) {
template<typename T, typename U>
pointer_inside_vector<T, U> piv(T *ptr, std::vector<U> &vec) {
return pointer_inside_vector<T, U>(ptr, vec);
}
@@ -393,7 +385,7 @@ inline const reflection::Object &GetUnionType(
auto enumdef = schema.enums()->Get(unionfield.type()->index());
// TODO: this is clumsy and slow, but no other way to find it?
auto type_field = parent.fields()->LookupByKey(
(unionfield.name()->str() + UnionTypeFieldSuffix()).c_str());
(unionfield.name()->str() + UnionTypeFieldSuffix()).c_str());
assert(type_field);
auto union_type = GetFieldI<uint8_t>(table, *type_field);
auto enumval = enumdef->values()->LookupByKey(union_type);
@@ -419,16 +411,14 @@ uint8_t *ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize,
uoffset_t elem_size, std::vector<uint8_t> *flatbuf,
const reflection::Object *root_table = nullptr);
template <typename T>
template<typename T>
void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val,
const Vector<T> *vec, std::vector<uint8_t> *flatbuf,
const reflection::Object *root_table = nullptr) {
auto delta_elem = static_cast<int>(newsize) - static_cast<int>(vec->size());
auto newelems = ResizeAnyVector(schema, newsize,
reinterpret_cast<const VectorOfAny *>(vec),
vec->size(),
static_cast<uoffset_t>(sizeof(T)), flatbuf,
root_table);
auto newelems = ResizeAnyVector(
schema, newsize, reinterpret_cast<const VectorOfAny *>(vec), vec->size(),
static_cast<uoffset_t>(sizeof(T)), flatbuf, root_table);
// Set new elements to "val".
for (int i = 0; i < delta_elem; i++) {
auto loc = newelems + i * sizeof(T);
@@ -479,10 +469,8 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
// root should point to the root type for this flatbuffer.
// buf should point to the start of flatbuffer data.
// length specifies the size of the flatbuffer data.
bool Verify(const reflection::Schema &schema,
const reflection::Object &root,
const uint8_t *buf,
size_t length);
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
const uint8_t *buf, size_t length);
} // namespace flatbuffers

View File

@@ -42,8 +42,8 @@ enum BaseType {
Union = 16
};
inline BaseType (&EnumValuesBaseType())[17] {
static BaseType values[] = {
inline const BaseType (&EnumValuesBaseType())[17] {
static const BaseType values[] = {
None,
UType,
Bool,
@@ -65,8 +65,8 @@ inline BaseType (&EnumValuesBaseType())[17] {
return values;
}
inline const char **EnumNamesBaseType() {
static const char *names[] = {
inline const char * const *EnumNamesBaseType() {
static const char * const names[] = {
"None",
"UType",
"Bool",
@@ -130,7 +130,7 @@ struct TypeBuilder {
void add_index(int32_t index) {
fbb_.AddElement<int32_t>(Type::VT_INDEX, index, -1);
}
TypeBuilder(flatbuffers::FlatBufferBuilder &_fbb)
explicit TypeBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
@@ -190,7 +190,7 @@ struct KeyValueBuilder {
void add_value(flatbuffers::Offset<flatbuffers::String> value) {
fbb_.AddOffset(KeyValue::VT_VALUE, value);
}
KeyValueBuilder(flatbuffers::FlatBufferBuilder &_fbb)
explicit KeyValueBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
@@ -283,7 +283,7 @@ struct EnumValBuilder {
void add_union_type(flatbuffers::Offset<Type> union_type) {
fbb_.AddOffset(EnumVal::VT_UNION_TYPE, union_type);
}
EnumValBuilder(flatbuffers::FlatBufferBuilder &_fbb)
explicit EnumValBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
@@ -398,7 +398,7 @@ struct EnumBuilder {
void add_documentation(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> documentation) {
fbb_.AddOffset(Enum::VT_DOCUMENTATION, documentation);
}
EnumBuilder(flatbuffers::FlatBufferBuilder &_fbb)
explicit EnumBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
@@ -561,7 +561,7 @@ struct FieldBuilder {
void add_documentation(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> documentation) {
fbb_.AddOffset(Field::VT_DOCUMENTATION, documentation);
}
FieldBuilder(flatbuffers::FlatBufferBuilder &_fbb)
explicit FieldBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
@@ -712,7 +712,7 @@ struct ObjectBuilder {
void add_documentation(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> documentation) {
fbb_.AddOffset(Object::VT_DOCUMENTATION, documentation);
}
ObjectBuilder(flatbuffers::FlatBufferBuilder &_fbb)
explicit ObjectBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
@@ -825,7 +825,7 @@ struct SchemaBuilder {
void add_root_table(flatbuffers::Offset<Object> root_table) {
fbb_.AddOffset(Schema::VT_ROOT_TABLE, root_table);
}
SchemaBuilder(flatbuffers::FlatBufferBuilder &_fbb)
explicit SchemaBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
@@ -875,6 +875,10 @@ inline const reflection::Schema *GetSchema(const void *buf) {
return flatbuffers::GetRoot<reflection::Schema>(buf);
}
inline const reflection::Schema *GetSizePrefixedSchema(const void *buf) {
return flatbuffers::GetSizePrefixedRoot<reflection::Schema>(buf);
}
inline const char *SchemaIdentifier() {
return "BFBS";
}
@@ -889,6 +893,11 @@ inline bool VerifySchemaBuffer(
return verifier.VerifyBuffer<reflection::Schema>(SchemaIdentifier());
}
inline bool VerifySizePrefixedSchemaBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<reflection::Schema>(SchemaIdentifier());
}
inline const char *SchemaExtension() {
return "bfbs";
}
@@ -899,6 +908,12 @@ inline void FinishSchemaBuffer(
fbb.Finish(root, SchemaIdentifier());
}
inline void FinishSizePrefixedSchemaBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<reflection::Schema> root) {
fbb.FinishSizePrefixed(root, SchemaIdentifier());
}
} // namespace reflection
#endif // FLATBUFFERS_GENERATED_REFLECTION_REFLECTION_H_

View File

@@ -37,18 +37,16 @@ class Registry {
// Generate text from an arbitrary FlatBuffer by looking up its
// file_identifier in the registry.
bool FlatBufferToText(const uint8_t *flatbuf, size_t len,
std::string *dest) {
bool FlatBufferToText(const uint8_t *flatbuf, size_t len, std::string *dest) {
// Get the identifier out of the buffer.
// If the buffer is truncated, exit.
if (len < sizeof(uoffset_t) +
FlatBufferBuilder::kFileIdentifierLength) {
if (len < sizeof(uoffset_t) + FlatBufferBuilder::kFileIdentifierLength) {
lasterror_ = "buffer truncated";
return false;
}
std::string ident(reinterpret_cast<const char *>(flatbuf) +
sizeof(uoffset_t),
FlatBufferBuilder::kFileIdentifierLength);
std::string ident(
reinterpret_cast<const char *>(flatbuf) + sizeof(uoffset_t),
FlatBufferBuilder::kFileIdentifierLength);
// Load and parse the schema.
Parser parser;
if (!LoadSchema(ident, &parser)) return false;
@@ -82,38 +80,36 @@ class Registry {
// If schemas used contain include statements, call this function for every
// directory the parser should search them for.
void AddIncludeDirectory(const char *path) {
include_paths_.push_back(path);
}
void AddIncludeDirectory(const char *path) { include_paths_.push_back(path); }
// Returns a human readable error if any of the above functions fail.
const std::string &GetLastError() { return lasterror_; }
private:
bool LoadSchema(const std::string &ident, Parser *parser) {
// Find the schema, if not, exit.
auto it = schemas_.find(ident);
if (it == schemas_.end()) {
// Don't attach the identifier, since it may not be human readable.
lasterror_ = "identifier for this buffer not in the registry";
return false;
}
auto &schema = it->second;
// Load the schema from disk. If not, exit.
std::string schematext;
if (!LoadFile(schema.path_.c_str(), false, &schematext)) {
lasterror_ = "could not load schema: " + schema.path_;
return false;
}
// Parse schema.
parser->opts = opts_;
if (!parser->Parse(schematext.c_str(), vector_data(include_paths_),
schema.path_.c_str())) {
lasterror_ = parser->error_;
return false;
}
return true;
}
bool LoadSchema(const std::string &ident, Parser *parser) {
// Find the schema, if not, exit.
auto it = schemas_.find(ident);
if (it == schemas_.end()) {
// Don't attach the identifier, since it may not be human readable.
lasterror_ = "identifier for this buffer not in the registry";
return false;
}
auto &schema = it->second;
// Load the schema from disk. If not, exit.
std::string schematext;
if (!LoadFile(schema.path_.c_str(), false, &schematext)) {
lasterror_ = "could not load schema: " + schema.path_;
return false;
}
// Parse schema.
parser->opts = opts_;
if (!parser->Parse(schematext.c_str(), vector_data(include_paths_),
schema.path_.c_str())) {
lasterror_ = parser->error_;
return false;
}
return true;
}
struct Schema {
std::string path_;

View File

@@ -17,6 +17,8 @@
#ifndef FLATBUFFERS_STL_EMULATION_H_
#define FLATBUFFERS_STL_EMULATION_H_
// clang-format off
#include <string>
#include <type_traits>
#include <vector>
@@ -36,6 +38,10 @@ namespace flatbuffers {
// Retrieve ::back() from a string in a way that is compatible with pre C++11
// STLs (e.g stlport).
inline char& string_back(std::string &value) {
return value[value.length() - 1];
}
inline char string_back(const std::string &value) {
return value[value.length() - 1];
}
@@ -45,12 +51,12 @@ inline char string_back(const std::string &value) {
template <typename T> inline T *vector_data(std::vector<T> &vector) {
// In some debug environments, operator[] does bounds checking, so &vector[0]
// can't be used.
return &(*vector.begin());
return vector.empty() ? nullptr : &vector[0];
}
template <typename T> inline const T *vector_data(
const std::vector<T> &vector) {
return &(*vector.begin());
return vector.empty() ? nullptr : &vector[0];
}
template <typename T, typename V>

View File

@@ -17,32 +17,32 @@
#ifndef FLATBUFFERS_UTIL_H_
#define FLATBUFFERS_UTIL_H_
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <winbase.h>
#include <direct.h>
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h> // Must be included before <direct.h>
# include <direct.h>
# include <winbase.h>
# undef interface // This is also important because of reasons
#else
#include <limits.h>
# include <limits.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "flatbuffers/base.h"
namespace flatbuffers {
// Convert an integer or floating point value to a string.
@@ -61,27 +61,30 @@ template<> inline std::string NumToString<unsigned char>(unsigned char t) {
return NumToString(static_cast<int>(t));
}
#if defined(FLATBUFFERS_CPP98_STL)
template <> inline std::string NumToString<long long>(long long t) {
char buf[21]; // (log((1 << 63) - 1) / log(10)) + 2
snprintf(buf, sizeof(buf), "%lld", t);
return std::string(buf);
}
template<> inline std::string NumToString<long long>(long long t) {
char buf[21]; // (log((1 << 63) - 1) / log(10)) + 2
snprintf(buf, sizeof(buf), "%lld", t);
return std::string(buf);
}
template <> inline std::string NumToString<unsigned long long>(
unsigned long long t) {
char buf[22]; // (log((1 << 63) - 1) / log(10)) + 1
snprintf(buf, sizeof(buf), "%llu", t);
return std::string(buf);
}
template<>
inline std::string NumToString<unsigned long long>(unsigned long long t) {
char buf[22]; // (log((1 << 63) - 1) / log(10)) + 1
snprintf(buf, sizeof(buf), "%llu", t);
return std::string(buf);
}
#endif // defined(FLATBUFFERS_CPP98_STL)
// Special versions for floats/doubles.
template<> inline std::string NumToString<double>(double t) {
template<typename T> std::string FloatToString(T t, int precision) {
// 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;
// Use std::fixed to surpress scientific notation.
ss << std::fixed << t;
ss << std::fixed;
// Default precision is 6, we want that to be higher for doubles.
ss << std::setprecision(precision);
ss << t;
auto s = ss.str();
// Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
auto p = s.find_last_not_of('0');
@@ -91,8 +94,12 @@ template<> inline std::string NumToString<double>(double t) {
}
return s;
}
template<> inline std::string NumToString<double>(double t) {
return FloatToString(t, 12);
}
template<> inline std::string NumToString<float>(float t) {
return NumToString(static_cast<double>(t));
return FloatToString(t, 6);
}
// Convert an integer value to a hexadecimal string.
@@ -100,10 +107,7 @@ template<> inline std::string NumToString<float>(float t) {
// For example, IntToStringHex(0x23, 8) returns the string "00000023".
inline std::string IntToStringHex(int i, int xdigits) {
std::stringstream ss;
ss << std::setw(xdigits)
<< std::setfill('0')
<< std::hex
<< std::uppercase
ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase
<< i;
return ss.str();
}
@@ -111,21 +115,25 @@ inline std::string IntToStringHex(int i, int xdigits) {
// Portable implementation of strtoll().
inline int64_t StringToInt(const char *str, char **endptr = nullptr,
int base = 10) {
// clang-format off
#ifdef _MSC_VER
return _strtoi64(str, endptr, base);
#else
return strtoll(str, endptr, base);
#endif
// clang-format on
}
// Portable implementation of strtoull().
inline uint64_t StringToUInt(const char *str, char **endptr = nullptr,
int base = 10) {
// clang-format off
#ifdef _MSC_VER
return _strtoui64(str, endptr, base);
#else
return strtoull(str, endptr, base);
#endif
// clang-format on
}
typedef bool (*LoadFileFunction)(const char *filename, bool binary,
@@ -134,9 +142,8 @@ typedef bool (*FileExistsFunction)(const char *filename);
LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function);
FileExistsFunction SetFileExistsFunction(FileExistsFunction
file_exists_function);
FileExistsFunction SetFileExistsFunction(
FileExistsFunction file_exists_function);
// Check if file "name" exists.
bool FileExists(const char *name);
@@ -216,7 +223,7 @@ inline std::string ConCatPathFileName(const std::string &path,
const std::string &filename) {
std::string filepath = path;
if (filepath.length()) {
char filepath_last_character = string_back(filepath);
char &filepath_last_character = string_back(filepath);
if (filepath_last_character == kPathSeparatorWindows) {
filepath_last_character = kPathSeparator;
} else if (filepath_last_character != kPathSeparator) {
@@ -224,6 +231,10 @@ inline std::string ConCatPathFileName(const std::string &path,
}
}
filepath += filename;
// Ignore './' at the start of filepath.
if (filepath[0] == '.' && filepath[1] == kPathSeparator) {
filepath.erase(0, 2);
}
return filepath;
}
@@ -239,16 +250,19 @@ inline std::string PosixPath(const char *path) {
inline void EnsureDirExists(const std::string &filepath) {
auto parent = StripFileName(filepath);
if (parent.length()) EnsureDirExists(parent);
// clang-format off
#ifdef _WIN32
(void)_mkdir(filepath.c_str());
#else
mkdir(filepath.c_str(), S_IRWXU|S_IRGRP|S_IXGRP);
#endif
// clang-format on
}
// Obtains the absolute path from any other path.
// Returns the input path if the absolute path couldn't be resolved.
inline std::string AbsolutePath(const std::string &filepath) {
// clang-format off
#ifdef FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
return filepath;
#else
@@ -262,6 +276,7 @@ inline std::string AbsolutePath(const std::string &filepath) {
? abs_path
: filepath;
#endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
// clang-format on
}
// To and from UTF-8 unicode conversion functions
@@ -279,7 +294,7 @@ inline int ToUTF8(uint32_t ucc, std::string *out) {
uint32_t remain_bits = i * 6;
// Store first byte:
(*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
(ucc >> remain_bits));
(ucc >> remain_bits));
// Store remaining bytes:
for (int j = i - 1; j >= 0; j--) {
(*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
@@ -306,12 +321,10 @@ inline int FromUTF8(const char **in) {
break;
}
}
if ((**in << len) & 0x80) return -1; // Bit after leading 1's must be 0.
if ((static_cast<const unsigned char>(**in) << len) & 0x80) return -1; // Bit after leading 1's must be 0.
if (!len) return *(*in)++;
// UTF-8 encoded values with a length are between 2 and 4 bytes.
if (len < 2 || len > 4) {
return -1;
}
if (len < 2 || len > 4) { return -1; }
// Grab initial bits of the code.
int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
for (int i = 0; i < len - 1; i++) {
@@ -321,28 +334,20 @@ inline int FromUTF8(const char **in) {
}
// UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
// UTF-16 surrogate pairs).
if (ucc >= 0xD800 && ucc <= 0xDFFF) {
return -1;
}
if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
// UTF-8 must represent code points in their shortest possible encoding.
switch (len) {
case 2:
// Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
if (ucc < 0x0080 || ucc > 0x07FF) {
return -1;
}
if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
break;
case 3:
// Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
if (ucc < 0x0800 || ucc > 0xFFFF) {
return -1;
}
if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
break;
case 4:
// Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
if (ucc < 0x10000 || ucc > 0x10FFFF) {
return -1;
}
if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
break;
}
return ucc;
@@ -421,7 +426,8 @@ inline bool EscapeString(const char *s, size_t length, std::string *_text,
text += "\\u";
text += IntToStringHex(ucc, 4);
} else if (ucc <= 0x10FFFF) {
// Encode Unicode SMP values to a surrogate pair using two \u escapes.
// Encode Unicode SMP values to a surrogate pair using two \u
// escapes.
uint32_t base = ucc - 0x10000;
auto high_surrogate = (base >> 10) + 0xD800;
auto low_surrogate = (base & 0x03FF) + 0xDC00;

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.flatbuffers;
import static com.google.flatbuffers.Constants.*;
import java.nio.ByteBuffer;
/// @file
/// @addtogroup flatbuffers_java_api
/// @{
/**
* Class that collects utility functions around `ByteBuffer`.
*/
public class ByteBufferUtil {
/**
* Extract the size prefix from a `ByteBuffer`.
*
* @param bb a size-prefixed buffer
* @return the size prefix
*/
public static int getSizePrefix(ByteBuffer bb) {
return bb.getInt(bb.position());
}
/**
* Create a duplicate of a size-prefixed `ByteBuffer` that has its position
* advanced just past the size prefix.
*
* @param bb a size-prefixed buffer
* @return a new buffer on the same underlying data that has skipped the
* size prefix
*/
public static ByteBuffer removeSizePrefix(ByteBuffer bb) {
ByteBuffer s = bb.duplicate();
s.position(s.position() + SIZE_PREFIX_LENGTH);
return s;
}
}
/// @}

View File

@@ -37,6 +37,8 @@ public class Constants {
static final int SIZEOF_DOUBLE = 8;
/** The number of bytes in a file identifier. */
static final int FILE_IDENTIFIER_LENGTH = 4;
/** The number of bytes in a size prefix. */
public static final int SIZE_PREFIX_LENGTH = 4;
}
/// @endcond

View File

@@ -196,7 +196,7 @@ public class FlatBufferBuilder {
int old_buf_size = bb.capacity();
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;
int new_buf_size = old_buf_size == 0 ? 1 : old_buf_size << 1;
bb.position(0);
ByteBuffer nbb = bb_factory.newByteBuffer(new_buf_size);
nbb.position(new_buf_size - old_buf_size);
@@ -816,14 +816,55 @@ public class FlatBufferBuilder {
* Finalize a buffer, pointing to the given `root_table`.
*
* @param root_table An offset to be added to the buffer.
* @param size_prefix Whether to prefix the size to the buffer.
*/
public void finish(int root_table) {
prep(minalign, SIZEOF_INT);
protected void finish(int root_table, boolean size_prefix) {
prep(minalign, SIZEOF_INT + (size_prefix ? SIZEOF_INT : 0));
addOffset(root_table);
if (size_prefix) {
addInt(bb.capacity() - space);
}
bb.position(space);
finished = true;
}
/**
* Finalize a buffer, pointing to the given `root_table`.
*
* @param root_table An offset to be added to the buffer.
*/
public void finish(int root_table) {
finish(root_table, false);
}
/**
* Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
*
* @param root_table An offset to be added to the buffer.
*/
public void finishSizePrefixed(int root_table) {
finish(root_table, true);
}
/**
* Finalize a buffer, pointing to the given `root_table`.
*
* @param root_table An offset to be added to the buffer.
* @param file_identifier A FlatBuffer file identifier to be added to the buffer before
* `root_table`.
* @param size_prefix Whether to prefix the size to the buffer.
*/
protected void finish(int root_table, String file_identifier, boolean size_prefix) {
prep(minalign, SIZEOF_INT + FILE_IDENTIFIER_LENGTH + (size_prefix ? SIZEOF_INT : 0));
if (file_identifier.length() != FILE_IDENTIFIER_LENGTH)
throw new AssertionError("FlatBuffers: file identifier must be length " +
FILE_IDENTIFIER_LENGTH);
for (int i = FILE_IDENTIFIER_LENGTH - 1; i >= 0; i--) {
addByte((byte)file_identifier.charAt(i));
}
finish(root_table, size_prefix);
}
/**
* Finalize a buffer, pointing to the given `root_table`.
*
@@ -832,14 +873,18 @@ public class FlatBufferBuilder {
* `root_table`.
*/
public void finish(int root_table, String file_identifier) {
prep(minalign, SIZEOF_INT + FILE_IDENTIFIER_LENGTH);
if (file_identifier.length() != FILE_IDENTIFIER_LENGTH)
throw new AssertionError("FlatBuffers: file identifier must be length " +
FILE_IDENTIFIER_LENGTH);
for (int i = FILE_IDENTIFIER_LENGTH - 1; i >= 0; i--) {
addByte((byte)file_identifier.charAt(i));
}
finish(root_table);
finish(root_table, file_identifier, false);
}
/**
* Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
*
* @param root_table An offset to be added to the buffer.
* @param file_identifier A FlatBuffer file identifier to be added to the buffer before
* `root_table`.
*/
public void finishSizePrefixed(int root_table, String file_identifier) {
finish(root_table, file_identifier, true);
}
/**

View File

@@ -122,7 +122,7 @@ public class Table {
cr.throwException();
}
} catch (CharacterCodingException x) {
throw new Error(x);
throw new RuntimeException(x);
}
return dst.flip().toString();
@@ -172,6 +172,27 @@ public class Table {
return bb;
}
/**
* Initialize vector as a ByteBuffer.
*
* This is more efficient than using duplicate, since it doesn't copy the data
* nor allocattes a new {@link ByteBuffer}, creating no garbage to be collected.
*
* @param bb The {@link ByteBuffer} for the array
* @param vector_offset The position of the vector in the byte buffer
* @param elem_size The size of each element in the array
* @return The {@link ByteBuffer} for the array
*/
protected ByteBuffer __vector_in_bytebuffer(ByteBuffer bb, int vector_offset, int elem_size) {
int o = this.__offset(vector_offset);
if (o == 0) return null;
int vectorstart = __vector(o);
bb.rewind();
bb.limit(vectorstart + __vector_len(o) * elem_size);
bb.position(vectorstart);
return bb;
}
/**
* Initialize any Table-derived type to point to the union at the given `offset`.
*

View File

@@ -30,6 +30,8 @@
//
using System;
using System.IO;
using System.Text;
namespace FlatBuffers
{
@@ -38,12 +40,12 @@ namespace FlatBuffers
/// </summary>
public class ByteBuffer
{
private readonly byte[] _buffer;
protected byte[] _buffer;
private int _pos; // Must track start of the buffer.
public int Length { get { return _buffer.Length; } }
public byte[] Data { get { return _buffer; } }
public ByteBuffer(int size) : this(new byte[size]) { }
public ByteBuffer(byte[] buffer) : this(buffer, 0) { }
@@ -63,11 +65,64 @@ namespace FlatBuffers
_pos = 0;
}
// Create a new ByteBuffer on the same underlying data.
// The new ByteBuffer's position will be same as this buffer's.
public ByteBuffer Duplicate()
{
return new ByteBuffer(_buffer, Position);
}
// Increases the size of the ByteBuffer, and copies the old data towards
// the end of the new buffer.
public void GrowFront(int newSize)
{
if ((Length & 0xC0000000) != 0)
throw new Exception(
"ByteBuffer: cannot grow buffer beyond 2 gigabytes.");
if (newSize < Length)
throw new Exception("ByteBuffer: cannot truncate buffer.");
byte[] newBuffer = new byte[newSize];
Buffer.BlockCopy(_buffer, 0, newBuffer, newSize - Length,
Length);
_buffer = newBuffer;
}
public byte[] ToArray(int pos, int len)
{
byte[] arr = new byte[len];
Buffer.BlockCopy(_buffer, pos, arr, 0, len);
return arr;
}
public byte[] ToSizedArray()
{
return ToArray(Position, Length - Position);
}
public byte[] ToFullArray()
{
return ToArray(0, Length);
}
public ArraySegment<byte> ToArraySegment(int pos, int len)
{
return new ArraySegment<byte>(_buffer, pos, len);
}
public MemoryStream ToMemoryStream(int pos, int len)
{
return new MemoryStream(_buffer, pos, len);
}
#if !UNSAFE_BYTEBUFFER
// Pre-allocated helper arrays for convertion.
private float[] floathelper = new[] { 0.0f };
private int[] inthelper = new[] { 0 };
private double[] doublehelper = new[] { 0.0 };
private ulong[] ulonghelper = new[] { 0UL };
#endif // !UNSAFE_BYTEBUFFER
// Helper functions for the unsafe version.
static public ushort ReverseBytes(ushort input)
@@ -136,7 +191,6 @@ namespace FlatBuffers
}
#endif // !UNSAFE_BYTEBUFFER
private void AssertOffsetAndLength(int offset, int length)
{
#if !BYTEBUFFER_NO_BOUNDS_CHECK
@@ -171,6 +225,13 @@ namespace FlatBuffers
PutByte(offset, value);
}
public void PutStringUTF8(int offset, string value)
{
AssertOffsetAndLength(offset, value.Length);
Encoding.UTF8.GetBytes(value, 0, value.Length,
_buffer, offset);
}
#if UNSAFE_BYTEBUFFER
// Unsafe but more efficient versions of Put*.
public void PutShort(int offset, short value)
@@ -321,6 +382,11 @@ namespace FlatBuffers
return _buffer[index];
}
public string GetStringUTF8(int startPos, int len)
{
return Encoding.UTF8.GetString(_buffer, startPos, len);
}
#if UNSAFE_BYTEBUFFER
// Unsafe but more efficient versions of Get*.
public short GetShort(int offset)

BIN
net/FlatBuffers/ByteBuffer.exe Executable file

Binary file not shown.

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace FlatBuffers
{
/// <summary>
/// Class that collects utility functions around `ByteBuffer`.
/// </summary>
public class ByteBufferUtil
{
// Extract the size prefix from a `ByteBuffer`.
public static int GetSizePrefix(ByteBuffer bb) {
return bb.GetInt(bb.Position);
}
// Create a duplicate of a size-prefixed `ByteBuffer` that has its position
// advanced just past the size prefix.
public static ByteBuffer RemoveSizePrefix(ByteBuffer bb) {
ByteBuffer s = bb.Duplicate();
s.Position += FlatBufferConstants.SizePrefixLength;
return s;
}
}
}

View File

@@ -59,7 +59,7 @@ namespace FlatBuffers
throw new ArgumentOutOfRangeException("initialSize",
initialSize, "Must be greater than zero");
_space = initialSize;
_bb = new ByteBuffer(new byte[initialSize]);
_bb = new ByteBuffer(initialSize);
}
/// <summary>
@@ -99,18 +99,7 @@ namespace FlatBuffers
// the end of the new buffer (since we build the buffer backwards).
void GrowBuffer()
{
var oldBuf = _bb.Data;
var oldBufSize = oldBuf.Length;
if ((oldBufSize & 0xC0000000) != 0)
throw new Exception(
"FlatBuffers: cannot grow buffer beyond 2 gigabytes.");
var newBufSize = oldBufSize << 1;
var newBuf = new byte[newBufSize];
Buffer.BlockCopy(oldBuf, 0, newBuf, newBufSize - oldBufSize,
oldBufSize);
_bb = new ByteBuffer(newBuf, newBufSize);
_bb.GrowFront(_bb.Length << 1);
}
// Prepare to write an element of `size` after `additional_bytes`
@@ -475,7 +464,7 @@ namespace FlatBuffers
AddByte(0);
var utf8StringLen = Encoding.UTF8.GetByteCount(s);
StartVector(1, utf8StringLen, 1);
Encoding.UTF8.GetBytes(s, 0, s.Length, _bb.Data, _space -= utf8StringLen);
_bb.PutStringUTF8(_space -= utf8StringLen, s);
return new StringOffset(EndVector().Value);
}
@@ -580,6 +569,25 @@ namespace FlatBuffers
}
/// @endcond
/// <summary>
/// Finalize a buffer, pointing to the given `root_table`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="sizePrefix">
/// Whether to prefix the size to the buffer.
/// </param>
protected void Finish(int rootTable, bool sizePrefix)
{
Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0));
AddOffset(rootTable);
if (sizePrefix) {
AddInt(_bb.Length - _space);
}
_bb.Position = _space;
}
/// <summary>
/// Finalize a buffer, pointing to the given `root_table`.
/// </summary>
@@ -588,9 +596,18 @@ namespace FlatBuffers
/// </param>
public void Finish(int rootTable)
{
Prep(_minAlign, sizeof(int));
AddOffset(rootTable);
_bb.Position = _space;
Finish(rootTable, false);
}
/// <summary>
/// Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
public void FinishSizePrefixed(int rootTable)
{
Finish(rootTable, true);
}
/// <summary>
@@ -615,41 +632,69 @@ namespace FlatBuffers
/// </returns>
public byte[] SizedByteArray()
{
var newArray = new byte[_bb.Data.Length - _bb.Position];
Buffer.BlockCopy(_bb.Data, _bb.Position, newArray, 0,
_bb.Data.Length - _bb.Position);
return newArray;
return _bb.ToSizedArray();
}
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
public void Finish(int rootTable, string fileIdentifier)
{
Prep(_minAlign, sizeof(int) +
FlatBufferConstants.FileIdentifierLength);
if (fileIdentifier.Length !=
FlatBufferConstants.FileIdentifierLength)
throw new ArgumentException(
"FlatBuffers: file identifier must be length " +
FlatBufferConstants.FileIdentifierLength,
"fileIdentifier");
for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0;
i--)
{
AddByte((byte)fileIdentifier[i]);
}
Finish(rootTable);
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
/// <param name="sizePrefix">
/// Whether to prefix the size to the buffer.
/// </param>
protected void Finish(int rootTable, string fileIdentifier, bool sizePrefix)
{
Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0) +
FlatBufferConstants.FileIdentifierLength);
if (fileIdentifier.Length !=
FlatBufferConstants.FileIdentifierLength)
throw new ArgumentException(
"FlatBuffers: file identifier must be length " +
FlatBufferConstants.FileIdentifierLength,
"fileIdentifier");
for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0;
i--)
{
AddByte((byte)fileIdentifier[i]);
}
Finish(rootTable, sizePrefix);
}
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
public void Finish(int rootTable, string fileIdentifier)
{
Finish(rootTable, fileIdentifier, false);
}
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`, with the size prefixed.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
public void FinishSizePrefixed(int rootTable, string fileIdentifier)
{
Finish(rootTable, fileIdentifier, true);
}
}
}

View File

@@ -24,5 +24,6 @@ namespace FlatBuffers
public static class FlatBufferConstants
{
public const int FileIdentifierLength = 4;
public const int SizePrefixLength = 4;
}
}

View File

@@ -60,7 +60,7 @@ namespace FlatBuffers
offset += bb.GetInt(offset);
var len = bb.GetInt(offset);
var startPos = offset + sizeof(int);
return Encoding.UTF8.GetString(bb.Data, startPos , len);
return bb.GetStringUTF8(startPos, len);
}
// Get the length of a vector whose offset is stored at "offset" in this object.
@@ -91,7 +91,7 @@ namespace FlatBuffers
var pos = this.__vector(o);
var len = this.__vector_len(o);
return new ArraySegment<byte>(this.bb.Data, pos, len);
return bb.ToArraySegment(pos, len);
}
// Initialize any Table-derived type to point to the union at the given offset.
@@ -126,10 +126,11 @@ namespace FlatBuffers
var startPos_1 = offset_1 + sizeof(int);
var startPos_2 = offset_2 + sizeof(int);
var len = Math.Min(len_1, len_2);
byte[] bbArray = bb.Data;
for(int i = 0; i < len; i++) {
if (bbArray[i + startPos_1] != bbArray[i + startPos_2])
return bbArray[i + startPos_1] - bbArray[i + startPos_2];
byte b1 = bb.Get(i + startPos_1);
byte b2 = bb.Get(i + startPos_2);
if (b1 != b2)
return b1 - b2;
}
return len_1 - len_2;
}
@@ -142,10 +143,10 @@ namespace FlatBuffers
var len_2 = key.Length;
var startPos_1 = offset_1 + sizeof(int);
var len = Math.Min(len_1, len_2);
byte[] bbArray = bb.Data;
for (int i = 0; i < len; i++) {
if (bbArray[i + startPos_1] != key[i])
return bbArray[i + startPos_1] - key[i];
byte b = bb.Get(i + startPos_1);
if (b != key[i])
return b - key[i];
}
return len_1 - len_2;
}

View File

@@ -1,15 +1,18 @@
{
"name": "flatbuffers",
"version": "1.8.0",
"version": "1.9.0",
"description": "Memory Efficient Serialization Library",
"files": ["js/flatbuffers.js"],
"main": "js/flatbuffers.js",
"files": ["js/flatbuffers.js", "js/flatbuffers.mjs"],
"main": "js/flatbuffers",
"module": "js/flatbuffers.mjs",
"directories": {
"doc": "docs",
"test": "tests"
},
"scripts": {
"test": "tests/JavaScriptTest.sh"
"test": "tests/JavaScriptTest.sh",
"append-esm-export": "sed \"s/this.flatbuffers = flatbuffers;/export { flatbuffers };/\" js/flatbuffers.js >> js/flatbuffers.mjs",
"prepublishOnly": "npm run append-esm-export"
},
"repository": {
"type": "git",

View File

@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>1.8.0</version>
<version>1.9.0</version>
<packaging>bundle</packaging>
<name>FlatBuffers Java API</name>
<description>

2
python/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/dist/
/*.egg-info/

View File

@@ -483,14 +483,32 @@ class Builder(object):
self.current_vtable[slotnum] = self.Offset()
## @endcond
def Finish(self, rootTable):
def __Finish(self, rootTable, sizePrefix):
"""Finish finalizes a buffer, pointing to the given `rootTable`."""
N.enforce_number(rootTable, N.UOffsetTFlags)
self.Prep(self.minalign, N.UOffsetTFlags.bytewidth)
prepSize = N.UOffsetTFlags.bytewidth
if sizePrefix:
prepSize += N.Int32Flags.bytewidth
self.Prep(self.minalign, prepSize)
self.PrependUOffsetTRelative(rootTable)
if sizePrefix:
size = len(self.Bytes) - self.Head()
N.enforce_number(size, N.Int32Flags)
self.PrependInt32(size)
self.finished = True
return self.Head()
def Finish(self, rootTable):
"""Finish finalizes a buffer, pointing to the given `rootTable`."""
return self.__Finish(rootTable, False)
def FinishSizePrefixed(self, rootTable):
"""
Finish finalizes a buffer, pointing to the given `rootTable`,
with the size prefixed.
"""
return self.__Finish(rootTable, True)
## @cond FLATBUFFERS_INTERNAL
def Prepend(self, flags, off):
self.Prep(flags.bytewidth, 0)

View File

@@ -0,0 +1,28 @@
# Copyright 2017 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.
from . import encode
from . import number_types
from . import packer
def GetSizePrefix(buf, offset):
"""Extract the size prefix from a buffer."""
return encode.Get(packer.int32, buf, offset)
def RemoveSizePrefix(buf, offset):
"""
Create a slice of a size-prefixed buffer that has
its position advanced just past the size prefix.
"""
return buf, offset + number_types.Int32Flags.bytewidth

View File

@@ -12,11 +12,38 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from datetime import datetime
from setuptools import setup
def version():
version = os.getenv('VERSION', None)
if version:
# Most git tags are prefixed with 'v' (example: v1.2.3) this is
# never desirable for artifact repositories, so we strip the
# leading 'v' if it's present.
return version[1:] if version.startswith('v') else version
else:
# Default version is an ISO8601 compiliant datetime. PyPI doesn't allow
# the colon ':' character in its versions, and time is required to allow
# for multiple publications to master in one day. This datetime string
# uses the "basic" ISO8601 format for both its date and time components
# to avoid issues with the colon character (ISO requires that date and
# time components of a date-time string must be uniformly basic or
# extended, which is why the date component does not have dashes.
#
# Publications using datetime versions should only be made from master
# to represent the HEAD moving forward.
version = datetime.utcnow().strftime('%Y%m%d%H%M%S')
print("VERSION environment variable not set, using datetime instead: {}"
.format(version))
return version
setup(
name='flatbuffers',
version='2015.05.14.0',
version=version(),
license='Apache 2.0',
author='FlatBuffers Contributors',
author_email='me@rwinslow.com',

View File

@@ -0,0 +1,18 @@
:: Copyright 2015 Google Inc. All rights reserved.
::
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
:: You may obtain a copy of the License at
::
:: http://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.
set buildtype=Release
if "%1"=="-b" set buildtype=%2
..\%buildtype%\flatc.exe --cpp --no-prefix -o ../include/flatbuffers reflection.fbs

View File

@@ -17,6 +17,12 @@ struct MonsterT;
struct Weapon;
struct WeaponT;
inline const flatbuffers::TypeTable *Vec3TypeTable();
inline const flatbuffers::TypeTable *MonsterTypeTable();
inline const flatbuffers::TypeTable *WeaponTypeTable();
enum Color {
Color_Red = 0,
Color_Green = 1,
@@ -25,8 +31,8 @@ enum Color {
Color_MAX = Color_Blue
};
inline Color (&EnumValuesColor())[3] {
static Color values[] = {
inline const Color (&EnumValuesColor())[3] {
static const Color values[] = {
Color_Red,
Color_Green,
Color_Blue
@@ -34,8 +40,8 @@ inline Color (&EnumValuesColor())[3] {
return values;
}
inline const char **EnumNamesColor() {
static const char *names[] = {
inline const char * const *EnumNamesColor() {
static const char * const names[] = {
"Red",
"Green",
"Blue",
@@ -56,16 +62,16 @@ enum Equipment {
Equipment_MAX = Equipment_Weapon
};
inline Equipment (&EnumValuesEquipment())[2] {
static Equipment values[] = {
inline const Equipment (&EnumValuesEquipment())[2] {
static const Equipment values[] = {
Equipment_NONE,
Equipment_Weapon
};
return values;
}
inline const char **EnumNamesEquipment() {
static const char *names[] = {
inline const char * const *EnumNamesEquipment() {
static const char * const names[] = {
"NONE",
"Weapon",
nullptr
@@ -185,6 +191,9 @@ struct MonsterT : public flatbuffers::NativeTable {
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MonsterT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return MonsterTypeTable();
}
enum {
VT_POS = 4,
VT_MANA = 6,
@@ -384,6 +393,9 @@ struct WeaponT : public flatbuffers::NativeTable {
struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef WeaponT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return WeaponTypeTable();
}
enum {
VT_NAME = 4,
VT_DAMAGE = 6
@@ -548,6 +560,7 @@ inline bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *obj, Eq
}
inline bool VerifyEquipmentVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types) {
if (!values || !types) return !values && !types;
if (values->size() != types->size()) return false;
for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) {
if (!VerifyEquipment(
@@ -602,70 +615,64 @@ inline void EquipmentUnion::Reset() {
type = Equipment_NONE;
}
inline flatbuffers::TypeTable *Vec3TypeTable();
inline flatbuffers::TypeTable *MonsterTypeTable();
inline flatbuffers::TypeTable *WeaponTypeTable();
inline flatbuffers::TypeTable *ColorTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
inline const flatbuffers::TypeTable *ColorTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_CHAR, 0, 0 },
{ flatbuffers::ET_CHAR, 0, 0 },
{ flatbuffers::ET_CHAR, 0, 0 }
};
static flatbuffers::TypeFunction type_refs[] = {
static const flatbuffers::TypeFunction type_refs[] = {
ColorTypeTable
};
static const char *names[] = {
static const char * const names[] = {
"Red",
"Green",
"Blue"
};
static flatbuffers::TypeTable tt = {
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_ENUM, 3, type_codes, type_refs, nullptr, names
};
return &tt;
}
inline flatbuffers::TypeTable *EquipmentTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
inline const flatbuffers::TypeTable *EquipmentTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, -1 },
{ flatbuffers::ET_SEQUENCE, 0, 0 }
};
static flatbuffers::TypeFunction type_refs[] = {
static const flatbuffers::TypeFunction type_refs[] = {
WeaponTypeTable
};
static const char *names[] = {
static const char * const names[] = {
"NONE",
"Weapon"
};
static flatbuffers::TypeTable tt = {
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_UNION, 2, type_codes, type_refs, nullptr, names
};
return &tt;
}
inline flatbuffers::TypeTable *Vec3TypeTable() {
static flatbuffers::TypeCode type_codes[] = {
inline const flatbuffers::TypeTable *Vec3TypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_FLOAT, 0, -1 },
{ flatbuffers::ET_FLOAT, 0, -1 },
{ flatbuffers::ET_FLOAT, 0, -1 }
};
static const int32_t values[] = { 0, 4, 8, 12 };
static const char *names[] = {
static const char * const names[] = {
"x",
"y",
"z"
};
static flatbuffers::TypeTable tt = {
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_STRUCT, 3, type_codes, nullptr, values, names
};
return &tt;
}
inline flatbuffers::TypeTable *MonsterTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
inline const flatbuffers::TypeTable *MonsterTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, 0 },
{ flatbuffers::ET_SHORT, 0, -1 },
{ flatbuffers::ET_SHORT, 0, -1 },
@@ -677,13 +684,13 @@ inline flatbuffers::TypeTable *MonsterTypeTable() {
{ flatbuffers::ET_UTYPE, 0, 3 },
{ flatbuffers::ET_SEQUENCE, 0, 3 }
};
static flatbuffers::TypeFunction type_refs[] = {
static const flatbuffers::TypeFunction type_refs[] = {
Vec3TypeTable,
ColorTypeTable,
WeaponTypeTable,
EquipmentTypeTable
};
static const char *names[] = {
static const char * const names[] = {
"pos",
"mana",
"hp",
@@ -695,22 +702,22 @@ inline flatbuffers::TypeTable *MonsterTypeTable() {
"equipped_type",
"equipped"
};
static flatbuffers::TypeTable tt = {
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_TABLE, 10, type_codes, type_refs, nullptr, names
};
return &tt;
}
inline flatbuffers::TypeTable *WeaponTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
inline const flatbuffers::TypeTable *WeaponTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_STRING, 0, -1 },
{ flatbuffers::ET_SHORT, 0, -1 }
};
static const char *names[] = {
static const char * const names[] = {
"name",
"damage"
};
static flatbuffers::TypeTable tt = {
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_TABLE, 2, type_codes, nullptr, nullptr, names
};
return &tt;
@@ -720,6 +727,10 @@ inline const MyGame::Sample::Monster *GetMonster(const void *buf) {
return flatbuffers::GetRoot<MyGame::Sample::Monster>(buf);
}
inline const MyGame::Sample::Monster *GetSizePrefixedMonster(const void *buf) {
return flatbuffers::GetSizePrefixedRoot<MyGame::Sample::Monster>(buf);
}
inline Monster *GetMutableMonster(void *buf) {
return flatbuffers::GetMutableRoot<Monster>(buf);
}
@@ -729,12 +740,23 @@ inline bool VerifyMonsterBuffer(
return verifier.VerifyBuffer<MyGame::Sample::Monster>(nullptr);
}
inline bool VerifySizePrefixedMonsterBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<MyGame::Sample::Monster>(nullptr);
}
inline void FinishMonsterBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<MyGame::Sample::Monster> root) {
fbb.Finish(root);
}
inline void FinishSizePrefixedMonsterBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<MyGame::Sample::Monster> root) {
fbb.FinishSizePrefixed(root);
}
inline flatbuffers::unique_ptr<MonsterT> UnPackMonster(
const void *buf,
const flatbuffers::resolver_function_t *res = nullptr) {

View File

@@ -14,13 +14,13 @@
* limitations under the License.
*/
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
using namespace MyGame::Sample;
// Example how to use FlatBuffers to create and read binary buffers.
int main(int /*argc*/, const char * /*argv*/[]) {
int main(int /*argc*/, const char * /*argv*/ []) {
// Build up a serialized buffer algorithmically:
flatbuffers::FlatBufferBuilder builder;
@@ -53,7 +53,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
auto orc = CreateMonster(builder, &position, 150, 80, name, inventory,
Color_Red, weapons, Equipment_Weapon, axe.Union());
builder.Finish(orc); // Serialize the root of the object.
builder.Finish(orc); // Serialize the root of the object.
// We now have a FlatBuffer we can store on disk or send over a network.
@@ -83,8 +83,8 @@ int main(int /*argc*/, const char * /*argv*/[]) {
(void)inv;
// Get and test the `weapons` FlatBuffers's `vector`.
std::string expected_weapon_names[] = {"Sword", "Axe"};
short expected_weapon_damages[] = {3, 5};
std::string expected_weapon_names[] = { "Sword", "Axe" };
short expected_weapon_damages[] = { 3, 5 };
auto weps = monster->weapons();
for (unsigned int i = 0; i < weps->size(); i++) {
assert(weps->Get(i)->name()->str() == expected_weapon_names[i]);
@@ -95,11 +95,10 @@ int main(int /*argc*/, const char * /*argv*/[]) {
// Get and test the `Equipment` union (`equipped` field).
assert(monster->equipped_type() == Equipment_Weapon);
auto equipped = static_cast<const Weapon*>(monster->equipped());
auto equipped = static_cast<const Weapon *>(monster->equipped());
assert(equipped->name()->str() == "Axe");
assert(equipped->damage() == 5);
(void)equipped;
printf("The FlatBuffer was successfully created and verified!\n");
}

View File

@@ -17,13 +17,13 @@
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
using namespace MyGame::Sample;
// This is an example of parsing text straight into a buffer and then
// generating flatbuffer (JSON) text from the buffer.
int main(int /*argc*/, const char * /*argv*/[]) {
int main(int /*argc*/, const char * /*argv*/ []) {
// load FlatBuffer schema (.fbs) and JSON from disk
std::string schemafile;
std::string jsonfile;

2
src/clang-format.sh Normal file
View File

@@ -0,0 +1,2 @@
clang-format -i -style=file include/flatbuffers/* src/*.cpp tests/test.cpp samples/*.cpp grpc/src/compiler/schema_interface.h grpc/tests/*.cpp
git checkout include/flatbuffers/reflection_generated.h

View File

@@ -20,24 +20,19 @@
#include "flatbuffers/util.h"
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4127) // C4127: conditional expression is constant
# pragma warning(push)
# pragma warning(disable : 4127) // C4127: conditional expression is constant
#endif
namespace flatbuffers {
void CodeWriter::operator+=(std::string text) {
while (true) {
auto begin = text.find("{{");
if (begin == std::string::npos) {
break;
}
if (begin == std::string::npos) { break; }
auto end = text.find("}}");
if (end == std::string::npos || end < begin) {
break;
}
if (end == std::string::npos || end < begin) { break; }
// Write all the text before the first {{ into the stream.
stream_.write(text.c_str(), begin);
@@ -119,7 +114,6 @@ std::string BaseGenerator::WrapInNameSpace(const Namespace *ns,
return qualified_name + name;
}
std::string BaseGenerator::WrapInNameSpace(const Definition &def) const {
return WrapInNameSpace(def.defined_namespace, def.name);
}
@@ -150,12 +144,12 @@ void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
if (config != nullptr && config->first_line != nullptr) {
code += std::string(prefix) + std::string(config->first_line) + "\n";
}
std::string line_prefix = std::string(prefix) +
((config != nullptr && config->content_line_prefix != nullptr) ?
config->content_line_prefix : "///");
for (auto it = dc.begin();
it != dc.end();
++it) {
std::string line_prefix =
std::string(prefix) +
((config != nullptr && config->content_line_prefix != nullptr)
? config->content_line_prefix
: "///");
for (auto it = dc.begin(); it != dc.end(); ++it) {
code += line_prefix + *it + "\n";
}
if (config != nullptr && config->last_line != nullptr) {
@@ -166,5 +160,5 @@ void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
} // namespace flatbuffers
#if defined(_MSC_VER)
#pragma warning(pop)
# pragma warning(pop)
#endif

View File

@@ -18,13 +18,12 @@
#include <list>
#define FLATC_VERSION "1.8.0 (" __DATE__ ")"
#define FLATC_VERSION "1.9.0 (" __DATE__ " " __TIME__ ")"
namespace flatbuffers {
void FlatCompiler::ParseFile(
flatbuffers::Parser &parser,
const std::string &filename,
flatbuffers::Parser &parser, const std::string &filename,
const std::string &contents,
std::vector<const char *> &include_directories) const {
auto local_include_directory = flatbuffers::StripFileName(filename);
@@ -46,11 +45,11 @@ void FlatCompiler::Error(const std::string &err, bool usage,
params_.error_fn(this, err, usage, show_exe_name);
}
std::string FlatCompiler::GetUsageString(const char* program_name) const {
std::string FlatCompiler::GetUsageString(const char *program_name) const {
std::stringstream ss;
ss << "Usage: " << program_name << " [OPTION]... FILE... [-- FILE...]\n";
for (size_t i = 0; i < params_.num_generators; ++i) {
const Generator& g = params_.generators[i];
const Generator &g = params_.generators[i];
std::stringstream full_name;
full_name << std::setw(12) << std::left << g.generator_opt_long;
@@ -59,71 +58,75 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const {
ss << " " << full_name.str() << " " << name << " " << help << ".\n";
}
// clang-format off
ss <<
" -o PATH Prefix PATH to all generated files.\n"
" -I PATH Search for includes in the specified path.\n"
" -M Print make rules for generated files.\n"
" --version Print the version number of flatc and exit.\n"
" --strict-json Strict JSON: field names must be / will be quoted,\n"
" no trailing commas in tables/vectors.\n"
" --allow-non-utf8 Pass non-UTF-8 input through parser and emit nonstandard\n"
" \\x escapes in JSON. (Default is to raise parse error on\n"
" non-UTF-8 input.)\n"
" --defaults-json Output fields whose value is the default when\n"
" writing JSON\n"
" --unknown-json Allow fields in JSON that are not defined in the\n"
" schema. These fields will be discared when generating\n"
" binaries.\n"
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
" --scoped-enums Use C++11 style scoped and strongly typed enums.\n"
" also implies --no-prefix.\n"
" --gen-includes (deprecated), this is the default behavior.\n"
" If the original behavior is required (no include\n"
" statements) use --no-includes.\n"
" --no-includes Don\'t generate include statements for included\n"
" schemas the generated file depends on (C++).\n"
" --gen-mutable Generate accessors that can mutate buffers in-place.\n"
" --gen-onefile Generate single output file for C# and Go.\n"
" --gen-name-strings Generate type name functions for C++.\n"
" --gen-object-api Generate an additional object-based API.\n"
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
" --cpp-str-type T Set object API string type (default std::string)\n"
" T::c_str() and T::length() must be supported\n"
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
" --object-prefix Customise class prefix for C++ object-based API.\n"
" --object-suffix Customise class suffix for C++ object-based API.\n"
" Default value is \"T\"\n"
" --no-js-exports Removes Node.js style export lines in JS.\n"
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
" --go-namespace Generate the overrided namespace in Golang.\n"
" --go-import Generate the overrided import for flatbuffers in Golang.\n"
" (default is \"github.com/google/flatbuffers/go\")\n"
" --raw-binary Allow binaries without file_indentifier to be read.\n"
" This may crash flatc given a mismatched schema.\n"
" --proto Input is a .proto, translate to .fbs.\n"
" --grpc Generate GRPC interfaces for the specified languages\n"
" --schema Serialize schemas instead of JSON (use with -b)\n"
" --bfbs-comments Add doc comments to the binary schema files.\n"
" --conform FILE Specify a schema the following schemas should be\n"
" an evolution of. Gives errors if not.\n"
" --conform-includes Include path for the schema given with --conform\n"
" PATH \n"
" --include-prefix Prefix this path to any generated include statements.\n"
" PATH\n"
" --keep-prefix Keep original prefix of schema include statement.\n"
" --no-fb-import Don't include flatbuffers import statement for TypeScript.\n"
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
" --reflect-types Add minimal type reflection to code generation.\n"
" --reflect-names Add minimal type/name reflection.\n"
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n"
"schema). FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input,\n"
"and written to the current directory or the path given by -o.\n"
"example: " << program_name << " -c -b schema1.fbs schema2.fbs data.json\n";
" -o PATH Prefix PATH to all generated files.\n"
" -I PATH Search for includes in the specified path.\n"
" -M Print make rules for generated files.\n"
" --version Print the version number of flatc and exit.\n"
" --strict-json Strict JSON: field names must be / will be quoted,\n"
" no trailing commas in tables/vectors.\n"
" --allow-non-utf8 Pass non-UTF-8 input through parser and emit nonstandard\n"
" \\x escapes in JSON. (Default is to raise parse error on\n"
" non-UTF-8 input.)\n"
" --defaults-json Output fields whose value is the default when\n"
" writing JSON\n"
" --unknown-json Allow fields in JSON that are not defined in the\n"
" schema. These fields will be discared when generating\n"
" binaries.\n"
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
" --scoped-enums Use C++11 style scoped and strongly typed enums.\n"
" also implies --no-prefix.\n"
" --gen-includes (deprecated), this is the default behavior.\n"
" If the original behavior is required (no include\n"
" statements) use --no-includes.\n"
" --no-includes Don\'t generate include statements for included\n"
" schemas the generated file depends on (C++).\n"
" --gen-mutable Generate accessors that can mutate buffers in-place.\n"
" --gen-onefile Generate single output file for C# and Go.\n"
" --gen-name-strings Generate type name functions for C++.\n"
" --gen-object-api Generate an additional object-based API.\n"
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
" --cpp-str-type T Set object API string type (default std::string)\n"
" T::c_str() and T::length() must be supported\n"
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
" --object-prefix Customise class prefix for C++ object-based API.\n"
" --object-suffix Customise class suffix for C++ object-based API.\n"
" Default value is \"T\"\n"
" --no-js-exports Removes Node.js style export lines in JS.\n"
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
" --go-namespace Generate the overrided namespace in Golang.\n"
" --go-import Generate the overrided import for flatbuffers in Golang.\n"
" (default is \"github.com/google/flatbuffers/go\")\n"
" --raw-binary Allow binaries without file_indentifier to be read.\n"
" This may crash flatc given a mismatched schema.\n"
" --size-prefixed Input binaries are size prefixed buffers.\n"
" --proto Input is a .proto, translate to .fbs.\n"
" --oneof-union Translate .proto oneofs to flatbuffer unions.\n"
" --grpc Generate GRPC interfaces for the specified languages\n"
" --schema Serialize schemas instead of JSON (use with -b)\n"
" --bfbs-comments Add doc comments to the binary schema files.\n"
" --conform FILE Specify a schema the following schemas should be\n"
" an evolution of. Gives errors if not.\n"
" --conform-includes Include path for the schema given with --conform\n"
" PATH \n"
" --include-prefix Prefix this path to any generated include statements.\n"
" PATH\n"
" --keep-prefix Keep original prefix of schema include statement.\n"
" --no-fb-import Don't include flatbuffers import statement for TypeScript.\n"
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
" --reflect-types Add minimal type reflection to code generation.\n"
" --reflect-names Add minimal type/name reflection.\n"
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n"
"schema). FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input,\n"
"and written to the current directory or the path given by -o.\n"
"example: " << program_name << " -c -b schema1.fbs schema2.fbs data.json\n";
// clang-format on
return ss.str();
}
int FlatCompiler::Compile(int argc, const char** argv) {
int FlatCompiler::Compile(int argc, const char **argv) {
if (params_.generators == nullptr || params_.num_generators == 0) {
return 0;
}
@@ -152,58 +155,58 @@ int FlatCompiler::Compile(int argc, const char** argv) {
if (arg == "-o") {
if (++argi >= argc) Error("missing path following: " + arg, true);
output_path = flatbuffers::ConCatPathFileName(
flatbuffers::PosixPath(argv[argi]), "");
} else if(arg == "-I") {
flatbuffers::PosixPath(argv[argi]), "");
} else if (arg == "-I") {
if (++argi >= argc) Error("missing path following" + arg, true);
include_directories_storage.push_back(
flatbuffers::PosixPath(argv[argi]));
flatbuffers::PosixPath(argv[argi]));
include_directories.push_back(
include_directories_storage.back().c_str());
} else if(arg == "--conform") {
include_directories_storage.back().c_str());
} else if (arg == "--conform") {
if (++argi >= argc) Error("missing path following" + arg, true);
conform_to_schema = flatbuffers::PosixPath(argv[argi]);
} else if (arg == "--conform-includes") {
if (++argi >= argc) Error("missing path following" + arg, true);
include_directories_storage.push_back(
flatbuffers::PosixPath(argv[argi]));
flatbuffers::PosixPath(argv[argi]));
conform_include_directories.push_back(
include_directories_storage.back().c_str());
include_directories_storage.back().c_str());
} else if (arg == "--include-prefix") {
if (++argi >= argc) Error("missing path following" + arg, true);
opts.include_prefix = flatbuffers::ConCatPathFileName(
flatbuffers::PosixPath(argv[argi]), "");
} else if(arg == "--keep-prefix") {
flatbuffers::PosixPath(argv[argi]), "");
} else if (arg == "--keep-prefix") {
opts.keep_include_path = true;
} else if(arg == "--strict-json") {
} else if (arg == "--strict-json") {
opts.strict_json = true;
} else if(arg == "--allow-non-utf8") {
} else if (arg == "--allow-non-utf8") {
opts.allow_non_utf8 = true;
} else if(arg == "--no-js-exports") {
} else if (arg == "--no-js-exports") {
opts.skip_js_exports = true;
} else if(arg == "--goog-js-export") {
} else if (arg == "--goog-js-export") {
opts.use_goog_js_export_format = true;
} else if(arg == "--go-namespace") {
} else if (arg == "--go-namespace") {
if (++argi >= argc) Error("missing golang namespace" + arg, true);
opts.go_namespace = argv[argi];
} else if(arg == "--go-import") {
} else if (arg == "--go-import") {
if (++argi >= argc) Error("missing golang import" + arg, true);
opts.go_import = argv[argi];
} else if(arg == "--defaults-json") {
} else if (arg == "--defaults-json") {
opts.output_default_scalars_in_json = true;
} else if (arg == "--unknown-json") {
opts.skip_unexpected_fields_in_json = true;
} else if(arg == "--no-prefix") {
} else if (arg == "--no-prefix") {
opts.prefixed_enums = false;
} else if(arg == "--scoped-enums") {
} else if (arg == "--scoped-enums") {
opts.prefixed_enums = false;
opts.scoped_enums = true;
} else if (arg == "--no-union-value-namespacing") {
opts.union_value_namespacing = false;
} else if(arg == "--gen-mutable") {
} else if (arg == "--gen-mutable") {
opts.mutable_buffer = true;
} else if(arg == "--gen-name-strings") {
} else if (arg == "--gen-name-strings") {
opts.generate_name_strings = true;
} else if(arg == "--gen-object-api") {
} else if (arg == "--gen-object-api") {
opts.generate_object_based_api = true;
} else if (arg == "--cpp-ptr-type") {
if (++argi >= argc) Error("missing type following" + arg, true);
@@ -219,40 +222,44 @@ int FlatCompiler::Compile(int argc, const char** argv) {
} else if (arg == "--object-suffix") {
if (++argi >= argc) Error("missing suffix following" + arg, true);
opts.object_suffix = argv[argi];
} else if(arg == "--gen-all") {
} else if (arg == "--gen-all") {
opts.generate_all = true;
opts.include_dependence_headers = false;
} else if(arg == "--gen-includes") {
} else if (arg == "--gen-includes") {
// Deprecated, remove this option some time in the future.
printf("warning: --gen-includes is deprecated (it is now default)\n");
} else if(arg == "--no-includes") {
} else if (arg == "--no-includes") {
opts.include_dependence_headers = false;
} else if (arg == "--gen-onefile") {
opts.one_file = true;
} else if (arg == "--raw-binary") {
raw_binary = true;
} else if(arg == "--") { // Separator between text and binary inputs.
} else if (arg == "--size-prefixed") {
opts.size_prefixed = true;
} else if (arg == "--") { // Separator between text and binary inputs.
binary_files_from = filenames.size();
} else if(arg == "--proto") {
} else if (arg == "--proto") {
opts.proto_mode = true;
} else if(arg == "--schema") {
} else if (arg == "--oneof-union") {
opts.proto_oneof_union = true;
} else if (arg == "--schema") {
schema_binary = true;
} else if(arg == "-M") {
} else if (arg == "-M") {
print_make_rules = true;
} else if(arg == "--version") {
} else if (arg == "--version") {
printf("flatc version %s\n", FLATC_VERSION);
exit(0);
} else if(arg == "--grpc") {
} else if (arg == "--grpc") {
grpc_enabled = true;
} else if(arg == "--bfbs-comments") {
} else if (arg == "--bfbs-comments") {
opts.binary_schema_comments = true;
} else if(arg == "--no-fb-import") {
} else if (arg == "--no-fb-import") {
opts.skip_flatbuffers_import = true;
} else if(arg == "--no-ts-reexport") {
} else if (arg == "--no-ts-reexport") {
opts.reexport_ts_modules = false;
} else if(arg == "--reflect-types") {
} else if (arg == "--reflect-types") {
opts.mini_reflect = IDLOptions::kTypes;
} else if(arg == "--reflect-names") {
} else if (arg == "--reflect-names") {
opts.mini_reflect = IDLOptions::kTypesAndNames;
} else {
for (size_t i = 0; i < params_.num_generators; ++i) {
@@ -266,7 +273,7 @@ int FlatCompiler::Compile(int argc, const char** argv) {
}
}
Error("unknown commandline argument: " + arg, true);
found:;
found:;
}
} else {
filenames.push_back(flatbuffers::PosixPath(argv[argi]));
@@ -293,23 +300,22 @@ int FlatCompiler::Compile(int argc, const char** argv) {
std::unique_ptr<flatbuffers::Parser> parser(new flatbuffers::Parser(opts));
for (auto file_it = filenames.begin();
file_it != filenames.end();
++file_it) {
for (auto file_it = filenames.begin(); file_it != filenames.end();
++file_it) {
auto &filename = *file_it;
std::string contents;
if (!flatbuffers::LoadFile(filename.c_str(), true, &contents))
Error("unable to load file: " + filename);
bool is_binary = static_cast<size_t>(file_it - filenames.begin()) >=
binary_files_from;
bool is_binary =
static_cast<size_t>(file_it - filenames.begin()) >= binary_files_from;
auto ext = flatbuffers::GetExtension(filename);
auto is_schema = ext == "fbs" || ext == "proto";
if (is_binary) {
parser->builder_.Clear();
parser->builder_.PushFlatBuffer(
reinterpret_cast<const uint8_t *>(contents.c_str()),
contents.length());
reinterpret_cast<const uint8_t *>(contents.c_str()),
contents.length());
if (!raw_binary) {
// Generally reading binaries that do not correspond to the schema
// will crash, and sadly there's no way around that when the binary
@@ -318,16 +324,15 @@ int FlatCompiler::Compile(int argc, const char** argv) {
// such an identifier, so by default we require them to match.
if (!parser->file_identifier_.length()) {
Error("current schema has no file_identifier: cannot test if \"" +
filename +
"\" matches the schema, use --raw-binary to read this file"
" anyway.");
} else if (!flatbuffers::BufferHasIdentifier(contents.c_str(),
parser->file_identifier_.c_str())) {
Error("binary \"" +
filename +
"\" does not have expected file_identifier \"" +
parser->file_identifier_ +
"\", use --raw-binary to read this file anyway.");
filename +
"\" matches the schema, use --raw-binary to read this file"
" anyway.");
} else if (!flatbuffers::BufferHasIdentifier(
contents.c_str(), parser->file_identifier_.c_str(), opts.size_prefixed)) {
Error("binary \"" + filename +
"\" does not have expected file_identifier \"" +
parser->file_identifier_ +
"\", use --raw-binary to read this file anyway.");
}
}
} else {
@@ -345,8 +350,9 @@ int FlatCompiler::Compile(int argc, const char** argv) {
if (!is_schema && !parser->builder_.GetSize()) {
// If a file doesn't end in .fbs, it must be json/binary. Ensure we
// didn't just parse a schema with a different extension.
Error("input file is neither json nor a .fbs (schema) file: " +
filename, true);
Error(
"input file is neither json nor a .fbs (schema) file: " + filename,
true);
}
if (is_schema && !conform_to_schema.empty()) {
auto err = parser->ConformTo(conform_parser);
@@ -358,8 +364,8 @@ int FlatCompiler::Compile(int argc, const char** argv) {
}
}
std::string filebase = flatbuffers::StripPath(
flatbuffers::StripExtension(filename));
std::string filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(filename));
for (size_t i = 0; i < params_.num_generators; ++i) {
parser->opts.lang = params_.generators[i].lang;
@@ -367,29 +373,28 @@ int FlatCompiler::Compile(int argc, const char** argv) {
if (!print_make_rules) {
flatbuffers::EnsureDirExists(output_path);
if ((!params_.generators[i].schema_only || is_schema) &&
!params_.generators[i].generate(*parser.get(), output_path, filebase)) {
!params_.generators[i].generate(*parser.get(), output_path,
filebase)) {
Error(std::string("Unable to generate ") +
params_.generators[i].lang_name +
" for " +
filebase);
params_.generators[i].lang_name + " for " + filebase);
}
} else {
std::string make_rule = params_.generators[i].make_rule(
*parser.get(), output_path, filename);
if (!make_rule.empty())
printf("%s\n", flatbuffers::WordWrap(
make_rule, 80, " ", " \\").c_str());
printf("%s\n",
flatbuffers::WordWrap(make_rule, 80, " ", " \\").c_str());
}
if (grpc_enabled) {
if (params_.generators[i].generateGRPC != nullptr) {
if (!params_.generators[i].generateGRPC(*parser.get(), output_path,
filebase)) {
filebase)) {
Error(std::string("Unable to generate GRPC interface for") +
params_.generators[i].lang_name);
}
} else {
Warn(std::string("GRPC interface generator not implemented for ")
+ params_.generators[i].lang_name);
Warn(std::string("GRPC interface generator not implemented for ") +
params_.generators[i].lang_name);
}
}
}

View File

@@ -19,26 +19,17 @@
static const char *g_program_name = nullptr;
static void Warn(const flatbuffers::FlatCompiler *flatc,
const std::string &warn,
bool show_exe_name) {
const std::string &warn, bool show_exe_name) {
(void)flatc;
if (show_exe_name) {
printf("%s: ", g_program_name);
}
if (show_exe_name) { printf("%s: ", g_program_name); }
printf("warning: %s\n", warn.c_str());
}
static void Error(const flatbuffers::FlatCompiler *flatc,
const std::string &err,
bool usage,
bool show_exe_name) {
if (show_exe_name) {
printf("%s: ", g_program_name);
}
const std::string &err, bool usage, bool show_exe_name) {
if (show_exe_name) { printf("%s: ", g_program_name); }
printf("error: %s\n", err.c_str());
if (usage) {
printf("%s", flatc->GetUsageString(g_program_name).c_str());
}
if (usage) { printf("%s", flatc->GetUsageString(g_program_name).c_str()); }
exit(1);
}
@@ -46,61 +37,43 @@ int main(int argc, const char *argv[]) {
g_program_name = argv[0];
const flatbuffers::FlatCompiler::Generator generators[] = {
{ flatbuffers::GenerateBinary, "-b", "--binary", "binary", false,
nullptr,
{ flatbuffers::GenerateBinary, "-b", "--binary", "binary", false, nullptr,
flatbuffers::IDLOptions::kBinary,
"Generate wire format binaries for any data definitions",
flatbuffers::BinaryMakeRule },
{ flatbuffers::GenerateTextFile, "-t", "--json", "text", false,
nullptr,
{ flatbuffers::GenerateTextFile, "-t", "--json", "text", false, nullptr,
flatbuffers::IDLOptions::kJson,
"Generate text output for any data definitions",
flatbuffers::TextMakeRule },
{ flatbuffers::GenerateCPP, "-c", "--cpp", "C++", true,
flatbuffers::GenerateCppGRPC,
flatbuffers::IDLOptions::kCpp,
"Generate C++ headers for tables/structs",
flatbuffers::CPPMakeRule },
{ flatbuffers::GenerateGo, "-g", "--go", "Go", true,
flatbuffers::GenerateGoGRPC,
flatbuffers::IDLOptions::kGo,
"Generate Go files for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateGeneral, "-j", "--java", "Java", true,
nullptr,
flatbuffers::IDLOptions::kJava,
{ flatbuffers::GenerateCPP, "-c", "--cpp", "C++", true,
flatbuffers::GenerateCppGRPC, flatbuffers::IDLOptions::kCpp,
"Generate C++ headers for tables/structs", flatbuffers::CPPMakeRule },
{ flatbuffers::GenerateGo, "-g", "--go", "Go", true,
flatbuffers::GenerateGoGRPC, flatbuffers::IDLOptions::kGo,
"Generate Go files for tables/structs", flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateGeneral, "-j", "--java", "Java", true,
flatbuffers::GenerateJavaGRPC, flatbuffers::IDLOptions::kJava,
"Generate Java classes for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateJS, "-s", "--js", "JavaScript", true,
nullptr,
{ flatbuffers::GenerateJS, "-s", "--js", "JavaScript", true, nullptr,
flatbuffers::IDLOptions::kJs,
"Generate JavaScript code for tables/structs",
flatbuffers::JSMakeRule },
{ flatbuffers::GenerateJS, "-T", "--ts", "TypeScript", true,
nullptr,
"Generate JavaScript code for tables/structs", flatbuffers::JSMakeRule },
{ flatbuffers::GenerateJS, "-T", "--ts", "TypeScript", true, nullptr,
flatbuffers::IDLOptions::kTs,
"Generate TypeScript code for tables/structs",
flatbuffers::JSMakeRule },
{ flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", true,
nullptr,
"Generate TypeScript code for tables/structs", flatbuffers::JSMakeRule },
{ flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", true, nullptr,
flatbuffers::IDLOptions::kCSharp,
"Generate C# classes for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePython, "-p", "--python", "Python", true,
nullptr,
"Generate C# classes for tables/structs", flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePython, "-p", "--python", "Python", true, nullptr,
flatbuffers::IDLOptions::kPython,
"Generate Python files for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true,
nullptr,
flatbuffers::IDLOptions::kPhp,
"Generate PHP files for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema", true,
nullptr,
flatbuffers::IDLOptions::kJsonSchema,
"Generate Json schema",
{ flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr,
flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema",
true, nullptr, flatbuffers::IDLOptions::kJsonSchema,
"Generate Json schema", flatbuffers::GeneralMakeRule },
};
flatbuffers::FlatCompiler::InitParams params;

View File

@@ -14,24 +14,27 @@
* limitations under the License.
*/
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include "flatbuffers/hash.h"
#include <stdio.h>
enum OutputFormat {
kDecimal,
kHexadecimal,
kHexadecimal0x
};
enum OutputFormat { kDecimal, kHexadecimal, kHexadecimal0x };
int main(int argc, char* argv[]) {
const char* name = argv[0];
int main(int argc, char *argv[]) {
const char *name = argv[0];
if (argc <= 1) {
printf("%s HASH [OPTION]... STRING... [-- STRING...]\n", name);
printf("Available hashing algorithms:\n 32 bit:\n");
size_t size = sizeof(flatbuffers::kHashFunctions32) /
printf("Available hashing algorithms:\n");
printf(" 16 bit:\n");
size_t size = sizeof(flatbuffers::kHashFunctions16) /
sizeof(flatbuffers::kHashFunctions16[0]);
for (size_t i = 0; i < size; ++i) {
printf(" * %s\n", flatbuffers::kHashFunctions16[i].name);
}
printf(" 32 bit:\n");
size = sizeof(flatbuffers::kHashFunctions32) /
sizeof(flatbuffers::kHashFunctions32[0]);
for (size_t i = 0; i < size; ++i) {
printf(" * %s\n", flatbuffers::kHashFunctions32[i].name);
@@ -50,14 +53,16 @@ int main(int argc, char* argv[]) {
return 0;
}
const char* hash_algorithm = argv[1];
const char *hash_algorithm = argv[1];
flatbuffers::NamedHashFunction<uint16_t>::HashFunction hash_function16 =
flatbuffers::FindHashFunction16(hash_algorithm);
flatbuffers::NamedHashFunction<uint32_t>::HashFunction hash_function32 =
flatbuffers::FindHashFunction32(hash_algorithm);
flatbuffers::NamedHashFunction<uint64_t>::HashFunction hash_function64 =
flatbuffers::FindHashFunction64(hash_algorithm);
if (!hash_function32 && !hash_function64) {
if (!hash_function16 && !hash_function32 && !hash_function64) {
printf("\"%s\" is not a known hash algorithm.\n", hash_algorithm);
return 0;
}
@@ -66,15 +71,21 @@ int main(int argc, char* argv[]) {
bool annotate = false;
bool escape_dash = false;
for (int i = 2; i < argc; i++) {
const char* arg = argv[i];
const char *arg = argv[i];
if (!escape_dash && arg[0] == '-') {
std::string opt = arg;
if (opt == "-d") output_format = kDecimal;
else if (opt == "-x") output_format = kHexadecimal;
else if (opt == "-0x") output_format = kHexadecimal0x;
else if (opt == "-c") annotate = true;
else if (opt == "--") escape_dash = true;
else printf("Unrecognized argument: \"%s\"\n", arg);
if (opt == "-d")
output_format = kDecimal;
else if (opt == "-x")
output_format = kHexadecimal;
else if (opt == "-0x")
output_format = kHexadecimal0x;
else if (opt == "-c")
annotate = true;
else if (opt == "--")
escape_dash = true;
else
printf("Unrecognized argument: \"%s\"\n", arg);
} else {
std::stringstream ss;
if (output_format == kDecimal) {
@@ -85,13 +96,14 @@ int main(int argc, char* argv[]) {
ss << std::hex;
ss << "0x";
}
if (hash_function32)
if (hash_function16)
ss << hash_function16(arg);
else if (hash_function32)
ss << hash_function32(arg);
else if (hash_function64)
ss << hash_function64(arg);
if (annotate)
ss << " /* \"" << arg << "\" */";
if (annotate) ss << " /* \"" << arg << "\" */";
ss << "\n";
@@ -100,4 +112,3 @@ int main(int argc, char* argv[]) {
}
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@@ -16,10 +16,10 @@
// independent from idl_parser, since this code is not needed for most clients
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers {
@@ -27,16 +27,15 @@ static std::string GenType(const Type &type, bool underlying = false) {
switch (type.base_type) {
case BASE_TYPE_STRUCT:
return type.struct_def->defined_namespace->GetFullyQualifiedName(
type.struct_def->name);
case BASE_TYPE_VECTOR:
return "[" + GenType(type.VectorType()) + "]";
type.struct_def->name);
case BASE_TYPE_VECTOR: return "[" + GenType(type.VectorType()) + "]";
default:
if (type.enum_def && !underlying) {
return type.enum_def->defined_namespace->GetFullyQualifiedName(
type.enum_def->name);
} else {
return kTypeNames[type.base_type];
}
if (type.enum_def && !underlying) {
return type.enum_def->defined_namespace->GetFullyQualifiedName(
type.enum_def->name);
} else {
return kTypeNames[type.base_type];
}
}
}
@@ -47,7 +46,7 @@ static void GenNameSpace(const Namespace &name_space, std::string *_schema,
auto &schema = *_schema;
schema += "namespace ";
for (auto it = name_space.components.begin();
it != name_space.components.end(); ++it) {
it != name_space.components.end(); ++it) {
if (it != name_space.components.begin()) schema += ".";
schema += *it;
}
@@ -69,6 +68,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
std::string schema;
schema += "// Generated from " + file_name + ".proto\n\n";
if (parser.opts.include_dependence_headers) {
// clang-format off
#ifdef FBS_GEN_INCLUDES // TODO: currently all in one file.
int num_includes = 0;
for (auto it = parser.included_files_.begin();
@@ -82,33 +82,40 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
}
if (num_includes) schema += "\n";
#endif
// clang-format on
}
// Generate code for all the enum declarations.
const Namespace *last_namespace = nullptr;
for (auto enum_def_it = parser.enums_.vec.begin();
enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
EnumDef &enum_def = **enum_def_it;
GenNameSpace(*enum_def.defined_namespace, &schema, &last_namespace);
GenComment(enum_def.doc_comment, &schema, nullptr);
schema += "enum " + enum_def.name + " : ";
if (enum_def.is_union)
schema += "union " + enum_def.name;
else
schema += "enum " + enum_def.name + " : ";
schema += GenType(enum_def.underlying_type, true) + " {\n";
for (auto it = enum_def.vals.vec.begin();
it != enum_def.vals.vec.end(); ++it) {
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
auto &ev = **it;
GenComment(ev.doc_comment, &schema, nullptr, " ");
schema += " " + ev.name + " = " + NumToString(ev.value) + ",\n";
if (enum_def.is_union)
schema += " " + GenType(ev.union_type) + ",\n";
else
schema += " " + ev.name + " = " + NumToString(ev.value) + ",\n";
}
schema += "}\n\n";
}
// Generate code for all structs/tables.
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
++it) {
StructDef &struct_def = **it;
GenNameSpace(*struct_def.defined_namespace, &schema, &last_namespace);
GenComment(struct_def.doc_comment, &schema, nullptr);
schema += "table " + struct_def.name + " {\n";
for (auto field_it = struct_def.fields.vec.begin();
field_it != struct_def.fields.vec.end(); ++field_it) {
field_it != struct_def.fields.vec.end(); ++field_it) {
auto &field = **field_it;
if (field.value.type.base_type != BASE_TYPE_UTYPE) {
GenComment(field.doc_comment, &schema, nullptr, " ");
@@ -123,12 +130,10 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
return schema;
}
bool GenerateFBS(const Parser &parser,
const std::string &path,
bool GenerateFBS(const Parser &parser, const std::string &path,
const std::string &file_name) {
return SaveFile((path + file_name + ".fbs").c_str(),
GenerateFBS(parser, file_name), false);
}
} // namespace flatbuffers

File diff suppressed because it is too large Load Diff

View File

@@ -16,21 +16,21 @@
// independent from idl_parser, since this code is not needed for most clients
#include <string>
#include <sstream>
#include <string>
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
#ifdef _WIN32
#include <direct.h>
#define PATH_SEPARATOR "\\"
#define mkdir(n, m) _mkdir(n)
# include <direct.h>
# define PATH_SEPARATOR "\\"
# define mkdir(n, m) _mkdir(n)
#else
#include <sys/stat.h>
#define PATH_SEPARATOR "/"
# include <sys/stat.h>
# define PATH_SEPARATOR "/"
#endif
namespace flatbuffers {
@@ -43,10 +43,11 @@ static std::string GeneratedFileName(const std::string &path,
namespace go {
// see https://golang.org/ref/spec#Keywords
static const char *g_golang_keywords[] = {
"break", "default", "func", "interface", "select", "case", "defer", "go",
"map", "struct", "chan", "else", "goto", "package", "switch", "const",
"fallthrough", "if", "range", "type", "continue", "for", "import", "return", "var",
static const char * const g_golang_keywords[] = {
"break", "default", "func", "interface", "select", "case", "defer",
"go", "map", "struct", "chan", "else", "goto", "package",
"switch", "const", "fallthrough", "if", "range", "type", "continue",
"for", "import", "return", "var",
};
static std::string GenGetter(const Type &type);
@@ -57,23 +58,20 @@ 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);
static std::string GoIdentity(const std::string& name) {
for (size_t i=0; i<sizeof(g_golang_keywords)/sizeof(g_golang_keywords[0]); i++) {
if (name == g_golang_keywords[i]) {
return MakeCamel(name + "_", false);
}
static std::string GoIdentity(const std::string &name) {
for (size_t i = 0;
i < sizeof(g_golang_keywords) / sizeof(g_golang_keywords[0]); i++) {
if (name == g_golang_keywords[i]) { return MakeCamel(name + "_", false); }
}
return MakeCamel(name, false);
}
// 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";
NumToString(field.value.offset) + "))\n\tif o != 0 {\n";
}
// Begin a class declaration.
@@ -169,7 +167,7 @@ static void InitializeExisting(const StructDef &struct_def,
// Implement the table accessor
static void GenTableAccessor(const StructDef &struct_def,
std::string *code_ptr) {
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -177,16 +175,15 @@ static void GenTableAccessor(const StructDef &struct_def,
code += "{\n";
if (struct_def.fixed) {
code += "\treturn rcv._tab.Table\n";
code += "\treturn rcv._tab.Table\n";
} else {
code += "\treturn rcv._tab\n";
code += "\treturn rcv._tab\n";
}
code += "}\n\n";
}
// Get the length of a vector.
static void GetVectorLen(const StructDef &struct_def,
const FieldDef &field,
static void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
@@ -198,8 +195,7 @@ static void GetVectorLen(const StructDef &struct_def,
}
// Get a [ubyte] vector as a byte slice.
static void GetUByteSlice(const StructDef &struct_def,
const FieldDef &field,
static void GetUByteSlice(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
@@ -219,7 +215,7 @@ static void GetScalarFieldOfStruct(const StructDef &struct_def,
GenReceiver(struct_def, code_ptr);
code += " " + MakeCamel(field.name);
code += "() " + TypeName(field) + " {\n";
code +="\treturn " + getter;
code += "\treturn " + getter;
code += "(rcv._tab.Pos + flatbuffers.UOffsetT(";
code += NumToString(field.value.offset) + "))\n}\n";
}
@@ -284,12 +280,11 @@ static void GetStructFieldOfTable(const StructDef &struct_def,
}
// Get the value of a string.
static void GetStringField(const StructDef &struct_def,
const FieldDef &field,
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 += " " + MakeCamel(field.name);
code += "() " + TypeName(field) + " ";
code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type);
code += "(o + rcv._tab.Pos)\n\t}\n\treturn nil\n";
@@ -297,8 +292,7 @@ static void GetStringField(const StructDef &struct_def,
}
// Get the value of a union from an object.
static void GetUnionField(const StructDef &struct_def,
const FieldDef &field,
static void GetUnionField(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -365,8 +359,8 @@ static void BeginBuilderArgs(const StructDef &struct_def,
std::string &code = *code_ptr;
if (code.substr(code.length() - 2) != "\n\n") {
// a previous mutate has not put an extra new line
code += "\n";
// a previous mutate has not put an extra new line
code += "\n";
}
code += "func Create" + struct_def.name;
code += "(builder *flatbuffers.Builder";
@@ -375,22 +369,19 @@ static void BeginBuilderArgs(const StructDef &struct_def,
// 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) {
const char *nameprefix, std::string *code_ptr) {
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
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 field name.
StructBuilderArgs(*field.value.type.struct_def,
(nameprefix + (field.name + "_")).c_str(),
code_ptr);
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else {
std::string &code = *code_ptr;
code += (std::string)", " + nameprefix;
code += (std::string) ", " + nameprefix;
code += GoIdentity(field.name);
code += " " + GenTypeBasic(field.value.type);
}
@@ -406,21 +397,18 @@ static void EndBuilderArgs(std::string *code_ptr) {
// Recursively generate struct construction statements and instert manual
// padding.
static void StructBuilderBody(const StructDef &struct_def,
const char *nameprefix,
std::string *code_ptr) {
const char *nameprefix, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "\tbuilder.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) {
it != struct_def.fields.vec.rend(); ++it) {
auto &field = **it;
if (field.padding)
code += "\tbuilder.Pad(" + NumToString(field.padding) + ")\n";
if (IsStruct(field.value.type)) {
StructBuilderBody(*field.value.type.struct_def,
(nameprefix + (field.name + "_")).c_str(),
code_ptr);
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else {
code += "\tbuilder.Prepend" + GenMethod(field) + "(";
code += nameprefix + GoIdentity(field.name) + ")\n";
@@ -447,8 +435,7 @@ static void GetStartOfTable(const StructDef &struct_def,
// Set the value of a table's field.
static void BuildFieldOfTable(const StructDef &struct_def,
const FieldDef &field,
const size_t offset,
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);
@@ -476,8 +463,7 @@ static void BuildFieldOfTable(const StructDef &struct_def,
// 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) {
const FieldDef &field, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "func " + struct_def.name + "Start";
code += MakeCamel(field.name);
@@ -508,8 +494,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
// Generate a struct field getter, conditioned on its child type(s).
static void GenStructAccessor(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field, std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr, "");
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
@@ -526,9 +511,7 @@ static void GenStructAccessor(const StructDef &struct_def,
GetStructFieldOfTable(struct_def, field, code_ptr);
}
break;
case BASE_TYPE_STRING:
GetStringField(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) {
@@ -538,11 +521,8 @@ static void GenStructAccessor(const StructDef &struct_def,
}
break;
}
case BASE_TYPE_UNION:
GetUnionField(struct_def, field, code_ptr);
break;
default:
assert(0);
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
default: assert(0);
}
}
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
@@ -555,8 +535,8 @@ static void GenStructAccessor(const StructDef &struct_def,
// Mutate the value of a struct's scalar.
static void MutateScalarFieldOfStruct(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
std::string type = MakeCamel(GenTypeBasic(field.value.type));
std::string setter = "rcv._tab.Mutate" + type;
@@ -569,8 +549,8 @@ static void MutateScalarFieldOfStruct(const StructDef &struct_def,
// Mutate the value of a table's scalar.
static void MutateScalarFieldOfTable(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
std::string type = MakeCamel(GenTypeBasic(field.value.type));
std::string setter = "rcv._tab.Mutate" + type + "Slot";
@@ -582,9 +562,8 @@ static void MutateScalarFieldOfTable(const StructDef &struct_def,
}
// Generate a struct field setter, conditioned on its child type(s).
static void GenStructMutator(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
static void GenStructMutator(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr, "");
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
@@ -601,8 +580,7 @@ static void GenTableBuilders(const StructDef &struct_def,
GetStartOfTable(struct_def, code_ptr);
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -617,8 +595,7 @@ static void GenTableBuilders(const StructDef &struct_def,
}
// Generate struct or table methods.
static void GenStruct(const StructDef &struct_def,
std::string *code_ptr) {
static void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
if (struct_def.generated) return;
GenComment(struct_def.doc_comment, code_ptr, nullptr);
@@ -636,8 +613,7 @@ static void GenStruct(const StructDef &struct_def,
// Generate struct fields accessors
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -661,8 +637,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
GenComment(enum_def.doc_comment, code_ptr, nullptr);
BeginEnum(code_ptr);
for (auto it = enum_def.vals.vec.begin();
it != enum_def.vals.vec.end();
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, nullptr, "\t");
@@ -671,8 +646,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
EndEnum(code_ptr);
BeginEnumNames(enum_def, code_ptr);
for (auto it = enum_def.vals.vec.begin();
it != enum_def.vals.vec.end();
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
auto &ev = **it;
EnumNameMember(enum_def, ev, code_ptr);
@@ -686,48 +660,43 @@ static std::string GenGetter(const Type &type) {
case BASE_TYPE_STRING: return "rcv._tab.ByteVector";
case BASE_TYPE_UNION: return "rcv._tab.Union";
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
default:
return "rcv._tab.Get" + MakeCamel(GenTypeGet(type));
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");
? MakeCamel(GenTypeBasic(field.value.type))
: (IsStruct(field.value.type) ? "Struct" : "UOffsetT");
}
static std::string GenTypeBasic(const Type &type) {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#GTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
// clang-format on
};
return ctypename[type.base_type];
}
static std::string GenTypePointer(const Type &type) {
switch (type.base_type) {
case BASE_TYPE_STRING:
return "[]byte";
case BASE_TYPE_VECTOR:
return GenTypeGet(type.VectorType());
case BASE_TYPE_STRUCT:
return type.struct_def->name;
case BASE_TYPE_STRING: return "[]byte";
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";
default: return "*flatbuffers.Table";
}
}
static std::string GenTypeGet(const Type &type) {
return IsScalar(type.base_type)
? GenTypeBasic(type)
: GenTypePointer(type);
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
}
static std::string TypeName(const FieldDef &field) {
@@ -749,7 +718,8 @@ class GoGenerator : public BaseGenerator {
public:
GoGenerator(const Parser &parser, const std::string &path,
const std::string &file_name, const std::string &go_namespace)
: BaseGenerator(parser, path, file_name, "" /* not used*/, "" /* not used */) {
: BaseGenerator(parser, path, file_name, "" /* not used*/,
"" /* not used */) {
std::istringstream iss(go_namespace);
std::string component;
while (std::getline(iss, component, '.')) {
@@ -802,8 +772,8 @@ class GoGenerator : public BaseGenerator {
if (needs_imports) {
code += "import (\n";
if (!parser_.opts.go_import.empty()) {
code += "\tflatbuffers \"" + parser_.opts.go_import +"\"\n";
} else{
code += "\tflatbuffers \"" + parser_.opts.go_import + "\"\n";
} else {
code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n";
}
code += ")\n\n";
@@ -815,12 +785,12 @@ class GoGenerator : public BaseGenerator {
bool needs_imports) {
if (!classcode.length()) return true;
Namespace& ns = go_namespace_.components.empty() ? *def.defined_namespace : go_namespace_;
Namespace &ns = go_namespace_.components.empty() ? *def.defined_namespace
: go_namespace_;
std::string code = "";
BeginFile(LastNamespacePart(ns), needs_imports, &code);
code += classcode;
std::string filename =
NamespaceDir(ns) + def.name + ".go";
std::string filename = NamespaceDir(ns) + def.name + ".go";
return SaveFile(filename.c_str(), code, false);
}

View File

@@ -16,17 +16,19 @@
// independent from idl_parser, since this code is not needed for most clients
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
#include "src/compiler/cpp_generator.h"
#include "src/compiler/go_generator.h"
#include "src/compiler/java_generator.h"
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4512) // C4512: 'class' : assignment operator could not be generated
# pragma warning(push)
# pragma warning(disable : 4512) // C4512: 'class' : assignment operator could
// not be generated
#endif
namespace flatbuffers {
@@ -35,8 +37,7 @@ class FlatBufMethod : public grpc_generator::Method {
public:
enum Streaming { kNone, kClient, kServer, kBiDi };
FlatBufMethod(const RPCCall *method)
: method_(method) {
FlatBufMethod(const RPCCall *method) : method_(method) {
streaming_ = kNone;
auto val = method_->attributes.Lookup("streaming");
if (val) {
@@ -46,14 +47,10 @@ class FlatBufMethod : public grpc_generator::Method {
}
}
grpc::string GetLeadingComments(const grpc::string) const {
return "";
}
grpc::string GetTrailingComments(const grpc::string) const {
return "";
}
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const {
return std::vector<grpc::string>();
return method_->rpc_comment;
}
std::string name() const { return method_->name; }
@@ -62,16 +59,13 @@ class FlatBufMethod : public grpc_generator::Method {
return "flatbuffers::grpc::Message<" + sd.name + ">";
}
std::string get_input_type_name() const {
return (*method_->request).name;
}
std::string get_output_type_name() const {
return (*method_->response).name;
}
std::string get_input_type_name() const { return (*method_->request).name; }
std::string get_output_type_name() const { return (*method_->response).name; }
bool get_module_and_message_path_input(
grpc::string * /*str*/, grpc::string /*generator_file_name*/,
bool /*generate_in_pb2_grpc*/, grpc::string /*import_prefix*/) const {
bool get_module_and_message_path_input(grpc::string * /*str*/,
grpc::string /*generator_file_name*/,
bool /*generate_in_pb2_grpc*/,
grpc::string /*import_prefix*/) const {
return true;
}
@@ -81,13 +75,9 @@ class FlatBufMethod : public grpc_generator::Method {
return true;
}
std::string input_type_name() const {
return GRPCType(*method_->request);
}
std::string input_type_name() const { return GRPCType(*method_->request); }
std::string output_type_name() const {
return GRPCType(*method_->response);
}
std::string output_type_name() const { return GRPCType(*method_->response); }
bool NoStreaming() const { return streaming_ == kNone; }
bool ClientStreaming() const { return streaming_ == kClient; }
@@ -103,14 +93,10 @@ class FlatBufService : public grpc_generator::Service {
public:
FlatBufService(const ServiceDef *service) : service_(service) {}
grpc::string GetLeadingComments(const grpc::string) const {
return "";
}
grpc::string GetTrailingComments(const grpc::string) const {
return "";
}
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const {
return std::vector<grpc::string>();
return service_->doc_comment;
}
std::string name() const { return service_->name; }
@@ -121,7 +107,7 @@ class FlatBufService : public grpc_generator::Service {
std::unique_ptr<const grpc_generator::Method> method(int i) const {
return std::unique_ptr<const grpc_generator::Method>(
new FlatBufMethod(service_->calls.vec[i]));
new FlatBufMethod(service_->calls.vec[i]));
};
private:
@@ -130,8 +116,7 @@ class FlatBufService : public grpc_generator::Service {
class FlatBufPrinter : public grpc_generator::Printer {
public:
FlatBufPrinter(std::string *str)
: str_(str), escape_char_('$'), indent_(0) {}
FlatBufPrinter(std::string *str) : str_(str), escape_char_('$'), indent_(0) {}
void Print(const std::map<std::string, std::string> &vars,
const char *string_template) {
@@ -154,15 +139,13 @@ class FlatBufPrinter : public grpc_generator::Printer {
}
void Print(const char *s) {
if (s == nullptr || std::strlen(s) == 0) {
return;
}
if (s == nullptr || std::strlen(s) == 0) { return; }
// Add this string, but for each part separated by \n, add indentation.
for (;;) {
// Current indentation.
str_->insert(str_->end(), indent_ * 2, ' ');
// See if this contains more than one line.
const char * lf = strchr(s, '\n');
const char *lf = strchr(s, '\n');
if (lf) {
(*str_) += std::string(s, lf + 1);
s = lf + 1;
@@ -175,7 +158,10 @@ class FlatBufPrinter : public grpc_generator::Printer {
}
void Indent() { indent_++; }
void Outdent() { indent_--; assert(indent_ >= 0); }
void Outdent() {
indent_--;
assert(indent_ >= 0);
}
private:
std::string *str_;
@@ -185,22 +171,15 @@ class FlatBufPrinter : public grpc_generator::Printer {
class FlatBufFile : public grpc_generator::File {
public:
enum Language {
kLanguageGo,
kLanguageCpp
};
enum Language { kLanguageGo, kLanguageCpp, kLanguageJava };
FlatBufFile(
const Parser &parser, const std::string &file_name, Language language)
: parser_(parser), file_name_(file_name), language_(language) {}
FlatBufFile(const Parser &parser, const std::string &file_name,
Language language)
: parser_(parser), file_name_(file_name), language_(language) {}
FlatBufFile &operator=(const FlatBufFile &);
grpc::string GetLeadingComments(const grpc::string) const {
return "";
}
grpc::string GetTrailingComments(const grpc::string) const {
return "";
}
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const {
return std::vector<grpc::string>();
}
@@ -229,6 +208,9 @@ class FlatBufFile : public grpc_generator::File {
case kLanguageGo: {
return "import \"github.com/google/flatbuffers/go\"";
}
case kLanguageJava: {
return "import com.google.flatbuffers.grpc.FlatbuffersUtils;";
}
}
return "";
}
@@ -238,13 +220,13 @@ class FlatBufFile : public grpc_generator::File {
};
std::unique_ptr<const grpc_generator::Service> service(int i) const {
return std::unique_ptr<const grpc_generator::Service> (
new FlatBufService(parser_.services_.vec[i]));
return std::unique_ptr<const grpc_generator::Service>(
new FlatBufService(parser_.services_.vec[i]));
}
std::unique_ptr<grpc_generator::Printer> CreatePrinter(std::string *str) const {
return std::unique_ptr<grpc_generator::Printer>(
new FlatBufPrinter(str));
std::unique_ptr<grpc_generator::Printer> CreatePrinter(
std::string *str) const {
return std::unique_ptr<grpc_generator::Printer>(new FlatBufPrinter(str));
}
private:
@@ -257,8 +239,10 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
public:
GoGRPCGenerator(const Parser &parser, const std::string &path,
const std::string &file_name)
: BaseGenerator(parser, path, file_name, "", "" /*Unused*/),
parser_(parser), path_(path), file_name_(file_name) {}
: BaseGenerator(parser, path, file_name, "", "" /*Unused*/),
parser_(parser),
path_(path),
file_name_(file_name) {}
bool generate() {
FlatBufFile file(parser_, file_name_, FlatBufFile::kLanguageGo);
@@ -268,10 +252,11 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
auto service = file.service(i);
const Definition *def = parser_.services_.vec[i];
p.package_name = LastNamespacePart(*(def->defined_namespace));
std::string output = grpc_go_generator::GenerateServiceSource(&file, service.get(), &p);
std::string filename = NamespaceDir(*def->defined_namespace) + def->name + "_grpc.go";
if (!flatbuffers::SaveFile(filename.c_str(), output, false))
return false;
std::string output =
grpc_go_generator::GenerateServiceSource(&file, service.get(), &p);
std::string filename =
NamespaceDir(*def->defined_namespace) + def->name + "_grpc.go";
if (!flatbuffers::SaveFile(filename.c_str(), output, false)) return false;
}
return true;
}
@@ -281,25 +266,22 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
const std::string &path_, &file_name_;
};
bool GenerateGoGRPC(const Parser &parser,
const std::string &path,
bool GenerateGoGRPC(const Parser &parser, const std::string &path,
const std::string &file_name) {
int nservices = 0;
for (auto it = parser.services_.vec.begin();
it != parser.services_.vec.end(); ++it) {
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
++it) {
if (!(*it)->generated) nservices++;
}
if (!nservices) return true;
return GoGRPCGenerator(parser, path, file_name).generate();
}
bool GenerateCppGRPC(const Parser &parser,
const std::string &path,
const std::string &file_name) {
bool GenerateCppGRPC(const Parser &parser, const std::string &path,
const std::string &file_name) {
int nservices = 0;
for (auto it = parser.services_.vec.begin();
it != parser.services_.vec.end(); ++it) {
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
++it) {
if (!(*it)->generated) nservices++;
}
if (!nservices) return true;
@@ -328,9 +310,43 @@ bool GenerateCppGRPC(const Parser &parser,
source_code, false);
}
class JavaGRPCGenerator : public flatbuffers::BaseGenerator {
public:
JavaGRPCGenerator(const Parser &parser, const std::string &path,
const std::string &file_name)
: BaseGenerator(parser, path, file_name, "", "." /*separator*/) {}
bool generate() {
FlatBufFile file(parser_, file_name_, FlatBufFile::kLanguageJava);
grpc_java_generator::Parameters p;
for (int i = 0; i < file.service_count(); i++) {
auto service = file.service(i);
const Definition *def = parser_.services_.vec[i];
p.package_name =
def->defined_namespace->GetFullyQualifiedName(""); // file.package();
std::string output =
grpc_java_generator::GenerateServiceSource(&file, service.get(), &p);
std::string filename =
NamespaceDir(*def->defined_namespace) + def->name + "Grpc.java";
if (!flatbuffers::SaveFile(filename.c_str(), output, false)) return false;
}
return true;
}
};
bool GenerateJavaGRPC(const Parser &parser, const std::string &path,
const std::string &file_name) {
int nservices = 0;
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
++it) {
if (!(*it)->generated) nservices++;
}
if (!nservices) return true;
return JavaGRPCGenerator(parser, path, file_name).generate();
}
} // namespace flatbuffers
#if defined(_MSC_VER)
#pragma warning(pop)
# pragma warning(pop)
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,23 +1,23 @@
/*
* 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.
*/
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include "flatbuffers/code_generators.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include <iostream>
namespace flatbuffers {
@@ -30,8 +30,7 @@ namespace jsons {
std::string GenNativeType(BaseType type) {
switch (type) {
case BASE_TYPE_BOOL:
return "boolean";
case BASE_TYPE_BOOL: return "boolean";
case BASE_TYPE_CHAR:
case BASE_TYPE_UCHAR:
case BASE_TYPE_SHORT:
@@ -41,16 +40,13 @@ std::string GenNativeType(BaseType type) {
case BASE_TYPE_LONG:
case BASE_TYPE_ULONG:
case BASE_TYPE_FLOAT:
case BASE_TYPE_DOUBLE:
return "number";
case BASE_TYPE_STRING:
return "string";
default:
return "";
case BASE_TYPE_DOUBLE: return "number";
case BASE_TYPE_STRING: return "string";
default: return "";
}
}
template <class T> std::string GenFullName(const T *enum_def) {
template<class T> std::string GenFullName(const T *enum_def) {
std::string full_name;
const auto &name_spaces = enum_def->defined_namespace->components;
for (auto ns = name_spaces.cbegin(); ns != name_spaces.cend(); ++ns) {
@@ -60,7 +56,7 @@ template <class T> std::string GenFullName(const T *enum_def) {
return full_name;
}
template <class T> std::string GenTypeRef(const T *enum_def) {
template<class T> std::string GenTypeRef(const T *enum_def) {
return "\"$ref\" : \"#/definitions/" + GenFullName(enum_def) + "\"";
}
@@ -93,11 +89,10 @@ std::string GenType(const Type &type) {
const auto &union_types = type.enum_def->vals.vec;
for (auto ut = union_types.cbegin(); ut < union_types.cend(); ++ut) {
auto &union_type = *ut;
if (union_type->union_type.base_type == BASE_TYPE_NONE) {
continue;
}
if (union_type->union_type.base_type == BASE_TYPE_NONE) { continue; }
if (union_type->union_type.base_type == BASE_TYPE_STRUCT) {
union_type_string.append("{ " + GenTypeRef(union_type->union_type.struct_def) + " }");
union_type_string.append(
"{ " + GenTypeRef(union_type->union_type.struct_def) + " }");
}
if (union_type != *type.enum_def->vals.vec.rbegin()) {
union_type_string.append(",");
@@ -106,10 +101,8 @@ std::string GenType(const Type &type) {
union_type_string.append("]");
return union_type_string;
}
case BASE_TYPE_UTYPE:
return GenTypeRef(type.enum_def);
default:
return GenType(GenNativeType(type.base_type));
case BASE_TYPE_UTYPE: return GenTypeRef(type.enum_def);
default: return GenType(GenNativeType(type.base_type));
}
}
@@ -130,35 +123,29 @@ class JsonSchemaGenerator : public BaseGenerator {
code_ += "{";
code_ += " \"$schema\": \"http://json-schema.org/draft-04/schema#\",";
code_ += " \"definitions\": {";
for (auto e = parser_.enums_.vec.cbegin();
e != parser_.enums_.vec.cend();
for (auto e = parser_.enums_.vec.cbegin(); e != parser_.enums_.vec.cend();
++e) {
code_ += " \"" + GenFullName(*e) + "\" : {";
code_ += " " + GenType("string") + ",";
std::string enumdef(" \"enum\": [");
for (auto enum_value = (*e)->vals.vec.begin();
enum_value != (*e)->vals.vec.end();
++enum_value) {
for (auto enum_value = (*e)->vals.vec.begin();
enum_value != (*e)->vals.vec.end(); ++enum_value) {
enumdef.append("\"" + (*enum_value)->name + "\"");
if (*enum_value != (*e)->vals.vec.back()) {
enumdef.append(", ");
}
if (*enum_value != (*e)->vals.vec.back()) { enumdef.append(", "); }
}
enumdef.append("]");
code_ += enumdef;
code_ += " },"; // close type
}
for (auto s = parser_.structs_.vec.cbegin();
s != parser_.structs_.vec.cend();
++s) {
for (auto s = parser_.structs_.vec.cbegin();
s != parser_.structs_.vec.cend(); ++s) {
const auto &structure = *s;
code_ += " \"" + GenFullName(structure) + "\" : {";
code_ += " " + GenType("object") + ",";
std::string comment;
const auto &comment_lines = structure->doc_comment;
for (auto comment_line = comment_lines.cbegin();
comment_line != comment_lines.cend();
++comment_line) {
comment_line != comment_lines.cend(); ++comment_line) {
comment.append(*comment_line);
}
if (comment.size() > 0) {
@@ -169,10 +156,9 @@ class JsonSchemaGenerator : public BaseGenerator {
const auto &properties = structure->fields.vec;
for (auto prop = properties.cbegin(); prop != properties.cend(); ++prop) {
const auto &property = *prop;
std::string typeLine(" \"" + property->name + "\" : { " + GenType(property->value.type) + " }");
if (property != properties.back()) {
typeLine.append(",");
}
std::string typeLine(" \"" + property->name + "\" : { " +
GenType(property->value.type) + " }");
if (property != properties.back()) { typeLine.append(","); }
code_ += typeLine;
}
code_ += " },"; // close properties
@@ -184,8 +170,7 @@ class JsonSchemaGenerator : public BaseGenerator {
if (requiredProperties.size() > 0) {
std::string required_string(" \"required\" : [");
for (auto req_prop = requiredProperties.cbegin();
req_prop != requiredProperties.cend();
++req_prop) {
req_prop != requiredProperties.cend(); ++req_prop) {
required_string.append("\"" + (*req_prop)->name + "\"");
if (*req_prop != requiredProperties.back()) {
required_string.append(", ");
@@ -196,9 +181,7 @@ class JsonSchemaGenerator : public BaseGenerator {
}
code_ += " \"additionalProperties\" : false";
std::string closeType(" }");
if (*s != parser_.structs_.vec.back()) {
closeType.append(",");
}
if (*s != parser_.structs_.vec.back()) { closeType.append(","); }
code_ += closeType; // close type
}
code_ += " },"; // close definitions

File diff suppressed because it is too large Load Diff

View File

@@ -18,10 +18,10 @@
#include <string>
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers {
namespace python {
@@ -35,7 +35,6 @@ static std::string GenTypeBasic(const Type &type);
static std::string GenTypeGet(const Type &type);
static std::string TypeName(const FieldDef &field);
// Hardcode spaces per indentation.
const std::string Indent = " ";
@@ -44,9 +43,8 @@ const std::string Indent = " ";
std::string OffsetPrefix(const FieldDef &field) {
return "\n" + Indent + Indent +
"o = flatbuffers.number_types.UOffsetTFlags.py_type" +
"(self._tab.Offset(" +
NumToString(field.value.offset) +
"))\n" + Indent + Indent + "if o != 0:\n";
"(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
Indent + Indent + "if o != 0:\n";
}
// Begin a class declaration.
@@ -109,8 +107,7 @@ static void InitializeExisting(const StructDef &struct_def,
}
// Get the length of a vector.
static void GetVectorLen(const StructDef &struct_def,
const FieldDef &field,
static void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
@@ -188,8 +185,7 @@ static void GetStructFieldOfTable(const StructDef &struct_def,
}
// Get the value of a string.
static void GetStringField(const StructDef &struct_def,
const FieldDef &field,
static void GetStringField(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -202,8 +198,7 @@ static void GetStringField(const StructDef &struct_def,
}
// Get the value of a union from an object.
static void GetUnionField(const StructDef &struct_def,
const FieldDef &field,
static void GetUnionField(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -284,9 +279,7 @@ static void GetVectorOfNonStructAsNumpy(const StructDef &struct_def,
// Currently, we only support accessing as numpy array if
// the vector type is a scalar.
if (!(IsScalar(vectortype.base_type))) {
return;
}
if (!(IsScalar(vectortype.base_type))) { return; }
GenReceiver(struct_def, code_ptr);
code += MakeCamel(field.name) + "AsNumpy(self):";
@@ -319,22 +312,19 @@ static void BeginBuilderArgs(const StructDef &struct_def,
// 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) {
const char *nameprefix, std::string *code_ptr) {
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
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 field name.
StructBuilderArgs(*field.value.type.struct_def,
(nameprefix + (field.name + "_")).c_str(),
code_ptr);
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else {
std::string &code = *code_ptr;
code += (std::string)", " + nameprefix;
code += (std::string) ", " + nameprefix;
code += MakeCamel(field.name, false);
}
}
@@ -349,21 +339,18 @@ static void EndBuilderArgs(std::string *code_ptr) {
// Recursively generate struct construction statements and instert manual
// padding.
static void StructBuilderBody(const StructDef &struct_def,
const char *nameprefix,
std::string *code_ptr) {
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) {
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,
(nameprefix + (field.name + "_")).c_str(),
code_ptr);
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else {
code += " builder.Prepend" + GenMethod(field) + "(";
code += nameprefix + MakeCamel(field.name, false) + ")\n";
@@ -389,8 +376,7 @@ static void GetStartOfTable(const StructDef &struct_def,
// Set the value of a table's field.
static void BuildFieldOfTable(const StructDef &struct_def,
const FieldDef &field,
const size_t offset,
const FieldDef &field, const size_t offset,
std::string *code_ptr) {
std::string &code = *code_ptr;
code += "def " + struct_def.name + "Add" + MakeCamel(field.name);
@@ -413,8 +399,7 @@ static void BuildFieldOfTable(const StructDef &struct_def,
// 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) {
const FieldDef &field, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "def " + struct_def.name + "Start";
code += MakeCamel(field.name);
@@ -445,8 +430,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
// Generate a struct field, conditioned on its child type(s).
static void GenStructAccessor(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field, std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr, "# ");
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
@@ -463,9 +447,7 @@ static void GenStructAccessor(const StructDef &struct_def,
GetStructFieldOfTable(struct_def, field, code_ptr);
}
break;
case BASE_TYPE_STRING:
GetStringField(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) {
@@ -476,11 +458,8 @@ static void GenStructAccessor(const StructDef &struct_def,
}
break;
}
case BASE_TYPE_UNION:
GetUnionField(struct_def, field, code_ptr);
break;
default:
assert(0);
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
default: assert(0);
}
}
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
@@ -494,8 +473,7 @@ static void GenTableBuilders(const StructDef &struct_def,
GetStartOfTable(struct_def, code_ptr);
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -510,8 +488,7 @@ static void GenTableBuilders(const StructDef &struct_def,
}
// Generate struct or table methods.
static void GenStruct(const StructDef &struct_def,
std::string *code_ptr) {
static void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
if (struct_def.generated) return;
GenComment(struct_def.doc_comment, code_ptr, nullptr, "# ");
@@ -525,8 +502,7 @@ static void GenStruct(const StructDef &struct_def,
// 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) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -548,8 +524,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
GenComment(enum_def.doc_comment, code_ptr, nullptr, "# ");
BeginEnum(enum_def.name, code_ptr);
for (auto it = enum_def.vals.vec.begin();
it != enum_def.vals.vec.end();
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, nullptr, "# ");
@@ -565,49 +540,44 @@ static std::string GenGetter(const Type &type) {
case BASE_TYPE_UNION: return "self._tab.Union(";
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
default:
return "self._tab.Get(flatbuffers.number_types." + \
MakeCamel(GenTypeGet(type)) + \
"Flags, ";
return "self._tab.Get(flatbuffers.number_types." +
MakeCamel(GenTypeGet(type)) + "Flags, ";
}
}
// 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" : "UOffsetTRelative");
? MakeCamel(GenTypeBasic(field.value.type))
: (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
}
static std::string GenTypeBasic(const Type &type) {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#PTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
// clang-format on
};
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_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";
default: return "*flatbuffers.Table";
}
}
static std::string GenTypeGet(const Type &type) {
return IsScalar(type.base_type)
? GenTypeBasic(type)
: GenTypePointer(type);
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
}
static std::string TypeName(const FieldDef &field) {
@@ -666,9 +636,7 @@ class PythonGenerator : public BaseGenerator {
std::string &code = *code_ptr;
code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n";
code += "# namespace: " + name_space_name + "\n\n";
if (needs_imports) {
code += "import flatbuffers\n\n";
}
if (needs_imports) { code += "import flatbuffers\n\n"; }
}
// Save out the generated code for a Python Table type.
@@ -688,8 +656,8 @@ class PythonGenerator : public BaseGenerator {
std::string code = "";
BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code);
code += classcode;
std::string filename = NamespaceDir(*def.defined_namespace) +
def.name + ".py";
std::string filename =
NamespaceDir(*def.defined_namespace) + def.name + ".py";
return SaveFile(filename.c_str(), code, false);
}
};

View File

@@ -17,15 +17,14 @@
// independent from idl_parser, since this code is not needed for most clients
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/flexbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/flexbuffers.h"
namespace flatbuffers {
static bool GenStruct(const StructDef &struct_def, const Table *table,
int indent, const IDLOptions &opts,
std::string *_text);
int indent, const IDLOptions &opts, std::string *_text);
// If indentation is less than 0, that indicates we don't want any newlines
// either.
@@ -33,9 +32,7 @@ const char *NewLine(const IDLOptions &opts) {
return opts.indent_step >= 0 ? "\n" : "";
}
int Indent(const IDLOptions &opts) {
return std::max(opts.indent_step, 0);
}
int Indent(const IDLOptions &opts) { return std::max(opts.indent_step, 0); }
// Output an identifier with or without quotes depending on strictness.
void OutputIdentifier(const std::string &name, const IDLOptions &opts,
@@ -49,15 +46,16 @@ void OutputIdentifier(const std::string &name, const IDLOptions &opts,
// 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> bool Print(T val, Type type, int /*indent*/,
Type * /*union_type*/,
const IDLOptions &opts,
std::string *_text) {
template<typename T>
bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
const IDLOptions &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));
auto enum_val = type.enum_def->ReverseLookup(static_cast<int64_t>(val));
if (enum_val) {
OutputIdentifier(enum_val->name, opts, _text);
text += "\"";
text += enum_val->name;
text += "\"";
return true;
}
}
@@ -72,9 +70,9 @@ template<typename T> bool Print(T val, Type type, int /*indent*/,
}
// Print a vector a sequence of JSON values, comma separated, wrapped in "[]".
template<typename T> bool PrintVector(const Vector<T> &v, Type type,
int indent, const IDLOptions &opts,
std::string *_text) {
template<typename T>
bool PrintVector(const Vector<T> &v, Type type, int indent,
const IDLOptions &opts, std::string *_text) {
std::string &text = *_text;
text += "[";
text += NewLine(opts);
@@ -90,8 +88,7 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
return false;
}
} else {
if (!Print(v[i], type, indent + Indent(opts), nullptr,
opts, _text)) {
if (!Print(v[i], type, indent + Indent(opts), nullptr, opts, _text)) {
return false;
}
}
@@ -103,11 +100,10 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
}
// Specialization of Print above for pointer types.
template<> bool Print<const void *>(const void *val,
Type type, int indent,
Type *union_type,
const IDLOptions &opts,
std::string *_text) {
template<>
bool Print<const void *>(const void *val, Type type, int indent,
Type *union_type, const IDLOptions &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
@@ -116,11 +112,8 @@ template<> bool Print<const void *>(const void *val,
return Print<const void *>(val, *union_type, indent, nullptr, opts,
_text);
case BASE_TYPE_STRUCT:
if (!GenStruct(*type.struct_def,
reinterpret_cast<const Table *>(val),
indent,
opts,
_text)) {
if (!GenStruct(*type.struct_def, reinterpret_cast<const Table *>(val),
indent, opts, _text)) {
return false;
}
break;
@@ -135,6 +128,7 @@ template<> bool Print<const void *>(const void *val,
type = type.VectorType();
// Call PrintVector above specifically for each element type:
switch (type.base_type) {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
case BASE_TYPE_ ## ENUM: \
@@ -146,6 +140,7 @@ template<> bool Print<const void *>(const void *val,
break;
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
// clang-format on
}
break;
default: assert(0);
@@ -161,24 +156,26 @@ template<typename T> static bool GenField(const FieldDef &fd,
std::string *_text) {
return Print(fixed ?
reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset) :
table->GetField<T>(fd.value.offset, 0), fd.value.type, indent, nullptr,
opts, _text);
table->GetField<T>(fd.value.offset,
IsFloat(fd.value.type.base_type) ?
static_cast<T>(strtod(fd.value.constant.c_str(), nullptr)) :
static_cast<T>(StringToInt(fd.value.constant.c_str()))),
fd.value.type, indent, nullptr, opts, _text);
}
static bool GenStruct(const StructDef &struct_def, const Table *table,
int indent, const IDLOptions &opts,
std::string *_text);
int indent, const IDLOptions &opts, std::string *_text);
// Generate text for non-scalar field.
static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
int indent, Type *union_type,
const IDLOptions &opts, std::string *_text) {
int indent, Type *union_type, const IDLOptions &opts,
std::string *_text) {
const void *val = nullptr;
if (fixed) {
// The only non-scalar fields in structs are structs.
assert(IsStruct(fd.value.type));
val = reinterpret_cast<const Struct *>(table)->
GetStruct<const void *>(fd.value.offset);
val = reinterpret_cast<const Struct *>(table)->GetStruct<const void *>(
fd.value.offset);
} else if (fd.flexbuffer) {
auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
auto root = flexbuffers::GetRoot(vec->data(), vec->size());
@@ -190,8 +187,8 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
return GenStruct(*fd.nested_flatbuffer, root, indent, opts, _text);
} else {
val = IsStruct(fd.value.type)
? table->GetStruct<const void *>(fd.value.offset)
: table->GetPointer<const void *>(fd.value.offset);
? table->GetStruct<const void *>(fd.value.offset)
: table->GetPointer<const void *>(fd.value.offset);
}
return Print(val, fd.value.type, indent, union_type, opts, _text);
}
@@ -199,20 +196,17 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
// Generate text for a struct or table, values separated by commas, indented,
// and bracketed by "{}"
static bool GenStruct(const StructDef &struct_def, const Table *table,
int indent, const IDLOptions &opts,
std::string *_text) {
int indent, const IDLOptions &opts, std::string *_text) {
std::string &text = *_text;
text += "{";
int fieldout = 0;
Type *union_type = nullptr;
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
it != struct_def.fields.vec.end(); ++it) {
FieldDef &fd = **it;
auto is_present = struct_def.fixed || table->CheckField(fd.value.offset);
auto output_anyway = opts.output_default_scalars_in_json &&
IsScalar(fd.value.type.base_type) &&
!fd.deprecated;
IsScalar(fd.value.type.base_type) && !fd.deprecated;
if (is_present || output_anyway) {
if (fieldout++) {
if (!opts.protobuf_ascii_alike) text += ",";
@@ -222,42 +216,38 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
OutputIdentifier(fd.name, opts, _text);
if (!opts.protobuf_ascii_alike ||
(fd.value.type.base_type != BASE_TYPE_STRUCT &&
fd.value.type.base_type != BASE_TYPE_VECTOR)) text += ":";
fd.value.type.base_type != BASE_TYPE_VECTOR))
text += ":";
text += " ";
if (is_present) {
switch (fd.value.type.base_type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
case BASE_TYPE_ ## ENUM: \
if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
opts, indent + Indent(opts), _text)) { \
return false; \
} \
break;
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
// Generate drop-thru case statements for all pointer types:
switch (fd.value.type.base_type) {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
case BASE_TYPE_ ## ENUM:
FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
if (!GenFieldOffset(fd, table, struct_def.fixed, indent + Indent(opts),
union_type, opts, _text)) {
return false;
}
case BASE_TYPE_ ## ENUM: \
if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
opts, indent + Indent(opts), _text)) { \
return false; \
} \
break;
}
if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
auto enum_val = fd.value.type.enum_def->ReverseLookup(
table->GetField<uint8_t>(fd.value.offset, 0));
assert(enum_val);
union_type = &enum_val->union_type;
}
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, GTYPE, NTYPE, PTYPE) \
case BASE_TYPE_ ## ENUM:
FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
if (!GenFieldOffset(fd, table, struct_def.fixed, indent + Indent(opts),
union_type, opts, _text)) {
return false;
}
break;
// clang-format on
}
else
{
text += fd.value.constant;
if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
auto enum_val = fd.value.type.enum_def->ReverseLookup(
table->GetField<uint8_t>(fd.value.offset, 0));
union_type = enum_val ? &enum_val->union_type : nullptr;
}
}
}
@@ -272,12 +262,10 @@ bool GenerateText(const Parser &parser, const void *flatbuffer,
std::string *_text) {
std::string &text = *_text;
assert(parser.root_struct_def_); // call SetRootType()
text.reserve(1024); // Reduce amount of inevitable reallocs.
if (!GenStruct(*parser.root_struct_def_,
GetRoot<Table>(flatbuffer),
0,
parser.opts,
_text)) {
text.reserve(1024); // Reduce amount of inevitable reallocs.
auto root = parser.opts.size_prefixed ?
GetSizePrefixedRoot<Table>(flatbuffer) : GetRoot<Table>(flatbuffer);
if (!GenStruct(*parser.root_struct_def_, root, 0, parser.opts, _text)) {
return false;
}
text += NewLine(parser.opts);
@@ -289,34 +277,29 @@ std::string TextFileName(const std::string &path,
return path + file_name + ".json";
}
bool GenerateTextFile(const Parser &parser,
const std::string &path,
bool GenerateTextFile(const Parser &parser, const std::string &path,
const std::string &file_name) {
if (!parser.builder_.GetSize() || !parser.root_struct_def_) return true;
std::string text;
if (!GenerateText(parser, parser.builder_.GetBufferPointer(), &text)) {
return false;
}
return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(),
text,
return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text,
false);
}
std::string TextMakeRule(const Parser &parser,
const std::string &path,
std::string TextMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
if (!parser.builder_.GetSize() || !parser.root_struct_def_) return "";
std::string filebase = flatbuffers::StripPath(
flatbuffers::StripExtension(file_name));
std::string filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
auto included_files = parser.GetIncludedFilesRecursive(
parser.root_struct_def_->file);
for (auto it = included_files.begin();
it != included_files.end(); ++it) {
auto included_files =
parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
for (auto it = included_files.begin(); it != included_files.end(); ++it) {
make_rule += " " + *it;
}
return make_rule;
}
} // namespace flatbuffers

File diff suppressed because it is too large Load Diff

View File

@@ -22,7 +22,8 @@
namespace flatbuffers {
int64_t GetAnyValueI(reflection::BaseType type, const uint8_t *data) {
# define FLATBUFFERS_GET(T) static_cast<int64_t>(ReadScalar<T>(data))
// clang-format off
#define FLATBUFFERS_GET(T) static_cast<int64_t>(ReadScalar<T>(data))
switch (type) {
case reflection::UType:
case reflection::Bool:
@@ -43,16 +44,17 @@ int64_t GetAnyValueI(reflection::BaseType type, const uint8_t *data) {
}
default: return 0; // Tables & vectors do not make sense.
}
# undef FLATBUFFERS_GET
#undef FLATBUFFERS_GET
// clang-format on
}
double GetAnyValueF(reflection::BaseType type, const uint8_t *data) {
switch (type) {
case reflection::Float: return static_cast<double>(ReadScalar<float>(data));
case reflection::Float: return static_cast<double>(ReadScalar<float>(data));
case reflection::Double: return ReadScalar<double>(data);
case reflection::String: {
auto s = reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) +
data);
auto s =
reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) + data);
return s ? strtod(s->c_str(), nullptr) : 0.0;
}
default: return static_cast<double>(GetAnyValueI(type, data));
@@ -65,8 +67,8 @@ std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
case reflection::Float:
case reflection::Double: return NumToString(GetAnyValueF(type, data));
case reflection::String: {
auto s = reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) +
data);
auto s =
reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) + data);
return s ? s->c_str() : "";
}
case reflection::Obj:
@@ -80,7 +82,7 @@ std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
s += "(struct)"; // TODO: implement this as well.
} else {
auto table_field = reinterpret_cast<const Table *>(
ReadScalar<uoffset_t>(data) + data);
ReadScalar<uoffset_t>(data) + data);
s += " { ";
auto fielddefs = objectdef.fields();
for (auto it = fielddefs->begin(); it != fielddefs->end(); ++it) {
@@ -104,15 +106,15 @@ std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
return "(table)";
}
case reflection::Vector:
return "[(elements)]"; // TODO: implement this as well.
case reflection::Union:
return "(union)"; // TODO: implement this as well.
return "[(elements)]"; // TODO: implement this as well.
case reflection::Union: return "(union)"; // TODO: implement this as well.
default: return NumToString(GetAnyValueI(type, data));
}
}
void SetAnyValueI(reflection::BaseType type, uint8_t *data, int64_t val) {
# define FLATBUFFERS_SET(T) WriteScalar(data, static_cast<T>(val))
// clang-format off
#define FLATBUFFERS_SET(T) WriteScalar(data, static_cast<T>(val))
switch (type) {
case reflection::UType:
case reflection::Bool:
@@ -129,12 +131,13 @@ void SetAnyValueI(reflection::BaseType type, uint8_t *data, int64_t val) {
// TODO: support strings
default: break;
}
# undef FLATBUFFERS_SET
#undef FLATBUFFERS_SET
// clang-format on
}
void SetAnyValueF(reflection::BaseType type, uint8_t *data, double val) {
switch (type) {
case reflection::Float: WriteScalar(data, static_cast<float>(val)); break;
case reflection::Float: WriteScalar(data, static_cast<float>(val)); break;
case reflection::Double: WriteScalar(data, val); break;
// TODO: support strings.
default: SetAnyValueI(type, data, static_cast<int64_t>(val)); break;
@@ -165,9 +168,11 @@ class ResizeContext {
ResizeContext(const reflection::Schema &schema, uoffset_t start, int delta,
std::vector<uint8_t> *flatbuf,
const reflection::Object *root_table = nullptr)
: schema_(schema), startptr_(vector_data(*flatbuf) + start),
delta_(delta), buf_(*flatbuf),
dag_check_(flatbuf->size() / sizeof(uoffset_t), false) {
: schema_(schema),
startptr_(vector_data(*flatbuf) + start),
delta_(delta),
buf_(*flatbuf),
dag_check_(flatbuf->size() / sizeof(uoffset_t), false) {
auto mask = static_cast<int>(sizeof(largest_scalar_t) - 1);
delta_ = (delta_ + mask) & ~mask;
if (!delta_) return; // We can't shrink by less than largest_scalar_t.
@@ -176,16 +181,17 @@ class ResizeContext {
Straddle<uoffset_t, 1>(vector_data(buf_), root, vector_data(buf_));
ResizeTable(root_table ? *root_table : *schema.root_table(), root);
// We can now add or remove bytes at start.
if (delta_ > 0) buf_.insert(buf_.begin() + start, delta_, 0);
else buf_.erase(buf_.begin() + start, buf_.begin() + start - delta_);
if (delta_ > 0)
buf_.insert(buf_.begin() + start, delta_, 0);
else
buf_.erase(buf_.begin() + start, buf_.begin() + start - delta_);
}
// Check if the range between first (lower address) and second straddles
// the insertion point. If it does, change the offset at offsetloc (of
// type T, with direction D).
template<typename T, int D> void Straddle(const void *first,
const void *second,
void *offsetloc) {
template<typename T, int D>
void Straddle(const void *first, const void *second, void *offsetloc) {
if (first <= startptr_ && second >= startptr_) {
WriteScalar<T>(offsetloc, ReadScalar<T>(offsetloc) + delta_ * D);
DagCheck(offsetloc) = true;
@@ -205,8 +211,7 @@ class ResizeContext {
}
void ResizeTable(const reflection::Object &objectdef, Table *table) {
if (DagCheck(table))
return; // Table already visited.
if (DagCheck(table)) return; // Table already visited.
auto vtable = table->GetVTable();
// Early out: since all fields inside the table must point forwards in
// memory, if the insertion point is before the table we can stop here.
@@ -228,13 +233,14 @@ class ResizeContext {
auto offset = table->GetOptionalFieldOffset(fielddef.offset());
if (!offset) continue;
// Ignore structs.
auto subobjectdef = base_type == reflection::Obj ?
schema_.objects()->Get(fielddef.type()->index()) : nullptr;
auto subobjectdef =
base_type == reflection::Obj
? schema_.objects()->Get(fielddef.type()->index())
: nullptr;
if (subobjectdef && subobjectdef->is_struct()) continue;
// Get this fields' offset, and read it if safe.
auto offsetloc = tableloc + offset;
if (DagCheck(offsetloc))
continue; // This offset already visited.
if (DagCheck(offsetloc)) continue; // This offset already visited.
auto ref = offsetloc + ReadScalar<uoffset_t>(offsetloc);
Straddle<uoffset_t, 1>(offsetloc, ref, offsetloc);
// Recurse.
@@ -248,16 +254,16 @@ class ResizeContext {
if (elem_type != reflection::Obj && elem_type != reflection::String)
break;
auto vec = reinterpret_cast<Vector<uoffset_t> *>(ref);
auto elemobjectdef = elem_type == reflection::Obj
? schema_.objects()->Get(fielddef.type()->index())
: nullptr;
auto elemobjectdef =
elem_type == reflection::Obj
? schema_.objects()->Get(fielddef.type()->index())
: nullptr;
if (elemobjectdef && elemobjectdef->is_struct()) break;
for (uoffset_t i = 0; i < vec->size(); i++) {
auto loc = vec->Data() + i * sizeof(uoffset_t);
if (DagCheck(loc))
continue; // This offset already visited.
if (DagCheck(loc)) continue; // This offset already visited.
auto dest = loc + vec->Get(i);
Straddle<uoffset_t, 1>(loc, dest ,loc);
Straddle<uoffset_t, 1>(loc, dest, loc);
if (elemobjectdef)
ResizeTable(*elemobjectdef, reinterpret_cast<Table *>(dest));
}
@@ -268,10 +274,8 @@ class ResizeContext {
reinterpret_cast<Table *>(ref));
break;
}
case reflection::String:
break;
default:
assert(false);
case reflection::String: break;
default: assert(false);
}
}
// Check if the vtable offset points beyond the insertion point.
@@ -292,8 +296,8 @@ class ResizeContext {
};
void SetString(const reflection::Schema &schema, const std::string &val,
const String *str, std::vector<uint8_t> *flatbuf,
const reflection::Object *root_table) {
const String *str, std::vector<uint8_t> *flatbuf,
const reflection::Object *root_table) {
auto delta = static_cast<int>(val.size()) - static_cast<int>(str->Length());
auto str_start = static_cast<uoffset_t>(
reinterpret_cast<const uint8_t *>(str) - vector_data(*flatbuf));
@@ -317,8 +321,8 @@ uint8_t *ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize,
const reflection::Object *root_table) {
auto delta_elem = static_cast<int>(newsize) - static_cast<int>(num_elems);
auto delta_bytes = delta_elem * static_cast<int>(elem_size);
auto vec_start = reinterpret_cast<const uint8_t *>(vec) -
vector_data(*flatbuf);
auto vec_start =
reinterpret_cast<const uint8_t *>(vec) - vector_data(*flatbuf);
auto start = static_cast<uoffset_t>(vec_start + sizeof(uoffset_t) +
elem_size * num_elems);
if (delta_bytes) {
@@ -363,8 +367,7 @@ void CopyInline(FlatBufferBuilder &fbb, const reflection::Field &fielddef,
Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
const reflection::Schema &schema,
const reflection::Object &objectdef,
const Table &table,
bool use_string_pooling) {
const Table &table, bool use_string_pooling) {
// Before we can construct the table, we have to first generate any
// subobjects, and collect their offsets.
std::vector<uoffset_t> offsets;
@@ -377,39 +380,41 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
switch (fielddef.type()->base_type()) {
case reflection::String: {
offset = use_string_pooling
? fbb.CreateSharedString(GetFieldS(table, fielddef)).o
: fbb.CreateString(GetFieldS(table, fielddef)).o;
? fbb.CreateSharedString(GetFieldS(table, fielddef)).o
: fbb.CreateString(GetFieldS(table, fielddef)).o;
break;
}
case reflection::Obj: {
auto &subobjectdef = *schema.objects()->Get(fielddef.type()->index());
if (!subobjectdef.is_struct()) {
offset = CopyTable(fbb, schema, subobjectdef,
*GetFieldT(table, fielddef)).o;
offset =
CopyTable(fbb, schema, subobjectdef, *GetFieldT(table, fielddef))
.o;
}
break;
}
case reflection::Union: {
auto &subobjectdef = GetUnionType(schema, objectdef, fielddef, table);
offset = CopyTable(fbb, schema, subobjectdef,
*GetFieldT(table, fielddef)).o;
offset =
CopyTable(fbb, schema, subobjectdef, *GetFieldT(table, fielddef)).o;
break;
}
case reflection::Vector: {
auto vec = table.GetPointer<const Vector<Offset<Table>> *>(
fielddef.offset());
auto vec =
table.GetPointer<const Vector<Offset<Table>> *>(fielddef.offset());
auto element_base_type = fielddef.type()->element();
auto elemobjectdef = element_base_type == reflection::Obj
? schema.objects()->Get(fielddef.type()->index())
: nullptr;
auto elemobjectdef =
element_base_type == reflection::Obj
? schema.objects()->Get(fielddef.type()->index())
: nullptr;
switch (element_base_type) {
case reflection::String: {
std::vector<Offset<const String *>> elements(vec->size());
auto vec_s = reinterpret_cast<const Vector<Offset<String>> *>(vec);
for (uoffset_t i = 0; i < vec_s->size(); i++) {
elements[i] = use_string_pooling
? fbb.CreateSharedString(vec_s->Get(i)).o
: fbb.CreateString(vec_s->Get(i)).o;
? fbb.CreateSharedString(vec_s->Get(i)).o
: fbb.CreateString(vec_s->Get(i)).o;
}
offset = fbb.CreateVector(elements).o;
break;
@@ -419,7 +424,7 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
std::vector<Offset<const Table *>> elements(vec->size());
for (uoffset_t i = 0; i < vec->size(); i++) {
elements[i] =
CopyTable(fbb, schema, *elemobjectdef, *vec->Get(i));
CopyTable(fbb, schema, *elemobjectdef, *vec->Get(i));
}
offset = fbb.CreateVector(elements).o;
break;
@@ -430,7 +435,7 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
auto element_size = GetTypeSize(element_base_type);
if (elemobjectdef && elemobjectdef->is_struct())
element_size = elemobjectdef->bytesize();
fbb.StartVector(element_size, vec->size());
fbb.StartVector(vec->size(), element_size);
fbb.PushBytes(vec->Data(), element_size * vec->size());
offset = fbb.EndVector(vec->size());
break;
@@ -441,14 +446,11 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
default: // Scalars.
break;
}
if (offset) {
offsets.push_back(offset);
}
if (offset) { offsets.push_back(offset); }
}
// Now we can build the actual table from either offsets or scalar data.
auto start = objectdef.is_struct()
? fbb.StartStruct(objectdef.minalign())
: fbb.StartTable();
auto start = objectdef.is_struct() ? fbb.StartStruct(objectdef.minalign())
: fbb.StartTable();
size_t offset_idx = 0;
for (auto it = fielddefs->begin(); it != fielddefs->end(); ++it) {
auto &fielddef = **it;
@@ -469,7 +471,7 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
case reflection::Vector:
fbb.AddOffset(fielddef.offset(), Offset<void>(offsets[offset_idx++]));
break;
default: { // Scalars.
default: { // Scalars.
auto size = GetTypeSize(base_type);
CopyInline(fbb, fielddef, table, size, size);
break;
@@ -487,51 +489,40 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
bool VerifyStruct(flatbuffers::Verifier &v,
const flatbuffers::Table &parent_table,
voffset_t field_offset,
const reflection::Object &obj,
voffset_t field_offset, const reflection::Object &obj,
bool required) {
auto offset = parent_table.GetOptionalFieldOffset(field_offset);
if (required && !offset) {
return false;
}
if (required && !offset) { return false; }
return !offset || v.Verify(reinterpret_cast<const uint8_t*>(&parent_table)
+ offset, obj.bytesize());
return !offset ||
v.Verify(reinterpret_cast<const uint8_t *>(&parent_table) + offset,
obj.bytesize());
}
bool VerifyVectorOfStructs(flatbuffers::Verifier &v,
const flatbuffers::Table &parent_table,
voffset_t field_offset,
const reflection::Object &obj,
bool required) {
auto p = parent_table.GetPointer<const uint8_t*>(field_offset);
const uint8_t* end;
if (required && !p) {
return false;
}
const reflection::Object &obj, bool required) {
auto p = parent_table.GetPointer<const uint8_t *>(field_offset);
const uint8_t *end;
if (required && !p) { return false; }
return !p || v.VerifyVector(p, obj.bytesize(), &end);
}
// forward declare to resolve cyclic deps between VerifyObject and VerifyVector
bool VerifyObject(flatbuffers::Verifier &v,
const reflection::Schema &schema,
bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
const reflection::Object &obj,
const flatbuffers::Table *table,
bool isRequired);
const flatbuffers::Table *table, bool required);
bool VerifyVector(flatbuffers::Verifier &v,
const reflection::Schema &schema,
bool VerifyVector(flatbuffers::Verifier &v, const reflection::Schema &schema,
const flatbuffers::Table &table,
const reflection::Field &vec_field) {
assert(vec_field.type()->base_type() == reflection::Vector);
if (!table.VerifyField<uoffset_t>(v, vec_field.offset()))
return false;
if (!table.VerifyField<uoffset_t>(v, vec_field.offset())) return false;
switch (vec_field.type()->element()) {
case reflection::None:
assert(false);
break;
case reflection::None: assert(false); break;
case reflection::UType:
return v.Verify(flatbuffers::GetFieldV<uint8_t>(table, vec_field));
case reflection::Bool:
@@ -553,17 +544,15 @@ bool VerifyVector(flatbuffers::Verifier &v,
return v.Verify(flatbuffers::GetFieldV<double>(table, vec_field));
case reflection::String: {
auto vecString =
flatbuffers::GetFieldV<flatbuffers::
Offset<flatbuffers::String>>(table, vec_field);
flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
table, vec_field);
if (v.Verify(vecString) && v.VerifyVectorOfStrings(vecString)) {
return true;
} else {
return false;
}
}
case reflection::Vector:
assert(false);
break;
case reflection::Vector: assert(false); break;
case reflection::Obj: {
auto obj = schema.objects()->Get(vec_field.type()->index());
if (obj->is_struct()) {
@@ -573,10 +562,9 @@ bool VerifyVector(flatbuffers::Verifier &v,
}
} else {
auto vec =
flatbuffers::GetFieldV<flatbuffers::
Offset<flatbuffers::Table>>(table, vec_field);
if (!v.Verify(vec))
return false;
flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::Table>>(
table, vec_field);
if (!v.Verify(vec)) return false;
if (vec) {
for (uoffset_t j = 0; j < vec->size(); j++) {
if (!VerifyObject(v, schema, *obj, vec->Get(j), true)) {
@@ -587,22 +575,16 @@ bool VerifyVector(flatbuffers::Verifier &v,
}
return true;
}
case reflection::Union:
assert(false);
break;
default:
assert(false);
break;
case reflection::Union: assert(false); break;
default: assert(false); break;
}
return false;
}
bool VerifyObject(flatbuffers::Verifier &v,
const reflection::Schema &schema,
bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
const reflection::Object &obj,
const flatbuffers::Table *table,
bool required) {
const flatbuffers::Table *table, bool required) {
if (!table) {
if (!required)
return true;
@@ -610,47 +592,37 @@ bool VerifyObject(flatbuffers::Verifier &v,
return false;
}
if (!table->VerifyTableStart(v))
return false;
if (!table->VerifyTableStart(v)) return false;
for (uoffset_t i = 0; i < obj.fields()->size(); i++) {
auto field_def = obj.fields()->Get(i);
switch (field_def->type()->base_type()) {
case reflection::None:
assert(false);
break;
case reflection::None: assert(false); break;
case reflection::UType:
if (!table->VerifyField<uint8_t>(v, field_def->offset()))
return false;
if (!table->VerifyField<uint8_t>(v, field_def->offset())) return false;
break;
case reflection::Bool:
case reflection::Byte:
case reflection::UByte:
if (!table->VerifyField<int8_t>(v, field_def->offset()))
return false;
if (!table->VerifyField<int8_t>(v, field_def->offset())) return false;
break;
case reflection::Short:
case reflection::UShort:
if (!table->VerifyField<int16_t>(v, field_def->offset()))
return false;
if (!table->VerifyField<int16_t>(v, field_def->offset())) return false;
break;
case reflection::Int:
case reflection::UInt:
if (!table->VerifyField<int32_t>(v, field_def->offset()))
return false;
if (!table->VerifyField<int32_t>(v, field_def->offset())) return false;
break;
case reflection::Long:
case reflection::ULong:
if (!table->VerifyField<int64_t>(v, field_def->offset()))
return false;
if (!table->VerifyField<int64_t>(v, field_def->offset())) return false;
break;
case reflection::Float:
if (!table->VerifyField<float>(v, field_def->offset()))
return false;
if (!table->VerifyField<float>(v, field_def->offset())) return false;
break;
case reflection::Double:
if (!table->VerifyField<double>(v, field_def->offset()))
return false;
if (!table->VerifyField<double>(v, field_def->offset())) return false;
break;
case reflection::String:
if (!table->VerifyField<uoffset_t>(v, field_def->offset()) ||
@@ -659,8 +631,7 @@ bool VerifyObject(flatbuffers::Verifier &v,
}
break;
case reflection::Vector:
if (!VerifyVector(v, schema, *table, *field_def))
return false;
if (!VerifyVector(v, schema, *table, *field_def)) return false;
break;
case reflection::Obj: {
auto child_obj = schema.objects()->Get(field_def->type()->index());
@@ -694,22 +665,17 @@ bool VerifyObject(flatbuffers::Verifier &v,
}
break;
}
default:
assert(false);
break;
default: assert(false); break;
}
}
if (!v.EndTable())
return false;
if (!v.EndTable()) return false;
return true;
}
bool Verify(const reflection::Schema &schema,
const reflection::Object &root,
const uint8_t *buf,
size_t length) {
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
const uint8_t *buf, size_t length) {
Verifier v(buf, length);
return VerifyObject(v, schema, root, flatbuffers::GetAnyRoot(buf), true);
}

View File

@@ -57,6 +57,7 @@ bool FileExists(const char *name) {
}
bool DirExists(const char *name) {
// clang-format off
#ifdef _WIN32
#define flatbuffers_stat _stat
#define FLATBUFFERS_S_IFDIR _S_IFDIR
@@ -64,6 +65,7 @@ bool DirExists(const char *name) {
#define flatbuffers_stat stat
#define FLATBUFFERS_S_IFDIR S_IFDIR
#endif
// clang-format on
struct flatbuffers_stat file_info;
if (flatbuffers_stat(name, &file_info) != 0) return false;
return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0;
@@ -78,8 +80,8 @@ LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
FileExistsFunction SetFileExistsFunction(
FileExistsFunction file_exists_function) {
FileExistsFunction previous_function = g_file_exists_function;
g_file_exists_function = file_exists_function ?
file_exists_function : FileExistsRaw;
g_file_exists_function =
file_exists_function ? file_exists_function : FileExistsRaw;
return previous_function;
}

View File

@@ -44,8 +44,7 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutByteCannotPutAtOffsetPastLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutByte(1, 99));
}
#endif
@@ -66,8 +65,7 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortCannotPutAtOffsetPastLength()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(2, 99));
}
#endif
@@ -76,16 +74,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(0, 99));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLengthAndOffset()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(1, 99));
}
#endif
@@ -108,24 +104,21 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntCannotPutAtOffsetPastLength()
{
var buffer = new byte[4];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(0, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLengthAndOffset()
{
var buffer = new byte[4];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
#endif
@@ -152,24 +145,21 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongCannotPutAtOffsetPastLength()
{
var buffer = new byte[8];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(0, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLengthAndOffset()
{
var buffer = new byte[8];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
#endif
@@ -187,8 +177,7 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetByteChecksOffset()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(()=>uut.Get(1));
}
#endif
@@ -207,16 +196,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksOffset()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(2));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksLength()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(1));
}
#endif
@@ -237,16 +224,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksOffset()
{
var buffer = new byte[4];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(4));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksLength()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(0));
}
#endif
@@ -271,16 +256,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksOffset()
{
var buffer = new byte[8];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(8));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksLength()
{
var buffer = new byte[7];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(7);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0));
}
#endif
@@ -317,5 +300,49 @@ namespace FlatBuffers.Test
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
[FlatBuffersTestMethod]
public void ByteBuffer_ToFullArray_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.ArrayEqual(buffer, uut.ToFullArray());
}
[FlatBuffersTestMethod]
public void ByteBuffer_ToSizedArray_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.ArrayEqual(buffer, uut.ToFullArray());
}
[FlatBuffersTestMethod]
public void ByteBuffer_Duplicate_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0));
// Advance by two bytes
uut.Position = 2; uut = uut.Duplicate();
Assert.AreEqual(0x0A0B, uut.GetShort(2));
// Advance by one more byte
uut.Position = 1; uut = uut.Duplicate();
Assert.AreEqual(0x0A, uut.Get(3));
}
}
}

View File

@@ -41,6 +41,9 @@
<Compile Include="..\..\net\FlatBuffers\ByteBuffer.cs">
<Link>FlatBuffers\ByteBuffer.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\ByteBufferUtil.cs">
<Link>FlatBuffers\ByteBufferUtil.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\IFlatbufferObject.cs">
<Link>FlatBuffers\IFlatbufferObject.cs</Link>
</Compile>
@@ -68,6 +71,9 @@
<Compile Include="..\MyGame\Example\Monster.cs">
<Link>MyGame\Example\Monster.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Referrable.cs">
<Link>MyGame\Example\Referrable.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Stat.cs">
<Link>MyGame\Example\Stat.cs</Link>
</Compile>

View File

@@ -32,6 +32,12 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void CanCreateNewFlatBufferFromScratch()
{
CanCreateNewFlatBufferFromScratch(true);
CanCreateNewFlatBufferFromScratch(false);
}
private void CanCreateNewFlatBufferFromScratch(bool sizePrefix)
{
// Second, let's create a FlatBuffer from scratch in C#, and test it also.
// We use an initial size of 1 to exercise the reallocation algorithm,
@@ -95,22 +101,40 @@ namespace FlatBuffers.Test
Monster.AddTestarrayoftables(fbb, sortMons);
var mon = Monster.EndMonster(fbb);
Monster.FinishMonsterBuffer(fbb, mon);
if (sizePrefix)
{
Monster.FinishSizePrefixedMonsterBuffer(fbb, mon);
}
else
{
Monster.FinishMonsterBuffer(fbb, mon);
}
// Dump to output directory so we can inspect later, if needed
using (var ms = new MemoryStream(fbb.DataBuffer.Data, fbb.DataBuffer.Position, fbb.Offset))
using (var ms = fbb.DataBuffer.ToMemoryStream(fbb.DataBuffer.Position, fbb.Offset))
{
var data = ms.ToArray();
File.WriteAllBytes(@"Resources/monsterdata_cstest.mon",data);
string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
File.WriteAllBytes(filename, data);
}
// Remove the size prefix if necessary for further testing
ByteBuffer dataBuffer = fbb.DataBuffer;
if (sizePrefix)
{
Assert.AreEqual(ByteBufferUtil.GetSizePrefix(dataBuffer) + FlatBufferConstants.SizePrefixLength,
dataBuffer.Length - dataBuffer.Position);
dataBuffer = ByteBufferUtil.RemoveSizePrefix(dataBuffer);
}
// Now assert the buffer
TestBuffer(fbb.DataBuffer);
TestBuffer(dataBuffer);
//Attempt to mutate Monster fields and check whether the buffer has been mutated properly
// revert to original values after testing
Monster monster = Monster.GetRootAsMonster(fbb.DataBuffer);
Monster monster = Monster.GetRootAsMonster(dataBuffer);
// mana is optional and does not exist in the buffer so the mutation should fail
// the mana field should retain its default value
@@ -161,12 +185,12 @@ namespace FlatBuffers.Test
pos.MutateX(1.0f);
Assert.AreEqual(pos.X, 1.0f);
TestBuffer(fbb.DataBuffer);
TestBuffer(dataBuffer);
}
private void TestBuffer(ByteBuffer bb)
{
var monster = Monster.GetRootAsMonster(bb);
Monster monster = Monster.GetRootAsMonster(bb);
Assert.AreEqual(80, monster.Hp);
Assert.AreEqual(150, monster.Mana);

View File

@@ -33,21 +33,21 @@ namespace FlatBuffers.Test
public void TestNumbers()
{
var builder = new FlatBufferBuilder(1);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray());
builder.AddBool(true);
Assert.ArrayEqual(new byte[] { 1 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 1 }, builder.DataBuffer.ToFullArray());
builder.AddSbyte(-127);
Assert.ArrayEqual(new byte[] { 129, 1 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 129, 1 }, builder.DataBuffer.ToFullArray());
builder.AddByte(255);
Assert.ArrayEqual(new byte[] { 0, 255, 129, 1 }, builder.DataBuffer.Data); // First pad
Assert.ArrayEqual(new byte[] { 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // First pad
builder.AddShort(-32222);
Assert.ArrayEqual(new byte[] { 0, 0, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // Second pad
Assert.ArrayEqual(new byte[] { 0, 0, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // Second pad
builder.AddUshort(0xFEEE);
Assert.ArrayEqual(new byte[] { 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // no pad
Assert.ArrayEqual(new byte[] { 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // no pad
builder.AddInt(-53687092);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // third pad
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // third pad
builder.AddUint(0x98765432);
Assert.ArrayEqual(new byte[] { 0x32, 0x54, 0x76, 0x98, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // no pad
Assert.ArrayEqual(new byte[] { 0x32, 0x54, 0x76, 0x98, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // no pad
}
[FlatBuffersTestMethod]
@@ -55,11 +55,11 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.AddUlong(0x1122334455667788);
Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.ToFullArray());
builder = new FlatBufferBuilder(1);
builder.AddLong(0x1122334455667788);
Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -67,11 +67,11 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartVector(sizeof(byte), 1, 1);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.AddByte(1);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.EndVector();
Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -79,13 +79,13 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartVector(sizeof(byte), 2, 1);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.AddByte(1);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.AddByte(2);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.EndVector();
Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -93,11 +93,11 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartVector(sizeof(ushort), 1, 1);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.AddUshort(1);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.EndVector();
Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -105,13 +105,13 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartVector(sizeof(ushort), 2, 1);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray());
builder.AddUshort(0xABCD);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0xCD, 0xAB }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0xCD, 0xAB }, builder.DataBuffer.ToFullArray());
builder.AddUshort(0xDCBA);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.ToFullArray());
builder.EndVector();
Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -119,7 +119,7 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.CreateString("foo");
Assert.ArrayEqual(new byte[] { 3, 0, 0, 0, (byte)'f', (byte)'o', (byte)'o', 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 3, 0, 0, 0, (byte)'f', (byte)'o', (byte)'o', 0 }, builder.DataBuffer.ToFullArray());
builder.CreateString("moop");
Assert.ArrayEqual(new byte[]
@@ -132,7 +132,7 @@ namespace FlatBuffers.Test
0, 0, 0, 0, // zero terminator with 3 byte pad
3, 0, 0, 0,
(byte)'f', (byte)'o', (byte)'o', 0
}, builder.DataBuffer.Data);
}, builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -144,7 +144,7 @@ namespace FlatBuffers.Test
{
3, 0, 0, 0,
0x01, 0x02, 0x03, 0
}, builder.DataBuffer.Data); // No padding
}, builder.DataBuffer.ToFullArray()); // No padding
builder.CreateString("\x04\x05\x06\x07");
Assert.ArrayEqual(new byte[]
{
@@ -156,7 +156,7 @@ namespace FlatBuffers.Test
0, 0, 0, 0, // zero terminator with 3 byte pad
3, 0, 0, 0,
0x01, 0x02, 0x03, 0
}, builder.DataBuffer.Data); // No padding
}, builder.DataBuffer.ToFullArray()); // No padding
}
[FlatBuffersTestMethod]
@@ -164,14 +164,14 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartObject(0);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray());
builder.EndObject();
Assert.ArrayEqual(new byte[]
{
4, 0, 4, 0,
4, 0, 0, 0
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -179,7 +179,7 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartObject(1);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray());
builder.AddBool(0, true, false);
builder.EndObject();
Assert.ArrayEqual(new byte[]
@@ -192,7 +192,7 @@ namespace FlatBuffers.Test
0, 0, 0, // padding
1, // value 0
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -200,7 +200,7 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartObject(1);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray());
builder.AddBool(0, false, false);
builder.EndObject();
Assert.ArrayEqual(new byte[]
@@ -211,7 +211,7 @@ namespace FlatBuffers.Test
// entry 0 is not stored (trimmed end of vtable)
4, 0, 0, 0, // int32 offset for start of vtable
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -219,7 +219,7 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartObject(1);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray());
builder.AddShort(0, 0x789A, 0);
builder.EndObject();
Assert.ArrayEqual(new byte[]
@@ -232,7 +232,7 @@ namespace FlatBuffers.Test
0, 0, // padding
0x9A, 0x78, //value 0
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -240,7 +240,7 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartObject(2);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray());
builder.AddShort(0, 0x3456, 0);
builder.AddShort(1, 0x789A, 0);
builder.EndObject();
@@ -254,7 +254,7 @@ namespace FlatBuffers.Test
0x9A, 0x78, // value 1
0x56, 0x34, // value 0
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -262,7 +262,7 @@ namespace FlatBuffers.Test
{
var builder = new FlatBufferBuilder(1);
builder.StartObject(2);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray());
builder.AddShort(0, 0x3456, 0);
builder.AddBool(1, true, false);
builder.EndObject();
@@ -276,7 +276,7 @@ namespace FlatBuffers.Test
0, 1, // padding + value 1
0x56, 0x34, // value 0
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -303,7 +303,7 @@ namespace FlatBuffers.Test
4, 0, 0, 0,
0, 0, 0, 0,
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -330,7 +330,7 @@ namespace FlatBuffers.Test
0, 0, 55, 0, // value 0
0, 0, 0, 0, // length of vector (not in sctruc)
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
@@ -361,7 +361,7 @@ namespace FlatBuffers.Test
0x78, 0x56, // vector value 0
0x34, 0x12, // vector value 1
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -391,7 +391,7 @@ namespace FlatBuffers.Test
0x00, 0x00, 0x34, 0x12, // struct value 1
0x00, 0x00, 0x00, 55, // struct value 0
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -425,7 +425,7 @@ namespace FlatBuffers.Test
44, // vector 0, 1
33, // vector 0, 0
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -438,7 +438,7 @@ namespace FlatBuffers.Test
var off = builder.EndObject();
builder.Finish(off);
Assert.ArrayEqual(new byte[]
byte[] padded = new byte[]
{
0, 0, 0, 0,
0, 0, 0, 0,
@@ -452,8 +452,13 @@ namespace FlatBuffers.Test
66, 0, // value 1
0, 33, // value 0
},
builder.DataBuffer.Data);
};
Assert.ArrayEqual(padded, builder.DataBuffer.ToFullArray());
// no padding in sized array
byte[] unpadded = new byte[padded.Length - 12];
Buffer.BlockCopy(padded, 12, unpadded, 0, unpadded.Length);
Assert.ArrayEqual(unpadded, builder.DataBuffer.ToSizedArray());
}
[FlatBuffersTestMethod]
@@ -504,7 +509,7 @@ namespace FlatBuffers.Test
44, // value 1, 0
33,
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
[FlatBuffersTestMethod]
@@ -519,7 +524,7 @@ namespace FlatBuffers.Test
var off = builder.EndObject();
builder.Finish(off);
Assert.ArrayEqual(new byte[]
byte[] padded = new byte[]
{
0, 0, 0, 0,
0, 0, 0, 0,
@@ -546,8 +551,61 @@ namespace FlatBuffers.Test
1, 1, 1, 1, // values
1, 1, 1, 1,
},
builder.DataBuffer.Data);
};
Assert.ArrayEqual(padded, builder.DataBuffer.ToFullArray());
// no padding in sized array
byte[] unpadded = new byte[padded.Length - 28];
Buffer.BlockCopy(padded, 28, unpadded, 0, unpadded.Length);
Assert.ArrayEqual(unpadded, builder.DataBuffer.ToSizedArray());
}
[FlatBuffersTestMethod]
public void TestBunchOfBoolsSizePrefixed()
{
var builder = new FlatBufferBuilder(1);
builder.StartObject(8);
for (var i = 0; i < 8; i++)
{
builder.AddBool(i, true, false);
}
var off = builder.EndObject();
builder.FinishSizePrefixed(off);
byte[] padded = new byte[]
{
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0, // padding to 64 bytes
36, 0, 0, 0, // size prefix
24, 0, 0, 0, // root of table, pointing to vtable offset (obj0)
20, 0, // vtable bytes
12, 0, // object length
11, 0, // start of value 0
10, 0, // start of value 1
9, 0, // start of value 2
8, 0, // start of value 3
7, 0, // start of value 4
6, 0, // start of value 5
5, 0, // start of value 6
4, 0, // start of value 7
20, 0, 0, 0, // int32 offset for start of vtable
1, 1, 1, 1, // values
1, 1, 1, 1,
};
Assert.ArrayEqual(padded, builder.DataBuffer.ToFullArray());
// no padding in sized array
byte[] unpadded = new byte[padded.Length - 24];
Buffer.BlockCopy(padded, 24, unpadded, 0, unpadded.Length);
Assert.ArrayEqual(unpadded, builder.DataBuffer.ToSizedArray());
}
[FlatBuffersTestMethod]
@@ -569,7 +627,7 @@ namespace FlatBuffers.Test
0, 0, 128, 63, // value
},
builder.DataBuffer.Data);
builder.DataBuffer.ToFullArray());
}
private void CheckObjects(int fieldCount, int objectCount)

View File

@@ -2,8 +2,22 @@
# Testing C# on Linux using Mono.
mcs -out:fbnettest.exe ../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs
./fbnettest.exe
mcs -debug -out:./fbnettest.exe \
../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/*.cs \
FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs
mono --debug ./fbnettest.exe
rm fbnettest.exe
rm Resources/monsterdata_cstest.mon
rm Resources/monsterdata_cstest_sp.mon
# Repeat with unsafe versions
mcs -debug -out:./fbnettest.exe \
-unsafe -d:UNSAFE_BYTEBUFFER \
../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/*.cs \
FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs
mono --debug ./fbnettest.exe
rm fbnettest.exe
rm Resources/monsterdata_cstest.mon
rm Resources/monsterdata_cstest_sp.mon

View File

@@ -21,6 +21,8 @@ import java.nio.channels.FileChannel;
import MyGame.Example.*;
import NamespaceA.*;
import NamespaceA.NamespaceB.*;
import com.google.flatbuffers.ByteBufferUtil;
import static com.google.flatbuffers.Constants.*;
import com.google.flatbuffers.FlatBufferBuilder;
class JavaTest {
@@ -53,7 +55,8 @@ class JavaTest {
// better for performance.
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
TestBuilderBasics(fbb);
TestBuilderBasics(fbb, true);
TestBuilderBasics(fbb, false);
TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer());
@@ -244,14 +247,14 @@ class JavaTest {
FlatBufferBuilder fbb = new FlatBufferBuilder(1, new MappedByteBufferFactory());
TestBuilderBasics(fbb);
TestBuilderBasics(fbb, false);
}
static void TestSizedInputStream() {
// Test on default FlatBufferBuilder that uses HeapByteBuffer
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
TestBuilderBasics(fbb);
TestBuilderBasics(fbb, false);
InputStream in = fbb.sizedInputStream();
byte[] array = fbb.sizedByteArray();
@@ -271,7 +274,7 @@ class JavaTest {
TestEq(count, array.length);
}
static void TestBuilderBasics(FlatBufferBuilder fbb) {
static void TestBuilderBasics(FlatBufferBuilder fbb, boolean sizePrefix) {
int[] names = {fbb.createString("Frodo"), fbb.createString("Barney"), fbb.createString("Wilma")};
int[] off = new int[3];
Monster.startMonster(fbb);
@@ -321,7 +324,11 @@ class JavaTest {
Monster.addTestarrayoftables(fbb, sortMons);
int mon = Monster.endMonster(fbb);
Monster.finishMonsterBuffer(fbb, mon);
if (sizePrefix) {
Monster.finishSizePrefixedMonsterBuffer(fbb, mon);
} else {
Monster.finishMonsterBuffer(fbb, mon);
}
// Write the result to a file for debugging purposes:
// Note that the binaries are not necessarily identical, since the JSON
@@ -329,7 +336,8 @@ class JavaTest {
// Java code. They are functionally equivalent though.
try {
FileChannel fc = new FileOutputStream("monsterdata_java_wire.mon").getChannel();
String filename = "monsterdata_java_wire" + (sizePrefix ? "_sp" : "") + ".mon";
FileChannel fc = new FileOutputStream(filename).getChannel();
fc.write(fbb.dataBuffer().duplicate());
fc.close();
} catch(java.io.IOException e) {
@@ -338,18 +346,24 @@ class JavaTest {
}
// Test it:
TestExtendedBuffer(fbb.dataBuffer());
ByteBuffer dataBuffer = fbb.dataBuffer();
if (sizePrefix) {
TestEq(ByteBufferUtil.getSizePrefix(dataBuffer) + SIZE_PREFIX_LENGTH,
dataBuffer.remaining());
dataBuffer = ByteBufferUtil.removeSizePrefix(dataBuffer);
}
TestExtendedBuffer(dataBuffer);
// Make sure it also works with read only ByteBuffers. This is slower,
// since creating strings incurs an additional copy
// (see Table.__string).
TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer());
TestExtendedBuffer(dataBuffer.asReadOnlyBuffer());
TestEnums();
//Attempt to mutate Monster fields and check whether the buffer has been mutated properly
// revert to original values after testing
Monster monster = Monster.getRootAsMonster(fbb.dataBuffer());
Monster monster = Monster.getRootAsMonster(dataBuffer);
// mana is optional and does not exist in the buffer so the mutation should fail
// the mana field should retain its default value

View File

@@ -98,8 +98,32 @@ public struct Monster : IFlatbufferObject
public ArraySegment<byte>? GetVectorOfDoublesBytes() { return __p.__vector_as_arraysegment(70); }
public bool MutateVectorOfDoubles(int j, double vector_of_doubles) { int o = __p.__offset(70); if (o != 0) { __p.bb.PutDouble(__p.__vector(o) + j * 8, vector_of_doubles); return true; } else { return false; } }
public MyGame.InParentNamespace? ParentNamespaceTest { get { int o = __p.__offset(72); return o != 0 ? (MyGame.InParentNamespace?)(new MyGame.InParentNamespace()).__assign(__p.__indirect(o + __p.bb_pos), __p.bb) : null; } }
public Referrable? VectorOfReferrables(int j) { int o = __p.__offset(74); return o != 0 ? (Referrable?)(new Referrable()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; }
public int VectorOfReferrablesLength { get { int o = __p.__offset(74); return o != 0 ? __p.__vector_len(o) : 0; } }
public Referrable? VectorOfReferrablesByKey(ulong key) { int o = __p.__offset(74); return o != 0 ? Referrable.__lookup_by_key(__p.__vector(o), key, __p.bb) : null; }
public ulong SingleWeakReference { get { int o = __p.__offset(76); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } }
public bool MutateSingleWeakReference(ulong single_weak_reference) { int o = __p.__offset(76); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, single_weak_reference); return true; } else { return false; } }
public ulong VectorOfWeakReferences(int j) { int o = __p.__offset(78); return o != 0 ? __p.bb.GetUlong(__p.__vector(o) + j * 8) : (ulong)0; }
public int VectorOfWeakReferencesLength { get { int o = __p.__offset(78); return o != 0 ? __p.__vector_len(o) : 0; } }
public ArraySegment<byte>? GetVectorOfWeakReferencesBytes() { return __p.__vector_as_arraysegment(78); }
public bool MutateVectorOfWeakReferences(int j, ulong vector_of_weak_references) { int o = __p.__offset(78); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_weak_references); return true; } else { return false; } }
public Referrable? VectorOfStrongReferrables(int j) { int o = __p.__offset(80); return o != 0 ? (Referrable?)(new Referrable()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; }
public int VectorOfStrongReferrablesLength { get { int o = __p.__offset(80); return o != 0 ? __p.__vector_len(o) : 0; } }
public Referrable? VectorOfStrongReferrablesByKey(ulong key) { int o = __p.__offset(80); return o != 0 ? Referrable.__lookup_by_key(__p.__vector(o), key, __p.bb) : null; }
public ulong CoOwningReference { get { int o = __p.__offset(82); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } }
public bool MutateCoOwningReference(ulong co_owning_reference) { int o = __p.__offset(82); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, co_owning_reference); return true; } else { return false; } }
public ulong VectorOfCoOwningReferences(int j) { int o = __p.__offset(84); return o != 0 ? __p.bb.GetUlong(__p.__vector(o) + j * 8) : (ulong)0; }
public int VectorOfCoOwningReferencesLength { get { int o = __p.__offset(84); return o != 0 ? __p.__vector_len(o) : 0; } }
public ArraySegment<byte>? GetVectorOfCoOwningReferencesBytes() { return __p.__vector_as_arraysegment(84); }
public bool MutateVectorOfCoOwningReferences(int j, ulong vector_of_co_owning_references) { int o = __p.__offset(84); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_co_owning_references); return true; } else { return false; } }
public ulong NonOwningReference { get { int o = __p.__offset(86); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } }
public bool MutateNonOwningReference(ulong non_owning_reference) { int o = __p.__offset(86); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, non_owning_reference); return true; } else { return false; } }
public ulong VectorOfNonOwningReferences(int j) { int o = __p.__offset(88); return o != 0 ? __p.bb.GetUlong(__p.__vector(o) + j * 8) : (ulong)0; }
public int VectorOfNonOwningReferencesLength { get { int o = __p.__offset(88); return o != 0 ? __p.__vector_len(o) : 0; } }
public ArraySegment<byte>? GetVectorOfNonOwningReferencesBytes() { return __p.__vector_as_arraysegment(88); }
public bool MutateVectorOfNonOwningReferences(int j, ulong vector_of_non_owning_references) { int o = __p.__offset(88); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } }
public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(35); }
public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(43); }
public static void AddPos(FlatBufferBuilder builder, Offset<Vec3> posOffset) { builder.AddStruct(0, posOffset.Value, 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); }
@@ -155,12 +179,31 @@ public struct Monster : IFlatbufferObject
public static VectorOffset CreateVectorOfDoublesVector(FlatBufferBuilder builder, double[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddDouble(data[i]); return builder.EndVector(); }
public static void StartVectorOfDoublesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); }
public static void AddParentNamespaceTest(FlatBufferBuilder builder, Offset<MyGame.InParentNamespace> parentNamespaceTestOffset) { builder.AddOffset(34, parentNamespaceTestOffset.Value, 0); }
public static void AddVectorOfReferrables(FlatBufferBuilder builder, VectorOffset vectorOfReferrablesOffset) { builder.AddOffset(35, vectorOfReferrablesOffset.Value, 0); }
public static VectorOffset CreateVectorOfReferrablesVector(FlatBufferBuilder builder, Offset<Referrable>[] data) { builder.StartVector(4, data.Length, 4); for (int i = data.Length - 1; i >= 0; i--) builder.AddOffset(data[i].Value); return builder.EndVector(); }
public static void StartVectorOfReferrablesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 4); }
public static void AddSingleWeakReference(FlatBufferBuilder builder, ulong singleWeakReference) { builder.AddUlong(36, singleWeakReference, 0); }
public static void AddVectorOfWeakReferences(FlatBufferBuilder builder, VectorOffset vectorOfWeakReferencesOffset) { builder.AddOffset(37, vectorOfWeakReferencesOffset.Value, 0); }
public static VectorOffset CreateVectorOfWeakReferencesVector(FlatBufferBuilder builder, ulong[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddUlong(data[i]); return builder.EndVector(); }
public static void StartVectorOfWeakReferencesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); }
public static void AddVectorOfStrongReferrables(FlatBufferBuilder builder, VectorOffset vectorOfStrongReferrablesOffset) { builder.AddOffset(38, vectorOfStrongReferrablesOffset.Value, 0); }
public static VectorOffset CreateVectorOfStrongReferrablesVector(FlatBufferBuilder builder, Offset<Referrable>[] data) { builder.StartVector(4, data.Length, 4); for (int i = data.Length - 1; i >= 0; i--) builder.AddOffset(data[i].Value); return builder.EndVector(); }
public static void StartVectorOfStrongReferrablesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 4); }
public static void AddCoOwningReference(FlatBufferBuilder builder, ulong coOwningReference) { builder.AddUlong(39, coOwningReference, 0); }
public static void AddVectorOfCoOwningReferences(FlatBufferBuilder builder, VectorOffset vectorOfCoOwningReferencesOffset) { builder.AddOffset(40, vectorOfCoOwningReferencesOffset.Value, 0); }
public static VectorOffset CreateVectorOfCoOwningReferencesVector(FlatBufferBuilder builder, ulong[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddUlong(data[i]); return builder.EndVector(); }
public static void StartVectorOfCoOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); }
public static void AddNonOwningReference(FlatBufferBuilder builder, ulong nonOwningReference) { builder.AddUlong(41, nonOwningReference, 0); }
public static void AddVectorOfNonOwningReferences(FlatBufferBuilder builder, VectorOffset vectorOfNonOwningReferencesOffset) { builder.AddOffset(42, vectorOfNonOwningReferencesOffset.Value, 0); }
public static VectorOffset CreateVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, ulong[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddUlong(data[i]); return builder.EndVector(); }
public static void StartVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); }
public static Offset<Monster> EndMonster(FlatBufferBuilder builder) {
int o = builder.EndObject();
builder.Required(o, 10); // name
return new Offset<Monster>(o);
}
public static void FinishMonsterBuffer(FlatBufferBuilder builder, Offset<Monster> offset) { builder.Finish(offset.Value, "MONS"); }
public static void FinishSizePrefixedMonsterBuffer(FlatBufferBuilder builder, Offset<Monster> offset) { builder.FinishSizePrefixed(offset.Value, "MONS"); }
public static VectorOffset CreateSortedVectorOfMonster(FlatBufferBuilder builder, Offset<Monster>[] offsets) {
Array.Sort(offsets, (Offset<Monster> o1, Offset<Monster> o2) => Table.CompareStrings(Table.__offset(10, o1.Value, builder.DataBuffer), Table.__offset(10, o2.Value, builder.DataBuffer), builder.DataBuffer));

View File

@@ -529,8 +529,135 @@ func (rcv *Monster) ParentNamespaceTest(obj *InParentNamespace) *InParentNamespa
return nil
}
func (rcv *Monster) VectorOfReferrables(obj *Referrable, j int) bool {
o := flatbuffers.UOffsetT(rcv._tab.Offset(74))
if o != 0 {
x := rcv._tab.Vector(o)
x += flatbuffers.UOffsetT(j) * 4
x = rcv._tab.Indirect(x)
obj.Init(rcv._tab.Bytes, x)
return true
}
return false
}
func (rcv *Monster) VectorOfReferrablesLength() int {
o := flatbuffers.UOffsetT(rcv._tab.Offset(74))
if o != 0 {
return rcv._tab.VectorLen(o)
}
return 0
}
func (rcv *Monster) SingleWeakReference() uint64 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(76))
if o != 0 {
return rcv._tab.GetUint64(o + rcv._tab.Pos)
}
return 0
}
func (rcv *Monster) MutateSingleWeakReference(n uint64) bool {
return rcv._tab.MutateUint64Slot(76, n)
}
func (rcv *Monster) VectorOfWeakReferences(j int) uint64 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(78))
if o != 0 {
a := rcv._tab.Vector(o)
return rcv._tab.GetUint64(a + flatbuffers.UOffsetT(j*8))
}
return 0
}
func (rcv *Monster) VectorOfWeakReferencesLength() int {
o := flatbuffers.UOffsetT(rcv._tab.Offset(78))
if o != 0 {
return rcv._tab.VectorLen(o)
}
return 0
}
func (rcv *Monster) VectorOfStrongReferrables(obj *Referrable, j int) bool {
o := flatbuffers.UOffsetT(rcv._tab.Offset(80))
if o != 0 {
x := rcv._tab.Vector(o)
x += flatbuffers.UOffsetT(j) * 4
x = rcv._tab.Indirect(x)
obj.Init(rcv._tab.Bytes, x)
return true
}
return false
}
func (rcv *Monster) VectorOfStrongReferrablesLength() int {
o := flatbuffers.UOffsetT(rcv._tab.Offset(80))
if o != 0 {
return rcv._tab.VectorLen(o)
}
return 0
}
func (rcv *Monster) CoOwningReference() uint64 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(82))
if o != 0 {
return rcv._tab.GetUint64(o + rcv._tab.Pos)
}
return 0
}
func (rcv *Monster) MutateCoOwningReference(n uint64) bool {
return rcv._tab.MutateUint64Slot(82, n)
}
func (rcv *Monster) VectorOfCoOwningReferences(j int) uint64 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(84))
if o != 0 {
a := rcv._tab.Vector(o)
return rcv._tab.GetUint64(a + flatbuffers.UOffsetT(j*8))
}
return 0
}
func (rcv *Monster) VectorOfCoOwningReferencesLength() int {
o := flatbuffers.UOffsetT(rcv._tab.Offset(84))
if o != 0 {
return rcv._tab.VectorLen(o)
}
return 0
}
func (rcv *Monster) NonOwningReference() uint64 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(86))
if o != 0 {
return rcv._tab.GetUint64(o + rcv._tab.Pos)
}
return 0
}
func (rcv *Monster) MutateNonOwningReference(n uint64) bool {
return rcv._tab.MutateUint64Slot(86, n)
}
func (rcv *Monster) VectorOfNonOwningReferences(j int) uint64 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(88))
if o != 0 {
a := rcv._tab.Vector(o)
return rcv._tab.GetUint64(a + flatbuffers.UOffsetT(j*8))
}
return 0
}
func (rcv *Monster) VectorOfNonOwningReferencesLength() int {
o := flatbuffers.UOffsetT(rcv._tab.Offset(88))
if o != 0 {
return rcv._tab.VectorLen(o)
}
return 0
}
func MonsterStart(builder *flatbuffers.Builder) {
builder.StartObject(35)
builder.StartObject(43)
}
func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) {
builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0)
@@ -670,6 +797,45 @@ func MonsterStartVectorOfDoublesVector(builder *flatbuffers.Builder, numElems in
func MonsterAddParentNamespaceTest(builder *flatbuffers.Builder, parentNamespaceTest flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(34, flatbuffers.UOffsetT(parentNamespaceTest), 0)
}
func MonsterAddVectorOfReferrables(builder *flatbuffers.Builder, vectorOfReferrables flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(35, flatbuffers.UOffsetT(vectorOfReferrables), 0)
}
func MonsterStartVectorOfReferrablesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT {
return builder.StartVector(4, numElems, 4)
}
func MonsterAddSingleWeakReference(builder *flatbuffers.Builder, singleWeakReference uint64) {
builder.PrependUint64Slot(36, singleWeakReference, 0)
}
func MonsterAddVectorOfWeakReferences(builder *flatbuffers.Builder, vectorOfWeakReferences flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(37, flatbuffers.UOffsetT(vectorOfWeakReferences), 0)
}
func MonsterStartVectorOfWeakReferencesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT {
return builder.StartVector(8, numElems, 8)
}
func MonsterAddVectorOfStrongReferrables(builder *flatbuffers.Builder, vectorOfStrongReferrables flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(38, flatbuffers.UOffsetT(vectorOfStrongReferrables), 0)
}
func MonsterStartVectorOfStrongReferrablesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT {
return builder.StartVector(4, numElems, 4)
}
func MonsterAddCoOwningReference(builder *flatbuffers.Builder, coOwningReference uint64) {
builder.PrependUint64Slot(39, coOwningReference, 0)
}
func MonsterAddVectorOfCoOwningReferences(builder *flatbuffers.Builder, vectorOfCoOwningReferences flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(40, flatbuffers.UOffsetT(vectorOfCoOwningReferences), 0)
}
func MonsterStartVectorOfCoOwningReferencesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT {
return builder.StartVector(8, numElems, 8)
}
func MonsterAddNonOwningReference(builder *flatbuffers.Builder, nonOwningReference uint64) {
builder.PrependUint64Slot(41, nonOwningReference, 0)
}
func MonsterAddVectorOfNonOwningReferences(builder *flatbuffers.Builder, vectorOfNonOwningReferences flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(42, flatbuffers.UOffsetT(vectorOfNonOwningReferences), 0)
}
func MonsterStartVectorOfNonOwningReferencesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT {
return builder.StartVector(8, numElems, 8)
}
func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return builder.EndObject()
}

View File

@@ -26,9 +26,11 @@ public final class Monster extends Table {
public boolean mutateHp(short hp) { int o = __offset(8); if (o != 0) { bb.putShort(o + bb_pos, hp); return true; } else { return false; } }
public String name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 10, 1); }
public int inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
public int inventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer inventoryAsByteBuffer() { return __vector_as_bytebuffer(14, 1); }
public ByteBuffer inventoryInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 14, 1); }
public boolean mutateInventory(int j, int inventory) { int o = __offset(14); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)inventory); return true; } else { return false; } }
public byte color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 8; }
public boolean mutateColor(byte color) { int o = __offset(16); if (o != 0) { bb.put(o + bb_pos, color); return true; } else { return false; } }
@@ -47,12 +49,14 @@ public final class Monster extends Table {
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.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int testarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; }
public Monster testarrayoftablesByKey(String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(__vector(o), key, bb) : null; }
public Monster testarrayoftablesByKey(String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Monster testarrayoftablesByKey(Monster obj, String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public Monster enemy() { return enemy(new Monster()); }
public Monster enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public int testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); }
public ByteBuffer testnestedflatbufferInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 30, 1); }
public Monster testnestedflatbufferAsMonster() { return testnestedflatbufferAsMonster(new Monster()); }
public Monster testnestedflatbufferAsMonster(Monster obj) { int o = __offset(30); return o != 0 ? obj.__assign(__indirect(__vector(o)), bb) : null; }
public boolean mutateTestnestedflatbuffer(int j, int testnestedflatbuffer) { int o = __offset(30); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)testnestedflatbuffer); return true; } else { return false; } }
@@ -79,6 +83,7 @@ public final class Monster extends Table {
public boolean testarrayofbools(int j) { int o = __offset(52); return o != 0 ? 0!=bb.get(__vector(o) + j * 1) : false; }
public int testarrayofboolsLength() { int o = __offset(52); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer testarrayofboolsAsByteBuffer() { return __vector_as_bytebuffer(52, 1); }
public ByteBuffer testarrayofboolsInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 52, 1); }
public boolean mutateTestarrayofbools(int j, boolean testarrayofbools) { int o = __offset(52); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)(testarrayofbools ? 1 : 0)); return true; } else { return false; } }
public float testf() { int o = __offset(54); return o != 0 ? bb.getFloat(o + bb_pos) : 3.14159f; }
public boolean mutateTestf(float testf) { int o = __offset(54); if (o != 0) { bb.putFloat(o + bb_pos, testf); return true; } else { return false; } }
@@ -94,6 +99,7 @@ public final class Monster extends Table {
public int flex(int j) { int o = __offset(64); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
public int flexLength() { int o = __offset(64); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer flexAsByteBuffer() { return __vector_as_bytebuffer(64, 1); }
public ByteBuffer flexInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 64, 1); }
public boolean mutateFlex(int j, int flex) { int o = __offset(64); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)flex); return true; } else { return false; } }
public Test test5(int j) { return test5(new Test(), j); }
public Test test5(Test obj, int j) { int o = __offset(66); return o != 0 ? obj.__assign(__vector(o) + j * 4, bb) : null; }
@@ -101,15 +107,48 @@ public final class Monster extends Table {
public long vectorOfLongs(int j) { int o = __offset(68); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
public int vectorOfLongsLength() { int o = __offset(68); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer vectorOfLongsAsByteBuffer() { return __vector_as_bytebuffer(68, 8); }
public ByteBuffer vectorOfLongsInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 68, 8); }
public boolean mutateVectorOfLongs(int j, long vector_of_longs) { int o = __offset(68); if (o != 0) { bb.putLong(__vector(o) + j * 8, vector_of_longs); return true; } else { return false; } }
public double vectorOfDoubles(int j) { int o = __offset(70); return o != 0 ? bb.getDouble(__vector(o) + j * 8) : 0; }
public int vectorOfDoublesLength() { int o = __offset(70); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer vectorOfDoublesAsByteBuffer() { return __vector_as_bytebuffer(70, 8); }
public ByteBuffer vectorOfDoublesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 70, 8); }
public boolean mutateVectorOfDoubles(int j, double vector_of_doubles) { int o = __offset(70); if (o != 0) { bb.putDouble(__vector(o) + j * 8, vector_of_doubles); return true; } else { return false; } }
public MyGame.InParentNamespace parentNamespaceTest() { return parentNamespaceTest(new MyGame.InParentNamespace()); }
public MyGame.InParentNamespace parentNamespaceTest(MyGame.InParentNamespace obj) { int o = __offset(72); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public Referrable vectorOfReferrables(int j) { return vectorOfReferrables(new Referrable(), j); }
public Referrable vectorOfReferrables(Referrable obj, int j) { int o = __offset(74); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int vectorOfReferrablesLength() { int o = __offset(74); return o != 0 ? __vector_len(o) : 0; }
public Referrable vectorOfReferrablesByKey(long key) { int o = __offset(74); return o != 0 ? Referrable.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Referrable vectorOfReferrablesByKey(Referrable obj, long key) { int o = __offset(74); return o != 0 ? Referrable.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public long singleWeakReference() { int o = __offset(76); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateSingleWeakReference(long single_weak_reference) { int o = __offset(76); if (o != 0) { bb.putLong(o + bb_pos, single_weak_reference); return true; } else { return false; } }
public long vectorOfWeakReferences(int j) { int o = __offset(78); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
public int vectorOfWeakReferencesLength() { int o = __offset(78); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer vectorOfWeakReferencesAsByteBuffer() { return __vector_as_bytebuffer(78, 8); }
public ByteBuffer vectorOfWeakReferencesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 78, 8); }
public boolean mutateVectorOfWeakReferences(int j, long vector_of_weak_references) { int o = __offset(78); if (o != 0) { bb.putLong(__vector(o) + j * 8, vector_of_weak_references); return true; } else { return false; } }
public Referrable vectorOfStrongReferrables(int j) { return vectorOfStrongReferrables(new Referrable(), j); }
public Referrable vectorOfStrongReferrables(Referrable obj, int j) { int o = __offset(80); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int vectorOfStrongReferrablesLength() { int o = __offset(80); return o != 0 ? __vector_len(o) : 0; }
public Referrable vectorOfStrongReferrablesByKey(long key) { int o = __offset(80); return o != 0 ? Referrable.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Referrable vectorOfStrongReferrablesByKey(Referrable obj, long key) { int o = __offset(80); return o != 0 ? Referrable.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public long coOwningReference() { int o = __offset(82); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateCoOwningReference(long co_owning_reference) { int o = __offset(82); if (o != 0) { bb.putLong(o + bb_pos, co_owning_reference); return true; } else { return false; } }
public long vectorOfCoOwningReferences(int j) { int o = __offset(84); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
public int vectorOfCoOwningReferencesLength() { int o = __offset(84); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer vectorOfCoOwningReferencesAsByteBuffer() { return __vector_as_bytebuffer(84, 8); }
public ByteBuffer vectorOfCoOwningReferencesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 84, 8); }
public boolean mutateVectorOfCoOwningReferences(int j, long vector_of_co_owning_references) { int o = __offset(84); if (o != 0) { bb.putLong(__vector(o) + j * 8, vector_of_co_owning_references); return true; } else { return false; } }
public long nonOwningReference() { int o = __offset(86); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateNonOwningReference(long non_owning_reference) { int o = __offset(86); if (o != 0) { bb.putLong(o + bb_pos, non_owning_reference); return true; } else { return false; } }
public long vectorOfNonOwningReferences(int j) { int o = __offset(88); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
public int vectorOfNonOwningReferencesLength() { int o = __offset(88); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer vectorOfNonOwningReferencesAsByteBuffer() { return __vector_as_bytebuffer(88, 8); }
public ByteBuffer vectorOfNonOwningReferencesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 88, 8); }
public boolean mutateVectorOfNonOwningReferences(int j, long vector_of_non_owning_references) { int o = __offset(88); if (o != 0) { bb.putLong(__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } }
public static void startMonster(FlatBufferBuilder builder) { builder.startObject(35); }
public static void startMonster(FlatBufferBuilder builder) { builder.startObject(43); }
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); }
@@ -165,17 +204,36 @@ public final class Monster extends Table {
public static int createVectorOfDoublesVector(FlatBufferBuilder builder, double[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addDouble(data[i]); return builder.endVector(); }
public static void startVectorOfDoublesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); }
public static void addParentNamespaceTest(FlatBufferBuilder builder, int parentNamespaceTestOffset) { builder.addOffset(34, parentNamespaceTestOffset, 0); }
public static void addVectorOfReferrables(FlatBufferBuilder builder, int vectorOfReferrablesOffset) { builder.addOffset(35, vectorOfReferrablesOffset, 0); }
public static int createVectorOfReferrablesVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
public static void startVectorOfReferrablesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
public static void addSingleWeakReference(FlatBufferBuilder builder, long singleWeakReference) { builder.addLong(36, singleWeakReference, 0L); }
public static void addVectorOfWeakReferences(FlatBufferBuilder builder, int vectorOfWeakReferencesOffset) { builder.addOffset(37, vectorOfWeakReferencesOffset, 0); }
public static int createVectorOfWeakReferencesVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); }
public static void startVectorOfWeakReferencesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); }
public static void addVectorOfStrongReferrables(FlatBufferBuilder builder, int vectorOfStrongReferrablesOffset) { builder.addOffset(38, vectorOfStrongReferrablesOffset, 0); }
public static int createVectorOfStrongReferrablesVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
public static void startVectorOfStrongReferrablesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
public static void addCoOwningReference(FlatBufferBuilder builder, long coOwningReference) { builder.addLong(39, coOwningReference, 0L); }
public static void addVectorOfCoOwningReferences(FlatBufferBuilder builder, int vectorOfCoOwningReferencesOffset) { builder.addOffset(40, vectorOfCoOwningReferencesOffset, 0); }
public static int createVectorOfCoOwningReferencesVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); }
public static void startVectorOfCoOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); }
public static void addNonOwningReference(FlatBufferBuilder builder, long nonOwningReference) { builder.addLong(41, nonOwningReference, 0L); }
public static void addVectorOfNonOwningReferences(FlatBufferBuilder builder, int vectorOfNonOwningReferencesOffset) { builder.addOffset(42, vectorOfNonOwningReferencesOffset, 0); }
public static int createVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); }
public static void startVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); }
public static int endMonster(FlatBufferBuilder builder) {
int o = builder.endObject();
builder.required(o, 10); // name
return o;
}
public static void finishMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset, "MONS"); }
public static void finishSizePrefixedMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset, "MONS"); }
@Override
protected int keysCompare(Integer o1, Integer o2, ByteBuffer _bb) { return compareStrings(__offset(10, o1, _bb), __offset(10, o2, _bb), _bb); }
public static Monster __lookup_by_key(int vectorLocation, String key, ByteBuffer bb) {
public static Monster __lookup_by_key(Monster obj, int vectorLocation, String key, ByteBuffer bb) {
byte[] byteKey = key.getBytes(Table.UTF8_CHARSET.get());
int span = bb.getInt(vectorLocation - 4);
int start = 0;
@@ -190,7 +248,7 @@ public final class Monster extends Table {
start += middle;
span -= middle;
} else {
return new Monster().__assign(tableOffset, bb);
return (obj == null ? new Monster() : obj).__assign(tableOffset, bb);
}
}
return null;

View File

@@ -489,22 +489,144 @@ class Monster extends Table
return $o != 0 ? $obj->init($this->__indirect($o + $this->bb_pos), $this->bb) : 0;
}
/**
* @returnVectorOffset
*/
public function getVectorOfReferrables($j)
{
$o = $this->__offset(74);
$obj = new Referrable();
return $o != 0 ? $obj->init($this->__indirect($this->__vector($o) + $j * 4), $this->bb) : null;
}
/**
* @return int
*/
public function getVectorOfReferrablesLength()
{
$o = $this->__offset(74);
return $o != 0 ? $this->__vector_len($o) : 0;
}
/**
* @return ulong
*/
public function getSingleWeakReference()
{
$o = $this->__offset(76);
return $o != 0 ? $this->bb->getUlong($o + $this->bb_pos) : 0;
}
/**
* @param int offset
* @return ulong
*/
public function getVectorOfWeakReferences($j)
{
$o = $this->__offset(78);
return $o != 0 ? $this->bb->getUlong($this->__vector($o) + $j * 8) : 0;
}
/**
* @return int
*/
public function getVectorOfWeakReferencesLength()
{
$o = $this->__offset(78);
return $o != 0 ? $this->__vector_len($o) : 0;
}
/**
* @returnVectorOffset
*/
public function getVectorOfStrongReferrables($j)
{
$o = $this->__offset(80);
$obj = new Referrable();
return $o != 0 ? $obj->init($this->__indirect($this->__vector($o) + $j * 4), $this->bb) : null;
}
/**
* @return int
*/
public function getVectorOfStrongReferrablesLength()
{
$o = $this->__offset(80);
return $o != 0 ? $this->__vector_len($o) : 0;
}
/**
* @return ulong
*/
public function getCoOwningReference()
{
$o = $this->__offset(82);
return $o != 0 ? $this->bb->getUlong($o + $this->bb_pos) : 0;
}
/**
* @param int offset
* @return ulong
*/
public function getVectorOfCoOwningReferences($j)
{
$o = $this->__offset(84);
return $o != 0 ? $this->bb->getUlong($this->__vector($o) + $j * 8) : 0;
}
/**
* @return int
*/
public function getVectorOfCoOwningReferencesLength()
{
$o = $this->__offset(84);
return $o != 0 ? $this->__vector_len($o) : 0;
}
/**
* @return ulong
*/
public function getNonOwningReference()
{
$o = $this->__offset(86);
return $o != 0 ? $this->bb->getUlong($o + $this->bb_pos) : 0;
}
/**
* @param int offset
* @return ulong
*/
public function getVectorOfNonOwningReferences($j)
{
$o = $this->__offset(88);
return $o != 0 ? $this->bb->getUlong($this->__vector($o) + $j * 8) : 0;
}
/**
* @return int
*/
public function getVectorOfNonOwningReferencesLength()
{
$o = $this->__offset(88);
return $o != 0 ? $this->__vector_len($o) : 0;
}
/**
* @param FlatBufferBuilder $builder
* @return void
*/
public static function startMonster(FlatBufferBuilder $builder)
{
$builder->StartObject(35);
$builder->StartObject(43);
}
/**
* @param FlatBufferBuilder $builder
* @return Monster
*/
public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf, $testf2, $testf3, $testarrayofstring2, $testarrayofsortedstruct, $flex, $test5, $vector_of_longs, $vector_of_doubles, $parent_namespace_test)
public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf, $testf2, $testf3, $testarrayofstring2, $testarrayofsortedstruct, $flex, $test5, $vector_of_longs, $vector_of_doubles, $parent_namespace_test, $vector_of_referrables, $single_weak_reference, $vector_of_weak_references, $vector_of_strong_referrables, $co_owning_reference, $vector_of_co_owning_references, $non_owning_reference, $vector_of_non_owning_references)
{
$builder->startObject(35);
$builder->startObject(43);
self::addPos($builder, $pos);
self::addMana($builder, $mana);
self::addHp($builder, $hp);
@@ -539,6 +661,14 @@ class Monster extends Table
self::addVectorOfLongs($builder, $vector_of_longs);
self::addVectorOfDoubles($builder, $vector_of_doubles);
self::addParentNamespaceTest($builder, $parent_namespace_test);
self::addVectorOfReferrables($builder, $vector_of_referrables);
self::addSingleWeakReference($builder, $single_weak_reference);
self::addVectorOfWeakReferences($builder, $vector_of_weak_references);
self::addVectorOfStrongReferrables($builder, $vector_of_strong_referrables);
self::addCoOwningReference($builder, $co_owning_reference);
self::addVectorOfCoOwningReferences($builder, $vector_of_co_owning_references);
self::addNonOwningReference($builder, $non_owning_reference);
self::addVectorOfNonOwningReferences($builder, $vector_of_non_owning_references);
$o = $builder->endObject();
$builder->required($o, 10); // name
return $o;
@@ -1167,6 +1297,206 @@ class Monster extends Table
$builder->addOffsetX(34, $parentNamespaceTest, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param VectorOffset
* @return void
*/
public static function addVectorOfReferrables(FlatBufferBuilder $builder, $vectorOfReferrables)
{
$builder->addOffsetX(35, $vectorOfReferrables, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param array offset array
* @return int vector offset
*/
public static function createVectorOfReferrablesVector(FlatBufferBuilder $builder, array $data)
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
}
return $builder->endVector();
}
/**
* @param FlatBufferBuilder $builder
* @param int $numElems
* @return void
*/
public static function startVectorOfReferrablesVector(FlatBufferBuilder $builder, $numElems)
{
$builder->startVector(4, $numElems, 4);
}
/**
* @param FlatBufferBuilder $builder
* @param ulong
* @return void
*/
public static function addSingleWeakReference(FlatBufferBuilder $builder, $singleWeakReference)
{
$builder->addUlongX(36, $singleWeakReference, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param VectorOffset
* @return void
*/
public static function addVectorOfWeakReferences(FlatBufferBuilder $builder, $vectorOfWeakReferences)
{
$builder->addOffsetX(37, $vectorOfWeakReferences, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param array offset array
* @return int vector offset
*/
public static function createVectorOfWeakReferencesVector(FlatBufferBuilder $builder, array $data)
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addUlong($data[$i]);
}
return $builder->endVector();
}
/**
* @param FlatBufferBuilder $builder
* @param int $numElems
* @return void
*/
public static function startVectorOfWeakReferencesVector(FlatBufferBuilder $builder, $numElems)
{
$builder->startVector(8, $numElems, 8);
}
/**
* @param FlatBufferBuilder $builder
* @param VectorOffset
* @return void
*/
public static function addVectorOfStrongReferrables(FlatBufferBuilder $builder, $vectorOfStrongReferrables)
{
$builder->addOffsetX(38, $vectorOfStrongReferrables, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param array offset array
* @return int vector offset
*/
public static function createVectorOfStrongReferrablesVector(FlatBufferBuilder $builder, array $data)
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
}
return $builder->endVector();
}
/**
* @param FlatBufferBuilder $builder
* @param int $numElems
* @return void
*/
public static function startVectorOfStrongReferrablesVector(FlatBufferBuilder $builder, $numElems)
{
$builder->startVector(4, $numElems, 4);
}
/**
* @param FlatBufferBuilder $builder
* @param ulong
* @return void
*/
public static function addCoOwningReference(FlatBufferBuilder $builder, $coOwningReference)
{
$builder->addUlongX(39, $coOwningReference, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param VectorOffset
* @return void
*/
public static function addVectorOfCoOwningReferences(FlatBufferBuilder $builder, $vectorOfCoOwningReferences)
{
$builder->addOffsetX(40, $vectorOfCoOwningReferences, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param array offset array
* @return int vector offset
*/
public static function createVectorOfCoOwningReferencesVector(FlatBufferBuilder $builder, array $data)
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addUlong($data[$i]);
}
return $builder->endVector();
}
/**
* @param FlatBufferBuilder $builder
* @param int $numElems
* @return void
*/
public static function startVectorOfCoOwningReferencesVector(FlatBufferBuilder $builder, $numElems)
{
$builder->startVector(8, $numElems, 8);
}
/**
* @param FlatBufferBuilder $builder
* @param ulong
* @return void
*/
public static function addNonOwningReference(FlatBufferBuilder $builder, $nonOwningReference)
{
$builder->addUlongX(41, $nonOwningReference, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param VectorOffset
* @return void
*/
public static function addVectorOfNonOwningReferences(FlatBufferBuilder $builder, $vectorOfNonOwningReferences)
{
$builder->addOffsetX(42, $vectorOfNonOwningReferences, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param array offset array
* @return int vector offset
*/
public static function createVectorOfNonOwningReferencesVector(FlatBufferBuilder $builder, array $data)
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addUlong($data[$i]);
}
return $builder->endVector();
}
/**
* @param FlatBufferBuilder $builder
* @param int $numElems
* @return void
*/
public static function startVectorOfNonOwningReferencesVector(FlatBufferBuilder $builder, $numElems)
{
$builder->startVector(8, $numElems, 8);
}
/**
* @param FlatBufferBuilder $builder
* @return int table offset

View File

@@ -433,7 +433,134 @@ class Monster(object):
return obj
return None
def MonsterStart(builder): builder.StartObject(35)
# Monster
def VectorOfReferrables(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(74))
if o != 0:
x = self._tab.Vector(o)
x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * 4
x = self._tab.Indirect(x)
from .Referrable import Referrable
obj = Referrable()
obj.Init(self._tab.Bytes, x)
return obj
return None
# Monster
def VectorOfReferrablesLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(74))
if o != 0:
return self._tab.VectorLen(o)
return 0
# Monster
def SingleWeakReference(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(76))
if o != 0:
return self._tab.Get(flatbuffers.number_types.Uint64Flags, o + self._tab.Pos)
return 0
# Monster
def VectorOfWeakReferences(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(78))
if o != 0:
a = self._tab.Vector(o)
return self._tab.Get(flatbuffers.number_types.Uint64Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 8))
return 0
# Monster
def VectorOfWeakReferencesAsNumpy(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(78))
if o != 0:
return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Uint64Flags, o)
return 0
# Monster
def VectorOfWeakReferencesLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(78))
if o != 0:
return self._tab.VectorLen(o)
return 0
# Monster
def VectorOfStrongReferrables(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(80))
if o != 0:
x = self._tab.Vector(o)
x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * 4
x = self._tab.Indirect(x)
from .Referrable import Referrable
obj = Referrable()
obj.Init(self._tab.Bytes, x)
return obj
return None
# Monster
def VectorOfStrongReferrablesLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(80))
if o != 0:
return self._tab.VectorLen(o)
return 0
# Monster
def CoOwningReference(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(82))
if o != 0:
return self._tab.Get(flatbuffers.number_types.Uint64Flags, o + self._tab.Pos)
return 0
# Monster
def VectorOfCoOwningReferences(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(84))
if o != 0:
a = self._tab.Vector(o)
return self._tab.Get(flatbuffers.number_types.Uint64Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 8))
return 0
# Monster
def VectorOfCoOwningReferencesAsNumpy(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(84))
if o != 0:
return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Uint64Flags, o)
return 0
# Monster
def VectorOfCoOwningReferencesLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(84))
if o != 0:
return self._tab.VectorLen(o)
return 0
# Monster
def NonOwningReference(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(86))
if o != 0:
return self._tab.Get(flatbuffers.number_types.Uint64Flags, o + self._tab.Pos)
return 0
# Monster
def VectorOfNonOwningReferences(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(88))
if o != 0:
a = self._tab.Vector(o)
return self._tab.Get(flatbuffers.number_types.Uint64Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 8))
return 0
# Monster
def VectorOfNonOwningReferencesAsNumpy(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(88))
if o != 0:
return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Uint64Flags, o)
return 0
# Monster
def VectorOfNonOwningReferencesLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(88))
if o != 0:
return self._tab.VectorLen(o)
return 0
def MonsterStart(builder): builder.StartObject(43)
def MonsterAddPos(builder, pos): builder.PrependStructSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(pos), 0)
def MonsterAddMana(builder, mana): builder.PrependInt16Slot(1, mana, 150)
def MonsterAddHp(builder, hp): builder.PrependInt16Slot(2, hp, 100)
@@ -480,4 +607,17 @@ def MonsterStartVectorOfLongsVector(builder, numElems): return builder.StartVect
def MonsterAddVectorOfDoubles(builder, vectorOfDoubles): builder.PrependUOffsetTRelativeSlot(33, flatbuffers.number_types.UOffsetTFlags.py_type(vectorOfDoubles), 0)
def MonsterStartVectorOfDoublesVector(builder, numElems): return builder.StartVector(8, numElems, 8)
def MonsterAddParentNamespaceTest(builder, parentNamespaceTest): builder.PrependUOffsetTRelativeSlot(34, flatbuffers.number_types.UOffsetTFlags.py_type(parentNamespaceTest), 0)
def MonsterAddVectorOfReferrables(builder, vectorOfReferrables): builder.PrependUOffsetTRelativeSlot(35, flatbuffers.number_types.UOffsetTFlags.py_type(vectorOfReferrables), 0)
def MonsterStartVectorOfReferrablesVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def MonsterAddSingleWeakReference(builder, singleWeakReference): builder.PrependUint64Slot(36, singleWeakReference, 0)
def MonsterAddVectorOfWeakReferences(builder, vectorOfWeakReferences): builder.PrependUOffsetTRelativeSlot(37, flatbuffers.number_types.UOffsetTFlags.py_type(vectorOfWeakReferences), 0)
def MonsterStartVectorOfWeakReferencesVector(builder, numElems): return builder.StartVector(8, numElems, 8)
def MonsterAddVectorOfStrongReferrables(builder, vectorOfStrongReferrables): builder.PrependUOffsetTRelativeSlot(38, flatbuffers.number_types.UOffsetTFlags.py_type(vectorOfStrongReferrables), 0)
def MonsterStartVectorOfStrongReferrablesVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def MonsterAddCoOwningReference(builder, coOwningReference): builder.PrependUint64Slot(39, coOwningReference, 0)
def MonsterAddVectorOfCoOwningReferences(builder, vectorOfCoOwningReferences): builder.PrependUOffsetTRelativeSlot(40, flatbuffers.number_types.UOffsetTFlags.py_type(vectorOfCoOwningReferences), 0)
def MonsterStartVectorOfCoOwningReferencesVector(builder, numElems): return builder.StartVector(8, numElems, 8)
def MonsterAddNonOwningReference(builder, nonOwningReference): builder.PrependUint64Slot(41, nonOwningReference, 0)
def MonsterAddVectorOfNonOwningReferences(builder, vectorOfNonOwningReferences): builder.PrependUOffsetTRelativeSlot(42, flatbuffers.number_types.UOffsetTFlags.py_type(vectorOfNonOwningReferences), 0)
def MonsterStartVectorOfNonOwningReferencesVector(builder, numElems): return builder.StartVector(8, numElems, 8)
def MonsterEnd(builder): return builder.EndObject()

View File

@@ -0,0 +1,350 @@
//Generated by flatc compiler (version 1.8.0)
//If you make any local changes, they will be lost
//source: monster_test.fbs
package MyGame.Example;
import com.google.flatbuffers.grpc.FlatbuffersUtils;
import java.nio.ByteBuffer;
import static io.grpc.MethodDescriptor.generateFullMethodName;
import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall;
import static io.grpc.stub.ClientCalls.asyncClientStreamingCall;
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
import static io.grpc.stub.ClientCalls.blockingUnaryCall;
import static io.grpc.stub.ClientCalls.futureUnaryCall;
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall;
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
/**
*/
@javax.annotation.Generated(
value = "by gRPC proto compiler",
comments = "Source: monster_test.fbs")
public final class MonsterStorageGrpc {
private MonsterStorageGrpc() {}
public static final String SERVICE_NAME = "MyGame.Example.MonsterStorage";
// Static method descriptors that strictly reflect the proto.
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
@java.lang.Deprecated // Use {@link #getStoreMethod()} instead.
public static final io.grpc.MethodDescriptor<MyGame.Example.Monster,
MyGame.Example.Stat> METHOD_STORE = getStoreMethod();
private static volatile io.grpc.MethodDescriptor<MyGame.Example.Monster,
MyGame.Example.Stat> getStoreMethod;
private static volatile FlatbuffersUtils.FBExtactor<MyGame.Example.Monster> extractorOfMonster;
private static FlatbuffersUtils.FBExtactor<MyGame.Example.Monster> getExtractorOfMonster() {
if (extractorOfMonster != null) return extractorOfMonster;
synchronized (MonsterStorageGrpc.class) {
if (extractorOfMonster != null) return extractorOfMonster;
extractorOfMonster = new FlatbuffersUtils.FBExtactor<MyGame.Example.Monster>() {
public MyGame.Example.Monster extract (ByteBuffer buffer) {
return MyGame.Example.Monster.getRootAsMonster(buffer);
}
};
return extractorOfMonster;
}
}
private static volatile FlatbuffersUtils.FBExtactor<MyGame.Example.Stat> extractorOfStat;
private static FlatbuffersUtils.FBExtactor<MyGame.Example.Stat> getExtractorOfStat() {
if (extractorOfStat != null) return extractorOfStat;
synchronized (MonsterStorageGrpc.class) {
if (extractorOfStat != null) return extractorOfStat;
extractorOfStat = new FlatbuffersUtils.FBExtactor<MyGame.Example.Stat>() {
public MyGame.Example.Stat extract (ByteBuffer buffer) {
return MyGame.Example.Stat.getRootAsStat(buffer);
}
};
return extractorOfStat;
}
}
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
public static io.grpc.MethodDescriptor<MyGame.Example.Monster,
MyGame.Example.Stat> getStoreMethod() {
io.grpc.MethodDescriptor<MyGame.Example.Monster, MyGame.Example.Stat> getStoreMethod;
if ((getStoreMethod = MonsterStorageGrpc.getStoreMethod) == null) {
synchronized (MonsterStorageGrpc.class) {
if ((getStoreMethod = MonsterStorageGrpc.getStoreMethod) == null) {
MonsterStorageGrpc.getStoreMethod = getStoreMethod =
io.grpc.MethodDescriptor.<MyGame.Example.Monster, MyGame.Example.Stat>newBuilder()
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"MyGame.Example.MonsterStorage", "Store"))
.setSampledToLocalTracing(true)
.setRequestMarshaller(FlatbuffersUtils.marshaller(
MyGame.Example.Monster.class, getExtractorOfMonster()))
.setResponseMarshaller(FlatbuffersUtils.marshaller(
MyGame.Example.Stat.class, getExtractorOfStat()))
.setSchemaDescriptor(null)
.build();
}
}
}
return getStoreMethod;
}
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
@java.lang.Deprecated // Use {@link #getRetrieveMethod()} instead.
public static final io.grpc.MethodDescriptor<MyGame.Example.Stat,
MyGame.Example.Monster> METHOD_RETRIEVE = getRetrieveMethod();
private static volatile io.grpc.MethodDescriptor<MyGame.Example.Stat,
MyGame.Example.Monster> getRetrieveMethod;
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
public static io.grpc.MethodDescriptor<MyGame.Example.Stat,
MyGame.Example.Monster> getRetrieveMethod() {
io.grpc.MethodDescriptor<MyGame.Example.Stat, MyGame.Example.Monster> getRetrieveMethod;
if ((getRetrieveMethod = MonsterStorageGrpc.getRetrieveMethod) == null) {
synchronized (MonsterStorageGrpc.class) {
if ((getRetrieveMethod = MonsterStorageGrpc.getRetrieveMethod) == null) {
MonsterStorageGrpc.getRetrieveMethod = getRetrieveMethod =
io.grpc.MethodDescriptor.<MyGame.Example.Stat, MyGame.Example.Monster>newBuilder()
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"MyGame.Example.MonsterStorage", "Retrieve"))
.setSampledToLocalTracing(true)
.setRequestMarshaller(FlatbuffersUtils.marshaller(
MyGame.Example.Stat.class, getExtractorOfStat()))
.setResponseMarshaller(FlatbuffersUtils.marshaller(
MyGame.Example.Monster.class, getExtractorOfMonster()))
.setSchemaDescriptor(null)
.build();
}
}
}
return getRetrieveMethod;
}
/**
* Creates a new async stub that supports all call types for the service
*/
public static MonsterStorageStub newStub(io.grpc.Channel channel) {
return new MonsterStorageStub(channel);
}
/**
* Creates a new blocking-style stub that supports unary and streaming output calls on the service
*/
public static MonsterStorageBlockingStub newBlockingStub(
io.grpc.Channel channel) {
return new MonsterStorageBlockingStub(channel);
}
/**
* Creates a new ListenableFuture-style stub that supports unary calls on the service
*/
public static MonsterStorageFutureStub newFutureStub(
io.grpc.Channel channel) {
return new MonsterStorageFutureStub(channel);
}
/**
*/
public static abstract class MonsterStorageImplBase implements io.grpc.BindableService {
/**
*/
public void store(MyGame.Example.Monster request,
io.grpc.stub.StreamObserver<MyGame.Example.Stat> responseObserver) {
asyncUnimplementedUnaryCall(getStoreMethod(), responseObserver);
}
/**
*/
public void retrieve(MyGame.Example.Stat request,
io.grpc.stub.StreamObserver<MyGame.Example.Monster> responseObserver) {
asyncUnimplementedUnaryCall(getRetrieveMethod(), responseObserver);
}
@java.lang.Override public final io.grpc.ServerServiceDefinition bindService() {
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
getStoreMethod(),
asyncUnaryCall(
new MethodHandlers<
MyGame.Example.Monster,
MyGame.Example.Stat>(
this, METHODID_STORE)))
.addMethod(
getRetrieveMethod(),
asyncServerStreamingCall(
new MethodHandlers<
MyGame.Example.Stat,
MyGame.Example.Monster>(
this, METHODID_RETRIEVE)))
.build();
}
}
/**
*/
public static final class MonsterStorageStub extends io.grpc.stub.AbstractStub<MonsterStorageStub> {
private MonsterStorageStub(io.grpc.Channel channel) {
super(channel);
}
private MonsterStorageStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
@java.lang.Override
protected MonsterStorageStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new MonsterStorageStub(channel, callOptions);
}
/**
*/
public void store(MyGame.Example.Monster request,
io.grpc.stub.StreamObserver<MyGame.Example.Stat> responseObserver) {
asyncUnaryCall(
getChannel().newCall(getStoreMethod(), getCallOptions()), request, responseObserver);
}
/**
*/
public void retrieve(MyGame.Example.Stat request,
io.grpc.stub.StreamObserver<MyGame.Example.Monster> responseObserver) {
asyncServerStreamingCall(
getChannel().newCall(getRetrieveMethod(), getCallOptions()), request, responseObserver);
}
}
/**
*/
public static final class MonsterStorageBlockingStub extends io.grpc.stub.AbstractStub<MonsterStorageBlockingStub> {
private MonsterStorageBlockingStub(io.grpc.Channel channel) {
super(channel);
}
private MonsterStorageBlockingStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
@java.lang.Override
protected MonsterStorageBlockingStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new MonsterStorageBlockingStub(channel, callOptions);
}
/**
*/
public MyGame.Example.Stat store(MyGame.Example.Monster request) {
return blockingUnaryCall(
getChannel(), getStoreMethod(), getCallOptions(), request);
}
/**
*/
public java.util.Iterator<MyGame.Example.Monster> retrieve(
MyGame.Example.Stat request) {
return blockingServerStreamingCall(
getChannel(), getRetrieveMethod(), getCallOptions(), request);
}
}
/**
*/
public static final class MonsterStorageFutureStub extends io.grpc.stub.AbstractStub<MonsterStorageFutureStub> {
private MonsterStorageFutureStub(io.grpc.Channel channel) {
super(channel);
}
private MonsterStorageFutureStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
@java.lang.Override
protected MonsterStorageFutureStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new MonsterStorageFutureStub(channel, callOptions);
}
/**
*/
public com.google.common.util.concurrent.ListenableFuture<MyGame.Example.Stat> store(
MyGame.Example.Monster request) {
return futureUnaryCall(
getChannel().newCall(getStoreMethod(), getCallOptions()), request);
}
}
private static final int METHODID_STORE = 0;
private static final int METHODID_RETRIEVE = 1;
private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {
private final MonsterStorageImplBase serviceImpl;
private final int methodId;
MethodHandlers(MonsterStorageImplBase serviceImpl, int methodId) {
this.serviceImpl = serviceImpl;
this.methodId = methodId;
}
@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
case METHODID_STORE:
serviceImpl.store((MyGame.Example.Monster) request,
(io.grpc.stub.StreamObserver<MyGame.Example.Stat>) responseObserver);
break;
case METHODID_RETRIEVE:
serviceImpl.retrieve((MyGame.Example.Stat) request,
(io.grpc.stub.StreamObserver<MyGame.Example.Monster>) responseObserver);
break;
default:
throw new AssertionError();
}
}
@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public io.grpc.stub.StreamObserver<Req> invoke(
io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
default:
throw new AssertionError();
}
}
}
private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
io.grpc.ServiceDescriptor result = serviceDescriptor;
if (result == null) {
synchronized (MonsterStorageGrpc.class) {
result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME)
.setSchemaDescriptor(null)
.addMethod(getStoreMethod())
.addMethod(getRetrieveMethod())
.build();
}
}
}
return result;
}
}

View File

@@ -0,0 +1,64 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace MyGame.Example
{
using global::System;
using global::FlatBuffers;
public struct Referrable : IFlatbufferObject
{
private Table __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public static Referrable GetRootAsReferrable(ByteBuffer _bb) { return GetRootAsReferrable(_bb, new Referrable()); }
public static Referrable GetRootAsReferrable(ByteBuffer _bb, Referrable obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
public void __init(int _i, ByteBuffer _bb) { __p.bb_pos = _i; __p.bb = _bb; }
public Referrable __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public ulong Id { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } }
public bool MutateId(ulong id) { int o = __p.__offset(4); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, id); return true; } else { return false; } }
public static Offset<Referrable> CreateReferrable(FlatBufferBuilder builder,
ulong id = 0) {
builder.StartObject(1);
Referrable.AddId(builder, id);
return Referrable.EndReferrable(builder);
}
public static void StartReferrable(FlatBufferBuilder builder) { builder.StartObject(1); }
public static void AddId(FlatBufferBuilder builder, ulong id) { builder.AddUlong(0, id, 0); }
public static Offset<Referrable> EndReferrable(FlatBufferBuilder builder) {
int o = builder.EndObject();
return new Offset<Referrable>(o);
}
public static VectorOffset CreateSortedVectorOfReferrable(FlatBufferBuilder builder, Offset<Referrable>[] offsets) {
Array.Sort(offsets, (Offset<Referrable> o1, Offset<Referrable> o2) => builder.DataBuffer.GetUlong(Table.__offset(4, o1.Value, builder.DataBuffer)).CompareTo(builder.DataBuffer.GetUlong(Table.__offset(4, o2.Value, builder.DataBuffer))));
return builder.CreateVectorOfTables(offsets);
}
public static Referrable? __lookup_by_key(int vectorLocation, ulong key, ByteBuffer bb) {
int span = bb.GetInt(vectorLocation - 4);
int start = 0;
while (span != 0) {
int middle = span / 2;
int tableOffset = Table.__indirect(vectorLocation + 4 * (start + middle), bb);
int comp = bb.GetUlong(Table.__offset(4, bb.Length - tableOffset, bb)).CompareTo(key);
if (comp > 0) {
span = middle;
} else if (comp < 0) {
middle++;
start += middle;
span -= middle;
} else {
return new Referrable().__assign(tableOffset, bb);
}
}
return null;
}
};
}

View File

@@ -0,0 +1,49 @@
// automatically generated by the FlatBuffers compiler, do not modify
package Example
import (
flatbuffers "github.com/google/flatbuffers/go"
)
type Referrable struct {
_tab flatbuffers.Table
}
func GetRootAsReferrable(buf []byte, offset flatbuffers.UOffsetT) *Referrable {
n := flatbuffers.GetUOffsetT(buf[offset:])
x := &Referrable{}
x.Init(buf, n+offset)
return x
}
func (rcv *Referrable) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *Referrable) Table() flatbuffers.Table {
return rcv._tab
}
func (rcv *Referrable) Id() uint64 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
if o != 0 {
return rcv._tab.GetUint64(o + rcv._tab.Pos)
}
return 0
}
func (rcv *Referrable) MutateId(n uint64) bool {
return rcv._tab.MutateUint64Slot(4, n)
}
func ReferrableStart(builder *flatbuffers.Builder) {
builder.StartObject(1)
}
func ReferrableAddId(builder *flatbuffers.Builder, id uint64) {
builder.PrependUint64Slot(0, id, 0)
}
func ReferrableEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return builder.EndObject()
}

View File

@@ -0,0 +1,62 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class Referrable extends Table {
public static Referrable getRootAsReferrable(ByteBuffer _bb) { return getRootAsReferrable(_bb, new Referrable()); }
public static Referrable getRootAsReferrable(ByteBuffer _bb, Referrable obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
public Referrable __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public long id() { int o = __offset(4); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateId(long id) { int o = __offset(4); if (o != 0) { bb.putLong(o + bb_pos, id); return true; } else { return false; } }
public static int createReferrable(FlatBufferBuilder builder,
long id) {
builder.startObject(1);
Referrable.addId(builder, id);
return Referrable.endReferrable(builder);
}
public static void startReferrable(FlatBufferBuilder builder) { builder.startObject(1); }
public static void addId(FlatBufferBuilder builder, long id) { builder.addLong(0, id, 0L); }
public static int endReferrable(FlatBufferBuilder builder) {
int o = builder.endObject();
return o;
}
@Override
protected int keysCompare(Integer o1, Integer o2, ByteBuffer _bb) {
long val_1 = _bb.getLong(__offset(4, o1, _bb));
long val_2 = _bb.getLong(__offset(4, o2, _bb));
return val_1 > val_2 ? 1 : val_1 < val_2 ? -1 : 0;
}
public static Referrable __lookup_by_key(Referrable obj, int vectorLocation, long key, ByteBuffer bb) {
int span = bb.getInt(vectorLocation - 4);
int start = 0;
while (span != 0) {
int middle = span / 2;
int tableOffset = __indirect(vectorLocation + 4 * (start + middle), bb);
long val = bb.getLong(__offset(4, bb.capacity() - tableOffset, bb));
int comp = val > key ? 1 : val < key ? -1 : 0;
if (comp > 0) {
span = middle;
} else if (comp < 0) {
middle++;
start += middle;
span -= middle;
} else {
return (obj == null ? new Referrable() : obj).__assign(tableOffset, bb);
}
}
return null;
}
}

View File

@@ -0,0 +1,99 @@
<?php
// automatically generated by the FlatBuffers compiler, do not modify
namespace MyGame\Example;
use \Google\FlatBuffers\Struct;
use \Google\FlatBuffers\Table;
use \Google\FlatBuffers\ByteBuffer;
use \Google\FlatBuffers\FlatBufferBuilder;
class Referrable extends Table
{
/**
* @param ByteBuffer $bb
* @return Referrable
*/
public static function getRootAsReferrable(ByteBuffer $bb)
{
$obj = new Referrable();
return ($obj->init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb));
}
public static function ReferrableIdentifier()
{
return "MONS";
}
public static function ReferrableBufferHasIdentifier(ByteBuffer $buf)
{
return self::__has_identifier($buf, self::ReferrableIdentifier());
}
public static function ReferrableExtension()
{
return "mon";
}
/**
* @param int $_i offset
* @param ByteBuffer $_bb
* @return Referrable
**/
public function init($_i, ByteBuffer $_bb)
{
$this->bb_pos = $_i;
$this->bb = $_bb;
return $this;
}
/**
* @return ulong
*/
public function getId()
{
$o = $this->__offset(4);
return $o != 0 ? $this->bb->getUlong($o + $this->bb_pos) : 0;
}
/**
* @param FlatBufferBuilder $builder
* @return void
*/
public static function startReferrable(FlatBufferBuilder $builder)
{
$builder->StartObject(1);
}
/**
* @param FlatBufferBuilder $builder
* @return Referrable
*/
public static function createReferrable(FlatBufferBuilder $builder, $id)
{
$builder->startObject(1);
self::addId($builder, $id);
$o = $builder->endObject();
return $o;
}
/**
* @param FlatBufferBuilder $builder
* @param ulong
* @return void
*/
public static function addId(FlatBufferBuilder $builder, $id)
{
$builder->addUlongX(0, $id, 0);
}
/**
* @param FlatBufferBuilder $builder
* @return int table offset
*/
public static function endReferrable(FlatBufferBuilder $builder)
{
$o = $builder->endObject();
return $o;
}
}

Some files were not shown because too many files have changed in this diff Show More