Create a "static" flatbuffer API
This provides a generated API for working with flatbuffer objects that
generates a statically determined layout for the flatbuffer and uses
that layout to construct the flatbuffer without needing to dynamically
allocate any memory. For situations where dynamic sizing is appropriate,
this API does allow for increasing the size of any vectors in the
flatbuffer objects.
This change includes a checked-in version of the generated code so that
reviewers for this and future changes can readily examine what the
generated code looks like.
Future tasks:
* Support for unions?
* Consider precomputing some constants for sizes/alignments rather than
massive constant expressions.
Change-Id: I6bf72d6c722d5390ab2239289a8a2a4e118c8d47
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/json_to_flatbuffer.h b/aos/json_to_flatbuffer.h
index b664036..6ef3544 100644
--- a/aos/json_to_flatbuffer.h
+++ b/aos/json_to_flatbuffer.h
@@ -12,6 +12,7 @@
#include "aos/fast_string_builder.h"
#include "aos/flatbuffer_utils.h"
#include "aos/flatbuffers.h"
+#include "aos/flatbuffers/builder.h"
#include "aos/util/file.h"
namespace aos {
@@ -38,6 +39,15 @@
JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()), fbb).o);
}
+template <typename T>
+inline fbs::Builder<T> JsonToStaticFlatbuffer(const std::string_view data) {
+ const aos::FlatbufferDetachedBuffer<typename T::Flatbuffer> fbs =
+ JsonToFlatbuffer<typename T::Flatbuffer>(data);
+ fbs::Builder<T> builder(std::make_unique<aos::fbs::VectorAllocator>());
+ CHECK(builder.get()->FromFlatbuffer(&fbs.message()));
+ return builder;
+}
+
struct JsonOptions {
// controls if the Json is written out on multiple lines or one.
bool multi_line = false;
@@ -53,7 +63,7 @@
// Converts a flatbuffer into a Json string.
// The methods below are generally more useful than TableFlatbufferToJson.
::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
- const ::flatbuffers::TypeTable *typetable,
+ const flatbuffers::TypeTable *typetable,
JsonOptions json_options = {});
// Converts a Flatbuffer<T> holding a flatbuffer to JSON.