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.
(for C and C++:Most of this documentation expects you to have the generator installed and available in $PATH, unless specified otherwise. Therefore, you might want to have a look at the installation page first.

Generating binding code

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.
tasklist.fbs
1
table Task {
2
id: ulong;
3
text: string;
4
date_created: ulong;
5
date_finished: ulong;
6
}
Copied!
Launch the following command to generate the binding code from the FlatBuffers schema file:
C++
C
1
# to generate for a single file
2
objectbox-generator -cpp tasklist.fbs
3
4
# to generate recursively for the current directory
5
objectbox-generator -cpp ./...
Copied!
The following files will be generated:
  • objectbox-model.h
  • objectbox-model.json
  • tasklist.obx.hpp
  • tasklist.obx.cpp
1
# to generate for a single file
2
objectbox-generator -c tasklist.fbs
3
4
# to generate recursively for the current directory
5
objectbox-generator -c ./...
Copied!
The following files will be generated:
  • objectbox-model.h
  • objectbox-model.json
  • tasklist.obx.h
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:
C++
C
main.cpp
1
#include "objectbox.hpp"
2
#include "objectbox-model.h"
3
#include "tasklist.obx.hpp"
4
5
int main(int argc, char* args[]) {
6
// create_obx_model() provided by objectbox-model.h
7
// obx interface contents provided by objectbox.hpp
8
obx::Store store(create_obx_model());
9
obx::Box<Task> box(store);
10
11
obx_id id = box.put({.text = "Buy milk"}); // Create
12
std::unique_ptr<Task> task = box.get(id); // Read
13
if (task) {
14
task->text += " & some bread";
15
box.put(*task); // Update
16
box.remove(id); // Delete
17
}
18
return 0;
19
}
Copied!
main.c
1
#include "objectbox.h"
2
#include "objectbox-model.h"
3
#include "tasklist.obx.h"
4
5
obx_err print_last_error() {
6
printf("Unexpected error: %d %s\n",
7
obx_last_error_code(), obx_last_error_message());
8
return obx_last_error_code();
9
}
10
11
int main(int argc, char* args[]) {
12
int rc = 0;
13
OBX_store* store = NULL;
14
OBX_box* box = NULL;
15
Task* task = NULL;
16
17
// Firstly, we need to create a model for our data and the store
18
{
19
OBX_model* model = create_obx_model(); // generated in objectbox-model.h
20
if (!model) goto handle_error;
21
if (obx_model_error_code(model)) {
22
printf("Model definition error: %d %s\n",
23
obx_model_error_code(model), obx_model_error_message(model));
24
obx_model_free(model);
25
goto handle_error;
26
}
27
28
OBX_store_options* opt = obx_opt();
29
obx_opt_model(opt, model);
30
store = obx_store_open(opt);
31
if (!store) goto handle_error;
32
33
// obx_store_open() takes ownership of model and opt and frees them.
34
}
35
36
box = obx_box(store, Task_ENTITY_ID); // Note the generated "Task_ENTITY_ID"
37
38
obx_id id = 0;
39
40
{ // Create
41
Task new_task = {.text = "Buy milk"};
42
id = Task_put(box, &new_task); // generated in tasklist.obx.h
43
if (!id) goto handle_error;
44
printf("New task inserted with ID %d\n", id);
45
}
46
47
{ // Read
48
task = Task_Get(store, box, id); // generated in tasklist.obx.h
49
if (!task) goto handle_error;
50
printf("Task %d read with text: %s\n", id, task->text);
51
}
52
53
{ // Update
54
const char* appendix = " & some bread";
55
56
// Updating a string property is a little more involved
57
// because of C memory management.
58
size_t old_text_len = task->text ? strlen(task->text) : 0;
59
char* new_text =
60
(char*) malloc((old_text_len + strlen(appendix) + 1) * sizeof(char));
61
62
if (task->text) {
63
memcpy(new_text, task->text, old_text_len);
64
65
// free the memory allocated previously before overwritting below
66
free(task->text);
67
}
68
memcpy(new_text + old_text_len, appendix, strlen(appendix) + 1);
69
task->text = new_text;
70
printf("Updated task %d with a new text: %s\n", id, task->text);
71
}
72
73
// Delete
74
if (obx_box_remove(box, id) != OBX_SUCCESS) goto handle_error;
75
76
free_resources: // free any remaining allocated resources
77
if (task) Task_free(task); // free allocs by Task_new_from_flatbuffer()
78
if (store) obx_store_close(store); // and close the store
79
return rc;
80
81
handle_error: // print error and clean up
82
rc = print_last_error();
83
if (rc <= 0) rc = 1;
84
goto free_resources;
85
}
Copied!
If you've followed the installation instructions, you should be able to compile the example by running:
C++
C
1
g++ main.cpp tasklist.obx.cpp -I. -std=c++11 -lobjectbox
Copied!
1
gcc main.c -I. -lobjectbox -lflatccrt
Copied!
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:
Last modified 7mo ago