Nebula
Loading...
Searching...
No Matches
flatbufferinterface.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
10#include "io/uri.h"
11#include "util/stringatom.h"
12#include "flatbuffers/flatbuffers.h"
14#include "util/blob.h"
15#include "io/ioserver.h"
16
17#define SerializeFlatbufferText(TYPE, ITEM) Flat::FlatbufferInterface::SerializeHelper<TYPE>(ITEM, TYPE##Identifier())
18#define SerializeFlatbufferTextDirect(TYPE, BUFFER) Flat::FlatbufferInterface::BufferToText(BUFFER, TYPE##Identifier())
19#define CompileFlatbuffer(TYPE, SOURCE, TARGET) Flat::FlatbufferInterface::Compile(SOURCE, TARGET, TYPE##Identifier())
20
21namespace Flat
22{
23
25{
26public:
27
29 static void Init();
31 static bool LoadSchema(IO::URI const& file);
33 static bool CompileSchema(IO::URI const& file);
35 static bool HasSchema(Util::StringAtom identifier);
36
38 template<typename BaseT, typename ItemT> static Util::String SerializeHelper(ItemT const& item, const char* ident);
39
41 template<typename BaseT, typename ItemT> static Util::Blob SerializeFlatbuffer(ItemT const& item);
42
43 template<typename BaseT> static Util::String SerializeToText(const uint8_t * buf);
44
46 template<typename BaseT, typename ItemT> static void DeserializeFlatbufferFile(ItemT& item, IO::URI const& file);
48 template<typename BaseT, typename ItemT> static void DeserializeFlatbuffer(ItemT& item, const uint8_t* buf);
49
51 static Util::String BufferToText(const uint8_t* buffer, Util::StringAtom identifier);
52
54 static bool Compile(IO::URI const& source, IO::URI const& targetFolder, const char* ident);
55
56};
57
58
59//------------------------------------------------------------------------------
62template<typename BaseT, typename ItemT> Util::String
63FlatbufferInterface::SerializeHelper(ItemT const& item, const char* ident)
64{
65 Util::StringAtom sIdent(ident);
67 flatbuffers::FlatBufferBuilder builder(65536);
68 builder.Finish(BaseT::Pack(builder, &item));
69 return FlatbufferInterface::BufferToText(builder.GetBufferPointer(), sIdent);
70}
71
72//------------------------------------------------------------------------------
75template <typename BaseT, typename ItemT> Util::Blob
77{
78 flatbuffers::FlatBufferBuilder builder(65536);
79 builder.Finish(BaseT::Pack(builder, &item));
80 return Util::Blob(builder.GetBufferPointer(), builder.GetSize());
81}
82
83//------------------------------------------------------------------------------
86template<typename BaseT, typename ItemT>
87void
88FlatbufferInterface::DeserializeFlatbuffer(ItemT& item, const uint8_t* buf)
89{
90 const BaseT* bItem = flatbuffers::GetRoot<BaseT>(buf);
91 bItem->UnPackTo(&item);
92}
93
94//------------------------------------------------------------------------------
97template<typename BaseT, typename ItemT>
98void
100{
101 Util::String contents;
102 if (IO::IoServer::Instance()->ReadFile(file, contents))
103 {
104 const BaseT* bItem = flatbuffers::GetRoot<BaseT>(contents.AsCharPtr());
105 bItem->UnPackTo(&item);
106 }
107}
108}
Definition flatbufferinterface.h:25
static void DeserializeFlatbufferFile(ItemT &item, IO::URI const &file)
Definition flatbufferinterface.h:99
static bool LoadSchema(IO::URI const &file)
Definition flatbufferinterface.cc:135
static Util::String SerializeHelper(ItemT const &item, const char *ident)
Helper function, use SerializeFlatbuffer macro instead.
Definition flatbufferinterface.h:63
static void Init()
Initialize datastructures and load all schema files in export.
Definition flatbufferinterface.cc:126
static Util::Blob SerializeFlatbuffer(ItemT const &item)
Serialize to binary blob.
Definition flatbufferinterface.h:76
static bool Compile(IO::URI const &source, IO::URI const &targetFolder, const char *ident)
compile flatbuffer json to binary
Definition flatbufferinterface.cc:183
static bool CompileSchema(IO::URI const &file)
Definition flatbufferinterface.cc:143
static Util::String SerializeToText(const uint8_t *buf)
static bool HasSchema(Util::StringAtom identifier)
Definition flatbufferinterface.cc:165
static void DeserializeFlatbuffer(ItemT &item, const uint8_t *buf)
Definition flatbufferinterface.h:88
static Util::String BufferToText(const uint8_t *buffer, Util::StringAtom identifier)
Definition flatbufferinterface.cc:173
An URI object can split a Uniform Resource Identifier string into its components or build a string fr...
Definition uri.h:67
The Util::Blob class encapsulates a chunk of raw memory into a C++ object which can be copied,...
Definition blob.h:22
A StringAtom.
Definition stringatom.h:22
#define n_assert(exp)
Definition debug.h:50
Flatbuffer to Nebula type conversion functions.
Definition flatbufferinterface.h:22
Nebula's universal string class.
Definition string.h:50
const char * AsCharPtr() const
return contents as character pointer
Definition string.h:540