...

Text file src/github.com/google/flatbuffers/docs/source/LobsterUsage.md

Documentation: github.com/google/flatbuffers/docs/source

     1Use in Lobster    {#flatbuffers_guide_use_lobster}
     2==============
     3
     4## Before you get started
     5
     6Before diving into the FlatBuffers usage in Lobster, it should be noted that the
     7[Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to general
     8FlatBuffers usage in all of the supported languages (including Lobster). This
     9page is designed to cover the nuances of FlatBuffers usage, specific to
    10Lobster.
    11
    12You should also have read the [Building](@ref flatbuffers_guide_building)
    13documentation to build `flatc` and should be familiar with
    14[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
    15[Writing a schema](@ref flatbuffers_guide_writing_schema).
    16
    17## FlatBuffers Lobster library code location
    18
    19The code for the FlatBuffers Lobster library can be found at
    20`flatbuffers/lobster`. You can browse the library code on the
    21[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/
    22lobster).
    23
    24## Testing the FlatBuffers Lobster library
    25
    26The code to test the Lobster library can be found at `flatbuffers/tests`.
    27The test code itself is located in [lobstertest.lobster](https://github.com/google/
    28flatbuffers/blob/master/tests/lobstertest.lobster).
    29
    30To run the tests, run `lobster lobstertest.lobster`. To obtain Lobster itself,
    31go to the [Lobster homepage](http://strlen.com/lobster) or
    32[github](https://github.com/aardappel/lobster) to learn how to build it for your
    33platform.
    34
    35## Using the FlatBuffers Lobster library
    36
    37*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
    38example of how to use FlatBuffers in Lobster.*
    39
    40There is support for both reading and writing FlatBuffers in Lobster.
    41
    42To use FlatBuffers in your own code, first generate Lobster classes from your
    43schema with the `--lobster` option to `flatc`. Then you can include both
    44FlatBuffers and the generated code to read or write a FlatBuffer.
    45
    46For example, here is how you would read a FlatBuffer binary file in Lobster:
    47First, import the library and the generated code. Then read a FlatBuffer binary
    48file into a string, which you pass to the `GetRootAsMonster` function:
    49
    50~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.lobster}
    51    include "monster_generated.lobster"
    52
    53    let fb = read_file("monsterdata_test.mon")
    54    assert fb
    55    let monster = MyGame_Example_GetRootAsMonster(fb)
    56~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    57
    58Now you can access values like this:
    59
    60~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.lobster}
    61    let hp = monster.hp
    62    let pos = monster.pos
    63~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    64
    65As you can see, even though `hp` and `pos` are functions that access FlatBuffer
    66data in-place in the string buffer, they appear as field accesses.
    67
    68## Speed
    69
    70Using FlatBuffers in Lobster should be relatively fast, as the implementation
    71makes use of native support for writing binary values, and access of vtables.
    72Both generated code and the runtime library are therefore small and fast.
    73
    74Actual speed will depend on whether you use Lobster as bytecode VM or compiled to
    75C++.
    76
    77## Text Parsing
    78
    79Lobster has full support for parsing JSON into FlatBuffers, or generating
    80JSON from FlatBuffers. See `samples/sample_test.lobster` for an example.
    81
    82This uses the C++ parser and generator underneath, so should be both fast and
    83conformant.
    84
    85<br>

View as plain text