FlatBuffers
An open source project by FPL.
|
Before diving into the FlatBuffers usage in Lobster, it should be noted that the Tutorial page has a complete guide to general FlatBuffers usage in all of the supported languages (including Lobster). This page is designed to cover the nuances of FlatBuffers usage, specific to Lobster.
You should also have read the Building documentation to build flatc
and should be familiar with Using the schema compiler and Writing a schema.
The code for the FlatBuffers Lobster library can be found at flatbuffers/lobster
. You can browse the library code on the FlatBuffers GitHub page.
The code to test the Lobster library can be found at flatbuffers/tests
. The test code itself is located in lobstertest.lobster.
To run the tests, run lobster lobstertest.lobster
. To obtain Lobster itself, go to the Lobster homepage or github to learn how to build it for your platform.
Note: See Tutorial for a more in-depth example of how to use FlatBuffers in Lobster.
There is support for both reading and writing FlatBuffers in Lobster.
To use FlatBuffers in your own code, first generate Lobster classes from your schema with the --lobster
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 Lobster: First, import the library and the generated code. Then read a FlatBuffer binary file into a string, which you pass to the GetRootAsMonster
function:
Now you can access values like this:
As you can see, even though hp
and pos
are functions that access FlatBuffer data in-place in the string buffer, they appear as field accesses.
Using FlatBuffers in Lobster should be relatively fast, as the implementation makes use of native support for writing binary values, and access of vtables. Both generated code and the runtime library are therefore small and fast.
Actual speed will depend on whether you use Lobster as bytecode VM or compiled to C++.
Lobster has full support for parsing JSON into FlatBuffers, or generating JSON from FlatBuffers. See samples/sample_test.lobster
for an example.
This uses the C++ parser and generator underneath, so should be both fast and conformant.