How to get started
ObjectBox Generator produces binding code for ObjectBox C & C++ APIs. This greatly simplifies the model declaration and FlatBuffers serialization, allowing you to concentrate on the application logic.
Generating binding code
ObjectBox Generator is a tool, which must be downloaded separately. For details, please check the installation page.
ObjectBox Generator uses FlatBuffer schema file (.fbs) as its primary input. The Generator also maintains some metadata around the data model in a JSON file (objectbox-model.json). Based on these two files, it generates code for the selected language (C or C++).
Let’s have a look at a sample schema and how Generator helps us.
Launch the following command to generate the binding code from the FlatBuffers schema file:
The following files will be generated:
objectbox-model.h
objectbox-model.json
tasklist.obx.hpp
tasklist.obx.cpp
You should add all these generated files to your source control (e.g. git), most importantly objectbox-model.json
which ensures compatibility with previous versions of your database after you make changes to the schema.
Working with Object Boxes
Bet you wondered where our name comes from :)
From an ObjectBox Store, you get Box instances to manage your entities. While you can have multiple Box instances of the same type (for the same Entity) "open" at once, it's usually preferable to just use one instance and pass it around your code.
Now, you can include the generated headers in your application and start working with your database. Consider the following main file:
It's required to have exactly one .cpp file in your project that defines OBX_CPP_FILE right before the inclusion of the "objectbox.hpp" header.
This #define
instructs the "objectbox.hpp" header to emit implementation definitions. If you accidentally have it in multiple files, the linker will complain about multiple symbols (having the same name).
If you've followed the installation instructions, you should be able to compile the example
If you are using CMake, like shown in the installation section, just add the generated tasklist.obx.cpp
file to the myapp
target.
The add_executable
call in the CMake file now looks like this:
The rest of the CMakeLists.txt file stays unchanged. You can now use CMake as expected.
If you use a build system other than CMake, it has to do the proper action so the generated file is added to the build.
The command snippet assumes you have the libraries installed in a path recognized by your OS (e.g. /usr/local/lib/) and all the referenced headers are in the same folder alongside the main.c/.cpp file.
Wherever you have access to a Box, you can use it to persist objects and fetch objects from disk. Boxes are thread-safe. Here are some of the basic operations, have a look at the objectbox.h(pp) for more:
put: persist an object at the given ID: either creating a new one or overwriting an existing one.
get: read an object from the database. There's also a variant that takes a list of IDs as an argument and returns multiple objects.
remove: deletes a previously persisted object from its box.
count: the number of objects stored in this box.
Examples
Have a look at the following TaskList example apps, depending on your programming language and preference:
C, cursor, no generated code - plain C; using flatcc directly; without any generated code
C, with generated code - plain C, using code generated by
objectbox-generator
C++, with generated code - C++, using code generated by
objectbox-generator
Last updated