Made all C++ files clang-formatted.
Also added missing generated files. Change-Id: Ifd22a643a08e3f2edfce92812ed57b87fc0e1875
This commit is contained in:
parent
5d9930aa0d
commit
89711c9c47
|
@ -1,7 +1,13 @@
|
|||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: Google
|
||||
Language: Cpp
|
||||
BasedOnStyle: Google
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Right
|
||||
IndentPPDirectives: AfterHash
|
||||
Cpp11BracedListStyle: false
|
||||
AlwaysBreakTemplateDeclarations: false
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
SpaceAfterTemplateKeyword: false
|
||||
AllowShortBlocksOnASingleLine: true
|
||||
...
|
||||
|
||||
|
|
|
@ -40,8 +40,8 @@
|
|||
#include <vector>
|
||||
|
||||
#ifndef GRPC_CUSTOM_STRING
|
||||
#include <string>
|
||||
#define GRPC_CUSTOM_STRING std::string
|
||||
# include <string>
|
||||
# define GRPC_CUSTOM_STRING std::string
|
||||
#endif
|
||||
|
||||
namespace grpc {
|
||||
|
|
|
@ -18,46 +18,48 @@
|
|||
|
||||
#include <grpc++/grpc++.h>
|
||||
|
||||
#include "monster_test_generated.h"
|
||||
#include "monster_test.grpc.fb.h"
|
||||
#include "monster_test_generated.h"
|
||||
|
||||
using namespace MyGame::Example;
|
||||
|
||||
// The callback implementation of our server, that derives from the generated
|
||||
// code. It implements all rpcs specified in the FlatBuffers schema.
|
||||
class ServiceImpl final : public MyGame::Example::MonsterStorage::Service {
|
||||
virtual ::grpc::Status Store(::grpc::ServerContext* context,
|
||||
const flatbuffers::grpc::Message<Monster> *request,
|
||||
flatbuffers::grpc::Message<Stat> *response)
|
||||
override {
|
||||
virtual ::grpc::Status Store(
|
||||
::grpc::ServerContext *context,
|
||||
const flatbuffers::grpc::Message<Monster> *request,
|
||||
flatbuffers::grpc::Message<Stat> *response) override {
|
||||
// Create a response from the incoming request name.
|
||||
fbb_.Clear();
|
||||
auto stat_offset = CreateStat(fbb_, fbb_.CreateString("Hello, " +
|
||||
request->GetRoot()->name()->str()));
|
||||
auto stat_offset = CreateStat(
|
||||
fbb_, fbb_.CreateString("Hello, " + request->GetRoot()->name()->str()));
|
||||
fbb_.Finish(stat_offset);
|
||||
// Transfer ownership of the message to gRPC
|
||||
*response = fbb_.ReleaseMessage<Stat>();
|
||||
return grpc::Status::OK;
|
||||
}
|
||||
virtual ::grpc::Status Retrieve(::grpc::ServerContext *context,
|
||||
const flatbuffers::grpc::Message<Stat> *request,
|
||||
::grpc::ServerWriter< flatbuffers::grpc::Message<Monster>>* writer)
|
||||
override {
|
||||
virtual ::grpc::Status Retrieve(
|
||||
::grpc::ServerContext *context,
|
||||
const flatbuffers::grpc::Message<Stat> *request,
|
||||
::grpc::ServerWriter<flatbuffers::grpc::Message<Monster>> *writer)
|
||||
override {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
fbb_.Clear();
|
||||
// Create 10 monsters for resposne.
|
||||
auto monster_offset =
|
||||
CreateMonster(fbb_, 0, 0, 0,
|
||||
fbb_.CreateString(request->GetRoot()->id()->str() +
|
||||
" No." + std::to_string(i)));
|
||||
fbb_.Finish(monster_offset);
|
||||
|
||||
for (int i=0; i<10; i++) {
|
||||
fbb_.Clear();
|
||||
// Create 10 monsters for resposne.
|
||||
auto monster_offset =
|
||||
CreateMonster(fbb_, 0, 0, 0, fbb_.CreateString(
|
||||
request->GetRoot()->id()->str() + " No." + std::to_string(i)));
|
||||
fbb_.Finish(monster_offset);
|
||||
flatbuffers::grpc::Message<Monster> monster =
|
||||
fbb_.ReleaseMessage<Monster>();
|
||||
|
||||
flatbuffers::grpc::Message<Monster> monster = fbb_.ReleaseMessage<Monster>();
|
||||
|
||||
// Send monster to client using streaming.
|
||||
writer->Write(monster);
|
||||
}
|
||||
return grpc::Status::OK;
|
||||
// Send monster to client using streaming.
|
||||
writer->Write(monster);
|
||||
}
|
||||
return grpc::Status::OK;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -90,7 +92,7 @@ void RunServer() {
|
|||
server_instance->Wait();
|
||||
}
|
||||
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
int main(int /*argc*/, const char * /*argv*/ []) {
|
||||
// Launch server.
|
||||
std::thread server_thread(RunServer);
|
||||
|
||||
|
@ -103,7 +105,6 @@ int main(int /*argc*/, const char * /*argv*/[]) {
|
|||
grpc::InsecureChannelCredentials());
|
||||
auto stub = MyGame::Example::MonsterStorage::NewStub(channel);
|
||||
|
||||
|
||||
flatbuffers::grpc::MessageBuilder fbb;
|
||||
{
|
||||
grpc::ClientContext context;
|
||||
|
@ -138,7 +139,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
|
|||
}
|
||||
}
|
||||
|
||||
#if !FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
|
||||
#if !FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
|
||||
{
|
||||
// Test that an invalid request errors out correctly
|
||||
grpc::ClientContext context;
|
||||
|
@ -149,9 +150,10 @@ int main(int /*argc*/, const char * /*argv*/[]) {
|
|||
// matches the protobuf gRPC status code for an unparseable message.
|
||||
assert(!status.ok());
|
||||
assert(status.error_code() == ::grpc::StatusCode::INTERNAL);
|
||||
assert(strcmp(status.error_message().c_str(), "Message verification failed") == 0);
|
||||
assert(strcmp(status.error_message().c_str(),
|
||||
"Message verification failed") == 0);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
server_instance->Shutdown();
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef FLATBUFFERS_BASE_H_
|
||||
#define FLATBUFFERS_BASE_H_
|
||||
|
||||
// clang-format off
|
||||
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
|
||||
defined(_MSC_VER) && defined(_DEBUG)
|
||||
#define _CRTDBG_MAP_ALLOC
|
||||
|
|
|
@ -49,7 +49,7 @@ class CodeWriter {
|
|||
// Associates a key with a value. All subsequent calls to operator+=, where
|
||||
// the specified key is contained in {{ and }} delimiters will be replaced by
|
||||
// the given value.
|
||||
void SetValue(const std::string& key, const std::string& value) {
|
||||
void SetValue(const std::string &key, const std::string &value) {
|
||||
value_map_[key] = value;
|
||||
}
|
||||
|
||||
|
@ -71,8 +71,7 @@ class BaseGenerator {
|
|||
public:
|
||||
virtual bool generate() = 0;
|
||||
|
||||
static std::string NamespaceDir(const Parser &parser,
|
||||
const std::string &path,
|
||||
static std::string NamespaceDir(const Parser &parser, const std::string &path,
|
||||
const Namespace &ns);
|
||||
|
||||
protected:
|
||||
|
@ -128,8 +127,7 @@ struct CommentConfig {
|
|||
};
|
||||
|
||||
extern void GenComment(const std::vector<std::string> &dc,
|
||||
std::string *code_ptr,
|
||||
const CommentConfig *config,
|
||||
std::string *code_ptr, const CommentConfig *config,
|
||||
const char *prefix = "");
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -14,15 +14,15 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#ifndef FLATC_H_
|
||||
#define FLATC_H_
|
||||
# define FLATC_H_
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
|
@ -49,12 +49,10 @@ class FlatCompiler {
|
|||
MakeRuleFn make_rule;
|
||||
};
|
||||
|
||||
typedef void (*WarnFn)(const FlatCompiler *flatc,
|
||||
const std::string &warn,
|
||||
typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn,
|
||||
bool show_exe_name);
|
||||
|
||||
typedef void (*ErrorFn)(const FlatCompiler *flatc,
|
||||
const std::string &err,
|
||||
typedef void (*ErrorFn)(const FlatCompiler *flatc, const std::string &err,
|
||||
bool usage, bool show_exe_name);
|
||||
|
||||
// Parameters required to initialize the FlatCompiler.
|
||||
|
@ -65,21 +63,20 @@ class FlatCompiler {
|
|||
warn_fn(nullptr),
|
||||
error_fn(nullptr) {}
|
||||
|
||||
const Generator* generators;
|
||||
const Generator *generators;
|
||||
size_t num_generators;
|
||||
WarnFn warn_fn;
|
||||
ErrorFn error_fn;
|
||||
};
|
||||
|
||||
explicit FlatCompiler(const InitParams& params) : params_(params) {}
|
||||
explicit FlatCompiler(const InitParams ¶ms) : params_(params) {}
|
||||
|
||||
int Compile(int argc, const char** argv);
|
||||
int Compile(int argc, const char **argv);
|
||||
|
||||
std::string GetUsageString(const char* program_name) const;
|
||||
std::string GetUsageString(const char *program_name) const;
|
||||
|
||||
private:
|
||||
void ParseFile(flatbuffers::Parser &parser,
|
||||
const std::string &filename,
|
||||
void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
|
||||
const std::string &contents,
|
||||
std::vector<const char *> &include_directories) const;
|
||||
|
||||
|
@ -91,7 +88,6 @@ class FlatCompiler {
|
|||
InitParams params_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATC_H_
|
||||
|
|
|
@ -24,12 +24,12 @@
|
|||
#include "flatbuffers/util.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <intrin.h>
|
||||
# include <intrin.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4127) // C4127: conditional expression is constant
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4127) // C4127: conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace flexbuffers {
|
||||
|
@ -60,24 +60,25 @@ enum Type {
|
|||
TYPE_INDIRECT_UINT = 7,
|
||||
TYPE_INDIRECT_FLOAT = 8,
|
||||
TYPE_MAP = 9,
|
||||
TYPE_VECTOR = 10, // Untyped.
|
||||
TYPE_VECTOR_INT = 11, // Typed any size (stores no type table).
|
||||
TYPE_VECTOR = 10, // Untyped.
|
||||
TYPE_VECTOR_INT = 11, // Typed any size (stores no type table).
|
||||
TYPE_VECTOR_UINT = 12,
|
||||
TYPE_VECTOR_FLOAT = 13,
|
||||
TYPE_VECTOR_KEY = 14,
|
||||
TYPE_VECTOR_STRING = 15,
|
||||
TYPE_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
|
||||
TYPE_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
|
||||
TYPE_VECTOR_UINT2 = 17,
|
||||
TYPE_VECTOR_FLOAT2 = 18,
|
||||
TYPE_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
|
||||
TYPE_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
|
||||
TYPE_VECTOR_UINT3 = 20,
|
||||
TYPE_VECTOR_FLOAT3 = 21,
|
||||
TYPE_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
|
||||
TYPE_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
|
||||
TYPE_VECTOR_UINT4 = 23,
|
||||
TYPE_VECTOR_FLOAT4 = 24,
|
||||
TYPE_BLOB = 25,
|
||||
TYPE_BOOL = 26,
|
||||
TYPE_VECTOR_BOOL = 36, // To Allow the same type of conversion of type to vector type
|
||||
TYPE_VECTOR_BOOL =
|
||||
36, // To Allow the same type of conversion of type to vector type
|
||||
};
|
||||
|
||||
inline bool IsInline(Type t) { return t <= TYPE_FLOAT || t == TYPE_BOOL; }
|
||||
|
@ -87,7 +88,8 @@ inline bool IsTypedVectorElementType(Type t) {
|
|||
}
|
||||
|
||||
inline bool IsTypedVector(Type t) {
|
||||
return (t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING) || t == TYPE_VECTOR_BOOL;
|
||||
return (t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING) ||
|
||||
t == TYPE_VECTOR_BOOL;
|
||||
}
|
||||
|
||||
inline bool IsFixedTypedVector(Type t) {
|
||||
|
@ -113,7 +115,8 @@ inline Type ToTypedVectorElementType(Type t) {
|
|||
inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) {
|
||||
assert(IsFixedTypedVector(t));
|
||||
auto fixed_type = t - TYPE_VECTOR_INT2;
|
||||
*len = static_cast<uint8_t>(fixed_type / 3 + 2); // 3 types each, starting from length 2.
|
||||
*len = static_cast<uint8_t>(fixed_type / 3 +
|
||||
2); // 3 types each, starting from length 2.
|
||||
return static_cast<Type>(fixed_type % 3 + TYPE_INT);
|
||||
}
|
||||
|
||||
|
@ -127,19 +130,20 @@ typedef int8_t quarter;
|
|||
// decently quick, but it is the most frequently executed function.
|
||||
// We could do an (unaligned) 64-bit read if we ifdef out the platforms for
|
||||
// which that doesn't work (or where we'd read into un-owned memory).
|
||||
template <typename R, typename T1, typename T2, typename T4, typename T8>
|
||||
template<typename R, typename T1, typename T2, typename T4, typename T8>
|
||||
R ReadSizedScalar(const uint8_t *data, uint8_t byte_width) {
|
||||
return byte_width < 4
|
||||
? (byte_width < 2 ? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
|
||||
: static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
|
||||
: (byte_width < 8 ? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
|
||||
: static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
|
||||
? (byte_width < 2
|
||||
? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
|
||||
: static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
|
||||
: (byte_width < 8
|
||||
? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
|
||||
: static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
|
||||
}
|
||||
|
||||
|
||||
inline int64_t ReadInt64(const uint8_t *data, uint8_t byte_width) {
|
||||
return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(data,
|
||||
byte_width);
|
||||
return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(
|
||||
data, byte_width);
|
||||
}
|
||||
|
||||
inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
|
||||
|
@ -148,6 +152,7 @@ inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
|
|||
// TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a
|
||||
// constant, which here it isn't. Test if memcpy is still faster than
|
||||
// the conditionals in ReadSizedScalar. Can also use inline asm.
|
||||
// clang-format off
|
||||
#ifdef _MSC_VER
|
||||
uint64_t u = 0;
|
||||
__movsb(reinterpret_cast<uint8_t *>(&u),
|
||||
|
@ -157,11 +162,12 @@ inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
|
|||
return ReadSizedScalar<uint64_t, uint8_t, uint16_t, uint32_t, uint64_t>(
|
||||
data, byte_width);
|
||||
#endif
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
inline double ReadDouble(const uint8_t *data, uint8_t byte_width) {
|
||||
return ReadSizedScalar<double, quarter, half, float, double>(data,
|
||||
byte_width);
|
||||
byte_width);
|
||||
}
|
||||
|
||||
inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) {
|
||||
|
@ -173,13 +179,14 @@ template<typename T> const uint8_t *Indirect(const uint8_t *offset) {
|
|||
}
|
||||
|
||||
inline BitWidth WidthU(uint64_t u) {
|
||||
#define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) { \
|
||||
#define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) \
|
||||
{ \
|
||||
if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \
|
||||
}
|
||||
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 8);
|
||||
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 16);
|
||||
FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 32);
|
||||
#undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
|
||||
#undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
|
||||
return BIT_WIDTH_64;
|
||||
}
|
||||
|
||||
|
@ -198,7 +205,7 @@ inline BitWidth WidthF(double f) {
|
|||
class Object {
|
||||
public:
|
||||
Object(const uint8_t *data, uint8_t byte_width)
|
||||
: data_(data), byte_width_(byte_width) {}
|
||||
: data_(data), byte_width_(byte_width) {}
|
||||
|
||||
protected:
|
||||
const uint8_t *data_;
|
||||
|
@ -216,15 +223,14 @@ class Sized : public Object {
|
|||
|
||||
class String : public Sized {
|
||||
public:
|
||||
String(const uint8_t *data, uint8_t byte_width)
|
||||
: Sized(data, byte_width) {}
|
||||
String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
|
||||
|
||||
size_t length() const { return size(); }
|
||||
const char *c_str() const { return reinterpret_cast<const char *>(data_); }
|
||||
std::string str() const { return std::string(c_str(), length()); }
|
||||
|
||||
static String EmptyString() {
|
||||
static const uint8_t empty_string[] = { 0/*len*/, 0/*terminator*/ };
|
||||
static const uint8_t empty_string[] = { 0 /*len*/, 0 /*terminator*/ };
|
||||
return String(empty_string + 1, 1);
|
||||
}
|
||||
bool IsTheEmptyString() const { return data_ == EmptyString().data_; }
|
||||
|
@ -233,10 +239,10 @@ class String : public Sized {
|
|||
class Blob : public Sized {
|
||||
public:
|
||||
Blob(const uint8_t *data_buf, uint8_t byte_width)
|
||||
: Sized(data_buf, byte_width) {}
|
||||
: Sized(data_buf, byte_width) {}
|
||||
|
||||
static Blob EmptyBlob() {
|
||||
static const uint8_t empty_blob[] = { 0/*len*/ };
|
||||
static const uint8_t empty_blob[] = { 0 /*len*/ };
|
||||
return Blob(empty_blob + 1, 1);
|
||||
}
|
||||
bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; }
|
||||
|
@ -245,13 +251,12 @@ class Blob : public Sized {
|
|||
|
||||
class Vector : public Sized {
|
||||
public:
|
||||
Vector(const uint8_t *data, uint8_t byte_width)
|
||||
: Sized(data, byte_width) {}
|
||||
Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
|
||||
|
||||
Reference operator[](size_t i) const;
|
||||
|
||||
static Vector EmptyVector() {
|
||||
static const uint8_t empty_vector[] = { 0/*len*/ };
|
||||
static const uint8_t empty_vector[] = { 0 /*len*/ };
|
||||
return Vector(empty_vector + 1, 1);
|
||||
}
|
||||
bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; }
|
||||
|
@ -260,12 +265,12 @@ class Vector : public Sized {
|
|||
class TypedVector : public Sized {
|
||||
public:
|
||||
TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type)
|
||||
: Sized(data, byte_width), type_(element_type) {}
|
||||
: Sized(data, byte_width), type_(element_type) {}
|
||||
|
||||
Reference operator[](size_t i) const;
|
||||
|
||||
static TypedVector EmptyTypedVector() {
|
||||
static const uint8_t empty_typed_vector[] = { 0/*len*/ };
|
||||
static const uint8_t empty_typed_vector[] = { 0 /*len*/ };
|
||||
return TypedVector(empty_typed_vector + 1, 1, TYPE_INT);
|
||||
}
|
||||
bool IsTheEmptyVector() const {
|
||||
|
@ -284,12 +289,12 @@ class FixedTypedVector : public Object {
|
|||
public:
|
||||
FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type,
|
||||
uint8_t len)
|
||||
: Object(data, byte_width), type_(element_type), len_(len) {}
|
||||
: Object(data, byte_width), type_(element_type), len_(len) {}
|
||||
|
||||
Reference operator[](size_t i) const;
|
||||
|
||||
static FixedTypedVector EmptyFixedTypedVector() {
|
||||
static const uint8_t fixed_empty_vector[] = { 0/* unused */ };
|
||||
static const uint8_t fixed_empty_vector[] = { 0 /* unused */ };
|
||||
return FixedTypedVector(fixed_empty_vector, 1, TYPE_INT, 0);
|
||||
}
|
||||
bool IsTheEmptyFixedTypedVector() const {
|
||||
|
@ -306,8 +311,7 @@ class FixedTypedVector : public Object {
|
|||
|
||||
class Map : public Vector {
|
||||
public:
|
||||
Map(const uint8_t *data, uint8_t byte_width)
|
||||
: Vector(data, byte_width) {}
|
||||
Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {}
|
||||
|
||||
Reference operator[](const char *key) const;
|
||||
Reference operator[](const std::string &key) const;
|
||||
|
@ -319,31 +323,31 @@ class Map : public Vector {
|
|||
auto keys_offset = data_ - byte_width_ * num_prefixed_fields;
|
||||
return TypedVector(Indirect(keys_offset, byte_width_),
|
||||
static_cast<uint8_t>(
|
||||
ReadUInt64(keys_offset + byte_width_, byte_width_)),
|
||||
ReadUInt64(keys_offset + byte_width_, byte_width_)),
|
||||
TYPE_KEY);
|
||||
}
|
||||
|
||||
static Map EmptyMap() {
|
||||
static const uint8_t empty_map[] = {
|
||||
0/*keys_len*/, 0/*keys_offset*/, 1/*keys_width*/, 0/*len*/
|
||||
0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/
|
||||
};
|
||||
return Map(empty_map + 4, 1);
|
||||
}
|
||||
|
||||
bool IsTheEmptyMap() const {
|
||||
return data_ == EmptyMap().data_;
|
||||
}
|
||||
bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; }
|
||||
};
|
||||
|
||||
class Reference {
|
||||
public:
|
||||
Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
|
||||
Type type)
|
||||
: data_(data), parent_width_(parent_width), byte_width_(byte_width),
|
||||
type_(type) {}
|
||||
: data_(data),
|
||||
parent_width_(parent_width),
|
||||
byte_width_(byte_width),
|
||||
type_(type) {}
|
||||
|
||||
Reference(const uint8_t *data, uint8_t parent_width, uint8_t packed_type)
|
||||
: data_(data), parent_width_(parent_width) {
|
||||
: data_(data), parent_width_(parent_width) {
|
||||
byte_width_ = 1U << static_cast<BitWidth>(packed_type & 3);
|
||||
type_ = static_cast<Type>(packed_type >> 2);
|
||||
}
|
||||
|
@ -352,13 +356,14 @@ class Reference {
|
|||
|
||||
bool IsNull() const { return type_ == TYPE_NULL; }
|
||||
bool IsBool() const { return type_ == TYPE_BOOL; }
|
||||
bool IsInt() const { return type_ == TYPE_INT ||
|
||||
type_ == TYPE_INDIRECT_INT; }
|
||||
bool IsUInt() const { return type_ == TYPE_UINT||
|
||||
type_ == TYPE_INDIRECT_UINT;; }
|
||||
bool IsInt() const { return type_ == TYPE_INT || type_ == TYPE_INDIRECT_INT; }
|
||||
bool IsUInt() const {
|
||||
return type_ == TYPE_UINT || type_ == TYPE_INDIRECT_UINT;
|
||||
}
|
||||
bool IsIntOrUint() const { return IsInt() || IsUInt(); }
|
||||
bool IsFloat() const { return type_ == TYPE_FLOAT ||
|
||||
type_ == TYPE_INDIRECT_FLOAT; }
|
||||
bool IsFloat() const {
|
||||
return type_ == TYPE_FLOAT || type_ == TYPE_INDIRECT_FLOAT;
|
||||
}
|
||||
bool IsNumeric() const { return IsIntOrUint() || IsFloat(); }
|
||||
bool IsString() const { return type_ == TYPE_STRING; }
|
||||
bool IsKey() const { return type_ == TYPE_KEY; }
|
||||
|
@ -367,7 +372,8 @@ class Reference {
|
|||
bool IsBlob() const { return type_ == TYPE_BLOB; }
|
||||
|
||||
bool AsBool() const {
|
||||
return (type_ == TYPE_BOOL ? ReadUInt64(data_, parent_width_) : AsUInt64()) != 0;
|
||||
return (type_ == TYPE_BOOL ? ReadUInt64(data_, parent_width_)
|
||||
: AsUInt64()) != 0;
|
||||
}
|
||||
|
||||
// Reads any type as a int64_t. Never fails, does most sensible conversion.
|
||||
|
@ -377,79 +383,82 @@ class Reference {
|
|||
if (type_ == TYPE_INT) {
|
||||
// A fast path for the common case.
|
||||
return ReadInt64(data_, parent_width_);
|
||||
} else switch (type_) {
|
||||
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
|
||||
case TYPE_UINT: return ReadUInt64(data_, parent_width_);
|
||||
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
|
||||
case TYPE_FLOAT: return static_cast<int64_t>(
|
||||
ReadDouble(data_, parent_width_));
|
||||
case TYPE_INDIRECT_FLOAT: return static_cast<int64_t>(
|
||||
ReadDouble(Indirect(), byte_width_));
|
||||
case TYPE_NULL: return 0;
|
||||
case TYPE_STRING: return flatbuffers::StringToInt(AsString().c_str());
|
||||
case TYPE_VECTOR: return static_cast<int64_t>(AsVector().size());
|
||||
case TYPE_BOOL: return ReadInt64(data_, parent_width_);
|
||||
default:
|
||||
// Convert other things to int.
|
||||
return 0;
|
||||
}
|
||||
} else
|
||||
switch (type_) {
|
||||
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
|
||||
case TYPE_UINT: return ReadUInt64(data_, parent_width_);
|
||||
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
|
||||
case TYPE_FLOAT:
|
||||
return static_cast<int64_t>(ReadDouble(data_, parent_width_));
|
||||
case TYPE_INDIRECT_FLOAT:
|
||||
return static_cast<int64_t>(ReadDouble(Indirect(), byte_width_));
|
||||
case TYPE_NULL: return 0;
|
||||
case TYPE_STRING: return flatbuffers::StringToInt(AsString().c_str());
|
||||
case TYPE_VECTOR: return static_cast<int64_t>(AsVector().size());
|
||||
case TYPE_BOOL: return ReadInt64(data_, parent_width_);
|
||||
default:
|
||||
// Convert other things to int.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: could specialize these to not use AsInt64() if that saves
|
||||
// extension ops in generated code, and use a faster op than ReadInt64.
|
||||
int32_t AsInt32() const { return static_cast<int32_t>(AsInt64()); }
|
||||
int16_t AsInt16() const { return static_cast<int16_t>(AsInt64()); }
|
||||
int8_t AsInt8() const { return static_cast<int8_t> (AsInt64()); }
|
||||
int8_t AsInt8() const { return static_cast<int8_t>(AsInt64()); }
|
||||
|
||||
uint64_t AsUInt64() const {
|
||||
if (type_ == TYPE_UINT) {
|
||||
// A fast path for the common case.
|
||||
return ReadUInt64(data_, parent_width_);
|
||||
} else switch (type_) {
|
||||
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
|
||||
case TYPE_INT: return ReadInt64(data_, parent_width_);
|
||||
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
|
||||
case TYPE_FLOAT: return static_cast<uint64_t>(
|
||||
ReadDouble(data_, parent_width_));
|
||||
case TYPE_INDIRECT_FLOAT: return static_cast<uint64_t>(
|
||||
ReadDouble(Indirect(), byte_width_));
|
||||
case TYPE_NULL: return 0;
|
||||
case TYPE_STRING: return flatbuffers::StringToUInt(AsString().c_str());
|
||||
case TYPE_VECTOR: return static_cast<uint64_t>(AsVector().size());
|
||||
case TYPE_BOOL: return ReadUInt64(data_, parent_width_);
|
||||
default:
|
||||
// Convert other things to uint.
|
||||
return 0;
|
||||
}
|
||||
} else
|
||||
switch (type_) {
|
||||
case TYPE_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
|
||||
case TYPE_INT: return ReadInt64(data_, parent_width_);
|
||||
case TYPE_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
|
||||
case TYPE_FLOAT:
|
||||
return static_cast<uint64_t>(ReadDouble(data_, parent_width_));
|
||||
case TYPE_INDIRECT_FLOAT:
|
||||
return static_cast<uint64_t>(ReadDouble(Indirect(), byte_width_));
|
||||
case TYPE_NULL: return 0;
|
||||
case TYPE_STRING: return flatbuffers::StringToUInt(AsString().c_str());
|
||||
case TYPE_VECTOR: return static_cast<uint64_t>(AsVector().size());
|
||||
case TYPE_BOOL: return ReadUInt64(data_, parent_width_);
|
||||
default:
|
||||
// Convert other things to uint.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t AsUInt32() const { return static_cast<uint32_t>(AsUInt64()); }
|
||||
uint16_t AsUInt16() const { return static_cast<uint16_t>(AsUInt64()); }
|
||||
uint8_t AsUInt8() const { return static_cast<uint8_t> (AsUInt64()); }
|
||||
uint8_t AsUInt8() const { return static_cast<uint8_t>(AsUInt64()); }
|
||||
|
||||
double AsDouble() const {
|
||||
if (type_ == TYPE_FLOAT) {
|
||||
// A fast path for the common case.
|
||||
return ReadDouble(data_, parent_width_);
|
||||
} else switch (type_) {
|
||||
case TYPE_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
|
||||
case TYPE_INT: return static_cast<double>(
|
||||
ReadInt64(data_, parent_width_));
|
||||
case TYPE_UINT: return static_cast<double>(
|
||||
ReadUInt64(data_, parent_width_));
|
||||
case TYPE_INDIRECT_INT: return static_cast<double>(
|
||||
ReadInt64(Indirect(), byte_width_));
|
||||
case TYPE_INDIRECT_UINT: return static_cast<double>(
|
||||
ReadUInt64(Indirect(), byte_width_));
|
||||
case TYPE_NULL: return 0.0;
|
||||
case TYPE_STRING: return strtod(AsString().c_str(), nullptr);
|
||||
case TYPE_VECTOR: return static_cast<double>(AsVector().size());
|
||||
case TYPE_BOOL: return static_cast<double>(
|
||||
ReadUInt64(data_, parent_width_));
|
||||
default:
|
||||
// Convert strings and other things to float.
|
||||
return 0;
|
||||
}
|
||||
} else
|
||||
switch (type_) {
|
||||
case TYPE_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
|
||||
case TYPE_INT:
|
||||
return static_cast<double>(ReadInt64(data_, parent_width_));
|
||||
case TYPE_UINT:
|
||||
return static_cast<double>(ReadUInt64(data_, parent_width_));
|
||||
case TYPE_INDIRECT_INT:
|
||||
return static_cast<double>(ReadInt64(Indirect(), byte_width_));
|
||||
case TYPE_INDIRECT_UINT:
|
||||
return static_cast<double>(ReadUInt64(Indirect(), byte_width_));
|
||||
case TYPE_NULL: return 0.0;
|
||||
case TYPE_STRING: return strtod(AsString().c_str(), nullptr);
|
||||
case TYPE_VECTOR: return static_cast<double>(AsVector().size());
|
||||
case TYPE_BOOL:
|
||||
return static_cast<double>(ReadUInt64(data_, parent_width_));
|
||||
default:
|
||||
// Convert strings and other things to float.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
float AsFloat() const { return static_cast<float>(AsDouble()); }
|
||||
|
@ -652,9 +661,7 @@ class Reference {
|
|||
memcpy(const_cast<char *>(s.c_str()), str, len);
|
||||
return true;
|
||||
}
|
||||
bool MutateString(const char *str) {
|
||||
return MutateString(str, strlen(str));
|
||||
}
|
||||
bool MutateString(const char *str) { return MutateString(str, strlen(str)); }
|
||||
bool MutateString(const std::string &str) {
|
||||
return MutateString(str.data(), str.length());
|
||||
}
|
||||
|
@ -664,9 +671,11 @@ class Reference {
|
|||
return flexbuffers::Indirect(data_, parent_width_);
|
||||
}
|
||||
|
||||
template<typename T> bool Mutate(const uint8_t *dest, T t, size_t byte_width,
|
||||
BitWidth value_width) {
|
||||
auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <= byte_width;
|
||||
template<typename T>
|
||||
bool Mutate(const uint8_t *dest, T t, size_t byte_width,
|
||||
BitWidth value_width) {
|
||||
auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <=
|
||||
byte_width;
|
||||
if (fits) {
|
||||
t = flatbuffers::EndianScalar(t);
|
||||
memcpy(const_cast<uint8_t *>(dest), &t, byte_width);
|
||||
|
@ -674,8 +683,9 @@ class Reference {
|
|||
return fits;
|
||||
}
|
||||
|
||||
template<typename T> bool MutateF(const uint8_t *dest, T t, size_t byte_width,
|
||||
BitWidth value_width) {
|
||||
template<typename T>
|
||||
bool MutateF(const uint8_t *dest, T t, size_t byte_width,
|
||||
BitWidth value_width) {
|
||||
if (byte_width == sizeof(double))
|
||||
return Mutate(dest, static_cast<double>(t), byte_width, value_width);
|
||||
if (byte_width == sizeof(float))
|
||||
|
@ -707,21 +717,25 @@ template<> inline double Reference::As<double>() { return AsDouble(); }
|
|||
template<> inline float Reference::As<float>() { return AsFloat(); }
|
||||
|
||||
template<> inline String Reference::As<String>() { return AsString(); }
|
||||
template<> inline std::string Reference::As<std::string>() { return AsString().str(); }
|
||||
template<> inline std::string Reference::As<std::string>() {
|
||||
return AsString().str();
|
||||
}
|
||||
|
||||
template<> inline Blob Reference::As<Blob>() { return AsBlob(); }
|
||||
template<> inline Vector Reference::As<Vector>() { return AsVector(); }
|
||||
template<> inline TypedVector Reference::As<TypedVector>() { return AsTypedVector(); }
|
||||
template<> inline FixedTypedVector Reference::As<FixedTypedVector>() { return AsFixedTypedVector(); }
|
||||
template<> inline TypedVector Reference::As<TypedVector>() {
|
||||
return AsTypedVector();
|
||||
}
|
||||
template<> inline FixedTypedVector Reference::As<FixedTypedVector>() {
|
||||
return AsFixedTypedVector();
|
||||
}
|
||||
template<> inline Map Reference::As<Map>() { return AsMap(); }
|
||||
|
||||
inline uint8_t PackedType(BitWidth bit_width, Type type) {
|
||||
return static_cast<uint8_t>(bit_width | (type << 2));
|
||||
}
|
||||
|
||||
inline uint8_t NullPackedType() {
|
||||
return PackedType(BIT_WIDTH_8, TYPE_NULL);
|
||||
}
|
||||
inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, TYPE_NULL); }
|
||||
|
||||
// Vector accessors.
|
||||
// Note: if you try to access outside of bounds, you get a Null value back
|
||||
|
@ -730,7 +744,7 @@ inline uint8_t NullPackedType() {
|
|||
// wanted 3d).
|
||||
// The Null converts seamlessly into a default value for any other type.
|
||||
// TODO(wvo): Could introduce an #ifdef that makes this into an assert?
|
||||
inline Reference Vector::operator[](size_t i) const {
|
||||
inline Reference Vector::operator[](size_t i) const {
|
||||
auto len = size();
|
||||
if (i >= len) return Reference(nullptr, 1, NullPackedType());
|
||||
auto packed_type = (data_ + len * byte_width_)[i];
|
||||
|
@ -738,14 +752,14 @@ inline Reference Vector::operator[](size_t i) const {
|
|||
return Reference(elem, byte_width_, packed_type);
|
||||
}
|
||||
|
||||
inline Reference TypedVector::operator[](size_t i) const {
|
||||
inline Reference TypedVector::operator[](size_t i) const {
|
||||
auto len = size();
|
||||
if (i >= len) return Reference(nullptr, 1, NullPackedType());
|
||||
auto elem = data_ + i * byte_width_;
|
||||
return Reference(elem, byte_width_, 1, type_);
|
||||
}
|
||||
|
||||
inline Reference FixedTypedVector::operator[](size_t i) const {
|
||||
inline Reference FixedTypedVector::operator[](size_t i) const {
|
||||
if (i >= len_) return Reference(nullptr, 1, NullPackedType());
|
||||
auto elem = data_ + i * byte_width_;
|
||||
return Reference(elem, byte_width_, 1, type_);
|
||||
|
@ -753,7 +767,7 @@ inline Reference FixedTypedVector::operator[](size_t i) const {
|
|||
|
||||
template<typename T> int KeyCompare(const void *key, const void *elem) {
|
||||
auto str_elem = reinterpret_cast<const char *>(
|
||||
Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
|
||||
Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
|
||||
auto skey = reinterpret_cast<const char *>(key);
|
||||
return strcmp(skey, str_elem);
|
||||
}
|
||||
|
@ -770,8 +784,7 @@ inline Reference Map::operator[](const char *key) const {
|
|||
case 8: comp = KeyCompare<uint64_t>; break;
|
||||
}
|
||||
auto res = std::bsearch(key, keys.data_, keys.size(), keys.byte_width_, comp);
|
||||
if (!res)
|
||||
return Reference(nullptr, 1, NullPackedType());
|
||||
if (!res) return Reference(nullptr, 1, NullPackedType());
|
||||
auto i = (reinterpret_cast<uint8_t *>(res) - keys.data_) / keys.byte_width_;
|
||||
return (*static_cast<const Vector *>(this))[i];
|
||||
}
|
||||
|
@ -817,8 +830,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
public:
|
||||
Builder(size_t initial_size = 256,
|
||||
BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS)
|
||||
: buf_(initial_size), finished_(false), flags_(flags),
|
||||
force_min_bit_width_(BIT_WIDTH_8), key_pool(KeyOffsetCompare(buf_)),
|
||||
: buf_(initial_size),
|
||||
finished_(false),
|
||||
flags_(flags),
|
||||
force_min_bit_width_(BIT_WIDTH_8),
|
||||
key_pool(KeyOffsetCompare(buf_)),
|
||||
string_pool(StringOffsetCompare(buf_)) {
|
||||
buf_.clear();
|
||||
}
|
||||
|
@ -831,9 +847,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
}
|
||||
|
||||
// Size of the buffer. Does not include unfinished values.
|
||||
size_t GetSize() const {
|
||||
return buf_.size();
|
||||
}
|
||||
size_t GetSize() const { return buf_.size(); }
|
||||
|
||||
// Reset all state so we can re-use the buffer.
|
||||
void Clear() {
|
||||
|
@ -851,26 +865,42 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
// vectors and elsewhere).
|
||||
|
||||
void Null() { stack_.push_back(Value()); }
|
||||
void Null(const char *key) { Key(key); Null(); }
|
||||
void Null(const char *key) {
|
||||
Key(key);
|
||||
Null();
|
||||
}
|
||||
|
||||
void Int(int64_t i) { stack_.push_back(Value(i, TYPE_INT, WidthI(i))); }
|
||||
void Int(const char *key, int64_t i) { Key(key); Int(i); }
|
||||
void Int(const char *key, int64_t i) {
|
||||
Key(key);
|
||||
Int(i);
|
||||
}
|
||||
|
||||
void UInt(uint64_t u) { stack_.push_back(Value(u, TYPE_UINT, WidthU(u))); }
|
||||
void UInt(const char *key, uint64_t u) { Key(key); Int(u); }
|
||||
void UInt(const char *key, uint64_t u) {
|
||||
Key(key);
|
||||
Int(u);
|
||||
}
|
||||
|
||||
void Float(float f) { stack_.push_back(Value(f)); }
|
||||
void Float(const char *key, float f) { Key(key); Float(f); }
|
||||
void Float(const char *key, float f) {
|
||||
Key(key);
|
||||
Float(f);
|
||||
}
|
||||
|
||||
void Double(double f) { stack_.push_back(Value(f)); }
|
||||
void Double(const char *key, double d) { Key(key); Double(d); }
|
||||
void Double(const char *key, double d) {
|
||||
Key(key);
|
||||
Double(d);
|
||||
}
|
||||
|
||||
void Bool(bool b) { stack_.push_back(Value(b)); }
|
||||
void Bool(const char *key, bool b) { Key(key); Bool(b); }
|
||||
|
||||
void IndirectInt(int64_t i) {
|
||||
PushIndirect(i, TYPE_INDIRECT_INT, WidthI(i));
|
||||
void Bool(const char *key, bool b) {
|
||||
Key(key);
|
||||
Bool(b);
|
||||
}
|
||||
|
||||
void IndirectInt(int64_t i) { PushIndirect(i, TYPE_INDIRECT_INT, WidthI(i)); }
|
||||
void IndirectInt(const char *key, int64_t i) {
|
||||
Key(key);
|
||||
IndirectInt(i);
|
||||
|
@ -939,9 +969,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
}
|
||||
return sloc;
|
||||
}
|
||||
size_t String(const char *str) {
|
||||
return String(str, strlen(str));
|
||||
}
|
||||
size_t String(const char *str) { return String(str, strlen(str)); }
|
||||
size_t String(const std::string &str) {
|
||||
return String(str.c_str(), str.size());
|
||||
}
|
||||
|
@ -974,9 +1002,15 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
// Also some FlatBuffers types?
|
||||
|
||||
size_t StartVector() { return stack_.size(); }
|
||||
size_t StartVector(const char *key) { Key(key); return stack_.size(); }
|
||||
size_t StartVector(const char *key) {
|
||||
Key(key);
|
||||
return stack_.size();
|
||||
}
|
||||
size_t StartMap() { return stack_.size(); }
|
||||
size_t StartMap(const char *key) { Key(key); return stack_.size(); }
|
||||
size_t StartMap(const char *key) {
|
||||
Key(key);
|
||||
return stack_.size();
|
||||
}
|
||||
|
||||
// TODO(wvo): allow this to specify an aligment greater than the natural
|
||||
// alignment.
|
||||
|
@ -1000,7 +1034,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
}
|
||||
// Now sort values, so later we can do a binary seach lookup.
|
||||
// We want to sort 2 array elements at a time.
|
||||
struct TwoValue { Value key; Value val; };
|
||||
struct TwoValue {
|
||||
Value key;
|
||||
Value val;
|
||||
};
|
||||
// TODO(wvo): strict aliasing?
|
||||
// TODO(wvo): allow the caller to indicate the data is already sorted
|
||||
// for maximum efficiency? With an assert to check sortedness to make sure
|
||||
|
@ -1011,22 +1048,22 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
// sorted fashion.
|
||||
// std::sort is typically already a lot faster on sorted data though.
|
||||
auto dict =
|
||||
reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) +
|
||||
start);
|
||||
reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) + start);
|
||||
std::sort(dict, dict + len,
|
||||
[&](const TwoValue &a, const TwoValue &b) -> bool {
|
||||
auto as = reinterpret_cast<const char *>(
|
||||
flatbuffers::vector_data(buf_) + a.key.u_);
|
||||
auto bs = reinterpret_cast<const char *>(
|
||||
flatbuffers::vector_data(buf_) + b.key.u_);
|
||||
auto comp = strcmp(as, bs);
|
||||
// If this assertion hits, you've added two keys with the same value to
|
||||
// this map.
|
||||
// TODO: Have to check for pointer equality, as some sort implementation
|
||||
// apparently call this function with the same element?? Why?
|
||||
assert(comp || &a == &b);
|
||||
return comp < 0;
|
||||
});
|
||||
auto as = reinterpret_cast<const char *>(
|
||||
flatbuffers::vector_data(buf_) + a.key.u_);
|
||||
auto bs = reinterpret_cast<const char *>(
|
||||
flatbuffers::vector_data(buf_) + b.key.u_);
|
||||
auto comp = strcmp(as, bs);
|
||||
// If this assertion hits, you've added two keys with the same
|
||||
// value to this map.
|
||||
// TODO: Have to check for pointer equality, as some sort
|
||||
// implementation apparently call this function with the same
|
||||
// element?? Why?
|
||||
assert(comp || &a == &b);
|
||||
return comp < 0;
|
||||
});
|
||||
// First create a vector out of all keys.
|
||||
// TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share
|
||||
// the first vector.
|
||||
|
@ -1043,7 +1080,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
f();
|
||||
return EndVector(start, false, false);
|
||||
}
|
||||
template <typename F, typename T> size_t Vector(F f, T &state) {
|
||||
template<typename F, typename T> size_t Vector(F f, T &state) {
|
||||
auto start = StartVector();
|
||||
f(state);
|
||||
return EndVector(start, false, false);
|
||||
|
@ -1053,8 +1090,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
f();
|
||||
return EndVector(start, false, false);
|
||||
}
|
||||
template <typename F, typename T> size_t Vector(const char *key, F f,
|
||||
T &state) {
|
||||
template<typename F, typename T>
|
||||
size_t Vector(const char *key, F f, T &state) {
|
||||
auto start = StartVector(key);
|
||||
f(state);
|
||||
return EndVector(start, false, false);
|
||||
|
@ -1070,8 +1107,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
EndVector(start, false, false);
|
||||
}
|
||||
}
|
||||
template<typename T> void Vector(const char *key, const T *elems,
|
||||
size_t len) {
|
||||
template<typename T>
|
||||
void Vector(const char *key, const T *elems, size_t len) {
|
||||
Key(key);
|
||||
Vector(elems, len);
|
||||
}
|
||||
|
@ -1084,7 +1121,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
f();
|
||||
return EndVector(start, true, false);
|
||||
}
|
||||
template <typename F, typename T> size_t TypedVector(F f, T &state) {
|
||||
template<typename F, typename T> size_t TypedVector(F f, T &state) {
|
||||
auto start = StartVector();
|
||||
f(state);
|
||||
return EndVector(start, true, false);
|
||||
|
@ -1094,8 +1131,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
f();
|
||||
return EndVector(start, true, false);
|
||||
}
|
||||
template <typename F, typename T> size_t TypedVector(const char *key, F f,
|
||||
T &state) {
|
||||
template<typename F, typename T>
|
||||
size_t TypedVector(const char *key, F f, T &state) {
|
||||
auto start = StartVector(key);
|
||||
f(state);
|
||||
return EndVector(start, true, false);
|
||||
|
@ -1110,8 +1147,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
return ScalarVector(elems, len, true);
|
||||
}
|
||||
|
||||
template<typename T> size_t FixedTypedVector(const char *key, const T *elems,
|
||||
size_t len) {
|
||||
template<typename T>
|
||||
size_t FixedTypedVector(const char *key, const T *elems, size_t len) {
|
||||
Key(key);
|
||||
return FixedTypedVector(elems, len);
|
||||
}
|
||||
|
@ -1121,7 +1158,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
f();
|
||||
return EndMap(start);
|
||||
}
|
||||
template <typename F, typename T> size_t Map(F f, T &state) {
|
||||
template<typename F, typename T> size_t Map(F f, T &state) {
|
||||
auto start = StartMap();
|
||||
f(state);
|
||||
return EndMap(start);
|
||||
|
@ -1131,8 +1168,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
f();
|
||||
return EndMap(start);
|
||||
}
|
||||
template <typename F, typename T> size_t Map(const char *key, F f,
|
||||
T &state) {
|
||||
template<typename F, typename T> size_t Map(const char *key, F f, T &state) {
|
||||
auto start = StartMap(key);
|
||||
f(state);
|
||||
return EndMap(start);
|
||||
|
@ -1160,9 +1196,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
void Add(const std::string &str) { String(str); }
|
||||
void Add(const flexbuffers::String &str) { String(str); }
|
||||
|
||||
template<typename T> void Add(const std::vector<T> &vec) {
|
||||
Vector(vec);
|
||||
}
|
||||
template<typename T> void Add(const std::vector<T> &vec) { Vector(vec); }
|
||||
|
||||
template<typename T> void Add(const char *key, const T &t) {
|
||||
Key(key);
|
||||
|
@ -1173,9 +1207,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
Map(map);
|
||||
}
|
||||
|
||||
template<typename T> void operator+=(const T &t) {
|
||||
Add(t);
|
||||
}
|
||||
template<typename T> void operator+=(const T &t) { Add(t); }
|
||||
|
||||
// This function is useful in combination with the Mutate* functions above.
|
||||
// It forces elements of vectors and maps to have a minimum size, such that
|
||||
|
@ -1220,8 +1252,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
}
|
||||
|
||||
void WriteBytes(const void *val, size_t size) {
|
||||
buf_.insert(buf_.end(),
|
||||
reinterpret_cast<const uint8_t *>(val),
|
||||
buf_.insert(buf_.end(), reinterpret_cast<const uint8_t *>(val),
|
||||
reinterpret_cast<const uint8_t *>(val) + size);
|
||||
}
|
||||
|
||||
|
@ -1235,8 +1266,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
switch (byte_width) {
|
||||
case 8: Write(f, byte_width); break;
|
||||
case 4: Write(static_cast<float>(f), byte_width); break;
|
||||
//case 2: Write(static_cast<half>(f), byte_width); break;
|
||||
//case 1: Write(static_cast<quarter>(f), byte_width); break;
|
||||
// case 2: Write(static_cast<half>(f), byte_width); break;
|
||||
// case 1: Write(static_cast<quarter>(f), byte_width); break;
|
||||
default: assert(0);
|
||||
}
|
||||
}
|
||||
|
@ -1267,9 +1298,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
template<typename T> static Type GetScalarType() {
|
||||
assert(flatbuffers::is_scalar<T>::value);
|
||||
return flatbuffers::is_floating_point<T>::value
|
||||
? TYPE_FLOAT
|
||||
: flatbuffers::is_same<T, bool>::value ? TYPE_BOOL
|
||||
: (flatbuffers::is_unsigned<T>::value ? TYPE_UINT : TYPE_INT);
|
||||
? TYPE_FLOAT
|
||||
: flatbuffers::is_same<T, bool>::value
|
||||
? TYPE_BOOL
|
||||
: (flatbuffers::is_unsigned<T>::value ? TYPE_UINT
|
||||
: TYPE_INT);
|
||||
}
|
||||
|
||||
struct Value {
|
||||
|
@ -1286,19 +1319,20 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
|
||||
Value() : i_(0), type_(TYPE_NULL), min_bit_width_(BIT_WIDTH_8) {}
|
||||
|
||||
Value(bool b) : u_(static_cast<uint64_t>(b)), type_(TYPE_BOOL), min_bit_width_(BIT_WIDTH_8) {}
|
||||
Value(bool b)
|
||||
: u_(static_cast<uint64_t>(b)),
|
||||
type_(TYPE_BOOL),
|
||||
min_bit_width_(BIT_WIDTH_8) {}
|
||||
|
||||
Value(int64_t i, Type t, BitWidth bw)
|
||||
: i_(i), type_(t), min_bit_width_(bw) {}
|
||||
: i_(i), type_(t), min_bit_width_(bw) {}
|
||||
Value(uint64_t u, Type t, BitWidth bw)
|
||||
: u_(u), type_(t), min_bit_width_(bw) {}
|
||||
: u_(u), type_(t), min_bit_width_(bw) {}
|
||||
|
||||
Value(float f)
|
||||
: f_(f), type_(TYPE_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
|
||||
Value(double f)
|
||||
: f_(f), type_(TYPE_FLOAT), min_bit_width_(WidthF(f)) {}
|
||||
Value(float f) : f_(f), type_(TYPE_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
|
||||
Value(double f) : f_(f), type_(TYPE_FLOAT), min_bit_width_(WidthF(f)) {}
|
||||
|
||||
uint8_t StoredPackedType(BitWidth parent_bit_width_= BIT_WIDTH_8) const {
|
||||
uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
|
||||
return PackedType(StoredWidth(parent_bit_width_), type_);
|
||||
}
|
||||
|
||||
|
@ -1315,15 +1349,15 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
byte_width <= sizeof(flatbuffers::largest_scalar_t);
|
||||
byte_width *= 2) {
|
||||
// Where are we going to write this offset?
|
||||
auto offset_loc =
|
||||
buf_size +
|
||||
flatbuffers::PaddingBytes(buf_size, byte_width) +
|
||||
elem_index * byte_width;
|
||||
auto offset_loc = buf_size +
|
||||
flatbuffers::PaddingBytes(buf_size, byte_width) +
|
||||
elem_index * byte_width;
|
||||
// Compute relative offset.
|
||||
auto offset = offset_loc - u_;
|
||||
// Does it fit?
|
||||
auto bit_width = WidthU(offset);
|
||||
if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) == byte_width)
|
||||
if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) ==
|
||||
byte_width)
|
||||
return bit_width;
|
||||
}
|
||||
assert(false); // Must match one of the sizes above.
|
||||
|
@ -1333,9 +1367,9 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
|
||||
BitWidth StoredWidth(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
|
||||
if (IsInline(type_)) {
|
||||
return (std::max)(min_bit_width_, parent_bit_width_);
|
||||
return (std::max)(min_bit_width_, parent_bit_width_);
|
||||
} else {
|
||||
return min_bit_width_;
|
||||
return min_bit_width_;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1343,19 +1377,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
void WriteAny(const Value &val, uint8_t byte_width) {
|
||||
switch (val.type_) {
|
||||
case TYPE_NULL:
|
||||
case TYPE_INT:
|
||||
Write(val.i_, byte_width);
|
||||
break;
|
||||
case TYPE_INT: Write(val.i_, byte_width); break;
|
||||
case TYPE_BOOL:
|
||||
case TYPE_UINT:
|
||||
Write(val.u_, byte_width);
|
||||
break;
|
||||
case TYPE_FLOAT:
|
||||
WriteDouble(val.f_, byte_width);
|
||||
break;
|
||||
default:
|
||||
WriteOffset(val.u_, byte_width);
|
||||
break;
|
||||
case TYPE_UINT: Write(val.u_, byte_width); break;
|
||||
case TYPE_FLOAT: WriteDouble(val.f_, byte_width); break;
|
||||
default: WriteOffset(val.u_, byte_width); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1369,8 +1395,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
return sloc;
|
||||
}
|
||||
|
||||
template<typename T> size_t ScalarVector(const T *elems, size_t len,
|
||||
bool fixed) {
|
||||
template<typename T>
|
||||
size_t ScalarVector(const T *elems, size_t len, bool fixed) {
|
||||
auto vector_type = GetScalarType<T>();
|
||||
auto byte_width = sizeof(T);
|
||||
auto bit_width = WidthB(byte_width);
|
||||
|
@ -1436,12 +1462,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
buf_.push_back(stack_[i].StoredPackedType(bit_width));
|
||||
}
|
||||
}
|
||||
return Value(static_cast<uint64_t>(vloc), keys
|
||||
? TYPE_MAP
|
||||
: (typed
|
||||
? ToTypedVector(vector_type, fixed ? vec_len : 0)
|
||||
: TYPE_VECTOR),
|
||||
bit_width);
|
||||
return Value(static_cast<uint64_t>(vloc),
|
||||
keys ? TYPE_MAP
|
||||
: (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0)
|
||||
: TYPE_VECTOR),
|
||||
bit_width);
|
||||
}
|
||||
|
||||
// You shouldn't really be copying instances of this class.
|
||||
|
@ -1473,10 +1498,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
struct StringOffsetCompare {
|
||||
StringOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
|
||||
bool operator()(const StringOffset &a, const StringOffset &b) const {
|
||||
auto stra = reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) +
|
||||
a.first);
|
||||
auto strb = reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) +
|
||||
b.first);
|
||||
auto stra = reinterpret_cast<const char *>(
|
||||
flatbuffers::vector_data(*buf_) + a.first);
|
||||
auto strb = reinterpret_cast<const char *>(
|
||||
flatbuffers::vector_data(*buf_) + b.first);
|
||||
return strncmp(stra, strb, (std::min)(a.second, b.second) + 1) < 0;
|
||||
}
|
||||
const std::vector<uint8_t> *buf_;
|
||||
|
@ -1491,8 +1516,8 @@ class Builder FLATBUFFERS_FINAL_CLASS {
|
|||
|
||||
} // namespace flexbuffers
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
# if defined(_MSC_VER)
|
||||
# pragma warning(pop)
|
||||
# endif
|
||||
|
||||
#endif // FLATBUFFERS_FLEXBUFFERS_H_
|
||||
|
|
|
@ -30,13 +30,12 @@ namespace grpc {
|
|||
// `grpc_slice` and also provides flatbuffers-specific helpers such as `Verify`
|
||||
// and `GetRoot`. Since it is backed by a `grpc_slice`, the underlying buffer
|
||||
// is refcounted and ownership is be managed automatically.
|
||||
template <class T>
|
||||
class Message {
|
||||
template<class T> class Message {
|
||||
public:
|
||||
Message() : slice_(grpc_empty_slice()) {}
|
||||
|
||||
Message(grpc_slice slice, bool add_ref)
|
||||
: slice_(add_ref ? grpc_slice_ref(slice) : slice) {}
|
||||
: slice_(add_ref ? grpc_slice_ref(slice) : slice) {}
|
||||
|
||||
Message &operator=(const Message &other) = delete;
|
||||
|
||||
|
@ -137,7 +136,7 @@ namespace detail {
|
|||
struct SliceAllocatorMember {
|
||||
SliceAllocator slice_allocator_;
|
||||
};
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// MessageBuilder is a gRPC-specific FlatBufferBuilder that uses SliceAllocator
|
||||
// to allocate gRPC buffers.
|
||||
|
@ -145,7 +144,7 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
|||
public FlatBufferBuilder {
|
||||
public:
|
||||
explicit MessageBuilder(uoffset_t initial_size = 1024)
|
||||
: FlatBufferBuilder(initial_size, &slice_allocator_, false) {}
|
||||
: FlatBufferBuilder(initial_size, &slice_allocator_, false) {}
|
||||
|
||||
MessageBuilder(const MessageBuilder &other) = delete;
|
||||
MessageBuilder &operator=(const MessageBuilder &other) = delete;
|
||||
|
@ -155,8 +154,7 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
|||
// GetMessage extracts the subslice of the buffer corresponding to the
|
||||
// flatbuffers-encoded region and wraps it in a `Message<T>` to handle buffer
|
||||
// ownership.
|
||||
template <class T>
|
||||
Message<T> GetMessage() {
|
||||
template<class T> Message<T> GetMessage() {
|
||||
auto buf_data = buf_.buf(); // pointer to memory
|
||||
auto buf_size = buf_.capacity(); // size of memory
|
||||
auto msg_data = buf_.data(); // pointer to msg
|
||||
|
@ -178,8 +176,7 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
|||
return msg;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Message<T> ReleaseMessage() {
|
||||
template<class T> Message<T> ReleaseMessage() {
|
||||
Message<T> msg = GetMessage<T>();
|
||||
Reset();
|
||||
return msg;
|
||||
|
@ -194,8 +191,7 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
|||
|
||||
namespace grpc {
|
||||
|
||||
template <class T>
|
||||
class SerializationTraits<flatbuffers::grpc::Message<T>> {
|
||||
template<class T> class SerializationTraits<flatbuffers::grpc::Message<T>> {
|
||||
public:
|
||||
static grpc::Status Serialize(const flatbuffers::grpc::Message<T> &msg,
|
||||
grpc_byte_buffer **buffer, bool *own_buffer) {
|
||||
|
@ -237,19 +233,19 @@ class SerializationTraits<flatbuffers::grpc::Message<T>> {
|
|||
*msg = flatbuffers::grpc::Message<T>(slice, false);
|
||||
}
|
||||
grpc_byte_buffer_destroy(buffer);
|
||||
#if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
|
||||
#if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
|
||||
return ::grpc::Status::OK;
|
||||
#else
|
||||
#else
|
||||
if (msg->Verify()) {
|
||||
return ::grpc::Status::OK;
|
||||
} else {
|
||||
return ::grpc::Status(::grpc::StatusCode::INTERNAL,
|
||||
"Message verification failed");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace grpc;
|
||||
} // namespace grpc
|
||||
|
||||
#endif // FLATBUFFERS_GRPC_H_
|
||||
|
|
|
@ -24,26 +24,22 @@
|
|||
|
||||
namespace flatbuffers {
|
||||
|
||||
template <typename T>
|
||||
struct FnvTraits {
|
||||
template<typename T> struct FnvTraits {
|
||||
static const T kFnvPrime;
|
||||
static const T kOffsetBasis;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct FnvTraits<uint32_t> {
|
||||
template<> struct FnvTraits<uint32_t> {
|
||||
static const uint32_t kFnvPrime = 0x01000193;
|
||||
static const uint32_t kOffsetBasis = 0x811C9DC5;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct FnvTraits<uint64_t> {
|
||||
template<> struct FnvTraits<uint64_t> {
|
||||
static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
|
||||
static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
T HashFnv1(const char *input) {
|
||||
template<typename T> T HashFnv1(const char *input) {
|
||||
T hash = FnvTraits<T>::kOffsetBasis;
|
||||
for (const char *c = input; *c; ++c) {
|
||||
hash *= FnvTraits<T>::kFnvPrime;
|
||||
|
@ -52,8 +48,7 @@ T HashFnv1(const char *input) {
|
|||
return hash;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T HashFnv1a(const char *input) {
|
||||
template<typename T> T HashFnv1a(const char *input) {
|
||||
T hash = FnvTraits<T>::kOffsetBasis;
|
||||
for (const char *c = input; *c; ++c) {
|
||||
hash ^= static_cast<unsigned char>(*c);
|
||||
|
@ -62,21 +57,20 @@ T HashFnv1a(const char *input) {
|
|||
return hash;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct NamedHashFunction {
|
||||
template<typename T> struct NamedHashFunction {
|
||||
const char *name;
|
||||
|
||||
typedef T (*HashFunction)(const char*);
|
||||
typedef T (*HashFunction)(const char *);
|
||||
HashFunction function;
|
||||
};
|
||||
|
||||
const NamedHashFunction<uint32_t> kHashFunctions32[] = {
|
||||
{ "fnv1_32", HashFnv1<uint32_t> },
|
||||
{ "fnv1_32", HashFnv1<uint32_t> },
|
||||
{ "fnv1a_32", HashFnv1a<uint32_t> },
|
||||
};
|
||||
|
||||
const NamedHashFunction<uint64_t> kHashFunctions64[] = {
|
||||
{ "fnv1_64", HashFnv1<uint64_t> },
|
||||
{ "fnv1_64", HashFnv1<uint64_t> },
|
||||
{ "fnv1a_64", HashFnv1a<uint64_t> },
|
||||
};
|
||||
|
||||
|
|
|
@ -18,17 +18,17 @@
|
|||
#define FLATBUFFERS_IDL_H_
|
||||
|
||||
#include <map>
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
|
||||
#include "flatbuffers/base.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
#include "flatbuffers/hash.h"
|
||||
#include "flatbuffers/reflection.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
|
||||
#if !defined(FLATBUFFERS_CPP98_STL)
|
||||
#include <functional>
|
||||
# include <functional>
|
||||
#endif // !defined(FLATBUFFERS_CPP98_STL)
|
||||
|
||||
// This file defines the data types representing a parsed IDL (Interface
|
||||
|
@ -39,6 +39,7 @@ namespace flatbuffers {
|
|||
// The order of these matters for Is*() functions below.
|
||||
// Additionally, Parser::ParseType assumes bool..string is a contiguous range
|
||||
// of type tokens.
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
|
||||
TD(NONE, "", uint8_t, byte, byte, byte, uint8) \
|
||||
TD(UTYPE, "", uint8_t, byte, byte, byte, uint8) /* begin scalar/int */ \
|
||||
|
@ -110,13 +111,12 @@ inline bool IsFloat (BaseType t) { return t == BASE_TYPE_FLOAT ||
|
|||
inline bool IsLong (BaseType t) { return t == BASE_TYPE_LONG ||
|
||||
t == BASE_TYPE_ULONG; }
|
||||
inline bool IsBool (BaseType t) { return t == BASE_TYPE_BOOL; }
|
||||
// clang-format on
|
||||
|
||||
extern const char *const kTypeNames[];
|
||||
extern const char kTypeSizes[];
|
||||
|
||||
inline size_t SizeOf(BaseType t) {
|
||||
return kTypeSizes[t];
|
||||
}
|
||||
inline size_t SizeOf(BaseType t) { return kTypeSizes[t]; }
|
||||
|
||||
struct StructDef;
|
||||
struct EnumDef;
|
||||
|
@ -125,13 +125,12 @@ class Parser;
|
|||
// Represents any type in the IDL, which is a combination of the BaseType
|
||||
// and additional information for vectors/structs_.
|
||||
struct Type {
|
||||
explicit Type(BaseType _base_type = BASE_TYPE_NONE,
|
||||
StructDef *_sd = nullptr, EnumDef *_ed = nullptr)
|
||||
: base_type(_base_type),
|
||||
element(BASE_TYPE_NONE),
|
||||
struct_def(_sd),
|
||||
enum_def(_ed)
|
||||
{}
|
||||
explicit Type(BaseType _base_type = BASE_TYPE_NONE, StructDef *_sd = nullptr,
|
||||
EnumDef *_ed = nullptr)
|
||||
: base_type(_base_type),
|
||||
element(BASE_TYPE_NONE),
|
||||
struct_def(_sd),
|
||||
enum_def(_ed) {}
|
||||
|
||||
bool operator==(const Type &o) {
|
||||
return base_type == o.base_type && element == o.element &&
|
||||
|
@ -151,8 +150,9 @@ struct Type {
|
|||
|
||||
// Represents a parsed scalar value, it's type, and field offset.
|
||||
struct Value {
|
||||
Value() : constant("0"), offset(static_cast<voffset_t>(
|
||||
~(static_cast<voffset_t>(0U)))) {}
|
||||
Value()
|
||||
: constant("0"),
|
||||
offset(static_cast<voffset_t>(~(static_cast<voffset_t>(0U)))) {}
|
||||
Type type;
|
||||
std::string constant;
|
||||
voffset_t offset;
|
||||
|
@ -163,9 +163,7 @@ struct Value {
|
|||
template<typename T> class SymbolTable {
|
||||
public:
|
||||
~SymbolTable() {
|
||||
for (auto it = vec.begin(); it != vec.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
for (auto it = vec.begin(); it != vec.end(); ++it) { delete *it; }
|
||||
}
|
||||
|
||||
bool Add(const std::string &name, T *e) {
|
||||
|
@ -193,15 +191,14 @@ template<typename T> class SymbolTable {
|
|||
}
|
||||
|
||||
public:
|
||||
std::map<std::string, T *> dict; // quick lookup
|
||||
std::vector<T *> vec; // Used to iterate in order of insertion
|
||||
std::map<std::string, T *> dict; // quick lookup
|
||||
std::vector<T *> vec; // Used to iterate in order of insertion
|
||||
};
|
||||
|
||||
// A name space, as set in the schema.
|
||||
struct Namespace {
|
||||
Namespace() : from_table(0) {}
|
||||
|
||||
|
||||
// Given a (potentally unqualified) name, return the "fully qualified" name
|
||||
// which has a full namespaced descriptor.
|
||||
// With max_components you can request less than the number of components
|
||||
|
@ -215,13 +212,16 @@ struct Namespace {
|
|||
|
||||
// Base class for all definition types (fields, structs_, enums_).
|
||||
struct Definition {
|
||||
Definition() : generated(false), defined_namespace(nullptr),
|
||||
serialized_location(0), index(-1), refcount(1) {}
|
||||
Definition()
|
||||
: generated(false),
|
||||
defined_namespace(nullptr),
|
||||
serialized_location(0),
|
||||
index(-1),
|
||||
refcount(1) {}
|
||||
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<
|
||||
reflection::KeyValue>>>
|
||||
SerializeAttributes(FlatBufferBuilder *builder,
|
||||
const Parser &parser) const;
|
||||
flatbuffers::Offset<
|
||||
flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
|
||||
SerializeAttributes(FlatBufferBuilder *builder, const Parser &parser) const;
|
||||
|
||||
std::string name;
|
||||
std::string file;
|
||||
|
@ -237,34 +237,38 @@ struct Definition {
|
|||
};
|
||||
|
||||
struct FieldDef : public Definition {
|
||||
FieldDef() : deprecated(false), required(false), key(false),
|
||||
native_inline(false), flexbuffer(false), nested_flatbuffer(NULL),
|
||||
padding(0) {}
|
||||
FieldDef()
|
||||
: deprecated(false),
|
||||
required(false),
|
||||
key(false),
|
||||
native_inline(false),
|
||||
flexbuffer(false),
|
||||
nested_flatbuffer(NULL),
|
||||
padding(0) {}
|
||||
|
||||
Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
|
||||
const Parser &parser) const;
|
||||
|
||||
Value value;
|
||||
bool deprecated; // Field is allowed to be present in old data, but can't be.
|
||||
// written in new data nor accessed in new code.
|
||||
bool required; // Field must always be present.
|
||||
bool key; // Field functions as a key for creating sorted vectors.
|
||||
bool deprecated; // Field is allowed to be present in old data, but can't be.
|
||||
// written in new data nor accessed in new code.
|
||||
bool required; // Field must always be present.
|
||||
bool key; // Field functions as a key for creating sorted vectors.
|
||||
bool native_inline; // Field will be defined inline (instead of as a pointer)
|
||||
// for native tables if field is a struct.
|
||||
bool flexbuffer; // This field contains FlexBuffer data.
|
||||
StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
|
||||
size_t padding; // Bytes to always pad after this field.
|
||||
bool flexbuffer; // This field contains FlexBuffer data.
|
||||
StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
|
||||
size_t padding; // Bytes to always pad after this field.
|
||||
};
|
||||
|
||||
struct StructDef : public Definition {
|
||||
StructDef()
|
||||
: fixed(false),
|
||||
predecl(true),
|
||||
sortbysize(true),
|
||||
has_key(false),
|
||||
minalign(1),
|
||||
bytesize(0)
|
||||
{}
|
||||
: fixed(false),
|
||||
predecl(true),
|
||||
sortbysize(true),
|
||||
has_key(false),
|
||||
minalign(1),
|
||||
bytesize(0) {}
|
||||
|
||||
void PadLastField(size_t min_align) {
|
||||
auto padding = PaddingBytes(bytesize, min_align);
|
||||
|
@ -300,8 +304,7 @@ inline size_t InlineAlignment(const Type &type) {
|
|||
}
|
||||
|
||||
struct EnumVal {
|
||||
EnumVal(const std::string &_name, int64_t _val)
|
||||
: name(_name), value(_val) {}
|
||||
EnumVal(const std::string &_name, int64_t _val) : name(_name), value(_val) {}
|
||||
|
||||
Offset<reflection::EnumVal> Serialize(FlatBufferBuilder *builder) const;
|
||||
|
||||
|
@ -315,12 +318,10 @@ struct EnumDef : public Definition {
|
|||
EnumDef() : is_union(false), uses_type_aliases(false) {}
|
||||
|
||||
EnumVal *ReverseLookup(int enum_idx, bool skip_union_default = true) {
|
||||
for (auto it = vals.vec.begin() + static_cast<int>(is_union &&
|
||||
skip_union_default);
|
||||
it != vals.vec.end(); ++it) {
|
||||
if ((*it)->value == enum_idx) {
|
||||
return *it;
|
||||
}
|
||||
for (auto it = vals.vec.begin() +
|
||||
static_cast<int>(is_union && skip_union_default);
|
||||
it != vals.vec.end(); ++it) {
|
||||
if ((*it)->value == enum_idx) { return *it; }
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -338,8 +339,7 @@ inline bool EqualByName(const Type &a, const Type &b) {
|
|||
return a.base_type == b.base_type && a.element == b.element &&
|
||||
(a.struct_def == b.struct_def ||
|
||||
a.struct_def->name == b.struct_def->name) &&
|
||||
(a.enum_def == b.enum_def ||
|
||||
a.enum_def->name == b.enum_def->name);
|
||||
(a.enum_def == b.enum_def || a.enum_def->name == b.enum_def->name);
|
||||
}
|
||||
|
||||
struct RPCCall {
|
||||
|
@ -389,16 +389,16 @@ struct IDLOptions {
|
|||
|
||||
// Possible options for the more general generator below.
|
||||
enum Language {
|
||||
kJava = 1 << 0,
|
||||
kJava = 1 << 0,
|
||||
kCSharp = 1 << 1,
|
||||
kGo = 1 << 2,
|
||||
kCpp = 1 << 3,
|
||||
kJs = 1 << 4,
|
||||
kGo = 1 << 2,
|
||||
kCpp = 1 << 3,
|
||||
kJs = 1 << 4,
|
||||
kPython = 1 << 5,
|
||||
kPhp = 1 << 6,
|
||||
kJson = 1 << 7,
|
||||
kPhp = 1 << 6,
|
||||
kJson = 1 << 7,
|
||||
kBinary = 1 << 8,
|
||||
kTs = 1 << 9,
|
||||
kTs = 1 << 9,
|
||||
kJsonSchema = 1 << 10,
|
||||
kMAX
|
||||
};
|
||||
|
@ -414,33 +414,35 @@ struct IDLOptions {
|
|||
unsigned long lang_to_generate;
|
||||
|
||||
IDLOptions()
|
||||
: strict_json(false),
|
||||
skip_js_exports(false),
|
||||
use_goog_js_export_format(false),
|
||||
output_default_scalars_in_json(false),
|
||||
indent_step(2),
|
||||
output_enum_identifiers(true), prefixed_enums(true), scoped_enums(false),
|
||||
include_dependence_headers(true),
|
||||
mutable_buffer(false),
|
||||
one_file(false),
|
||||
proto_mode(false),
|
||||
generate_all(false),
|
||||
skip_unexpected_fields_in_json(false),
|
||||
generate_name_strings(false),
|
||||
generate_object_based_api(false),
|
||||
cpp_object_api_pointer_type("std::unique_ptr"),
|
||||
gen_nullable(false),
|
||||
object_suffix("T"),
|
||||
union_value_namespacing(true),
|
||||
allow_non_utf8(false),
|
||||
keep_include_path(false),
|
||||
binary_schema_comments(false),
|
||||
skip_flatbuffers_import(false),
|
||||
reexport_ts_modules(true),
|
||||
protobuf_ascii_alike(false),
|
||||
lang(IDLOptions::kJava),
|
||||
mini_reflect(IDLOptions::kNone),
|
||||
lang_to_generate(0) {}
|
||||
: strict_json(false),
|
||||
skip_js_exports(false),
|
||||
use_goog_js_export_format(false),
|
||||
output_default_scalars_in_json(false),
|
||||
indent_step(2),
|
||||
output_enum_identifiers(true),
|
||||
prefixed_enums(true),
|
||||
scoped_enums(false),
|
||||
include_dependence_headers(true),
|
||||
mutable_buffer(false),
|
||||
one_file(false),
|
||||
proto_mode(false),
|
||||
generate_all(false),
|
||||
skip_unexpected_fields_in_json(false),
|
||||
generate_name_strings(false),
|
||||
generate_object_based_api(false),
|
||||
cpp_object_api_pointer_type("std::unique_ptr"),
|
||||
gen_nullable(false),
|
||||
object_suffix("T"),
|
||||
union_value_namespacing(true),
|
||||
allow_non_utf8(false),
|
||||
keep_include_path(false),
|
||||
binary_schema_comments(false),
|
||||
skip_flatbuffers_import(false),
|
||||
reexport_ts_modules(true),
|
||||
protobuf_ascii_alike(false),
|
||||
lang(IDLOptions::kJava),
|
||||
mini_reflect(IDLOptions::kNone),
|
||||
lang_to_generate(0) {}
|
||||
};
|
||||
|
||||
// This encapsulates where the parser is in the current source file.
|
||||
|
@ -466,7 +468,7 @@ struct ParserState {
|
|||
class CheckedError {
|
||||
public:
|
||||
explicit CheckedError(bool error)
|
||||
: is_error_(error), has_been_checked_(false) {}
|
||||
: is_error_(error), has_been_checked_(false) {}
|
||||
|
||||
CheckedError &operator=(const CheckedError &other) {
|
||||
is_error_ = other.is_error_;
|
||||
|
@ -481,7 +483,10 @@ class CheckedError {
|
|||
|
||||
~CheckedError() { assert(has_been_checked_); }
|
||||
|
||||
bool Check() { has_been_checked_ = true; return is_error_; }
|
||||
bool Check() {
|
||||
has_been_checked_ = true;
|
||||
return is_error_;
|
||||
}
|
||||
|
||||
private:
|
||||
bool is_error_;
|
||||
|
@ -490,23 +495,25 @@ class CheckedError {
|
|||
|
||||
// Additionally, in GCC we can get these errors statically, for additional
|
||||
// assurance:
|
||||
// clang-format off
|
||||
#ifdef __GNUC__
|
||||
#define FLATBUFFERS_CHECKED_ERROR CheckedError \
|
||||
__attribute__((warn_unused_result))
|
||||
#else
|
||||
#define FLATBUFFERS_CHECKED_ERROR CheckedError
|
||||
#endif
|
||||
// clang-format on
|
||||
|
||||
class Parser : public ParserState {
|
||||
public:
|
||||
explicit Parser(const IDLOptions &options = IDLOptions())
|
||||
: current_namespace_(nullptr),
|
||||
empty_namespace_(nullptr),
|
||||
root_struct_def_(nullptr),
|
||||
opts(options),
|
||||
uses_flexbuffers_(false),
|
||||
source_(nullptr),
|
||||
anonymous_counter(0) {
|
||||
: current_namespace_(nullptr),
|
||||
empty_namespace_(nullptr),
|
||||
root_struct_def_(nullptr),
|
||||
opts(options),
|
||||
uses_flexbuffers_(false),
|
||||
source_(nullptr),
|
||||
anonymous_counter(0) {
|
||||
// Start out with the empty namespace being current.
|
||||
empty_namespace_ = new Namespace();
|
||||
namespaces_.push_back(empty_namespace_);
|
||||
|
@ -581,7 +588,7 @@ class Parser : public ParserState {
|
|||
|
||||
StructDef *LookupStruct(const std::string &id) const;
|
||||
|
||||
private:
|
||||
private:
|
||||
void Message(const std::string &msg);
|
||||
void Warning(const std::string &msg);
|
||||
FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
|
||||
|
@ -606,6 +613,7 @@ private:
|
|||
FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field,
|
||||
size_t parent_fieldn,
|
||||
const StructDef *parent_struct_def);
|
||||
// clang-format off
|
||||
#if defined(FLATBUFFERS_CPP98_STL)
|
||||
typedef CheckedError (*ParseTableDelimitersBody)(
|
||||
const std::string &name, size_t &fieldn, const StructDef *struct_def,
|
||||
|
@ -615,6 +623,7 @@ private:
|
|||
const StructDef*, void*)>
|
||||
ParseTableDelimitersBody;
|
||||
#endif // defined(FLATBUFFERS_CPP98_STL)
|
||||
// clang-format on
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTableDelimiters(size_t &fieldn,
|
||||
const StructDef *struct_def,
|
||||
ParseTableDelimitersBody body,
|
||||
|
@ -623,6 +632,7 @@ private:
|
|||
std::string *value, uoffset_t *ovalue);
|
||||
void SerializeStruct(const StructDef &struct_def, const Value &val);
|
||||
void AddVector(bool sortbysize, int count);
|
||||
// clang-format off
|
||||
#if defined(FLATBUFFERS_CPP98_STL)
|
||||
typedef CheckedError (*ParseVectorDelimitersBody)(size_t &count,
|
||||
void *state);
|
||||
|
@ -630,6 +640,7 @@ private:
|
|||
typedef std::function<CheckedError(size_t&, void*)>
|
||||
ParseVectorDelimitersBody;
|
||||
#endif // defined(FLATBUFFERS_CPP98_STL)
|
||||
// clang-format on
|
||||
FLATBUFFERS_CHECKED_ERROR ParseVectorDelimiters(
|
||||
size_t &count, ParseVectorDelimitersBody body, void *state);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseVector(const Type &type, uoffset_t *ovalue);
|
||||
|
@ -844,7 +855,7 @@ bool GenerateCppGRPC(const Parser &parser,
|
|||
bool GenerateGoGRPC(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
|
||||
|
||||
// Generate GRPC Java classes.
|
||||
// See idl_gen_grpc.cpp
|
||||
bool GenerateJavaGRPC(const Parser &parser,
|
||||
|
|
|
@ -65,8 +65,7 @@ struct IterationVisitor {
|
|||
virtual void EndVector() {}
|
||||
virtual void Element(size_t /*i*/, ElementaryType /*type*/,
|
||||
const TypeTable * /*type_table*/,
|
||||
const uint8_t * /*val*/)
|
||||
{}
|
||||
const uint8_t * /*val*/) {}
|
||||
virtual ~IterationVisitor() {}
|
||||
};
|
||||
|
||||
|
@ -75,34 +74,24 @@ inline size_t InlineSize(ElementaryType type, const TypeTable *type_table) {
|
|||
case ET_UTYPE:
|
||||
case ET_BOOL:
|
||||
case ET_CHAR:
|
||||
case ET_UCHAR:
|
||||
return 1;
|
||||
case ET_UCHAR: return 1;
|
||||
case ET_SHORT:
|
||||
case ET_USHORT:
|
||||
return 2;
|
||||
case ET_USHORT: return 2;
|
||||
case ET_INT:
|
||||
case ET_UINT:
|
||||
case ET_FLOAT:
|
||||
case ET_STRING:
|
||||
return 4;
|
||||
case ET_STRING: return 4;
|
||||
case ET_LONG:
|
||||
case ET_ULONG:
|
||||
case ET_DOUBLE:
|
||||
return 8;
|
||||
case ET_DOUBLE: return 8;
|
||||
case ET_SEQUENCE:
|
||||
switch (type_table->st) {
|
||||
case ST_TABLE:
|
||||
case ST_UNION:
|
||||
return 4;
|
||||
case ST_STRUCT:
|
||||
return type_table->values[type_table->num_elems];
|
||||
default:
|
||||
assert(false);
|
||||
return 1;
|
||||
case ST_UNION: return 4;
|
||||
case ST_STRUCT: return type_table->values[type_table->num_elems];
|
||||
default: assert(false); return 1;
|
||||
}
|
||||
default:
|
||||
assert(false);
|
||||
return 1;
|
||||
default: assert(false); return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,7 +107,7 @@ inline int32_t LookupEnum(int32_t enum_val, const int32_t *values,
|
|||
template<typename T> const char *EnumName(T tval, const TypeTable *type_table) {
|
||||
if (!type_table || !type_table->names) return nullptr;
|
||||
auto i = LookupEnum(static_cast<int32_t>(tval), type_table->values,
|
||||
type_table->num_elems);
|
||||
type_table->num_elems);
|
||||
if (i >= 0 && i < static_cast<int32_t>(type_table->num_elems)) {
|
||||
return type_table->names[i];
|
||||
}
|
||||
|
@ -126,13 +115,11 @@ template<typename T> const char *EnumName(T tval, const TypeTable *type_table) {
|
|||
}
|
||||
|
||||
void IterateObject(const uint8_t *obj, const TypeTable *type_table,
|
||||
IterationVisitor *visitor);
|
||||
IterationVisitor *visitor);
|
||||
|
||||
inline void IterateValue(ElementaryType type, const uint8_t *val,
|
||||
const TypeTable *type_table,
|
||||
const uint8_t *prev_val,
|
||||
soffset_t vector_index,
|
||||
IterationVisitor *visitor) {
|
||||
const TypeTable *type_table, const uint8_t *prev_val,
|
||||
soffset_t vector_index, IterationVisitor *visitor) {
|
||||
switch (type) {
|
||||
case ET_UTYPE: {
|
||||
auto tval = *reinterpret_cast<const uint8_t *>(val);
|
||||
|
@ -200,9 +187,7 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
|
|||
val += ReadScalar<uoffset_t>(val);
|
||||
IterateObject(val, type_table, visitor);
|
||||
break;
|
||||
case ST_STRUCT:
|
||||
IterateObject(val, type_table, visitor);
|
||||
break;
|
||||
case ST_STRUCT: IterateObject(val, type_table, visitor); break;
|
||||
case ST_UNION: {
|
||||
val += ReadScalar<uoffset_t>(val);
|
||||
assert(prev_val);
|
||||
|
@ -211,10 +196,10 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
|
|||
auto type_vec = reinterpret_cast<const Vector<uint8_t> *>(prev_val);
|
||||
union_type = type_vec->Get(static_cast<uoffset_t>(vector_index));
|
||||
}
|
||||
auto type_code_idx = LookupEnum(union_type, type_table->values,
|
||||
type_table->num_elems);
|
||||
if (type_code_idx >= 0 && type_code_idx <
|
||||
static_cast<int32_t>(type_table->num_elems)) {
|
||||
auto type_code_idx =
|
||||
LookupEnum(union_type, type_table->values, type_table->num_elems);
|
||||
if (type_code_idx >= 0 &&
|
||||
type_code_idx < static_cast<int32_t>(type_table->num_elems)) {
|
||||
auto type_code = type_table->type_codes[type_code_idx];
|
||||
switch (type_code.base_type) {
|
||||
case ET_SEQUENCE: {
|
||||
|
@ -225,17 +210,14 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
|
|||
case ET_STRING:
|
||||
visitor->String(reinterpret_cast<const String *>(val));
|
||||
break;
|
||||
default:
|
||||
visitor->Unknown(val);
|
||||
default: visitor->Unknown(val);
|
||||
}
|
||||
} else {
|
||||
visitor->Unknown(val);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ST_ENUM:
|
||||
assert(false);
|
||||
break;
|
||||
case ST_ENUM: assert(false); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -257,14 +239,12 @@ inline void IterateObject(const uint8_t *obj, const TypeTable *type_table,
|
|||
auto is_vector = type_code.is_vector != 0;
|
||||
auto ref_idx = type_code.sequence_ref;
|
||||
const TypeTable *ref = nullptr;
|
||||
if (ref_idx >= 0) {
|
||||
ref = type_table->type_refs[ref_idx]();
|
||||
}
|
||||
if (ref_idx >= 0) { ref = type_table->type_refs[ref_idx](); }
|
||||
auto name = type_table->names ? type_table->names[i] : nullptr;
|
||||
const uint8_t *val = nullptr;
|
||||
if (type_table->st == ST_TABLE) {
|
||||
val = reinterpret_cast<const Table *>(obj)->GetAddressOf(
|
||||
FieldIndexToOffset(static_cast<voffset_t>(i)));
|
||||
FieldIndexToOffset(static_cast<voffset_t>(i)));
|
||||
} else {
|
||||
val = obj + type_table->values[i];
|
||||
}
|
||||
|
@ -310,24 +290,29 @@ struct ToStringVisitor : public IterationVisitor {
|
|||
const char *name, const uint8_t *val) {
|
||||
if (!val) return;
|
||||
if (set_idx) s += ", ";
|
||||
if (name) { s += name; s += ": "; }
|
||||
if (name) {
|
||||
s += name;
|
||||
s += ": ";
|
||||
}
|
||||
}
|
||||
template<typename T> void Named(T x, const char *name) {
|
||||
if (name) s+= name;
|
||||
else s+= NumToString(x);
|
||||
if (name)
|
||||
s += name;
|
||||
else
|
||||
s += NumToString(x);
|
||||
}
|
||||
void UType(uint8_t x, const char *name) { Named(x, name); }
|
||||
void Bool(bool x) { s+= x ? "true" : "false"; }
|
||||
void Bool(bool x) { s += x ? "true" : "false"; }
|
||||
void Char(int8_t x, const char *name) { Named(x, name); }
|
||||
void UChar(uint8_t x, const char *name) { Named(x, name); }
|
||||
void Short(int16_t x, const char *name) { Named(x, name); }
|
||||
void UShort(uint16_t x, const char *name) { Named(x, name); }
|
||||
void Int(int32_t x, const char *name) { Named(x, name); }
|
||||
void UInt(uint32_t x, const char *name) { Named(x, name); }
|
||||
void Long(int64_t x) { s+= NumToString(x); }
|
||||
void ULong(uint64_t x) { s+= NumToString(x); }
|
||||
void Float(float x) { s+= NumToString(x); }
|
||||
void Double(double x) { s+= NumToString(x); }
|
||||
void Long(int64_t x) { s += NumToString(x); }
|
||||
void ULong(uint64_t x) { s += NumToString(x); }
|
||||
void Float(float x) { s += NumToString(x); }
|
||||
void Double(double x) { s += NumToString(x); }
|
||||
void String(const struct String *str) {
|
||||
EscapeString(str->c_str(), str->size(), &s, true);
|
||||
}
|
||||
|
|
|
@ -30,14 +30,18 @@ namespace flatbuffers {
|
|||
|
||||
// ------------------------- GETTERS -------------------------
|
||||
|
||||
inline bool IsScalar (reflection::BaseType t) { return t >= reflection::UType &&
|
||||
t <= reflection::Double; }
|
||||
inline bool IsInteger(reflection::BaseType t) { return t >= reflection::UType &&
|
||||
t <= reflection::ULong; }
|
||||
inline bool IsFloat (reflection::BaseType t) { return t == reflection::Float ||
|
||||
t == reflection::Double; }
|
||||
inline bool IsLong (reflection::BaseType t) { return t == reflection::Long ||
|
||||
t == reflection::ULong; }
|
||||
inline bool IsScalar(reflection::BaseType t) {
|
||||
return t >= reflection::UType && t <= reflection::Double;
|
||||
}
|
||||
inline bool IsInteger(reflection::BaseType t) {
|
||||
return t >= reflection::UType && t <= reflection::ULong;
|
||||
}
|
||||
inline bool IsFloat(reflection::BaseType t) {
|
||||
return t == reflection::Float || t == reflection::Double;
|
||||
}
|
||||
inline bool IsLong(reflection::BaseType t) {
|
||||
return t == reflection::Long || t == reflection::ULong;
|
||||
}
|
||||
|
||||
// Size of a basic type, don't use with structs.
|
||||
inline size_t GetTypeSize(reflection::BaseType base_type) {
|
||||
|
@ -48,8 +52,7 @@ inline size_t GetTypeSize(reflection::BaseType base_type) {
|
|||
|
||||
// Same as above, but now correctly returns the size of a struct if
|
||||
// the field (or vector element) is a struct.
|
||||
inline size_t GetTypeSizeInline(reflection::BaseType base_type,
|
||||
int type_index,
|
||||
inline size_t GetTypeSizeInline(reflection::BaseType base_type, int type_index,
|
||||
const reflection::Schema &schema) {
|
||||
if (base_type == reflection::Obj &&
|
||||
schema.objects()->Get(type_index)->is_struct()) {
|
||||
|
@ -80,16 +83,16 @@ template<typename T> T GetFieldDefaultF(const reflection::Field &field) {
|
|||
}
|
||||
|
||||
// Get a field, if you know it's an integer, and its exact type.
|
||||
template<typename T> T GetFieldI(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
template<typename T>
|
||||
T GetFieldI(const Table &table, const reflection::Field &field) {
|
||||
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
|
||||
return table.GetField<T>(field.offset(),
|
||||
static_cast<T>(field.default_integer()));
|
||||
}
|
||||
|
||||
// Get a field, if you know it's floating point and its exact type.
|
||||
template<typename T> T GetFieldF(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
template<typename T>
|
||||
T GetFieldF(const Table &table, const reflection::Field &field) {
|
||||
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
|
||||
return table.GetField<T>(field.offset(),
|
||||
static_cast<T>(field.default_real()));
|
||||
|
@ -103,8 +106,8 @@ inline const String *GetFieldS(const Table &table,
|
|||
}
|
||||
|
||||
// Get a field, if you know it's a vector.
|
||||
template<typename T> Vector<T> *GetFieldV(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
template<typename T>
|
||||
Vector<T> *GetFieldV(const Table &table, const reflection::Field &field) {
|
||||
assert(field.type()->base_type() == reflection::Vector &&
|
||||
sizeof(T) == GetTypeSize(field.type()->element()));
|
||||
return table.GetPointer<Vector<T> *>(field.offset());
|
||||
|
@ -119,8 +122,7 @@ inline VectorOfAny *GetFieldAnyV(const Table &table,
|
|||
}
|
||||
|
||||
// Get a field, if you know it's a table.
|
||||
inline Table *GetFieldT(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
inline Table *GetFieldT(const Table &table, const reflection::Field &field) {
|
||||
assert(field.type()->base_type() == reflection::Obj ||
|
||||
field.type()->base_type() == reflection::Union);
|
||||
return table.GetPointer<Table *>(field.offset());
|
||||
|
@ -153,8 +155,7 @@ double GetAnyValueF(reflection::BaseType type, const uint8_t *data);
|
|||
// All scalars converted using stringstream, strings as-is, and all other
|
||||
// data types provide some level of debug-pretty-printing.
|
||||
std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
|
||||
const reflection::Schema *schema,
|
||||
int type_index);
|
||||
const reflection::Schema *schema, int type_index);
|
||||
|
||||
// Get any table field as a 64bit int, regardless of what type it is.
|
||||
inline int64_t GetAnyFieldI(const Table &table,
|
||||
|
@ -165,14 +166,12 @@ inline int64_t GetAnyFieldI(const Table &table,
|
|||
}
|
||||
|
||||
// Get any table field as a double, regardless of what type it is.
|
||||
inline double GetAnyFieldF(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
inline double GetAnyFieldF(const Table &table, const reflection::Field &field) {
|
||||
auto field_ptr = table.GetAddressOf(field.offset());
|
||||
return field_ptr ? GetAnyValueF(field.type()->base_type(), field_ptr)
|
||||
: field.default_real();
|
||||
}
|
||||
|
||||
|
||||
// Get any table field as a string, regardless of what type it is.
|
||||
// You may pass nullptr for the schema if you don't care to have fields that
|
||||
// are of table type pretty-printed.
|
||||
|
@ -186,15 +185,13 @@ inline std::string GetAnyFieldS(const Table &table,
|
|||
}
|
||||
|
||||
// Get any struct field as a 64bit int, regardless of what type it is.
|
||||
inline int64_t GetAnyFieldI(const Struct &st,
|
||||
const reflection::Field &field) {
|
||||
inline int64_t GetAnyFieldI(const Struct &st, const reflection::Field &field) {
|
||||
return GetAnyValueI(field.type()->base_type(),
|
||||
st.GetAddressOf(field.offset()));
|
||||
}
|
||||
|
||||
// Get any struct field as a double, regardless of what type it is.
|
||||
inline double GetAnyFieldF(const Struct &st,
|
||||
const reflection::Field &field) {
|
||||
inline double GetAnyFieldF(const Struct &st, const reflection::Field &field) {
|
||||
return GetAnyValueF(field.type()->base_type(),
|
||||
st.GetAddressOf(field.offset()));
|
||||
}
|
||||
|
@ -228,8 +225,8 @@ inline std::string GetAnyVectorElemS(const VectorOfAny *vec,
|
|||
// Get a vector element that's a table/string/vector from a generic vector.
|
||||
// Pass Table/String/VectorOfAny as template parameter.
|
||||
// Warning: does no typechecking.
|
||||
template<typename T> T *GetAnyVectorElemPointer(const VectorOfAny *vec,
|
||||
size_t i) {
|
||||
template<typename T>
|
||||
T *GetAnyVectorElemPointer(const VectorOfAny *vec, size_t i) {
|
||||
auto elem_ptr = vec->Data() + sizeof(uoffset_t) * i;
|
||||
return (T *)(elem_ptr + ReadScalar<uoffset_t>(elem_ptr));
|
||||
}
|
||||
|
@ -239,34 +236,32 @@ template<typename T> T *GetAnyVectorElemPointer(const VectorOfAny *vec,
|
|||
// Get elem_size from GetTypeSizeInline().
|
||||
// Note: little-endian data on all platforms, use EndianScalar() instead of
|
||||
// raw pointer access with scalars).
|
||||
template<typename T> T *GetAnyVectorElemAddressOf(const VectorOfAny *vec,
|
||||
size_t i,
|
||||
size_t elem_size) {
|
||||
template<typename T>
|
||||
T *GetAnyVectorElemAddressOf(const VectorOfAny *vec, size_t i,
|
||||
size_t elem_size) {
|
||||
// C-cast to allow const conversion.
|
||||
return (T *)(vec->Data() + elem_size * i);
|
||||
}
|
||||
|
||||
// Similarly, for elements of tables.
|
||||
template<typename T> T *GetAnyFieldAddressOf(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
template<typename T>
|
||||
T *GetAnyFieldAddressOf(const Table &table, const reflection::Field &field) {
|
||||
return (T *)table.GetAddressOf(field.offset());
|
||||
}
|
||||
|
||||
// Similarly, for elements of structs.
|
||||
template<typename T> T *GetAnyFieldAddressOf(const Struct &st,
|
||||
const reflection::Field &field) {
|
||||
template<typename T>
|
||||
T *GetAnyFieldAddressOf(const Struct &st, const reflection::Field &field) {
|
||||
return (T *)st.GetAddressOf(field.offset());
|
||||
}
|
||||
|
||||
// ------------------------- SETTERS -------------------------
|
||||
|
||||
// Set any scalar field, if you know its exact type.
|
||||
template<typename T> bool SetField(Table *table, const reflection::Field &field,
|
||||
T val) {
|
||||
template<typename T>
|
||||
bool SetField(Table *table, const reflection::Field &field, T val) {
|
||||
reflection::BaseType type = field.type()->base_type();
|
||||
if (!IsScalar(type)) {
|
||||
return false;
|
||||
}
|
||||
if (!IsScalar(type)) { return false; }
|
||||
assert(sizeof(T) == GetTypeSize(type));
|
||||
T def;
|
||||
if (IsInteger(type)) {
|
||||
|
@ -306,7 +301,7 @@ inline bool SetAnyFieldF(Table *table, const reflection::Field &field,
|
|||
|
||||
// Set any table field as a string, regardless of what type it is.
|
||||
inline bool SetAnyFieldS(Table *table, const reflection::Field &field,
|
||||
const char *val) {
|
||||
const char *val) {
|
||||
auto field_ptr = table->GetAddressOf(field.offset());
|
||||
if (!field_ptr) return false;
|
||||
SetAnyValueS(field.type()->base_type(), field_ptr, val);
|
||||
|
@ -352,7 +347,6 @@ inline void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type,
|
|||
SetAnyValueS(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
|
||||
}
|
||||
|
||||
|
||||
// ------------------------- RESIZING SETTERS -------------------------
|
||||
|
||||
// "smart" pointer for use with resizing vectors: turns a pointer inside
|
||||
|
@ -360,27 +354,25 @@ inline void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type,
|
|||
template<typename T, typename U> class pointer_inside_vector {
|
||||
public:
|
||||
pointer_inside_vector(T *ptr, std::vector<U> &vec)
|
||||
: offset_(reinterpret_cast<uint8_t *>(ptr) -
|
||||
reinterpret_cast<uint8_t *>(flatbuffers::vector_data(vec))),
|
||||
vec_(vec) {}
|
||||
: offset_(reinterpret_cast<uint8_t *>(ptr) -
|
||||
reinterpret_cast<uint8_t *>(flatbuffers::vector_data(vec))),
|
||||
vec_(vec) {}
|
||||
|
||||
T *operator*() const {
|
||||
return reinterpret_cast<T *>(
|
||||
reinterpret_cast<uint8_t *>(
|
||||
flatbuffers::vector_data(vec_)) + offset_);
|
||||
}
|
||||
T *operator->() const {
|
||||
return operator*();
|
||||
reinterpret_cast<uint8_t *>(flatbuffers::vector_data(vec_)) + offset_);
|
||||
}
|
||||
T *operator->() const { return operator*(); }
|
||||
void operator=(const pointer_inside_vector &piv);
|
||||
|
||||
private:
|
||||
size_t offset_;
|
||||
std::vector<U> &vec_;
|
||||
};
|
||||
|
||||
// Helper to create the above easily without specifying template args.
|
||||
template<typename T, typename U> pointer_inside_vector<T, U> piv(T *ptr,
|
||||
std::vector<U> &vec) {
|
||||
template<typename T, typename U>
|
||||
pointer_inside_vector<T, U> piv(T *ptr, std::vector<U> &vec) {
|
||||
return pointer_inside_vector<T, U>(ptr, vec);
|
||||
}
|
||||
|
||||
|
@ -393,7 +385,7 @@ inline const reflection::Object &GetUnionType(
|
|||
auto enumdef = schema.enums()->Get(unionfield.type()->index());
|
||||
// TODO: this is clumsy and slow, but no other way to find it?
|
||||
auto type_field = parent.fields()->LookupByKey(
|
||||
(unionfield.name()->str() + UnionTypeFieldSuffix()).c_str());
|
||||
(unionfield.name()->str() + UnionTypeFieldSuffix()).c_str());
|
||||
assert(type_field);
|
||||
auto union_type = GetFieldI<uint8_t>(table, *type_field);
|
||||
auto enumval = enumdef->values()->LookupByKey(union_type);
|
||||
|
@ -419,16 +411,14 @@ uint8_t *ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize,
|
|||
uoffset_t elem_size, std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table = nullptr);
|
||||
|
||||
template <typename T>
|
||||
template<typename T>
|
||||
void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val,
|
||||
const Vector<T> *vec, std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table = nullptr) {
|
||||
auto delta_elem = static_cast<int>(newsize) - static_cast<int>(vec->size());
|
||||
auto newelems = ResizeAnyVector(schema, newsize,
|
||||
reinterpret_cast<const VectorOfAny *>(vec),
|
||||
vec->size(),
|
||||
static_cast<uoffset_t>(sizeof(T)), flatbuf,
|
||||
root_table);
|
||||
auto newelems = ResizeAnyVector(
|
||||
schema, newsize, reinterpret_cast<const VectorOfAny *>(vec), vec->size(),
|
||||
static_cast<uoffset_t>(sizeof(T)), flatbuf, root_table);
|
||||
// Set new elements to "val".
|
||||
for (int i = 0; i < delta_elem; i++) {
|
||||
auto loc = newelems + i * sizeof(T);
|
||||
|
@ -479,10 +469,8 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||
// root should point to the root type for this flatbuffer.
|
||||
// buf should point to the start of flatbuffer data.
|
||||
// length specifies the size of the flatbuffer data.
|
||||
bool Verify(const reflection::Schema &schema,
|
||||
const reflection::Object &root,
|
||||
const uint8_t *buf,
|
||||
size_t length);
|
||||
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
|
||||
const uint8_t *buf, size_t length);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
|
|
@ -37,18 +37,16 @@ class Registry {
|
|||
|
||||
// Generate text from an arbitrary FlatBuffer by looking up its
|
||||
// file_identifier in the registry.
|
||||
bool FlatBufferToText(const uint8_t *flatbuf, size_t len,
|
||||
std::string *dest) {
|
||||
bool FlatBufferToText(const uint8_t *flatbuf, size_t len, std::string *dest) {
|
||||
// Get the identifier out of the buffer.
|
||||
// If the buffer is truncated, exit.
|
||||
if (len < sizeof(uoffset_t) +
|
||||
FlatBufferBuilder::kFileIdentifierLength) {
|
||||
if (len < sizeof(uoffset_t) + FlatBufferBuilder::kFileIdentifierLength) {
|
||||
lasterror_ = "buffer truncated";
|
||||
return false;
|
||||
}
|
||||
std::string ident(reinterpret_cast<const char *>(flatbuf) +
|
||||
sizeof(uoffset_t),
|
||||
FlatBufferBuilder::kFileIdentifierLength);
|
||||
std::string ident(
|
||||
reinterpret_cast<const char *>(flatbuf) + sizeof(uoffset_t),
|
||||
FlatBufferBuilder::kFileIdentifierLength);
|
||||
// Load and parse the schema.
|
||||
Parser parser;
|
||||
if (!LoadSchema(ident, &parser)) return false;
|
||||
|
@ -82,38 +80,36 @@ class Registry {
|
|||
|
||||
// If schemas used contain include statements, call this function for every
|
||||
// directory the parser should search them for.
|
||||
void AddIncludeDirectory(const char *path) {
|
||||
include_paths_.push_back(path);
|
||||
}
|
||||
void AddIncludeDirectory(const char *path) { include_paths_.push_back(path); }
|
||||
|
||||
// Returns a human readable error if any of the above functions fail.
|
||||
const std::string &GetLastError() { return lasterror_; }
|
||||
|
||||
private:
|
||||
bool LoadSchema(const std::string &ident, Parser *parser) {
|
||||
// Find the schema, if not, exit.
|
||||
auto it = schemas_.find(ident);
|
||||
if (it == schemas_.end()) {
|
||||
// Don't attach the identifier, since it may not be human readable.
|
||||
lasterror_ = "identifier for this buffer not in the registry";
|
||||
return false;
|
||||
}
|
||||
auto &schema = it->second;
|
||||
// Load the schema from disk. If not, exit.
|
||||
std::string schematext;
|
||||
if (!LoadFile(schema.path_.c_str(), false, &schematext)) {
|
||||
lasterror_ = "could not load schema: " + schema.path_;
|
||||
return false;
|
||||
}
|
||||
// Parse schema.
|
||||
parser->opts = opts_;
|
||||
if (!parser->Parse(schematext.c_str(), vector_data(include_paths_),
|
||||
schema.path_.c_str())) {
|
||||
lasterror_ = parser->error_;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool LoadSchema(const std::string &ident, Parser *parser) {
|
||||
// Find the schema, if not, exit.
|
||||
auto it = schemas_.find(ident);
|
||||
if (it == schemas_.end()) {
|
||||
// Don't attach the identifier, since it may not be human readable.
|
||||
lasterror_ = "identifier for this buffer not in the registry";
|
||||
return false;
|
||||
}
|
||||
auto &schema = it->second;
|
||||
// Load the schema from disk. If not, exit.
|
||||
std::string schematext;
|
||||
if (!LoadFile(schema.path_.c_str(), false, &schematext)) {
|
||||
lasterror_ = "could not load schema: " + schema.path_;
|
||||
return false;
|
||||
}
|
||||
// Parse schema.
|
||||
parser->opts = opts_;
|
||||
if (!parser->Parse(schematext.c_str(), vector_data(include_paths_),
|
||||
schema.path_.c_str())) {
|
||||
lasterror_ = parser->error_;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct Schema {
|
||||
std::string path_;
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#ifndef FLATBUFFERS_STL_EMULATION_H_
|
||||
#define FLATBUFFERS_STL_EMULATION_H_
|
||||
|
||||
// clang-format off
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
|
|
@ -17,32 +17,31 @@
|
|||
#ifndef FLATBUFFERS_UTIL_H_
|
||||
#define FLATBUFFERS_UTIL_H_
|
||||
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include <winbase.h>
|
||||
#include <direct.h>
|
||||
# ifndef WIN32_LEAN_AND_MEAN
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# endif
|
||||
# ifndef NOMINMAX
|
||||
# define NOMINMAX
|
||||
# endif
|
||||
# include <direct.h>
|
||||
# include <winbase.h>
|
||||
# include <windows.h>
|
||||
#else
|
||||
#include <limits.h>
|
||||
# include <limits.h>
|
||||
#endif
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "flatbuffers/base.h"
|
||||
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// Convert an integer or floating point value to a string.
|
||||
|
@ -61,18 +60,18 @@ template<> inline std::string NumToString<unsigned char>(unsigned char t) {
|
|||
return NumToString(static_cast<int>(t));
|
||||
}
|
||||
#if defined(FLATBUFFERS_CPP98_STL)
|
||||
template <> inline std::string NumToString<long long>(long long t) {
|
||||
char buf[21]; // (log((1 << 63) - 1) / log(10)) + 2
|
||||
snprintf(buf, sizeof(buf), "%lld", t);
|
||||
return std::string(buf);
|
||||
}
|
||||
template<> inline std::string NumToString<long long>(long long t) {
|
||||
char buf[21]; // (log((1 << 63) - 1) / log(10)) + 2
|
||||
snprintf(buf, sizeof(buf), "%lld", t);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
template <> inline std::string NumToString<unsigned long long>(
|
||||
unsigned long long t) {
|
||||
char buf[22]; // (log((1 << 63) - 1) / log(10)) + 1
|
||||
snprintf(buf, sizeof(buf), "%llu", t);
|
||||
return std::string(buf);
|
||||
}
|
||||
template<>
|
||||
inline std::string NumToString<unsigned long long>(unsigned long long t) {
|
||||
char buf[22]; // (log((1 << 63) - 1) / log(10)) + 1
|
||||
snprintf(buf, sizeof(buf), "%llu", t);
|
||||
return std::string(buf);
|
||||
}
|
||||
#endif // defined(FLATBUFFERS_CPP98_STL)
|
||||
|
||||
// Special versions for floats/doubles.
|
||||
|
@ -100,10 +99,7 @@ template<> inline std::string NumToString<float>(float t) {
|
|||
// For example, IntToStringHex(0x23, 8) returns the string "00000023".
|
||||
inline std::string IntToStringHex(int i, int xdigits) {
|
||||
std::stringstream ss;
|
||||
ss << std::setw(xdigits)
|
||||
<< std::setfill('0')
|
||||
<< std::hex
|
||||
<< std::uppercase
|
||||
ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase
|
||||
<< i;
|
||||
return ss.str();
|
||||
}
|
||||
|
@ -111,21 +107,25 @@ inline std::string IntToStringHex(int i, int xdigits) {
|
|||
// Portable implementation of strtoll().
|
||||
inline int64_t StringToInt(const char *str, char **endptr = nullptr,
|
||||
int base = 10) {
|
||||
// clang-format off
|
||||
#ifdef _MSC_VER
|
||||
return _strtoi64(str, endptr, base);
|
||||
#else
|
||||
return strtoll(str, endptr, base);
|
||||
#endif
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// Portable implementation of strtoull().
|
||||
inline uint64_t StringToUInt(const char *str, char **endptr = nullptr,
|
||||
int base = 10) {
|
||||
// clang-format off
|
||||
#ifdef _MSC_VER
|
||||
return _strtoui64(str, endptr, base);
|
||||
#else
|
||||
return strtoull(str, endptr, base);
|
||||
#endif
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
typedef bool (*LoadFileFunction)(const char *filename, bool binary,
|
||||
|
@ -134,9 +134,8 @@ typedef bool (*FileExistsFunction)(const char *filename);
|
|||
|
||||
LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function);
|
||||
|
||||
FileExistsFunction SetFileExistsFunction(FileExistsFunction
|
||||
file_exists_function);
|
||||
|
||||
FileExistsFunction SetFileExistsFunction(
|
||||
FileExistsFunction file_exists_function);
|
||||
|
||||
// Check if file "name" exists.
|
||||
bool FileExists(const char *name);
|
||||
|
@ -239,16 +238,19 @@ inline std::string PosixPath(const char *path) {
|
|||
inline void EnsureDirExists(const std::string &filepath) {
|
||||
auto parent = StripFileName(filepath);
|
||||
if (parent.length()) EnsureDirExists(parent);
|
||||
// clang-format off
|
||||
#ifdef _WIN32
|
||||
(void)_mkdir(filepath.c_str());
|
||||
#else
|
||||
mkdir(filepath.c_str(), S_IRWXU|S_IRGRP|S_IXGRP);
|
||||
#endif
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// Obtains the absolute path from any other path.
|
||||
// Returns the input path if the absolute path couldn't be resolved.
|
||||
inline std::string AbsolutePath(const std::string &filepath) {
|
||||
// clang-format off
|
||||
#ifdef FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
|
||||
return filepath;
|
||||
#else
|
||||
|
@ -262,6 +264,7 @@ inline std::string AbsolutePath(const std::string &filepath) {
|
|||
? abs_path
|
||||
: filepath;
|
||||
#endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// To and from UTF-8 unicode conversion functions
|
||||
|
@ -279,7 +282,7 @@ inline int ToUTF8(uint32_t ucc, std::string *out) {
|
|||
uint32_t remain_bits = i * 6;
|
||||
// Store first byte:
|
||||
(*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
|
||||
(ucc >> remain_bits));
|
||||
(ucc >> remain_bits));
|
||||
// Store remaining bytes:
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
(*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
|
||||
|
@ -309,9 +312,7 @@ inline int FromUTF8(const char **in) {
|
|||
if ((**in << len) & 0x80) return -1; // Bit after leading 1's must be 0.
|
||||
if (!len) return *(*in)++;
|
||||
// UTF-8 encoded values with a length are between 2 and 4 bytes.
|
||||
if (len < 2 || len > 4) {
|
||||
return -1;
|
||||
}
|
||||
if (len < 2 || len > 4) { return -1; }
|
||||
// Grab initial bits of the code.
|
||||
int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
|
@ -321,28 +322,20 @@ inline int FromUTF8(const char **in) {
|
|||
}
|
||||
// UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
|
||||
// UTF-16 surrogate pairs).
|
||||
if (ucc >= 0xD800 && ucc <= 0xDFFF) {
|
||||
return -1;
|
||||
}
|
||||
if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
|
||||
// UTF-8 must represent code points in their shortest possible encoding.
|
||||
switch (len) {
|
||||
case 2:
|
||||
// Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
|
||||
if (ucc < 0x0080 || ucc > 0x07FF) {
|
||||
return -1;
|
||||
}
|
||||
if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
|
||||
break;
|
||||
case 3:
|
||||
// Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
|
||||
if (ucc < 0x0800 || ucc > 0xFFFF) {
|
||||
return -1;
|
||||
}
|
||||
if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
|
||||
break;
|
||||
case 4:
|
||||
// Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
|
||||
if (ucc < 0x10000 || ucc > 0x10FFFF) {
|
||||
return -1;
|
||||
}
|
||||
if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
|
||||
break;
|
||||
}
|
||||
return ucc;
|
||||
|
@ -421,7 +414,8 @@ inline bool EscapeString(const char *s, size_t length, std::string *_text,
|
|||
text += "\\u";
|
||||
text += IntToStringHex(ucc, 4);
|
||||
} else if (ucc <= 0x10FFFF) {
|
||||
// Encode Unicode SMP values to a surrogate pair using two \u escapes.
|
||||
// Encode Unicode SMP values to a surrogate pair using two \u
|
||||
// escapes.
|
||||
uint32_t base = ucc - 0x10000;
|
||||
auto high_surrogate = (base >> 10) + 0xD800;
|
||||
auto low_surrogate = (base & 0x03FF) + 0xDC00;
|
||||
|
|
|
@ -14,13 +14,13 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
|
||||
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
|
||||
|
||||
using namespace MyGame::Sample;
|
||||
|
||||
// Example how to use FlatBuffers to create and read binary buffers.
|
||||
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
int main(int /*argc*/, const char * /*argv*/ []) {
|
||||
// Build up a serialized buffer algorithmically:
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
|
@ -53,7 +53,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
|
|||
auto orc = CreateMonster(builder, &position, 150, 80, name, inventory,
|
||||
Color_Red, weapons, Equipment_Weapon, axe.Union());
|
||||
|
||||
builder.Finish(orc); // Serialize the root of the object.
|
||||
builder.Finish(orc); // Serialize the root of the object.
|
||||
|
||||
// We now have a FlatBuffer we can store on disk or send over a network.
|
||||
|
||||
|
@ -83,8 +83,8 @@ int main(int /*argc*/, const char * /*argv*/[]) {
|
|||
(void)inv;
|
||||
|
||||
// Get and test the `weapons` FlatBuffers's `vector`.
|
||||
std::string expected_weapon_names[] = {"Sword", "Axe"};
|
||||
short expected_weapon_damages[] = {3, 5};
|
||||
std::string expected_weapon_names[] = { "Sword", "Axe" };
|
||||
short expected_weapon_damages[] = { 3, 5 };
|
||||
auto weps = monster->weapons();
|
||||
for (unsigned int i = 0; i < weps->size(); i++) {
|
||||
assert(weps->Get(i)->name()->str() == expected_weapon_names[i]);
|
||||
|
@ -95,11 +95,10 @@ int main(int /*argc*/, const char * /*argv*/[]) {
|
|||
|
||||
// Get and test the `Equipment` union (`equipped` field).
|
||||
assert(monster->equipped_type() == Equipment_Weapon);
|
||||
auto equipped = static_cast<const Weapon*>(monster->equipped());
|
||||
auto equipped = static_cast<const Weapon *>(monster->equipped());
|
||||
assert(equipped->name()->str() == "Axe");
|
||||
assert(equipped->damage() == 5);
|
||||
(void)equipped;
|
||||
|
||||
printf("The FlatBuffer was successfully created and verified!\n");
|
||||
}
|
||||
|
||||
|
|
|
@ -17,13 +17,13 @@
|
|||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
|
||||
#include "monster_generated.h" // Already includes "flatbuffers/flatbuffers.h".
|
||||
|
||||
using namespace MyGame::Sample;
|
||||
|
||||
// This is an example of parsing text straight into a buffer and then
|
||||
// generating flatbuffer (JSON) text from the buffer.
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
int main(int /*argc*/, const char * /*argv*/ []) {
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
clang-format -i -style=file include/flatbuffers/* src/*.cpp tests/test.cpp samples/*.cpp grpc/src/compiler/schema_interface.h grpc/tests/*.cpp
|
||||
git checkout include/flatbuffers/reflection_generated.h
|
|
@ -20,24 +20,19 @@
|
|||
#include "flatbuffers/util.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4127) // C4127: conditional expression is constant
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4127) // C4127: conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
void CodeWriter::operator+=(std::string text) {
|
||||
|
||||
while (true) {
|
||||
auto begin = text.find("{{");
|
||||
if (begin == std::string::npos) {
|
||||
break;
|
||||
}
|
||||
if (begin == std::string::npos) { break; }
|
||||
|
||||
auto end = text.find("}}");
|
||||
if (end == std::string::npos || end < begin) {
|
||||
break;
|
||||
}
|
||||
if (end == std::string::npos || end < begin) { break; }
|
||||
|
||||
// Write all the text before the first {{ into the stream.
|
||||
stream_.write(text.c_str(), begin);
|
||||
|
@ -119,7 +114,6 @@ std::string BaseGenerator::WrapInNameSpace(const Namespace *ns,
|
|||
return qualified_name + name;
|
||||
}
|
||||
|
||||
|
||||
std::string BaseGenerator::WrapInNameSpace(const Definition &def) const {
|
||||
return WrapInNameSpace(def.defined_namespace, def.name);
|
||||
}
|
||||
|
@ -150,12 +144,12 @@ void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
|
|||
if (config != nullptr && config->first_line != nullptr) {
|
||||
code += std::string(prefix) + std::string(config->first_line) + "\n";
|
||||
}
|
||||
std::string line_prefix = std::string(prefix) +
|
||||
((config != nullptr && config->content_line_prefix != nullptr) ?
|
||||
config->content_line_prefix : "///");
|
||||
for (auto it = dc.begin();
|
||||
it != dc.end();
|
||||
++it) {
|
||||
std::string line_prefix =
|
||||
std::string(prefix) +
|
||||
((config != nullptr && config->content_line_prefix != nullptr)
|
||||
? config->content_line_prefix
|
||||
: "///");
|
||||
for (auto it = dc.begin(); it != dc.end(); ++it) {
|
||||
code += line_prefix + *it + "\n";
|
||||
}
|
||||
if (config != nullptr && config->last_line != nullptr) {
|
||||
|
@ -166,5 +160,5 @@ void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
|
|||
} // namespace flatbuffers
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
|
262
src/flatc.cpp
262
src/flatc.cpp
|
@ -20,12 +20,10 @@
|
|||
|
||||
#define FLATC_VERSION "1.8.0 (" __DATE__ " " __TIME__ ")"
|
||||
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
void FlatCompiler::ParseFile(
|
||||
flatbuffers::Parser &parser,
|
||||
const std::string &filename,
|
||||
flatbuffers::Parser &parser, const std::string &filename,
|
||||
const std::string &contents,
|
||||
std::vector<const char *> &include_directories) const {
|
||||
auto local_include_directory = flatbuffers::StripFileName(filename);
|
||||
|
@ -47,11 +45,11 @@ void FlatCompiler::Error(const std::string &err, bool usage,
|
|||
params_.error_fn(this, err, usage, show_exe_name);
|
||||
}
|
||||
|
||||
std::string FlatCompiler::GetUsageString(const char* program_name) const {
|
||||
std::string FlatCompiler::GetUsageString(const char *program_name) const {
|
||||
std::stringstream ss;
|
||||
ss << "Usage: " << program_name << " [OPTION]... FILE... [-- FILE...]\n";
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
const Generator& g = params_.generators[i];
|
||||
const Generator &g = params_.generators[i];
|
||||
|
||||
std::stringstream full_name;
|
||||
full_name << std::setw(12) << std::left << g.generator_opt_long;
|
||||
|
@ -60,71 +58,73 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const {
|
|||
|
||||
ss << " " << full_name.str() << " " << name << " " << help << ".\n";
|
||||
}
|
||||
// clang-format off
|
||||
ss <<
|
||||
" -o PATH Prefix PATH to all generated files.\n"
|
||||
" -I PATH Search for includes in the specified path.\n"
|
||||
" -M Print make rules for generated files.\n"
|
||||
" --version Print the version number of flatc and exit.\n"
|
||||
" --strict-json Strict JSON: field names must be / will be quoted,\n"
|
||||
" no trailing commas in tables/vectors.\n"
|
||||
" --allow-non-utf8 Pass non-UTF-8 input through parser and emit nonstandard\n"
|
||||
" \\x escapes in JSON. (Default is to raise parse error on\n"
|
||||
" non-UTF-8 input.)\n"
|
||||
" --defaults-json Output fields whose value is the default when\n"
|
||||
" writing JSON\n"
|
||||
" --unknown-json Allow fields in JSON that are not defined in the\n"
|
||||
" schema. These fields will be discared when generating\n"
|
||||
" binaries.\n"
|
||||
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
|
||||
" --scoped-enums Use C++11 style scoped and strongly typed enums.\n"
|
||||
" also implies --no-prefix.\n"
|
||||
" --gen-includes (deprecated), this is the default behavior.\n"
|
||||
" If the original behavior is required (no include\n"
|
||||
" statements) use --no-includes.\n"
|
||||
" --no-includes Don\'t generate include statements for included\n"
|
||||
" schemas the generated file depends on (C++).\n"
|
||||
" --gen-mutable Generate accessors that can mutate buffers in-place.\n"
|
||||
" --gen-onefile Generate single output file for C# and Go.\n"
|
||||
" --gen-name-strings Generate type name functions for C++.\n"
|
||||
" --gen-object-api Generate an additional object-based API.\n"
|
||||
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
|
||||
" --cpp-str-type T Set object API string type (default std::string)\n"
|
||||
" T::c_str() and T::length() must be supported\n"
|
||||
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
||||
" --object-prefix Customise class prefix for C++ object-based API.\n"
|
||||
" --object-suffix Customise class suffix for C++ object-based API.\n"
|
||||
" Default value is \"T\"\n"
|
||||
" --no-js-exports Removes Node.js style export lines in JS.\n"
|
||||
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
|
||||
" --go-namespace Generate the overrided namespace in Golang.\n"
|
||||
" --go-import Generate the overrided import for flatbuffers in Golang.\n"
|
||||
" (default is \"github.com/google/flatbuffers/go\")\n"
|
||||
" --raw-binary Allow binaries without file_indentifier to be read.\n"
|
||||
" This may crash flatc given a mismatched schema.\n"
|
||||
" --proto Input is a .proto, translate to .fbs.\n"
|
||||
" --grpc Generate GRPC interfaces for the specified languages\n"
|
||||
" --schema Serialize schemas instead of JSON (use with -b)\n"
|
||||
" --bfbs-comments Add doc comments to the binary schema files.\n"
|
||||
" --conform FILE Specify a schema the following schemas should be\n"
|
||||
" an evolution of. Gives errors if not.\n"
|
||||
" --conform-includes Include path for the schema given with --conform\n"
|
||||
" PATH \n"
|
||||
" --include-prefix Prefix this path to any generated include statements.\n"
|
||||
" PATH\n"
|
||||
" --keep-prefix Keep original prefix of schema include statement.\n"
|
||||
" --no-fb-import Don't include flatbuffers import statement for TypeScript.\n"
|
||||
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
|
||||
" --reflect-types Add minimal type reflection to code generation.\n"
|
||||
" --reflect-names Add minimal type/name reflection.\n"
|
||||
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n"
|
||||
"schema). FILEs after the -- must be binary flatbuffer format files.\n"
|
||||
"Output files are named using the base file name of the input,\n"
|
||||
"and written to the current directory or the path given by -o.\n"
|
||||
"example: " << program_name << " -c -b schema1.fbs schema2.fbs data.json\n";
|
||||
" -o PATH Prefix PATH to all generated files.\n"
|
||||
" -I PATH Search for includes in the specified path.\n"
|
||||
" -M Print make rules for generated files.\n"
|
||||
" --version Print the version number of flatc and exit.\n"
|
||||
" --strict-json Strict JSON: field names must be / will be quoted,\n"
|
||||
" no trailing commas in tables/vectors.\n"
|
||||
" --allow-non-utf8 Pass non-UTF-8 input through parser and emit nonstandard\n"
|
||||
" \\x escapes in JSON. (Default is to raise parse error on\n"
|
||||
" non-UTF-8 input.)\n"
|
||||
" --defaults-json Output fields whose value is the default when\n"
|
||||
" writing JSON\n"
|
||||
" --unknown-json Allow fields in JSON that are not defined in the\n"
|
||||
" schema. These fields will be discared when generating\n"
|
||||
" binaries.\n"
|
||||
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
|
||||
" --scoped-enums Use C++11 style scoped and strongly typed enums.\n"
|
||||
" also implies --no-prefix.\n"
|
||||
" --gen-includes (deprecated), this is the default behavior.\n"
|
||||
" If the original behavior is required (no include\n"
|
||||
" statements) use --no-includes.\n"
|
||||
" --no-includes Don\'t generate include statements for included\n"
|
||||
" schemas the generated file depends on (C++).\n"
|
||||
" --gen-mutable Generate accessors that can mutate buffers in-place.\n"
|
||||
" --gen-onefile Generate single output file for C# and Go.\n"
|
||||
" --gen-name-strings Generate type name functions for C++.\n"
|
||||
" --gen-object-api Generate an additional object-based API.\n"
|
||||
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
|
||||
" --cpp-str-type T Set object API string type (default std::string)\n"
|
||||
" T::c_str() and T::length() must be supported\n"
|
||||
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
||||
" --object-prefix Customise class prefix for C++ object-based API.\n"
|
||||
" --object-suffix Customise class suffix for C++ object-based API.\n"
|
||||
" Default value is \"T\"\n"
|
||||
" --no-js-exports Removes Node.js style export lines in JS.\n"
|
||||
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
|
||||
" --go-namespace Generate the overrided namespace in Golang.\n"
|
||||
" --go-import Generate the overrided import for flatbuffers in Golang.\n"
|
||||
" (default is \"github.com/google/flatbuffers/go\")\n"
|
||||
" --raw-binary Allow binaries without file_indentifier to be read.\n"
|
||||
" This may crash flatc given a mismatched schema.\n"
|
||||
" --proto Input is a .proto, translate to .fbs.\n"
|
||||
" --grpc Generate GRPC interfaces for the specified languages\n"
|
||||
" --schema Serialize schemas instead of JSON (use with -b)\n"
|
||||
" --bfbs-comments Add doc comments to the binary schema files.\n"
|
||||
" --conform FILE Specify a schema the following schemas should be\n"
|
||||
" an evolution of. Gives errors if not.\n"
|
||||
" --conform-includes Include path for the schema given with --conform\n"
|
||||
" PATH \n"
|
||||
" --include-prefix Prefix this path to any generated include statements.\n"
|
||||
" PATH\n"
|
||||
" --keep-prefix Keep original prefix of schema include statement.\n"
|
||||
" --no-fb-import Don't include flatbuffers import statement for TypeScript.\n"
|
||||
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
|
||||
" --reflect-types Add minimal type reflection to code generation.\n"
|
||||
" --reflect-names Add minimal type/name reflection.\n"
|
||||
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n"
|
||||
"schema). FILEs after the -- must be binary flatbuffer format files.\n"
|
||||
"Output files are named using the base file name of the input,\n"
|
||||
"and written to the current directory or the path given by -o.\n"
|
||||
"example: " << program_name << " -c -b schema1.fbs schema2.fbs data.json\n";
|
||||
// clang-format on
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
int FlatCompiler::Compile(int argc, const char** argv) {
|
||||
int FlatCompiler::Compile(int argc, const char **argv) {
|
||||
if (params_.generators == nullptr || params_.num_generators == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -153,58 +153,58 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
if (arg == "-o") {
|
||||
if (++argi >= argc) Error("missing path following: " + arg, true);
|
||||
output_path = flatbuffers::ConCatPathFileName(
|
||||
flatbuffers::PosixPath(argv[argi]), "");
|
||||
} else if(arg == "-I") {
|
||||
flatbuffers::PosixPath(argv[argi]), "");
|
||||
} else if (arg == "-I") {
|
||||
if (++argi >= argc) Error("missing path following" + arg, true);
|
||||
include_directories_storage.push_back(
|
||||
flatbuffers::PosixPath(argv[argi]));
|
||||
flatbuffers::PosixPath(argv[argi]));
|
||||
include_directories.push_back(
|
||||
include_directories_storage.back().c_str());
|
||||
} else if(arg == "--conform") {
|
||||
include_directories_storage.back().c_str());
|
||||
} else if (arg == "--conform") {
|
||||
if (++argi >= argc) Error("missing path following" + arg, true);
|
||||
conform_to_schema = flatbuffers::PosixPath(argv[argi]);
|
||||
} else if (arg == "--conform-includes") {
|
||||
if (++argi >= argc) Error("missing path following" + arg, true);
|
||||
include_directories_storage.push_back(
|
||||
flatbuffers::PosixPath(argv[argi]));
|
||||
flatbuffers::PosixPath(argv[argi]));
|
||||
conform_include_directories.push_back(
|
||||
include_directories_storage.back().c_str());
|
||||
include_directories_storage.back().c_str());
|
||||
} else if (arg == "--include-prefix") {
|
||||
if (++argi >= argc) Error("missing path following" + arg, true);
|
||||
opts.include_prefix = flatbuffers::ConCatPathFileName(
|
||||
flatbuffers::PosixPath(argv[argi]), "");
|
||||
} else if(arg == "--keep-prefix") {
|
||||
flatbuffers::PosixPath(argv[argi]), "");
|
||||
} else if (arg == "--keep-prefix") {
|
||||
opts.keep_include_path = true;
|
||||
} else if(arg == "--strict-json") {
|
||||
} else if (arg == "--strict-json") {
|
||||
opts.strict_json = true;
|
||||
} else if(arg == "--allow-non-utf8") {
|
||||
} else if (arg == "--allow-non-utf8") {
|
||||
opts.allow_non_utf8 = true;
|
||||
} else if(arg == "--no-js-exports") {
|
||||
} else if (arg == "--no-js-exports") {
|
||||
opts.skip_js_exports = true;
|
||||
} else if(arg == "--goog-js-export") {
|
||||
} else if (arg == "--goog-js-export") {
|
||||
opts.use_goog_js_export_format = true;
|
||||
} else if(arg == "--go-namespace") {
|
||||
} else if (arg == "--go-namespace") {
|
||||
if (++argi >= argc) Error("missing golang namespace" + arg, true);
|
||||
opts.go_namespace = argv[argi];
|
||||
} else if(arg == "--go-import") {
|
||||
} else if (arg == "--go-import") {
|
||||
if (++argi >= argc) Error("missing golang import" + arg, true);
|
||||
opts.go_import = argv[argi];
|
||||
} else if(arg == "--defaults-json") {
|
||||
} else if (arg == "--defaults-json") {
|
||||
opts.output_default_scalars_in_json = true;
|
||||
} else if (arg == "--unknown-json") {
|
||||
opts.skip_unexpected_fields_in_json = true;
|
||||
} else if(arg == "--no-prefix") {
|
||||
} else if (arg == "--no-prefix") {
|
||||
opts.prefixed_enums = false;
|
||||
} else if(arg == "--scoped-enums") {
|
||||
} else if (arg == "--scoped-enums") {
|
||||
opts.prefixed_enums = false;
|
||||
opts.scoped_enums = true;
|
||||
} else if (arg == "--no-union-value-namespacing") {
|
||||
opts.union_value_namespacing = false;
|
||||
} else if(arg == "--gen-mutable") {
|
||||
} else if (arg == "--gen-mutable") {
|
||||
opts.mutable_buffer = true;
|
||||
} else if(arg == "--gen-name-strings") {
|
||||
} else if (arg == "--gen-name-strings") {
|
||||
opts.generate_name_strings = true;
|
||||
} else if(arg == "--gen-object-api") {
|
||||
} else if (arg == "--gen-object-api") {
|
||||
opts.generate_object_based_api = true;
|
||||
} else if (arg == "--cpp-ptr-type") {
|
||||
if (++argi >= argc) Error("missing type following" + arg, true);
|
||||
|
@ -220,40 +220,40 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
} else if (arg == "--object-suffix") {
|
||||
if (++argi >= argc) Error("missing suffix following" + arg, true);
|
||||
opts.object_suffix = argv[argi];
|
||||
} else if(arg == "--gen-all") {
|
||||
} else if (arg == "--gen-all") {
|
||||
opts.generate_all = true;
|
||||
opts.include_dependence_headers = false;
|
||||
} else if(arg == "--gen-includes") {
|
||||
} else if (arg == "--gen-includes") {
|
||||
// Deprecated, remove this option some time in the future.
|
||||
printf("warning: --gen-includes is deprecated (it is now default)\n");
|
||||
} else if(arg == "--no-includes") {
|
||||
} else if (arg == "--no-includes") {
|
||||
opts.include_dependence_headers = false;
|
||||
} else if (arg == "--gen-onefile") {
|
||||
opts.one_file = true;
|
||||
} else if (arg == "--raw-binary") {
|
||||
raw_binary = true;
|
||||
} else if(arg == "--") { // Separator between text and binary inputs.
|
||||
} else if (arg == "--") { // Separator between text and binary inputs.
|
||||
binary_files_from = filenames.size();
|
||||
} else if(arg == "--proto") {
|
||||
} else if (arg == "--proto") {
|
||||
opts.proto_mode = true;
|
||||
} else if(arg == "--schema") {
|
||||
} else if (arg == "--schema") {
|
||||
schema_binary = true;
|
||||
} else if(arg == "-M") {
|
||||
} else if (arg == "-M") {
|
||||
print_make_rules = true;
|
||||
} else if(arg == "--version") {
|
||||
} else if (arg == "--version") {
|
||||
printf("flatc version %s\n", FLATC_VERSION);
|
||||
exit(0);
|
||||
} else if(arg == "--grpc") {
|
||||
} else if (arg == "--grpc") {
|
||||
grpc_enabled = true;
|
||||
} else if(arg == "--bfbs-comments") {
|
||||
} else if (arg == "--bfbs-comments") {
|
||||
opts.binary_schema_comments = true;
|
||||
} else if(arg == "--no-fb-import") {
|
||||
} else if (arg == "--no-fb-import") {
|
||||
opts.skip_flatbuffers_import = true;
|
||||
} else if(arg == "--no-ts-reexport") {
|
||||
} else if (arg == "--no-ts-reexport") {
|
||||
opts.reexport_ts_modules = false;
|
||||
} else if(arg == "--reflect-types") {
|
||||
} else if (arg == "--reflect-types") {
|
||||
opts.mini_reflect = IDLOptions::kTypes;
|
||||
} else if(arg == "--reflect-names") {
|
||||
} else if (arg == "--reflect-names") {
|
||||
opts.mini_reflect = IDLOptions::kTypesAndNames;
|
||||
} else {
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
|
@ -267,7 +267,7 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
}
|
||||
}
|
||||
Error("unknown commandline argument: " + arg, true);
|
||||
found:;
|
||||
found:;
|
||||
}
|
||||
} else {
|
||||
filenames.push_back(flatbuffers::PosixPath(argv[argi]));
|
||||
|
@ -294,23 +294,22 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
|
||||
std::unique_ptr<flatbuffers::Parser> parser(new flatbuffers::Parser(opts));
|
||||
|
||||
for (auto file_it = filenames.begin();
|
||||
file_it != filenames.end();
|
||||
++file_it) {
|
||||
for (auto file_it = filenames.begin(); file_it != filenames.end();
|
||||
++file_it) {
|
||||
auto &filename = *file_it;
|
||||
std::string contents;
|
||||
if (!flatbuffers::LoadFile(filename.c_str(), true, &contents))
|
||||
Error("unable to load file: " + filename);
|
||||
|
||||
bool is_binary = static_cast<size_t>(file_it - filenames.begin()) >=
|
||||
binary_files_from;
|
||||
bool is_binary =
|
||||
static_cast<size_t>(file_it - filenames.begin()) >= binary_files_from;
|
||||
auto ext = flatbuffers::GetExtension(filename);
|
||||
auto is_schema = ext == "fbs" || ext == "proto";
|
||||
if (is_binary) {
|
||||
parser->builder_.Clear();
|
||||
parser->builder_.PushFlatBuffer(
|
||||
reinterpret_cast<const uint8_t *>(contents.c_str()),
|
||||
contents.length());
|
||||
reinterpret_cast<const uint8_t *>(contents.c_str()),
|
||||
contents.length());
|
||||
if (!raw_binary) {
|
||||
// Generally reading binaries that do not correspond to the schema
|
||||
// will crash, and sadly there's no way around that when the binary
|
||||
|
@ -319,16 +318,15 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
// such an identifier, so by default we require them to match.
|
||||
if (!parser->file_identifier_.length()) {
|
||||
Error("current schema has no file_identifier: cannot test if \"" +
|
||||
filename +
|
||||
"\" matches the schema, use --raw-binary to read this file"
|
||||
" anyway.");
|
||||
} else if (!flatbuffers::BufferHasIdentifier(contents.c_str(),
|
||||
parser->file_identifier_.c_str())) {
|
||||
Error("binary \"" +
|
||||
filename +
|
||||
"\" does not have expected file_identifier \"" +
|
||||
parser->file_identifier_ +
|
||||
"\", use --raw-binary to read this file anyway.");
|
||||
filename +
|
||||
"\" matches the schema, use --raw-binary to read this file"
|
||||
" anyway.");
|
||||
} else if (!flatbuffers::BufferHasIdentifier(
|
||||
contents.c_str(), parser->file_identifier_.c_str())) {
|
||||
Error("binary \"" + filename +
|
||||
"\" does not have expected file_identifier \"" +
|
||||
parser->file_identifier_ +
|
||||
"\", use --raw-binary to read this file anyway.");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -346,8 +344,9 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
if (!is_schema && !parser->builder_.GetSize()) {
|
||||
// If a file doesn't end in .fbs, it must be json/binary. Ensure we
|
||||
// didn't just parse a schema with a different extension.
|
||||
Error("input file is neither json nor a .fbs (schema) file: " +
|
||||
filename, true);
|
||||
Error(
|
||||
"input file is neither json nor a .fbs (schema) file: " + filename,
|
||||
true);
|
||||
}
|
||||
if (is_schema && !conform_to_schema.empty()) {
|
||||
auto err = parser->ConformTo(conform_parser);
|
||||
|
@ -359,8 +358,8 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
}
|
||||
}
|
||||
|
||||
std::string filebase = flatbuffers::StripPath(
|
||||
flatbuffers::StripExtension(filename));
|
||||
std::string filebase =
|
||||
flatbuffers::StripPath(flatbuffers::StripExtension(filename));
|
||||
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
parser->opts.lang = params_.generators[i].lang;
|
||||
|
@ -368,29 +367,28 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||
if (!print_make_rules) {
|
||||
flatbuffers::EnsureDirExists(output_path);
|
||||
if ((!params_.generators[i].schema_only || is_schema) &&
|
||||
!params_.generators[i].generate(*parser.get(), output_path, filebase)) {
|
||||
!params_.generators[i].generate(*parser.get(), output_path,
|
||||
filebase)) {
|
||||
Error(std::string("Unable to generate ") +
|
||||
params_.generators[i].lang_name +
|
||||
" for " +
|
||||
filebase);
|
||||
params_.generators[i].lang_name + " for " + filebase);
|
||||
}
|
||||
} else {
|
||||
std::string make_rule = params_.generators[i].make_rule(
|
||||
*parser.get(), output_path, filename);
|
||||
if (!make_rule.empty())
|
||||
printf("%s\n", flatbuffers::WordWrap(
|
||||
make_rule, 80, " ", " \\").c_str());
|
||||
printf("%s\n",
|
||||
flatbuffers::WordWrap(make_rule, 80, " ", " \\").c_str());
|
||||
}
|
||||
if (grpc_enabled) {
|
||||
if (params_.generators[i].generateGRPC != nullptr) {
|
||||
if (!params_.generators[i].generateGRPC(*parser.get(), output_path,
|
||||
filebase)) {
|
||||
filebase)) {
|
||||
Error(std::string("Unable to generate GRPC interface for") +
|
||||
params_.generators[i].lang_name);
|
||||
}
|
||||
} else {
|
||||
Warn(std::string("GRPC interface generator not implemented for ")
|
||||
+ params_.generators[i].lang_name);
|
||||
Warn(std::string("GRPC interface generator not implemented for ") +
|
||||
params_.generators[i].lang_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,26 +19,17 @@
|
|||
static const char *g_program_name = nullptr;
|
||||
|
||||
static void Warn(const flatbuffers::FlatCompiler *flatc,
|
||||
const std::string &warn,
|
||||
bool show_exe_name) {
|
||||
const std::string &warn, bool show_exe_name) {
|
||||
(void)flatc;
|
||||
if (show_exe_name) {
|
||||
printf("%s: ", g_program_name);
|
||||
}
|
||||
if (show_exe_name) { printf("%s: ", g_program_name); }
|
||||
printf("warning: %s\n", warn.c_str());
|
||||
}
|
||||
|
||||
static void Error(const flatbuffers::FlatCompiler *flatc,
|
||||
const std::string &err,
|
||||
bool usage,
|
||||
bool show_exe_name) {
|
||||
if (show_exe_name) {
|
||||
printf("%s: ", g_program_name);
|
||||
}
|
||||
const std::string &err, bool usage, bool show_exe_name) {
|
||||
if (show_exe_name) { printf("%s: ", g_program_name); }
|
||||
printf("error: %s\n", err.c_str());
|
||||
if (usage) {
|
||||
printf("%s", flatc->GetUsageString(g_program_name).c_str());
|
||||
}
|
||||
if (usage) { printf("%s", flatc->GetUsageString(g_program_name).c_str()); }
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -46,61 +37,43 @@ int main(int argc, const char *argv[]) {
|
|||
g_program_name = argv[0];
|
||||
|
||||
const flatbuffers::FlatCompiler::Generator generators[] = {
|
||||
{ flatbuffers::GenerateBinary, "-b", "--binary", "binary", false,
|
||||
nullptr,
|
||||
{ flatbuffers::GenerateBinary, "-b", "--binary", "binary", false, nullptr,
|
||||
flatbuffers::IDLOptions::kBinary,
|
||||
"Generate wire format binaries for any data definitions",
|
||||
flatbuffers::BinaryMakeRule },
|
||||
{ flatbuffers::GenerateTextFile, "-t", "--json", "text", false,
|
||||
nullptr,
|
||||
{ flatbuffers::GenerateTextFile, "-t", "--json", "text", false, nullptr,
|
||||
flatbuffers::IDLOptions::kJson,
|
||||
"Generate text output for any data definitions",
|
||||
flatbuffers::TextMakeRule },
|
||||
{ flatbuffers::GenerateCPP, "-c", "--cpp", "C++", true,
|
||||
flatbuffers::GenerateCppGRPC,
|
||||
flatbuffers::IDLOptions::kCpp,
|
||||
"Generate C++ headers for tables/structs",
|
||||
flatbuffers::CPPMakeRule },
|
||||
{ flatbuffers::GenerateGo, "-g", "--go", "Go", true,
|
||||
flatbuffers::GenerateGoGRPC,
|
||||
flatbuffers::IDLOptions::kGo,
|
||||
"Generate Go files for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GenerateGeneral, "-j", "--java", "Java", true,
|
||||
flatbuffers::GenerateJavaGRPC,
|
||||
flatbuffers::IDLOptions::kJava,
|
||||
{ flatbuffers::GenerateCPP, "-c", "--cpp", "C++", true,
|
||||
flatbuffers::GenerateCppGRPC, flatbuffers::IDLOptions::kCpp,
|
||||
"Generate C++ headers for tables/structs", flatbuffers::CPPMakeRule },
|
||||
{ flatbuffers::GenerateGo, "-g", "--go", "Go", true,
|
||||
flatbuffers::GenerateGoGRPC, flatbuffers::IDLOptions::kGo,
|
||||
"Generate Go files for tables/structs", flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GenerateGeneral, "-j", "--java", "Java", true,
|
||||
flatbuffers::GenerateJavaGRPC, flatbuffers::IDLOptions::kJava,
|
||||
"Generate Java classes for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GenerateJS, "-s", "--js", "JavaScript", true,
|
||||
nullptr,
|
||||
{ flatbuffers::GenerateJS, "-s", "--js", "JavaScript", true, nullptr,
|
||||
flatbuffers::IDLOptions::kJs,
|
||||
"Generate JavaScript code for tables/structs",
|
||||
flatbuffers::JSMakeRule },
|
||||
{ flatbuffers::GenerateJS, "-T", "--ts", "TypeScript", true,
|
||||
nullptr,
|
||||
"Generate JavaScript code for tables/structs", flatbuffers::JSMakeRule },
|
||||
{ flatbuffers::GenerateJS, "-T", "--ts", "TypeScript", true, nullptr,
|
||||
flatbuffers::IDLOptions::kTs,
|
||||
"Generate TypeScript code for tables/structs",
|
||||
flatbuffers::JSMakeRule },
|
||||
{ flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", true,
|
||||
nullptr,
|
||||
"Generate TypeScript code for tables/structs", flatbuffers::JSMakeRule },
|
||||
{ flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", true, nullptr,
|
||||
flatbuffers::IDLOptions::kCSharp,
|
||||
"Generate C# classes for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GeneratePython, "-p", "--python", "Python", true,
|
||||
nullptr,
|
||||
"Generate C# classes for tables/structs", flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GeneratePython, "-p", "--python", "Python", true, nullptr,
|
||||
flatbuffers::IDLOptions::kPython,
|
||||
"Generate Python files for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true,
|
||||
nullptr,
|
||||
flatbuffers::IDLOptions::kPhp,
|
||||
"Generate PHP files for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema", true,
|
||||
nullptr,
|
||||
flatbuffers::IDLOptions::kJsonSchema,
|
||||
"Generate Json schema",
|
||||
{ flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr,
|
||||
flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs",
|
||||
flatbuffers::GeneralMakeRule },
|
||||
{ flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema",
|
||||
true, nullptr, flatbuffers::IDLOptions::kJsonSchema,
|
||||
"Generate Json schema", flatbuffers::GeneralMakeRule },
|
||||
};
|
||||
|
||||
flatbuffers::FlatCompiler::InitParams params;
|
||||
|
|
|
@ -14,20 +14,16 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "flatbuffers/hash.h"
|
||||
#include <stdio.h>
|
||||
|
||||
enum OutputFormat {
|
||||
kDecimal,
|
||||
kHexadecimal,
|
||||
kHexadecimal0x
|
||||
};
|
||||
enum OutputFormat { kDecimal, kHexadecimal, kHexadecimal0x };
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
const char* name = argv[0];
|
||||
int main(int argc, char *argv[]) {
|
||||
const char *name = argv[0];
|
||||
if (argc <= 1) {
|
||||
printf("%s HASH [OPTION]... STRING... [-- STRING...]\n", name);
|
||||
printf("Available hashing algorithms:\n 32 bit:\n");
|
||||
|
@ -50,7 +46,7 @@ int main(int argc, char* argv[]) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
const char* hash_algorithm = argv[1];
|
||||
const char *hash_algorithm = argv[1];
|
||||
|
||||
flatbuffers::NamedHashFunction<uint32_t>::HashFunction hash_function32 =
|
||||
flatbuffers::FindHashFunction32(hash_algorithm);
|
||||
|
@ -66,15 +62,21 @@ int main(int argc, char* argv[]) {
|
|||
bool annotate = false;
|
||||
bool escape_dash = false;
|
||||
for (int i = 2; i < argc; i++) {
|
||||
const char* arg = argv[i];
|
||||
const char *arg = argv[i];
|
||||
if (!escape_dash && arg[0] == '-') {
|
||||
std::string opt = arg;
|
||||
if (opt == "-d") output_format = kDecimal;
|
||||
else if (opt == "-x") output_format = kHexadecimal;
|
||||
else if (opt == "-0x") output_format = kHexadecimal0x;
|
||||
else if (opt == "-c") annotate = true;
|
||||
else if (opt == "--") escape_dash = true;
|
||||
else printf("Unrecognized argument: \"%s\"\n", arg);
|
||||
if (opt == "-d")
|
||||
output_format = kDecimal;
|
||||
else if (opt == "-x")
|
||||
output_format = kHexadecimal;
|
||||
else if (opt == "-0x")
|
||||
output_format = kHexadecimal0x;
|
||||
else if (opt == "-c")
|
||||
annotate = true;
|
||||
else if (opt == "--")
|
||||
escape_dash = true;
|
||||
else
|
||||
printf("Unrecognized argument: \"%s\"\n", arg);
|
||||
} else {
|
||||
std::stringstream ss;
|
||||
if (output_format == kDecimal) {
|
||||
|
@ -90,8 +92,7 @@ int main(int argc, char* argv[]) {
|
|||
else if (hash_function64)
|
||||
ss << hash_function64(arg);
|
||||
|
||||
if (annotate)
|
||||
ss << " /* \"" << arg << "\" */";
|
||||
if (annotate) ss << " /* \"" << arg << "\" */";
|
||||
|
||||
ss << "\n";
|
||||
|
||||
|
@ -100,4 +101,3 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -16,10 +16,10 @@
|
|||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/code_generators.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
|
@ -27,16 +27,15 @@ static std::string GenType(const Type &type, bool underlying = false) {
|
|||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRUCT:
|
||||
return type.struct_def->defined_namespace->GetFullyQualifiedName(
|
||||
type.struct_def->name);
|
||||
case BASE_TYPE_VECTOR:
|
||||
return "[" + GenType(type.VectorType()) + "]";
|
||||
type.struct_def->name);
|
||||
case BASE_TYPE_VECTOR: return "[" + GenType(type.VectorType()) + "]";
|
||||
default:
|
||||
if (type.enum_def && !underlying) {
|
||||
return type.enum_def->defined_namespace->GetFullyQualifiedName(
|
||||
type.enum_def->name);
|
||||
} else {
|
||||
return kTypeNames[type.base_type];
|
||||
}
|
||||
if (type.enum_def && !underlying) {
|
||||
return type.enum_def->defined_namespace->GetFullyQualifiedName(
|
||||
type.enum_def->name);
|
||||
} else {
|
||||
return kTypeNames[type.base_type];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +46,7 @@ static void GenNameSpace(const Namespace &name_space, std::string *_schema,
|
|||
auto &schema = *_schema;
|
||||
schema += "namespace ";
|
||||
for (auto it = name_space.components.begin();
|
||||
it != name_space.components.end(); ++it) {
|
||||
it != name_space.components.end(); ++it) {
|
||||
if (it != name_space.components.begin()) schema += ".";
|
||||
schema += *it;
|
||||
}
|
||||
|
@ -69,6 +68,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
|
|||
std::string schema;
|
||||
schema += "// Generated from " + file_name + ".proto\n\n";
|
||||
if (parser.opts.include_dependence_headers) {
|
||||
// clang-format off
|
||||
#ifdef FBS_GEN_INCLUDES // TODO: currently all in one file.
|
||||
int num_includes = 0;
|
||||
for (auto it = parser.included_files_.begin();
|
||||
|
@ -82,18 +82,19 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
|
|||
}
|
||||
if (num_includes) schema += "\n";
|
||||
#endif
|
||||
// clang-format on
|
||||
}
|
||||
// Generate code for all the enum declarations.
|
||||
const Namespace *last_namespace = nullptr;
|
||||
for (auto enum_def_it = parser.enums_.vec.begin();
|
||||
enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
|
||||
enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
|
||||
EnumDef &enum_def = **enum_def_it;
|
||||
GenNameSpace(*enum_def.defined_namespace, &schema, &last_namespace);
|
||||
GenComment(enum_def.doc_comment, &schema, nullptr);
|
||||
schema += "enum " + enum_def.name + " : ";
|
||||
schema += GenType(enum_def.underlying_type, true) + " {\n";
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end(); ++it) {
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
GenComment(ev.doc_comment, &schema, nullptr, " ");
|
||||
schema += " " + ev.name + " = " + NumToString(ev.value) + ",\n";
|
||||
|
@ -101,14 +102,14 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
|
|||
schema += "}\n\n";
|
||||
}
|
||||
// Generate code for all structs/tables.
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
|
||||
++it) {
|
||||
StructDef &struct_def = **it;
|
||||
GenNameSpace(*struct_def.defined_namespace, &schema, &last_namespace);
|
||||
GenComment(struct_def.doc_comment, &schema, nullptr);
|
||||
schema += "table " + struct_def.name + " {\n";
|
||||
for (auto field_it = struct_def.fields.vec.begin();
|
||||
field_it != struct_def.fields.vec.end(); ++field_it) {
|
||||
field_it != struct_def.fields.vec.end(); ++field_it) {
|
||||
auto &field = **field_it;
|
||||
if (field.value.type.base_type != BASE_TYPE_UTYPE) {
|
||||
GenComment(field.doc_comment, &schema, nullptr, " ");
|
||||
|
@ -123,12 +124,10 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
|
|||
return schema;
|
||||
}
|
||||
|
||||
bool GenerateFBS(const Parser &parser,
|
||||
const std::string &path,
|
||||
bool GenerateFBS(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
return SaveFile((path + file_name + ".fbs").c_str(),
|
||||
GenerateFBS(parser, file_name), false);
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -16,21 +16,21 @@
|
|||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/code_generators.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#define PATH_SEPARATOR "\\"
|
||||
#define mkdir(n, m) _mkdir(n)
|
||||
# include <direct.h>
|
||||
# define PATH_SEPARATOR "\\"
|
||||
# define mkdir(n, m) _mkdir(n)
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#define PATH_SEPARATOR "/"
|
||||
# include <sys/stat.h>
|
||||
# define PATH_SEPARATOR "/"
|
||||
#endif
|
||||
|
||||
namespace flatbuffers {
|
||||
|
@ -44,9 +44,10 @@ namespace go {
|
|||
|
||||
// see https://golang.org/ref/spec#Keywords
|
||||
static const char *g_golang_keywords[] = {
|
||||
"break", "default", "func", "interface", "select", "case", "defer", "go",
|
||||
"map", "struct", "chan", "else", "goto", "package", "switch", "const",
|
||||
"fallthrough", "if", "range", "type", "continue", "for", "import", "return", "var",
|
||||
"break", "default", "func", "interface", "select", "case", "defer",
|
||||
"go", "map", "struct", "chan", "else", "goto", "package",
|
||||
"switch", "const", "fallthrough", "if", "range", "type", "continue",
|
||||
"for", "import", "return", "var",
|
||||
};
|
||||
|
||||
static std::string GenGetter(const Type &type);
|
||||
|
@ -57,23 +58,20 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr);
|
|||
static std::string GenTypeBasic(const Type &type);
|
||||
static std::string GenTypeGet(const Type &type);
|
||||
static std::string TypeName(const FieldDef &field);
|
||||
static std::string GoIdentity(const std::string& name) {
|
||||
for (size_t i=0; i<sizeof(g_golang_keywords)/sizeof(g_golang_keywords[0]); i++) {
|
||||
if (name == g_golang_keywords[i]) {
|
||||
return MakeCamel(name + "_", false);
|
||||
}
|
||||
static std::string GoIdentity(const std::string &name) {
|
||||
for (size_t i = 0;
|
||||
i < sizeof(g_golang_keywords) / sizeof(g_golang_keywords[0]); i++) {
|
||||
if (name == g_golang_keywords[i]) { return MakeCamel(name + "_", false); }
|
||||
}
|
||||
|
||||
return MakeCamel(name, false);
|
||||
}
|
||||
|
||||
|
||||
// Most field accessors need to retrieve and test the field offset first,
|
||||
// this is the prefix code for that.
|
||||
std::string OffsetPrefix(const FieldDef &field) {
|
||||
return "{\n\to := flatbuffers.UOffsetT(rcv._tab.Offset(" +
|
||||
NumToString(field.value.offset) +
|
||||
"))\n\tif o != 0 {\n";
|
||||
NumToString(field.value.offset) + "))\n\tif o != 0 {\n";
|
||||
}
|
||||
|
||||
// Begin a class declaration.
|
||||
|
@ -169,7 +167,7 @@ static void InitializeExisting(const StructDef &struct_def,
|
|||
|
||||
// Implement the table accessor
|
||||
static void GenTableAccessor(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
|
@ -177,16 +175,15 @@ static void GenTableAccessor(const StructDef &struct_def,
|
|||
code += "{\n";
|
||||
|
||||
if (struct_def.fixed) {
|
||||
code += "\treturn rcv._tab.Table\n";
|
||||
code += "\treturn rcv._tab.Table\n";
|
||||
} else {
|
||||
code += "\treturn rcv._tab\n";
|
||||
code += "\treturn rcv._tab\n";
|
||||
}
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get the length of a vector.
|
||||
static void GetVectorLen(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
|
@ -198,8 +195,7 @@ static void GetVectorLen(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Get a [ubyte] vector as a byte slice.
|
||||
static void GetUByteSlice(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetUByteSlice(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
|
@ -219,7 +215,7 @@ static void GetScalarFieldOfStruct(const StructDef &struct_def,
|
|||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "() " + TypeName(field) + " {\n";
|
||||
code +="\treturn " + getter;
|
||||
code += "\treturn " + getter;
|
||||
code += "(rcv._tab.Pos + flatbuffers.UOffsetT(";
|
||||
code += NumToString(field.value.offset) + "))\n}\n";
|
||||
}
|
||||
|
@ -284,12 +280,11 @@ static void GetStructFieldOfTable(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Get the value of a string.
|
||||
static void GetStringField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetStringField(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += " " + MakeCamel(field.name);
|
||||
code += "() " + TypeName(field) + " ";
|
||||
code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type);
|
||||
code += "(o + rcv._tab.Pos)\n\t}\n\treturn nil\n";
|
||||
|
@ -297,8 +292,7 @@ static void GetStringField(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Get the value of a union from an object.
|
||||
static void GetUnionField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetUnionField(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
|
@ -365,8 +359,8 @@ static void BeginBuilderArgs(const StructDef &struct_def,
|
|||
std::string &code = *code_ptr;
|
||||
|
||||
if (code.substr(code.length() - 2) != "\n\n") {
|
||||
// a previous mutate has not put an extra new line
|
||||
code += "\n";
|
||||
// a previous mutate has not put an extra new line
|
||||
code += "\n";
|
||||
}
|
||||
code += "func Create" + struct_def.name;
|
||||
code += "(builder *flatbuffers.Builder";
|
||||
|
@ -375,22 +369,19 @@ static void BeginBuilderArgs(const StructDef &struct_def,
|
|||
// Recursively generate arguments for a constructor, to deal with nested
|
||||
// structs.
|
||||
static void StructBuilderArgs(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *code_ptr) {
|
||||
const char *nameprefix, std::string *code_ptr) {
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (IsStruct(field.value.type)) {
|
||||
// Generate arguments for a struct inside a struct. To ensure names
|
||||
// don't clash, and to make it obvious these arguments are constructing
|
||||
// a nested struct, prefix the name with the field name.
|
||||
StructBuilderArgs(*field.value.type.struct_def,
|
||||
(nameprefix + (field.name + "_")).c_str(),
|
||||
code_ptr);
|
||||
(nameprefix + (field.name + "_")).c_str(), code_ptr);
|
||||
} else {
|
||||
std::string &code = *code_ptr;
|
||||
code += (std::string)", " + nameprefix;
|
||||
code += (std::string) ", " + nameprefix;
|
||||
code += GoIdentity(field.name);
|
||||
code += " " + GenTypeBasic(field.value.type);
|
||||
}
|
||||
|
@ -406,21 +397,18 @@ static void EndBuilderArgs(std::string *code_ptr) {
|
|||
// Recursively generate struct construction statements and instert manual
|
||||
// padding.
|
||||
static void StructBuilderBody(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *code_ptr) {
|
||||
const char *nameprefix, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "\tbuilder.Prep(" + NumToString(struct_def.minalign) + ", ";
|
||||
code += NumToString(struct_def.bytesize) + ")\n";
|
||||
for (auto it = struct_def.fields.vec.rbegin();
|
||||
it != struct_def.fields.vec.rend();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.rend(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.padding)
|
||||
code += "\tbuilder.Pad(" + NumToString(field.padding) + ")\n";
|
||||
if (IsStruct(field.value.type)) {
|
||||
StructBuilderBody(*field.value.type.struct_def,
|
||||
(nameprefix + (field.name + "_")).c_str(),
|
||||
code_ptr);
|
||||
(nameprefix + (field.name + "_")).c_str(), code_ptr);
|
||||
} else {
|
||||
code += "\tbuilder.Prepend" + GenMethod(field) + "(";
|
||||
code += nameprefix + GoIdentity(field.name) + ")\n";
|
||||
|
@ -447,8 +435,7 @@ static void GetStartOfTable(const StructDef &struct_def,
|
|||
|
||||
// Set the value of a table's field.
|
||||
static void BuildFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
const size_t offset,
|
||||
const FieldDef &field, const size_t offset,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + struct_def.name + "Add" + MakeCamel(field.name);
|
||||
|
@ -476,8 +463,7 @@ static void BuildFieldOfTable(const StructDef &struct_def,
|
|||
|
||||
// Set the value of one of the members of a table's vector.
|
||||
static void BuildVectorOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + struct_def.name + "Start";
|
||||
code += MakeCamel(field.name);
|
||||
|
@ -508,8 +494,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
|
|||
|
||||
// Generate a struct field getter, conditioned on its child type(s).
|
||||
static void GenStructAccessor(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field, std::string *code_ptr) {
|
||||
GenComment(field.doc_comment, code_ptr, nullptr, "");
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
|
@ -526,9 +511,7 @@ static void GenStructAccessor(const StructDef &struct_def,
|
|||
GetStructFieldOfTable(struct_def, field, code_ptr);
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
GetStringField(struct_def, field, code_ptr);
|
||||
break;
|
||||
case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
|
||||
case BASE_TYPE_VECTOR: {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
if (vectortype.base_type == BASE_TYPE_STRUCT) {
|
||||
|
@ -538,11 +521,8 @@ static void GenStructAccessor(const StructDef &struct_def,
|
|||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
GetUnionField(struct_def, field, code_ptr);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
|
||||
default: assert(0);
|
||||
}
|
||||
}
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
|
@ -555,8 +535,8 @@ static void GenStructAccessor(const StructDef &struct_def,
|
|||
|
||||
// Mutate the value of a struct's scalar.
|
||||
static void MutateScalarFieldOfStruct(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
std::string type = MakeCamel(GenTypeBasic(field.value.type));
|
||||
std::string setter = "rcv._tab.Mutate" + type;
|
||||
|
@ -569,8 +549,8 @@ static void MutateScalarFieldOfStruct(const StructDef &struct_def,
|
|||
|
||||
// Mutate the value of a table's scalar.
|
||||
static void MutateScalarFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
std::string type = MakeCamel(GenTypeBasic(field.value.type));
|
||||
std::string setter = "rcv._tab.Mutate" + type + "Slot";
|
||||
|
@ -582,9 +562,8 @@ static void MutateScalarFieldOfTable(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Generate a struct field setter, conditioned on its child type(s).
|
||||
static void GenStructMutator(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
static void GenStructMutator(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
GenComment(field.doc_comment, code_ptr, nullptr, "");
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
|
@ -601,8 +580,7 @@ static void GenTableBuilders(const StructDef &struct_def,
|
|||
GetStartOfTable(struct_def, code_ptr);
|
||||
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
|
@ -617,8 +595,7 @@ static void GenTableBuilders(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Generate struct or table methods.
|
||||
static void GenStruct(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
static void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
|
||||
if (struct_def.generated) return;
|
||||
|
||||
GenComment(struct_def.doc_comment, code_ptr, nullptr);
|
||||
|
@ -636,8 +613,7 @@ static void GenStruct(const StructDef &struct_def,
|
|||
|
||||
// Generate struct fields accessors
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
|
@ -661,8 +637,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
|
|||
|
||||
GenComment(enum_def.doc_comment, code_ptr, nullptr);
|
||||
BeginEnum(code_ptr);
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
GenComment(ev.doc_comment, code_ptr, nullptr, "\t");
|
||||
|
@ -671,8 +646,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
|
|||
EndEnum(code_ptr);
|
||||
|
||||
BeginEnumNames(enum_def, code_ptr);
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
EnumNameMember(enum_def, ev, code_ptr);
|
||||
|
@ -686,48 +660,43 @@ static std::string GenGetter(const Type &type) {
|
|||
case BASE_TYPE_STRING: return "rcv._tab.ByteVector";
|
||||
case BASE_TYPE_UNION: return "rcv._tab.Union";
|
||||
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
|
||||
default:
|
||||
return "rcv._tab.Get" + MakeCamel(GenTypeGet(type));
|
||||
default: return "rcv._tab.Get" + MakeCamel(GenTypeGet(type));
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the method name for use with add/put calls.
|
||||
static std::string GenMethod(const FieldDef &field) {
|
||||
return IsScalar(field.value.type.base_type)
|
||||
? MakeCamel(GenTypeBasic(field.value.type))
|
||||
: (IsStruct(field.value.type) ? "Struct" : "UOffsetT");
|
||||
? MakeCamel(GenTypeBasic(field.value.type))
|
||||
: (IsStruct(field.value.type) ? "Struct" : "UOffsetT");
|
||||
}
|
||||
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
#GTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// clang-format on
|
||||
};
|
||||
return ctypename[type.base_type];
|
||||
}
|
||||
|
||||
static std::string GenTypePointer(const Type &type) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING:
|
||||
return "[]byte";
|
||||
case BASE_TYPE_VECTOR:
|
||||
return GenTypeGet(type.VectorType());
|
||||
case BASE_TYPE_STRUCT:
|
||||
return type.struct_def->name;
|
||||
case BASE_TYPE_STRING: return "[]byte";
|
||||
case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
|
||||
case BASE_TYPE_STRUCT: return type.struct_def->name;
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
default:
|
||||
return "*flatbuffers.Table";
|
||||
default: return "*flatbuffers.Table";
|
||||
}
|
||||
}
|
||||
|
||||
static std::string GenTypeGet(const Type &type) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
: GenTypePointer(type);
|
||||
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
|
||||
}
|
||||
|
||||
static std::string TypeName(const FieldDef &field) {
|
||||
|
@ -749,7 +718,8 @@ class GoGenerator : public BaseGenerator {
|
|||
public:
|
||||
GoGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name, const std::string &go_namespace)
|
||||
: BaseGenerator(parser, path, file_name, "" /* not used*/, "" /* not used */) {
|
||||
: BaseGenerator(parser, path, file_name, "" /* not used*/,
|
||||
"" /* not used */) {
|
||||
std::istringstream iss(go_namespace);
|
||||
std::string component;
|
||||
while (std::getline(iss, component, '.')) {
|
||||
|
@ -802,8 +772,8 @@ class GoGenerator : public BaseGenerator {
|
|||
if (needs_imports) {
|
||||
code += "import (\n";
|
||||
if (!parser_.opts.go_import.empty()) {
|
||||
code += "\tflatbuffers \"" + parser_.opts.go_import +"\"\n";
|
||||
} else{
|
||||
code += "\tflatbuffers \"" + parser_.opts.go_import + "\"\n";
|
||||
} else {
|
||||
code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n";
|
||||
}
|
||||
code += ")\n\n";
|
||||
|
@ -815,12 +785,12 @@ class GoGenerator : public BaseGenerator {
|
|||
bool needs_imports) {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
Namespace& ns = go_namespace_.components.empty() ? *def.defined_namespace : go_namespace_;
|
||||
Namespace &ns = go_namespace_.components.empty() ? *def.defined_namespace
|
||||
: go_namespace_;
|
||||
std::string code = "";
|
||||
BeginFile(LastNamespacePart(ns), needs_imports, &code);
|
||||
code += classcode;
|
||||
std::string filename =
|
||||
NamespaceDir(ns) + def.name + ".go";
|
||||
std::string filename = NamespaceDir(ns) + def.name + ".go";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,18 +16,19 @@
|
|||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/code_generators.h"
|
||||
|
||||
#include "src/compiler/cpp_generator.h"
|
||||
#include "src/compiler/go_generator.h"
|
||||
#include "src/compiler/java_generator.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4512) // C4512: 'class' : assignment operator could not be generated
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4512) // C4512: 'class' : assignment operator could
|
||||
// not be generated
|
||||
#endif
|
||||
|
||||
namespace flatbuffers {
|
||||
|
@ -36,8 +37,7 @@ class FlatBufMethod : public grpc_generator::Method {
|
|||
public:
|
||||
enum Streaming { kNone, kClient, kServer, kBiDi };
|
||||
|
||||
FlatBufMethod(const RPCCall *method)
|
||||
: method_(method) {
|
||||
FlatBufMethod(const RPCCall *method) : method_(method) {
|
||||
streaming_ = kNone;
|
||||
auto val = method_->attributes.Lookup("streaming");
|
||||
if (val) {
|
||||
|
@ -47,12 +47,8 @@ class FlatBufMethod : public grpc_generator::Method {
|
|||
}
|
||||
}
|
||||
|
||||
grpc::string GetLeadingComments(const grpc::string) const {
|
||||
return "";
|
||||
}
|
||||
grpc::string GetTrailingComments(const grpc::string) const {
|
||||
return "";
|
||||
}
|
||||
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
|
||||
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
|
||||
std::vector<grpc::string> GetAllComments() const {
|
||||
return method_->rpc_comment;
|
||||
}
|
||||
|
@ -63,16 +59,13 @@ class FlatBufMethod : public grpc_generator::Method {
|
|||
return "flatbuffers::grpc::Message<" + sd.name + ">";
|
||||
}
|
||||
|
||||
std::string get_input_type_name() const {
|
||||
return (*method_->request).name;
|
||||
}
|
||||
std::string get_output_type_name() const {
|
||||
return (*method_->response).name;
|
||||
}
|
||||
std::string get_input_type_name() const { return (*method_->request).name; }
|
||||
std::string get_output_type_name() const { return (*method_->response).name; }
|
||||
|
||||
bool get_module_and_message_path_input(
|
||||
grpc::string * /*str*/, grpc::string /*generator_file_name*/,
|
||||
bool /*generate_in_pb2_grpc*/, grpc::string /*import_prefix*/) const {
|
||||
bool get_module_and_message_path_input(grpc::string * /*str*/,
|
||||
grpc::string /*generator_file_name*/,
|
||||
bool /*generate_in_pb2_grpc*/,
|
||||
grpc::string /*import_prefix*/) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -82,13 +75,9 @@ class FlatBufMethod : public grpc_generator::Method {
|
|||
return true;
|
||||
}
|
||||
|
||||
std::string input_type_name() const {
|
||||
return GRPCType(*method_->request);
|
||||
}
|
||||
std::string input_type_name() const { return GRPCType(*method_->request); }
|
||||
|
||||
std::string output_type_name() const {
|
||||
return GRPCType(*method_->response);
|
||||
}
|
||||
std::string output_type_name() const { return GRPCType(*method_->response); }
|
||||
|
||||
bool NoStreaming() const { return streaming_ == kNone; }
|
||||
bool ClientStreaming() const { return streaming_ == kClient; }
|
||||
|
@ -104,12 +93,8 @@ class FlatBufService : public grpc_generator::Service {
|
|||
public:
|
||||
FlatBufService(const ServiceDef *service) : service_(service) {}
|
||||
|
||||
grpc::string GetLeadingComments(const grpc::string) const {
|
||||
return "";
|
||||
}
|
||||
grpc::string GetTrailingComments(const grpc::string) const {
|
||||
return "";
|
||||
}
|
||||
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
|
||||
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
|
||||
std::vector<grpc::string> GetAllComments() const {
|
||||
return service_->doc_comment;
|
||||
}
|
||||
|
@ -122,7 +107,7 @@ class FlatBufService : public grpc_generator::Service {
|
|||
|
||||
std::unique_ptr<const grpc_generator::Method> method(int i) const {
|
||||
return std::unique_ptr<const grpc_generator::Method>(
|
||||
new FlatBufMethod(service_->calls.vec[i]));
|
||||
new FlatBufMethod(service_->calls.vec[i]));
|
||||
};
|
||||
|
||||
private:
|
||||
|
@ -131,8 +116,7 @@ class FlatBufService : public grpc_generator::Service {
|
|||
|
||||
class FlatBufPrinter : public grpc_generator::Printer {
|
||||
public:
|
||||
FlatBufPrinter(std::string *str)
|
||||
: str_(str), escape_char_('$'), indent_(0) {}
|
||||
FlatBufPrinter(std::string *str) : str_(str), escape_char_('$'), indent_(0) {}
|
||||
|
||||
void Print(const std::map<std::string, std::string> &vars,
|
||||
const char *string_template) {
|
||||
|
@ -155,15 +139,13 @@ class FlatBufPrinter : public grpc_generator::Printer {
|
|||
}
|
||||
|
||||
void Print(const char *s) {
|
||||
if (s == nullptr || std::strlen(s) == 0) {
|
||||
return;
|
||||
}
|
||||
if (s == nullptr || std::strlen(s) == 0) { return; }
|
||||
// Add this string, but for each part separated by \n, add indentation.
|
||||
for (;;) {
|
||||
// Current indentation.
|
||||
str_->insert(str_->end(), indent_ * 2, ' ');
|
||||
// See if this contains more than one line.
|
||||
const char * lf = strchr(s, '\n');
|
||||
const char *lf = strchr(s, '\n');
|
||||
if (lf) {
|
||||
(*str_) += std::string(s, lf + 1);
|
||||
s = lf + 1;
|
||||
|
@ -176,7 +158,10 @@ class FlatBufPrinter : public grpc_generator::Printer {
|
|||
}
|
||||
|
||||
void Indent() { indent_++; }
|
||||
void Outdent() { indent_--; assert(indent_ >= 0); }
|
||||
void Outdent() {
|
||||
indent_--;
|
||||
assert(indent_ >= 0);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string *str_;
|
||||
|
@ -186,23 +171,15 @@ class FlatBufPrinter : public grpc_generator::Printer {
|
|||
|
||||
class FlatBufFile : public grpc_generator::File {
|
||||
public:
|
||||
enum Language {
|
||||
kLanguageGo,
|
||||
kLanguageCpp,
|
||||
kLanguageJava
|
||||
};
|
||||
enum Language { kLanguageGo, kLanguageCpp, kLanguageJava };
|
||||
|
||||
FlatBufFile(
|
||||
const Parser &parser, const std::string &file_name, Language language)
|
||||
: parser_(parser), file_name_(file_name), language_(language) {}
|
||||
FlatBufFile(const Parser &parser, const std::string &file_name,
|
||||
Language language)
|
||||
: parser_(parser), file_name_(file_name), language_(language) {}
|
||||
FlatBufFile &operator=(const FlatBufFile &);
|
||||
|
||||
grpc::string GetLeadingComments(const grpc::string) const {
|
||||
return "";
|
||||
}
|
||||
grpc::string GetTrailingComments(const grpc::string) const {
|
||||
return "";
|
||||
}
|
||||
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
|
||||
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
|
||||
std::vector<grpc::string> GetAllComments() const {
|
||||
return std::vector<grpc::string>();
|
||||
}
|
||||
|
@ -243,13 +220,13 @@ class FlatBufFile : public grpc_generator::File {
|
|||
};
|
||||
|
||||
std::unique_ptr<const grpc_generator::Service> service(int i) const {
|
||||
return std::unique_ptr<const grpc_generator::Service> (
|
||||
new FlatBufService(parser_.services_.vec[i]));
|
||||
return std::unique_ptr<const grpc_generator::Service>(
|
||||
new FlatBufService(parser_.services_.vec[i]));
|
||||
}
|
||||
|
||||
std::unique_ptr<grpc_generator::Printer> CreatePrinter(std::string *str) const {
|
||||
return std::unique_ptr<grpc_generator::Printer>(
|
||||
new FlatBufPrinter(str));
|
||||
std::unique_ptr<grpc_generator::Printer> CreatePrinter(
|
||||
std::string *str) const {
|
||||
return std::unique_ptr<grpc_generator::Printer>(new FlatBufPrinter(str));
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -262,8 +239,10 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
|
|||
public:
|
||||
GoGRPCGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name)
|
||||
: BaseGenerator(parser, path, file_name, "", "" /*Unused*/),
|
||||
parser_(parser), path_(path), file_name_(file_name) {}
|
||||
: BaseGenerator(parser, path, file_name, "", "" /*Unused*/),
|
||||
parser_(parser),
|
||||
path_(path),
|
||||
file_name_(file_name) {}
|
||||
|
||||
bool generate() {
|
||||
FlatBufFile file(parser_, file_name_, FlatBufFile::kLanguageGo);
|
||||
|
@ -273,10 +252,11 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
|
|||
auto service = file.service(i);
|
||||
const Definition *def = parser_.services_.vec[i];
|
||||
p.package_name = LastNamespacePart(*(def->defined_namespace));
|
||||
std::string output = grpc_go_generator::GenerateServiceSource(&file, service.get(), &p);
|
||||
std::string filename = NamespaceDir(*def->defined_namespace) + def->name + "_grpc.go";
|
||||
if (!flatbuffers::SaveFile(filename.c_str(), output, false))
|
||||
return false;
|
||||
std::string output =
|
||||
grpc_go_generator::GenerateServiceSource(&file, service.get(), &p);
|
||||
std::string filename =
|
||||
NamespaceDir(*def->defined_namespace) + def->name + "_grpc.go";
|
||||
if (!flatbuffers::SaveFile(filename.c_str(), output, false)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -286,25 +266,22 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
|
|||
const std::string &path_, &file_name_;
|
||||
};
|
||||
|
||||
bool GenerateGoGRPC(const Parser &parser,
|
||||
const std::string &path,
|
||||
bool GenerateGoGRPC(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
int nservices = 0;
|
||||
for (auto it = parser.services_.vec.begin();
|
||||
it != parser.services_.vec.end(); ++it) {
|
||||
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
|
||||
++it) {
|
||||
if (!(*it)->generated) nservices++;
|
||||
}
|
||||
if (!nservices) return true;
|
||||
return GoGRPCGenerator(parser, path, file_name).generate();
|
||||
}
|
||||
|
||||
bool GenerateCppGRPC(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
|
||||
bool GenerateCppGRPC(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
int nservices = 0;
|
||||
for (auto it = parser.services_.vec.begin();
|
||||
it != parser.services_.vec.end(); ++it) {
|
||||
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
|
||||
++it) {
|
||||
if (!(*it)->generated) nservices++;
|
||||
}
|
||||
if (!nservices) return true;
|
||||
|
@ -335,8 +312,8 @@ bool GenerateCppGRPC(const Parser &parser,
|
|||
|
||||
class JavaGRPCGenerator : public flatbuffers::BaseGenerator {
|
||||
public:
|
||||
JavaGRPCGenerator(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name)
|
||||
JavaGRPCGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name)
|
||||
: BaseGenerator(parser, path, file_name, "", "." /*separator*/),
|
||||
parser_(parser),
|
||||
path_(path),
|
||||
|
@ -347,7 +324,7 @@ class JavaGRPCGenerator : public flatbuffers::BaseGenerator {
|
|||
grpc_java_generator::Parameters p;
|
||||
for (int i = 0; i < file.service_count(); i++) {
|
||||
auto service = file.service(i);
|
||||
const Definition* def = parser_.services_.vec[i];
|
||||
const Definition *def = parser_.services_.vec[i];
|
||||
p.package_name =
|
||||
def->defined_namespace->GetFullyQualifiedName(""); // file.package();
|
||||
std::string output =
|
||||
|
@ -360,12 +337,12 @@ class JavaGRPCGenerator : public flatbuffers::BaseGenerator {
|
|||
}
|
||||
|
||||
protected:
|
||||
const Parser& parser_;
|
||||
const Parser &parser_;
|
||||
const std::string &path_, &file_name_;
|
||||
};
|
||||
|
||||
bool GenerateJavaGRPC(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name) {
|
||||
bool GenerateJavaGRPC(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
int nservices = 0;
|
||||
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
|
||||
++it) {
|
||||
|
@ -378,5 +355,5 @@ bool GenerateJavaGRPC(const Parser& parser, const std::string& path,
|
|||
} // namespace flatbuffers
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
|
1848
src/idl_gen_js.cpp
1848
src/idl_gen_js.cpp
File diff suppressed because it is too large
Load Diff
|
@ -1,23 +1,23 @@
|
|||
/*
|
||||
* Copyright 2014 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
* Copyright 2014 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
|
@ -30,8 +30,7 @@ namespace jsons {
|
|||
|
||||
std::string GenNativeType(BaseType type) {
|
||||
switch (type) {
|
||||
case BASE_TYPE_BOOL:
|
||||
return "boolean";
|
||||
case BASE_TYPE_BOOL: return "boolean";
|
||||
case BASE_TYPE_CHAR:
|
||||
case BASE_TYPE_UCHAR:
|
||||
case BASE_TYPE_SHORT:
|
||||
|
@ -41,16 +40,13 @@ std::string GenNativeType(BaseType type) {
|
|||
case BASE_TYPE_LONG:
|
||||
case BASE_TYPE_ULONG:
|
||||
case BASE_TYPE_FLOAT:
|
||||
case BASE_TYPE_DOUBLE:
|
||||
return "number";
|
||||
case BASE_TYPE_STRING:
|
||||
return "string";
|
||||
default:
|
||||
return "";
|
||||
case BASE_TYPE_DOUBLE: return "number";
|
||||
case BASE_TYPE_STRING: return "string";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
template <class T> std::string GenFullName(const T *enum_def) {
|
||||
template<class T> std::string GenFullName(const T *enum_def) {
|
||||
std::string full_name;
|
||||
const auto &name_spaces = enum_def->defined_namespace->components;
|
||||
for (auto ns = name_spaces.cbegin(); ns != name_spaces.cend(); ++ns) {
|
||||
|
@ -60,7 +56,7 @@ template <class T> std::string GenFullName(const T *enum_def) {
|
|||
return full_name;
|
||||
}
|
||||
|
||||
template <class T> std::string GenTypeRef(const T *enum_def) {
|
||||
template<class T> std::string GenTypeRef(const T *enum_def) {
|
||||
return "\"$ref\" : \"#/definitions/" + GenFullName(enum_def) + "\"";
|
||||
}
|
||||
|
||||
|
@ -93,11 +89,10 @@ std::string GenType(const Type &type) {
|
|||
const auto &union_types = type.enum_def->vals.vec;
|
||||
for (auto ut = union_types.cbegin(); ut < union_types.cend(); ++ut) {
|
||||
auto &union_type = *ut;
|
||||
if (union_type->union_type.base_type == BASE_TYPE_NONE) {
|
||||
continue;
|
||||
}
|
||||
if (union_type->union_type.base_type == BASE_TYPE_NONE) { continue; }
|
||||
if (union_type->union_type.base_type == BASE_TYPE_STRUCT) {
|
||||
union_type_string.append("{ " + GenTypeRef(union_type->union_type.struct_def) + " }");
|
||||
union_type_string.append(
|
||||
"{ " + GenTypeRef(union_type->union_type.struct_def) + " }");
|
||||
}
|
||||
if (union_type != *type.enum_def->vals.vec.rbegin()) {
|
||||
union_type_string.append(",");
|
||||
|
@ -106,10 +101,8 @@ std::string GenType(const Type &type) {
|
|||
union_type_string.append("]");
|
||||
return union_type_string;
|
||||
}
|
||||
case BASE_TYPE_UTYPE:
|
||||
return GenTypeRef(type.enum_def);
|
||||
default:
|
||||
return GenType(GenNativeType(type.base_type));
|
||||
case BASE_TYPE_UTYPE: return GenTypeRef(type.enum_def);
|
||||
default: return GenType(GenNativeType(type.base_type));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,35 +123,29 @@ class JsonSchemaGenerator : public BaseGenerator {
|
|||
code_ += "{";
|
||||
code_ += " \"$schema\": \"http://json-schema.org/draft-04/schema#\",";
|
||||
code_ += " \"definitions\": {";
|
||||
for (auto e = parser_.enums_.vec.cbegin();
|
||||
e != parser_.enums_.vec.cend();
|
||||
for (auto e = parser_.enums_.vec.cbegin(); e != parser_.enums_.vec.cend();
|
||||
++e) {
|
||||
code_ += " \"" + GenFullName(*e) + "\" : {";
|
||||
code_ += " " + GenType("string") + ",";
|
||||
std::string enumdef(" \"enum\": [");
|
||||
for (auto enum_value = (*e)->vals.vec.begin();
|
||||
enum_value != (*e)->vals.vec.end();
|
||||
++enum_value) {
|
||||
for (auto enum_value = (*e)->vals.vec.begin();
|
||||
enum_value != (*e)->vals.vec.end(); ++enum_value) {
|
||||
enumdef.append("\"" + (*enum_value)->name + "\"");
|
||||
if (*enum_value != (*e)->vals.vec.back()) {
|
||||
enumdef.append(", ");
|
||||
}
|
||||
if (*enum_value != (*e)->vals.vec.back()) { enumdef.append(", "); }
|
||||
}
|
||||
enumdef.append("]");
|
||||
code_ += enumdef;
|
||||
code_ += " },"; // close type
|
||||
}
|
||||
for (auto s = parser_.structs_.vec.cbegin();
|
||||
s != parser_.structs_.vec.cend();
|
||||
++s) {
|
||||
for (auto s = parser_.structs_.vec.cbegin();
|
||||
s != parser_.structs_.vec.cend(); ++s) {
|
||||
const auto &structure = *s;
|
||||
code_ += " \"" + GenFullName(structure) + "\" : {";
|
||||
code_ += " " + GenType("object") + ",";
|
||||
std::string comment;
|
||||
const auto &comment_lines = structure->doc_comment;
|
||||
for (auto comment_line = comment_lines.cbegin();
|
||||
comment_line != comment_lines.cend();
|
||||
++comment_line) {
|
||||
comment_line != comment_lines.cend(); ++comment_line) {
|
||||
comment.append(*comment_line);
|
||||
}
|
||||
if (comment.size() > 0) {
|
||||
|
@ -169,10 +156,9 @@ class JsonSchemaGenerator : public BaseGenerator {
|
|||
const auto &properties = structure->fields.vec;
|
||||
for (auto prop = properties.cbegin(); prop != properties.cend(); ++prop) {
|
||||
const auto &property = *prop;
|
||||
std::string typeLine(" \"" + property->name + "\" : { " + GenType(property->value.type) + " }");
|
||||
if (property != properties.back()) {
|
||||
typeLine.append(",");
|
||||
}
|
||||
std::string typeLine(" \"" + property->name + "\" : { " +
|
||||
GenType(property->value.type) + " }");
|
||||
if (property != properties.back()) { typeLine.append(","); }
|
||||
code_ += typeLine;
|
||||
}
|
||||
code_ += " },"; // close properties
|
||||
|
@ -184,8 +170,7 @@ class JsonSchemaGenerator : public BaseGenerator {
|
|||
if (requiredProperties.size() > 0) {
|
||||
std::string required_string(" \"required\" : [");
|
||||
for (auto req_prop = requiredProperties.cbegin();
|
||||
req_prop != requiredProperties.cend();
|
||||
++req_prop) {
|
||||
req_prop != requiredProperties.cend(); ++req_prop) {
|
||||
required_string.append("\"" + (*req_prop)->name + "\"");
|
||||
if (*req_prop != requiredProperties.back()) {
|
||||
required_string.append(", ");
|
||||
|
@ -196,9 +181,7 @@ class JsonSchemaGenerator : public BaseGenerator {
|
|||
}
|
||||
code_ += " \"additionalProperties\" : false";
|
||||
std::string closeType(" }");
|
||||
if (*s != parser_.structs_.vec.back()) {
|
||||
closeType.append(",");
|
||||
}
|
||||
if (*s != parser_.structs_.vec.back()) { closeType.append(","); }
|
||||
code_ += closeType; // close type
|
||||
}
|
||||
code_ += " },"; // close definitions
|
||||
|
|
1659
src/idl_gen_php.cpp
1659
src/idl_gen_php.cpp
File diff suppressed because it is too large
Load Diff
|
@ -18,10 +18,10 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/code_generators.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace python {
|
||||
|
@ -35,7 +35,6 @@ static std::string GenTypeBasic(const Type &type);
|
|||
static std::string GenTypeGet(const Type &type);
|
||||
static std::string TypeName(const FieldDef &field);
|
||||
|
||||
|
||||
// Hardcode spaces per indentation.
|
||||
const std::string Indent = " ";
|
||||
|
||||
|
@ -44,9 +43,8 @@ const std::string Indent = " ";
|
|||
std::string OffsetPrefix(const FieldDef &field) {
|
||||
return "\n" + Indent + Indent +
|
||||
"o = flatbuffers.number_types.UOffsetTFlags.py_type" +
|
||||
"(self._tab.Offset(" +
|
||||
NumToString(field.value.offset) +
|
||||
"))\n" + Indent + Indent + "if o != 0:\n";
|
||||
"(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
|
||||
Indent + Indent + "if o != 0:\n";
|
||||
}
|
||||
|
||||
// Begin a class declaration.
|
||||
|
@ -109,8 +107,7 @@ static void InitializeExisting(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Get the length of a vector.
|
||||
static void GetVectorLen(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
|
@ -188,8 +185,7 @@ static void GetStructFieldOfTable(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Get the value of a string.
|
||||
static void GetStringField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetStringField(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
|
@ -202,8 +198,7 @@ static void GetStringField(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Get the value of a union from an object.
|
||||
static void GetUnionField(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
static void GetUnionField(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
|
@ -284,9 +279,7 @@ static void GetVectorOfNonStructAsNumpy(const StructDef &struct_def,
|
|||
|
||||
// Currently, we only support accessing as numpy array if
|
||||
// the vector type is a scalar.
|
||||
if (!(IsScalar(vectortype.base_type))) {
|
||||
return;
|
||||
}
|
||||
if (!(IsScalar(vectortype.base_type))) { return; }
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += MakeCamel(field.name) + "AsNumpy(self):";
|
||||
|
@ -319,22 +312,19 @@ static void BeginBuilderArgs(const StructDef &struct_def,
|
|||
// Recursively generate arguments for a constructor, to deal with nested
|
||||
// structs.
|
||||
static void StructBuilderArgs(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *code_ptr) {
|
||||
const char *nameprefix, std::string *code_ptr) {
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (IsStruct(field.value.type)) {
|
||||
// Generate arguments for a struct inside a struct. To ensure names
|
||||
// don't clash, and to make it obvious these arguments are constructing
|
||||
// a nested struct, prefix the name with the field name.
|
||||
StructBuilderArgs(*field.value.type.struct_def,
|
||||
(nameprefix + (field.name + "_")).c_str(),
|
||||
code_ptr);
|
||||
(nameprefix + (field.name + "_")).c_str(), code_ptr);
|
||||
} else {
|
||||
std::string &code = *code_ptr;
|
||||
code += (std::string)", " + nameprefix;
|
||||
code += (std::string) ", " + nameprefix;
|
||||
code += MakeCamel(field.name, false);
|
||||
}
|
||||
}
|
||||
|
@ -349,21 +339,18 @@ static void EndBuilderArgs(std::string *code_ptr) {
|
|||
// Recursively generate struct construction statements and instert manual
|
||||
// padding.
|
||||
static void StructBuilderBody(const StructDef &struct_def,
|
||||
const char *nameprefix,
|
||||
std::string *code_ptr) {
|
||||
const char *nameprefix, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += " builder.Prep(" + NumToString(struct_def.minalign) + ", ";
|
||||
code += NumToString(struct_def.bytesize) + ")\n";
|
||||
for (auto it = struct_def.fields.vec.rbegin();
|
||||
it != struct_def.fields.vec.rend();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.rend(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.padding)
|
||||
code += " builder.Pad(" + NumToString(field.padding) + ")\n";
|
||||
if (IsStruct(field.value.type)) {
|
||||
StructBuilderBody(*field.value.type.struct_def,
|
||||
(nameprefix + (field.name + "_")).c_str(),
|
||||
code_ptr);
|
||||
(nameprefix + (field.name + "_")).c_str(), code_ptr);
|
||||
} else {
|
||||
code += " builder.Prepend" + GenMethod(field) + "(";
|
||||
code += nameprefix + MakeCamel(field.name, false) + ")\n";
|
||||
|
@ -389,8 +376,7 @@ static void GetStartOfTable(const StructDef &struct_def,
|
|||
|
||||
// Set the value of a table's field.
|
||||
static void BuildFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
const size_t offset,
|
||||
const FieldDef &field, const size_t offset,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "def " + struct_def.name + "Add" + MakeCamel(field.name);
|
||||
|
@ -413,8 +399,7 @@ static void BuildFieldOfTable(const StructDef &struct_def,
|
|||
|
||||
// Set the value of one of the members of a table's vector.
|
||||
static void BuildVectorOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "def " + struct_def.name + "Start";
|
||||
code += MakeCamel(field.name);
|
||||
|
@ -445,8 +430,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
|
|||
|
||||
// Generate a struct field, conditioned on its child type(s).
|
||||
static void GenStructAccessor(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
const FieldDef &field, std::string *code_ptr) {
|
||||
GenComment(field.doc_comment, code_ptr, nullptr, "# ");
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
|
@ -463,9 +447,7 @@ static void GenStructAccessor(const StructDef &struct_def,
|
|||
GetStructFieldOfTable(struct_def, field, code_ptr);
|
||||
}
|
||||
break;
|
||||
case BASE_TYPE_STRING:
|
||||
GetStringField(struct_def, field, code_ptr);
|
||||
break;
|
||||
case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
|
||||
case BASE_TYPE_VECTOR: {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
if (vectortype.base_type == BASE_TYPE_STRUCT) {
|
||||
|
@ -476,11 +458,8 @@ static void GenStructAccessor(const StructDef &struct_def,
|
|||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
GetUnionField(struct_def, field, code_ptr);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
|
||||
default: assert(0);
|
||||
}
|
||||
}
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
|
@ -494,8 +473,7 @@ static void GenTableBuilders(const StructDef &struct_def,
|
|||
GetStartOfTable(struct_def, code_ptr);
|
||||
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
|
@ -510,8 +488,7 @@ static void GenTableBuilders(const StructDef &struct_def,
|
|||
}
|
||||
|
||||
// Generate struct or table methods.
|
||||
static void GenStruct(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
static void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
|
||||
if (struct_def.generated) return;
|
||||
|
||||
GenComment(struct_def.doc_comment, code_ptr, nullptr, "# ");
|
||||
|
@ -525,8 +502,7 @@ static void GenStruct(const StructDef &struct_def,
|
|||
// accessor object. This is to allow object reuse.
|
||||
InitializeExisting(struct_def, code_ptr);
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
|
@ -548,8 +524,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
|
|||
|
||||
GenComment(enum_def.doc_comment, code_ptr, nullptr, "# ");
|
||||
BeginEnum(enum_def.name, code_ptr);
|
||||
for (auto it = enum_def.vals.vec.begin();
|
||||
it != enum_def.vals.vec.end();
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
auto &ev = **it;
|
||||
GenComment(ev.doc_comment, code_ptr, nullptr, "# ");
|
||||
|
@ -565,49 +540,44 @@ static std::string GenGetter(const Type &type) {
|
|||
case BASE_TYPE_UNION: return "self._tab.Union(";
|
||||
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
|
||||
default:
|
||||
return "self._tab.Get(flatbuffers.number_types." + \
|
||||
MakeCamel(GenTypeGet(type)) + \
|
||||
"Flags, ";
|
||||
return "self._tab.Get(flatbuffers.number_types." +
|
||||
MakeCamel(GenTypeGet(type)) + "Flags, ";
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the method name for use with add/put calls.
|
||||
static std::string GenMethod(const FieldDef &field) {
|
||||
return IsScalar(field.value.type.base_type)
|
||||
? MakeCamel(GenTypeBasic(field.value.type))
|
||||
: (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
|
||||
? MakeCamel(GenTypeBasic(field.value.type))
|
||||
: (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
|
||||
}
|
||||
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
#PTYPE,
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// clang-format on
|
||||
};
|
||||
return ctypename[type.base_type];
|
||||
}
|
||||
|
||||
static std::string GenTypePointer(const Type &type) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING:
|
||||
return "string";
|
||||
case BASE_TYPE_VECTOR:
|
||||
return GenTypeGet(type.VectorType());
|
||||
case BASE_TYPE_STRUCT:
|
||||
return type.struct_def->name;
|
||||
case BASE_TYPE_STRING: return "string";
|
||||
case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
|
||||
case BASE_TYPE_STRUCT: return type.struct_def->name;
|
||||
case BASE_TYPE_UNION:
|
||||
// fall through
|
||||
default:
|
||||
return "*flatbuffers.Table";
|
||||
default: return "*flatbuffers.Table";
|
||||
}
|
||||
}
|
||||
|
||||
static std::string GenTypeGet(const Type &type) {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
: GenTypePointer(type);
|
||||
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
|
||||
}
|
||||
|
||||
static std::string TypeName(const FieldDef &field) {
|
||||
|
@ -666,9 +636,7 @@ class PythonGenerator : public BaseGenerator {
|
|||
std::string &code = *code_ptr;
|
||||
code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n";
|
||||
code += "# namespace: " + name_space_name + "\n\n";
|
||||
if (needs_imports) {
|
||||
code += "import flatbuffers\n\n";
|
||||
}
|
||||
if (needs_imports) { code += "import flatbuffers\n\n"; }
|
||||
}
|
||||
|
||||
// Save out the generated code for a Python Table type.
|
||||
|
@ -688,8 +656,8 @@ class PythonGenerator : public BaseGenerator {
|
|||
std::string code = "";
|
||||
BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code);
|
||||
code += classcode;
|
||||
std::string filename = NamespaceDir(*def.defined_namespace) +
|
||||
def.name + ".py";
|
||||
std::string filename =
|
||||
NamespaceDir(*def.defined_namespace) + def.name + ".py";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -17,15 +17,14 @@
|
|||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text);
|
||||
int indent, const IDLOptions &opts, std::string *_text);
|
||||
|
||||
// If indentation is less than 0, that indicates we don't want any newlines
|
||||
// either.
|
||||
|
@ -33,9 +32,7 @@ const char *NewLine(const IDLOptions &opts) {
|
|||
return opts.indent_step >= 0 ? "\n" : "";
|
||||
}
|
||||
|
||||
int Indent(const IDLOptions &opts) {
|
||||
return std::max(opts.indent_step, 0);
|
||||
}
|
||||
int Indent(const IDLOptions &opts) { return std::max(opts.indent_step, 0); }
|
||||
|
||||
// Output an identifier with or without quotes depending on strictness.
|
||||
void OutputIdentifier(const std::string &name, const IDLOptions &opts,
|
||||
|
@ -49,10 +46,9 @@ void OutputIdentifier(const std::string &name, const IDLOptions &opts,
|
|||
// Print (and its template specialization below for pointers) generate text
|
||||
// for a single FlatBuffer value into JSON format.
|
||||
// The general case for scalars:
|
||||
template<typename T> bool Print(T val, Type type, int /*indent*/,
|
||||
Type * /*union_type*/,
|
||||
const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
template<typename T>
|
||||
bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
|
||||
const IDLOptions &opts, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
if (type.enum_def && opts.output_enum_identifiers) {
|
||||
auto enum_val = type.enum_def->ReverseLookup(static_cast<int>(val));
|
||||
|
@ -72,9 +68,9 @@ template<typename T> bool Print(T val, Type type, int /*indent*/,
|
|||
}
|
||||
|
||||
// Print a vector a sequence of JSON values, comma separated, wrapped in "[]".
|
||||
template<typename T> bool PrintVector(const Vector<T> &v, Type type,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
template<typename T>
|
||||
bool PrintVector(const Vector<T> &v, Type type, int indent,
|
||||
const IDLOptions &opts, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "[";
|
||||
text += NewLine(opts);
|
||||
|
@ -90,8 +86,7 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
|
|||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!Print(v[i], type, indent + Indent(opts), nullptr,
|
||||
opts, _text)) {
|
||||
if (!Print(v[i], type, indent + Indent(opts), nullptr, opts, _text)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -103,11 +98,10 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
|
|||
}
|
||||
|
||||
// Specialization of Print above for pointer types.
|
||||
template<> bool Print<const void *>(const void *val,
|
||||
Type type, int indent,
|
||||
Type *union_type,
|
||||
const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
template<>
|
||||
bool Print<const void *>(const void *val, Type type, int indent,
|
||||
Type *union_type, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_UNION:
|
||||
// If this assert hits, you have an corrupt buffer, a union type field
|
||||
|
@ -116,11 +110,8 @@ template<> bool Print<const void *>(const void *val,
|
|||
return Print<const void *>(val, *union_type, indent, nullptr, opts,
|
||||
_text);
|
||||
case BASE_TYPE_STRUCT:
|
||||
if (!GenStruct(*type.struct_def,
|
||||
reinterpret_cast<const Table *>(val),
|
||||
indent,
|
||||
opts,
|
||||
_text)) {
|
||||
if (!GenStruct(*type.struct_def, reinterpret_cast<const Table *>(val),
|
||||
indent, opts, _text)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
@ -135,6 +126,7 @@ template<> bool Print<const void *>(const void *val,
|
|||
type = type.VectorType();
|
||||
// Call PrintVector above specifically for each element type:
|
||||
switch (type.base_type) {
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
|
@ -146,6 +138,7 @@ template<> bool Print<const void *>(const void *val,
|
|||
break;
|
||||
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// clang-format on
|
||||
}
|
||||
break;
|
||||
default: assert(0);
|
||||
|
@ -154,31 +147,28 @@ template<> bool Print<const void *>(const void *val,
|
|||
}
|
||||
|
||||
// Generate text for a scalar field.
|
||||
template<typename T> static bool GenField(const FieldDef &fd,
|
||||
const Table *table, bool fixed,
|
||||
const IDLOptions &opts,
|
||||
int indent,
|
||||
std::string *_text) {
|
||||
return Print(fixed ?
|
||||
reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset) :
|
||||
table->GetField<T>(fd.value.offset, 0), fd.value.type, indent, nullptr,
|
||||
opts, _text);
|
||||
template<typename T>
|
||||
static bool GenField(const FieldDef &fd, const Table *table, bool fixed,
|
||||
const IDLOptions &opts, int indent, std::string *_text) {
|
||||
return Print(fixed ? reinterpret_cast<const Struct *>(table)->GetField<T>(
|
||||
fd.value.offset)
|
||||
: table->GetField<T>(fd.value.offset, 0),
|
||||
fd.value.type, indent, nullptr, opts, _text);
|
||||
}
|
||||
|
||||
static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text);
|
||||
int indent, const IDLOptions &opts, std::string *_text);
|
||||
|
||||
// Generate text for non-scalar field.
|
||||
static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
||||
int indent, Type *union_type,
|
||||
const IDLOptions &opts, std::string *_text) {
|
||||
int indent, Type *union_type, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
const void *val = nullptr;
|
||||
if (fixed) {
|
||||
// The only non-scalar fields in structs are structs.
|
||||
assert(IsStruct(fd.value.type));
|
||||
val = reinterpret_cast<const Struct *>(table)->
|
||||
GetStruct<const void *>(fd.value.offset);
|
||||
val = reinterpret_cast<const Struct *>(table)->GetStruct<const void *>(
|
||||
fd.value.offset);
|
||||
} else if (fd.flexbuffer) {
|
||||
auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
|
||||
auto root = flexbuffers::GetRoot(vec->data(), vec->size());
|
||||
|
@ -190,8 +180,8 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
|||
return GenStruct(*fd.nested_flatbuffer, root, indent, opts, _text);
|
||||
} else {
|
||||
val = IsStruct(fd.value.type)
|
||||
? table->GetStruct<const void *>(fd.value.offset)
|
||||
: table->GetPointer<const void *>(fd.value.offset);
|
||||
? table->GetStruct<const void *>(fd.value.offset)
|
||||
: table->GetPointer<const void *>(fd.value.offset);
|
||||
}
|
||||
return Print(val, fd.value.type, indent, union_type, opts, _text);
|
||||
}
|
||||
|
@ -199,20 +189,17 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
|||
// Generate text for a struct or table, values separated by commas, indented,
|
||||
// and bracketed by "{}"
|
||||
static bool GenStruct(const StructDef &struct_def, const Table *table,
|
||||
int indent, const IDLOptions &opts,
|
||||
std::string *_text) {
|
||||
int indent, const IDLOptions &opts, std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
text += "{";
|
||||
int fieldout = 0;
|
||||
Type *union_type = nullptr;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
FieldDef &fd = **it;
|
||||
auto is_present = struct_def.fixed || table->CheckField(fd.value.offset);
|
||||
auto output_anyway = opts.output_default_scalars_in_json &&
|
||||
IsScalar(fd.value.type.base_type) &&
|
||||
!fd.deprecated;
|
||||
IsScalar(fd.value.type.base_type) && !fd.deprecated;
|
||||
if (is_present || output_anyway) {
|
||||
if (fieldout++) {
|
||||
if (!opts.protobuf_ascii_alike) text += ",";
|
||||
|
@ -222,18 +209,20 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
|
|||
OutputIdentifier(fd.name, opts, _text);
|
||||
if (!opts.protobuf_ascii_alike ||
|
||||
(fd.value.type.base_type != BASE_TYPE_STRUCT &&
|
||||
fd.value.type.base_type != BASE_TYPE_VECTOR)) text += ":";
|
||||
fd.value.type.base_type != BASE_TYPE_VECTOR))
|
||||
text += ":";
|
||||
text += " ";
|
||||
if (is_present) {
|
||||
switch (fd.value.type.base_type) {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
|
||||
opts, indent + Indent(opts), _text)) { \
|
||||
return false; \
|
||||
} \
|
||||
break;
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
case BASE_TYPE_ ## ENUM: \
|
||||
if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
|
||||
opts, indent + Indent(opts), _text)) { \
|
||||
return false; \
|
||||
} \
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
|
||||
#undef FLATBUFFERS_TD
|
||||
// Generate drop-thru case statements for all pointer types:
|
||||
|
@ -247,16 +236,15 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
|
|||
return false;
|
||||
}
|
||||
break;
|
||||
// clang-format on
|
||||
}
|
||||
if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
|
||||
auto enum_val = fd.value.type.enum_def->ReverseLookup(
|
||||
table->GetField<uint8_t>(fd.value.offset, 0));
|
||||
table->GetField<uint8_t>(fd.value.offset, 0));
|
||||
assert(enum_val);
|
||||
union_type = &enum_val->union_type;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
text += fd.value.constant;
|
||||
}
|
||||
}
|
||||
|
@ -272,12 +260,9 @@ bool GenerateText(const Parser &parser, const void *flatbuffer,
|
|||
std::string *_text) {
|
||||
std::string &text = *_text;
|
||||
assert(parser.root_struct_def_); // call SetRootType()
|
||||
text.reserve(1024); // Reduce amount of inevitable reallocs.
|
||||
if (!GenStruct(*parser.root_struct_def_,
|
||||
GetRoot<Table>(flatbuffer),
|
||||
0,
|
||||
parser.opts,
|
||||
_text)) {
|
||||
text.reserve(1024); // Reduce amount of inevitable reallocs.
|
||||
if (!GenStruct(*parser.root_struct_def_, GetRoot<Table>(flatbuffer), 0,
|
||||
parser.opts, _text)) {
|
||||
return false;
|
||||
}
|
||||
text += NewLine(parser.opts);
|
||||
|
@ -289,34 +274,29 @@ std::string TextFileName(const std::string &path,
|
|||
return path + file_name + ".json";
|
||||
}
|
||||
|
||||
bool GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
bool GenerateTextFile(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
if (!parser.builder_.GetSize() || !parser.root_struct_def_) return true;
|
||||
std::string text;
|
||||
if (!GenerateText(parser, parser.builder_.GetBufferPointer(), &text)) {
|
||||
return false;
|
||||
}
|
||||
return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(),
|
||||
text,
|
||||
return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text,
|
||||
false);
|
||||
}
|
||||
|
||||
std::string TextMakeRule(const Parser &parser,
|
||||
const std::string &path,
|
||||
std::string TextMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
if (!parser.builder_.GetSize() || !parser.root_struct_def_) return "";
|
||||
std::string filebase = flatbuffers::StripPath(
|
||||
flatbuffers::StripExtension(file_name));
|
||||
std::string filebase =
|
||||
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
|
||||
std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
|
||||
auto included_files = parser.GetIncludedFilesRecursive(
|
||||
parser.root_struct_def_->file);
|
||||
for (auto it = included_files.begin();
|
||||
it != included_files.end(); ++it) {
|
||||
auto included_files =
|
||||
parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
|
||||
for (auto it = included_files.begin(); it != included_files.end(); ++it) {
|
||||
make_rule += " " + *it;
|
||||
}
|
||||
return make_rule;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -22,7 +22,8 @@
|
|||
namespace flatbuffers {
|
||||
|
||||
int64_t GetAnyValueI(reflection::BaseType type, const uint8_t *data) {
|
||||
# define FLATBUFFERS_GET(T) static_cast<int64_t>(ReadScalar<T>(data))
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_GET(T) static_cast<int64_t>(ReadScalar<T>(data))
|
||||
switch (type) {
|
||||
case reflection::UType:
|
||||
case reflection::Bool:
|
||||
|
@ -43,16 +44,17 @@ int64_t GetAnyValueI(reflection::BaseType type, const uint8_t *data) {
|
|||
}
|
||||
default: return 0; // Tables & vectors do not make sense.
|
||||
}
|
||||
# undef FLATBUFFERS_GET
|
||||
#undef FLATBUFFERS_GET
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
double GetAnyValueF(reflection::BaseType type, const uint8_t *data) {
|
||||
switch (type) {
|
||||
case reflection::Float: return static_cast<double>(ReadScalar<float>(data));
|
||||
case reflection::Float: return static_cast<double>(ReadScalar<float>(data));
|
||||
case reflection::Double: return ReadScalar<double>(data);
|
||||
case reflection::String: {
|
||||
auto s = reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) +
|
||||
data);
|
||||
auto s =
|
||||
reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) + data);
|
||||
return s ? strtod(s->c_str(), nullptr) : 0.0;
|
||||
}
|
||||
default: return static_cast<double>(GetAnyValueI(type, data));
|
||||
|
@ -65,8 +67,8 @@ std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
|
|||
case reflection::Float:
|
||||
case reflection::Double: return NumToString(GetAnyValueF(type, data));
|
||||
case reflection::String: {
|
||||
auto s = reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) +
|
||||
data);
|
||||
auto s =
|
||||
reinterpret_cast<const String *>(ReadScalar<uoffset_t>(data) + data);
|
||||
return s ? s->c_str() : "";
|
||||
}
|
||||
case reflection::Obj:
|
||||
|
@ -80,7 +82,7 @@ std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
|
|||
s += "(struct)"; // TODO: implement this as well.
|
||||
} else {
|
||||
auto table_field = reinterpret_cast<const Table *>(
|
||||
ReadScalar<uoffset_t>(data) + data);
|
||||
ReadScalar<uoffset_t>(data) + data);
|
||||
s += " { ";
|
||||
auto fielddefs = objectdef.fields();
|
||||
for (auto it = fielddefs->begin(); it != fielddefs->end(); ++it) {
|
||||
|
@ -104,15 +106,15 @@ std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data,
|
|||
return "(table)";
|
||||
}
|
||||
case reflection::Vector:
|
||||
return "[(elements)]"; // TODO: implement this as well.
|
||||
case reflection::Union:
|
||||
return "(union)"; // TODO: implement this as well.
|
||||
return "[(elements)]"; // TODO: implement this as well.
|
||||
case reflection::Union: return "(union)"; // TODO: implement this as well.
|
||||
default: return NumToString(GetAnyValueI(type, data));
|
||||
}
|
||||
}
|
||||
|
||||
void SetAnyValueI(reflection::BaseType type, uint8_t *data, int64_t val) {
|
||||
# define FLATBUFFERS_SET(T) WriteScalar(data, static_cast<T>(val))
|
||||
// clang-format off
|
||||
#define FLATBUFFERS_SET(T) WriteScalar(data, static_cast<T>(val))
|
||||
switch (type) {
|
||||
case reflection::UType:
|
||||
case reflection::Bool:
|
||||
|
@ -129,12 +131,13 @@ void SetAnyValueI(reflection::BaseType type, uint8_t *data, int64_t val) {
|
|||
// TODO: support strings
|
||||
default: break;
|
||||
}
|
||||
# undef FLATBUFFERS_SET
|
||||
#undef FLATBUFFERS_SET
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
void SetAnyValueF(reflection::BaseType type, uint8_t *data, double val) {
|
||||
switch (type) {
|
||||
case reflection::Float: WriteScalar(data, static_cast<float>(val)); break;
|
||||
case reflection::Float: WriteScalar(data, static_cast<float>(val)); break;
|
||||
case reflection::Double: WriteScalar(data, val); break;
|
||||
// TODO: support strings.
|
||||
default: SetAnyValueI(type, data, static_cast<int64_t>(val)); break;
|
||||
|
@ -165,9 +168,11 @@ class ResizeContext {
|
|||
ResizeContext(const reflection::Schema &schema, uoffset_t start, int delta,
|
||||
std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table = nullptr)
|
||||
: schema_(schema), startptr_(vector_data(*flatbuf) + start),
|
||||
delta_(delta), buf_(*flatbuf),
|
||||
dag_check_(flatbuf->size() / sizeof(uoffset_t), false) {
|
||||
: schema_(schema),
|
||||
startptr_(vector_data(*flatbuf) + start),
|
||||
delta_(delta),
|
||||
buf_(*flatbuf),
|
||||
dag_check_(flatbuf->size() / sizeof(uoffset_t), false) {
|
||||
auto mask = static_cast<int>(sizeof(largest_scalar_t) - 1);
|
||||
delta_ = (delta_ + mask) & ~mask;
|
||||
if (!delta_) return; // We can't shrink by less than largest_scalar_t.
|
||||
|
@ -176,16 +181,17 @@ class ResizeContext {
|
|||
Straddle<uoffset_t, 1>(vector_data(buf_), root, vector_data(buf_));
|
||||
ResizeTable(root_table ? *root_table : *schema.root_table(), root);
|
||||
// We can now add or remove bytes at start.
|
||||
if (delta_ > 0) buf_.insert(buf_.begin() + start, delta_, 0);
|
||||
else buf_.erase(buf_.begin() + start, buf_.begin() + start - delta_);
|
||||
if (delta_ > 0)
|
||||
buf_.insert(buf_.begin() + start, delta_, 0);
|
||||
else
|
||||
buf_.erase(buf_.begin() + start, buf_.begin() + start - delta_);
|
||||
}
|
||||
|
||||
// Check if the range between first (lower address) and second straddles
|
||||
// the insertion point. If it does, change the offset at offsetloc (of
|
||||
// type T, with direction D).
|
||||
template<typename T, int D> void Straddle(const void *first,
|
||||
const void *second,
|
||||
void *offsetloc) {
|
||||
template<typename T, int D>
|
||||
void Straddle(const void *first, const void *second, void *offsetloc) {
|
||||
if (first <= startptr_ && second >= startptr_) {
|
||||
WriteScalar<T>(offsetloc, ReadScalar<T>(offsetloc) + delta_ * D);
|
||||
DagCheck(offsetloc) = true;
|
||||
|
@ -205,8 +211,7 @@ class ResizeContext {
|
|||
}
|
||||
|
||||
void ResizeTable(const reflection::Object &objectdef, Table *table) {
|
||||
if (DagCheck(table))
|
||||
return; // Table already visited.
|
||||
if (DagCheck(table)) return; // Table already visited.
|
||||
auto vtable = table->GetVTable();
|
||||
// Early out: since all fields inside the table must point forwards in
|
||||
// memory, if the insertion point is before the table we can stop here.
|
||||
|
@ -228,13 +233,14 @@ class ResizeContext {
|
|||
auto offset = table->GetOptionalFieldOffset(fielddef.offset());
|
||||
if (!offset) continue;
|
||||
// Ignore structs.
|
||||
auto subobjectdef = base_type == reflection::Obj ?
|
||||
schema_.objects()->Get(fielddef.type()->index()) : nullptr;
|
||||
auto subobjectdef =
|
||||
base_type == reflection::Obj
|
||||
? schema_.objects()->Get(fielddef.type()->index())
|
||||
: nullptr;
|
||||
if (subobjectdef && subobjectdef->is_struct()) continue;
|
||||
// Get this fields' offset, and read it if safe.
|
||||
auto offsetloc = tableloc + offset;
|
||||
if (DagCheck(offsetloc))
|
||||
continue; // This offset already visited.
|
||||
if (DagCheck(offsetloc)) continue; // This offset already visited.
|
||||
auto ref = offsetloc + ReadScalar<uoffset_t>(offsetloc);
|
||||
Straddle<uoffset_t, 1>(offsetloc, ref, offsetloc);
|
||||
// Recurse.
|
||||
|
@ -248,16 +254,16 @@ class ResizeContext {
|
|||
if (elem_type != reflection::Obj && elem_type != reflection::String)
|
||||
break;
|
||||
auto vec = reinterpret_cast<Vector<uoffset_t> *>(ref);
|
||||
auto elemobjectdef = elem_type == reflection::Obj
|
||||
? schema_.objects()->Get(fielddef.type()->index())
|
||||
: nullptr;
|
||||
auto elemobjectdef =
|
||||
elem_type == reflection::Obj
|
||||
? schema_.objects()->Get(fielddef.type()->index())
|
||||
: nullptr;
|
||||
if (elemobjectdef && elemobjectdef->is_struct()) break;
|
||||
for (uoffset_t i = 0; i < vec->size(); i++) {
|
||||
auto loc = vec->Data() + i * sizeof(uoffset_t);
|
||||
if (DagCheck(loc))
|
||||
continue; // This offset already visited.
|
||||
if (DagCheck(loc)) continue; // This offset already visited.
|
||||
auto dest = loc + vec->Get(i);
|
||||
Straddle<uoffset_t, 1>(loc, dest ,loc);
|
||||
Straddle<uoffset_t, 1>(loc, dest, loc);
|
||||
if (elemobjectdef)
|
||||
ResizeTable(*elemobjectdef, reinterpret_cast<Table *>(dest));
|
||||
}
|
||||
|
@ -268,10 +274,8 @@ class ResizeContext {
|
|||
reinterpret_cast<Table *>(ref));
|
||||
break;
|
||||
}
|
||||
case reflection::String:
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
case reflection::String: break;
|
||||
default: assert(false);
|
||||
}
|
||||
}
|
||||
// Check if the vtable offset points beyond the insertion point.
|
||||
|
@ -292,8 +296,8 @@ class ResizeContext {
|
|||
};
|
||||
|
||||
void SetString(const reflection::Schema &schema, const std::string &val,
|
||||
const String *str, std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table) {
|
||||
const String *str, std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table) {
|
||||
auto delta = static_cast<int>(val.size()) - static_cast<int>(str->Length());
|
||||
auto str_start = static_cast<uoffset_t>(
|
||||
reinterpret_cast<const uint8_t *>(str) - vector_data(*flatbuf));
|
||||
|
@ -317,8 +321,8 @@ uint8_t *ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize,
|
|||
const reflection::Object *root_table) {
|
||||
auto delta_elem = static_cast<int>(newsize) - static_cast<int>(num_elems);
|
||||
auto delta_bytes = delta_elem * static_cast<int>(elem_size);
|
||||
auto vec_start = reinterpret_cast<const uint8_t *>(vec) -
|
||||
vector_data(*flatbuf);
|
||||
auto vec_start =
|
||||
reinterpret_cast<const uint8_t *>(vec) - vector_data(*flatbuf);
|
||||
auto start = static_cast<uoffset_t>(vec_start + sizeof(uoffset_t) +
|
||||
elem_size * num_elems);
|
||||
if (delta_bytes) {
|
||||
|
@ -363,8 +367,7 @@ void CopyInline(FlatBufferBuilder &fbb, const reflection::Field &fielddef,
|
|||
Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
||||
const reflection::Schema &schema,
|
||||
const reflection::Object &objectdef,
|
||||
const Table &table,
|
||||
bool use_string_pooling) {
|
||||
const Table &table, bool use_string_pooling) {
|
||||
// Before we can construct the table, we have to first generate any
|
||||
// subobjects, and collect their offsets.
|
||||
std::vector<uoffset_t> offsets;
|
||||
|
@ -377,39 +380,41 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||
switch (fielddef.type()->base_type()) {
|
||||
case reflection::String: {
|
||||
offset = use_string_pooling
|
||||
? fbb.CreateSharedString(GetFieldS(table, fielddef)).o
|
||||
: fbb.CreateString(GetFieldS(table, fielddef)).o;
|
||||
? fbb.CreateSharedString(GetFieldS(table, fielddef)).o
|
||||
: fbb.CreateString(GetFieldS(table, fielddef)).o;
|
||||
break;
|
||||
}
|
||||
case reflection::Obj: {
|
||||
auto &subobjectdef = *schema.objects()->Get(fielddef.type()->index());
|
||||
if (!subobjectdef.is_struct()) {
|
||||
offset = CopyTable(fbb, schema, subobjectdef,
|
||||
*GetFieldT(table, fielddef)).o;
|
||||
offset =
|
||||
CopyTable(fbb, schema, subobjectdef, *GetFieldT(table, fielddef))
|
||||
.o;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case reflection::Union: {
|
||||
auto &subobjectdef = GetUnionType(schema, objectdef, fielddef, table);
|
||||
offset = CopyTable(fbb, schema, subobjectdef,
|
||||
*GetFieldT(table, fielddef)).o;
|
||||
offset =
|
||||
CopyTable(fbb, schema, subobjectdef, *GetFieldT(table, fielddef)).o;
|
||||
break;
|
||||
}
|
||||
case reflection::Vector: {
|
||||
auto vec = table.GetPointer<const Vector<Offset<Table>> *>(
|
||||
fielddef.offset());
|
||||
auto vec =
|
||||
table.GetPointer<const Vector<Offset<Table>> *>(fielddef.offset());
|
||||
auto element_base_type = fielddef.type()->element();
|
||||
auto elemobjectdef = element_base_type == reflection::Obj
|
||||
? schema.objects()->Get(fielddef.type()->index())
|
||||
: nullptr;
|
||||
auto elemobjectdef =
|
||||
element_base_type == reflection::Obj
|
||||
? schema.objects()->Get(fielddef.type()->index())
|
||||
: nullptr;
|
||||
switch (element_base_type) {
|
||||
case reflection::String: {
|
||||
std::vector<Offset<const String *>> elements(vec->size());
|
||||
auto vec_s = reinterpret_cast<const Vector<Offset<String>> *>(vec);
|
||||
for (uoffset_t i = 0; i < vec_s->size(); i++) {
|
||||
elements[i] = use_string_pooling
|
||||
? fbb.CreateSharedString(vec_s->Get(i)).o
|
||||
: fbb.CreateString(vec_s->Get(i)).o;
|
||||
? fbb.CreateSharedString(vec_s->Get(i)).o
|
||||
: fbb.CreateString(vec_s->Get(i)).o;
|
||||
}
|
||||
offset = fbb.CreateVector(elements).o;
|
||||
break;
|
||||
|
@ -419,7 +424,7 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||
std::vector<Offset<const Table *>> elements(vec->size());
|
||||
for (uoffset_t i = 0; i < vec->size(); i++) {
|
||||
elements[i] =
|
||||
CopyTable(fbb, schema, *elemobjectdef, *vec->Get(i));
|
||||
CopyTable(fbb, schema, *elemobjectdef, *vec->Get(i));
|
||||
}
|
||||
offset = fbb.CreateVector(elements).o;
|
||||
break;
|
||||
|
@ -441,14 +446,11 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||
default: // Scalars.
|
||||
break;
|
||||
}
|
||||
if (offset) {
|
||||
offsets.push_back(offset);
|
||||
}
|
||||
if (offset) { offsets.push_back(offset); }
|
||||
}
|
||||
// Now we can build the actual table from either offsets or scalar data.
|
||||
auto start = objectdef.is_struct()
|
||||
? fbb.StartStruct(objectdef.minalign())
|
||||
: fbb.StartTable();
|
||||
auto start = objectdef.is_struct() ? fbb.StartStruct(objectdef.minalign())
|
||||
: fbb.StartTable();
|
||||
size_t offset_idx = 0;
|
||||
for (auto it = fielddefs->begin(); it != fielddefs->end(); ++it) {
|
||||
auto &fielddef = **it;
|
||||
|
@ -469,7 +471,7 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||
case reflection::Vector:
|
||||
fbb.AddOffset(fielddef.offset(), Offset<void>(offsets[offset_idx++]));
|
||||
break;
|
||||
default: { // Scalars.
|
||||
default: { // Scalars.
|
||||
auto size = GetTypeSize(base_type);
|
||||
CopyInline(fbb, fielddef, table, size, size);
|
||||
break;
|
||||
|
@ -487,51 +489,40 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||
|
||||
bool VerifyStruct(flatbuffers::Verifier &v,
|
||||
const flatbuffers::Table &parent_table,
|
||||
voffset_t field_offset,
|
||||
const reflection::Object &obj,
|
||||
voffset_t field_offset, const reflection::Object &obj,
|
||||
bool required) {
|
||||
auto offset = parent_table.GetOptionalFieldOffset(field_offset);
|
||||
if (required && !offset) {
|
||||
return false;
|
||||
}
|
||||
if (required && !offset) { return false; }
|
||||
|
||||
return !offset || v.Verify(reinterpret_cast<const uint8_t*>(&parent_table)
|
||||
+ offset, obj.bytesize());
|
||||
return !offset ||
|
||||
v.Verify(reinterpret_cast<const uint8_t *>(&parent_table) + offset,
|
||||
obj.bytesize());
|
||||
}
|
||||
|
||||
bool VerifyVectorOfStructs(flatbuffers::Verifier &v,
|
||||
const flatbuffers::Table &parent_table,
|
||||
voffset_t field_offset,
|
||||
const reflection::Object &obj,
|
||||
bool required) {
|
||||
auto p = parent_table.GetPointer<const uint8_t*>(field_offset);
|
||||
const uint8_t* end;
|
||||
if (required && !p) {
|
||||
return false;
|
||||
}
|
||||
const reflection::Object &obj, bool required) {
|
||||
auto p = parent_table.GetPointer<const uint8_t *>(field_offset);
|
||||
const uint8_t *end;
|
||||
if (required && !p) { return false; }
|
||||
|
||||
return !p || v.VerifyVector(p, obj.bytesize(), &end);
|
||||
}
|
||||
|
||||
// forward declare to resolve cyclic deps between VerifyObject and VerifyVector
|
||||
bool VerifyObject(flatbuffers::Verifier &v,
|
||||
const reflection::Schema &schema,
|
||||
bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
|
||||
const reflection::Object &obj,
|
||||
const flatbuffers::Table *table,
|
||||
bool isRequired);
|
||||
const flatbuffers::Table *table, bool isRequired);
|
||||
|
||||
bool VerifyVector(flatbuffers::Verifier &v,
|
||||
const reflection::Schema &schema,
|
||||
bool VerifyVector(flatbuffers::Verifier &v, const reflection::Schema &schema,
|
||||
const flatbuffers::Table &table,
|
||||
const reflection::Field &vec_field) {
|
||||
assert(vec_field.type()->base_type() == reflection::Vector);
|
||||
if (!table.VerifyField<uoffset_t>(v, vec_field.offset()))
|
||||
return false;
|
||||
if (!table.VerifyField<uoffset_t>(v, vec_field.offset())) return false;
|
||||
|
||||
switch (vec_field.type()->element()) {
|
||||
case reflection::None:
|
||||
assert(false);
|
||||
break;
|
||||
case reflection::None: assert(false); break;
|
||||
case reflection::UType:
|
||||
return v.Verify(flatbuffers::GetFieldV<uint8_t>(table, vec_field));
|
||||
case reflection::Bool:
|
||||
|
@ -553,17 +544,15 @@ bool VerifyVector(flatbuffers::Verifier &v,
|
|||
return v.Verify(flatbuffers::GetFieldV<double>(table, vec_field));
|
||||
case reflection::String: {
|
||||
auto vecString =
|
||||
flatbuffers::GetFieldV<flatbuffers::
|
||||
Offset<flatbuffers::String>>(table, vec_field);
|
||||
flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
|
||||
table, vec_field);
|
||||
if (v.Verify(vecString) && v.VerifyVectorOfStrings(vecString)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case reflection::Vector:
|
||||
assert(false);
|
||||
break;
|
||||
case reflection::Vector: assert(false); break;
|
||||
case reflection::Obj: {
|
||||
auto obj = schema.objects()->Get(vec_field.type()->index());
|
||||
if (obj->is_struct()) {
|
||||
|
@ -573,10 +562,9 @@ bool VerifyVector(flatbuffers::Verifier &v,
|
|||
}
|
||||
} else {
|
||||
auto vec =
|
||||
flatbuffers::GetFieldV<flatbuffers::
|
||||
Offset<flatbuffers::Table>>(table, vec_field);
|
||||
if (!v.Verify(vec))
|
||||
return false;
|
||||
flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::Table>>(
|
||||
table, vec_field);
|
||||
if (!v.Verify(vec)) return false;
|
||||
if (vec) {
|
||||
for (uoffset_t j = 0; j < vec->size(); j++) {
|
||||
if (!VerifyObject(v, schema, *obj, vec->Get(j), true)) {
|
||||
|
@ -587,22 +575,16 @@ bool VerifyVector(flatbuffers::Verifier &v,
|
|||
}
|
||||
return true;
|
||||
}
|
||||
case reflection::Union:
|
||||
assert(false);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
case reflection::Union: assert(false); break;
|
||||
default: assert(false); break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VerifyObject(flatbuffers::Verifier &v,
|
||||
const reflection::Schema &schema,
|
||||
bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
|
||||
const reflection::Object &obj,
|
||||
const flatbuffers::Table *table,
|
||||
bool required) {
|
||||
const flatbuffers::Table *table, bool required) {
|
||||
if (!table) {
|
||||
if (!required)
|
||||
return true;
|
||||
|
@ -610,47 +592,37 @@ bool VerifyObject(flatbuffers::Verifier &v,
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!table->VerifyTableStart(v))
|
||||
return false;
|
||||
if (!table->VerifyTableStart(v)) return false;
|
||||
|
||||
for (uoffset_t i = 0; i < obj.fields()->size(); i++) {
|
||||
auto field_def = obj.fields()->Get(i);
|
||||
switch (field_def->type()->base_type()) {
|
||||
case reflection::None:
|
||||
assert(false);
|
||||
break;
|
||||
case reflection::None: assert(false); break;
|
||||
case reflection::UType:
|
||||
if (!table->VerifyField<uint8_t>(v, field_def->offset()))
|
||||
return false;
|
||||
if (!table->VerifyField<uint8_t>(v, field_def->offset())) return false;
|
||||
break;
|
||||
case reflection::Bool:
|
||||
case reflection::Byte:
|
||||
case reflection::UByte:
|
||||
if (!table->VerifyField<int8_t>(v, field_def->offset()))
|
||||
return false;
|
||||
if (!table->VerifyField<int8_t>(v, field_def->offset())) return false;
|
||||
break;
|
||||
case reflection::Short:
|
||||
case reflection::UShort:
|
||||
if (!table->VerifyField<int16_t>(v, field_def->offset()))
|
||||
return false;
|
||||
if (!table->VerifyField<int16_t>(v, field_def->offset())) return false;
|
||||
break;
|
||||
case reflection::Int:
|
||||
case reflection::UInt:
|
||||
if (!table->VerifyField<int32_t>(v, field_def->offset()))
|
||||
return false;
|
||||
if (!table->VerifyField<int32_t>(v, field_def->offset())) return false;
|
||||
break;
|
||||
case reflection::Long:
|
||||
case reflection::ULong:
|
||||
if (!table->VerifyField<int64_t>(v, field_def->offset()))
|
||||
return false;
|
||||
if (!table->VerifyField<int64_t>(v, field_def->offset())) return false;
|
||||
break;
|
||||
case reflection::Float:
|
||||
if (!table->VerifyField<float>(v, field_def->offset()))
|
||||
return false;
|
||||
if (!table->VerifyField<float>(v, field_def->offset())) return false;
|
||||
break;
|
||||
case reflection::Double:
|
||||
if (!table->VerifyField<double>(v, field_def->offset()))
|
||||
return false;
|
||||
if (!table->VerifyField<double>(v, field_def->offset())) return false;
|
||||
break;
|
||||
case reflection::String:
|
||||
if (!table->VerifyField<uoffset_t>(v, field_def->offset()) ||
|
||||
|
@ -659,8 +631,7 @@ bool VerifyObject(flatbuffers::Verifier &v,
|
|||
}
|
||||
break;
|
||||
case reflection::Vector:
|
||||
if (!VerifyVector(v, schema, *table, *field_def))
|
||||
return false;
|
||||
if (!VerifyVector(v, schema, *table, *field_def)) return false;
|
||||
break;
|
||||
case reflection::Obj: {
|
||||
auto child_obj = schema.objects()->Get(field_def->type()->index());
|
||||
|
@ -694,22 +665,17 @@ bool VerifyObject(flatbuffers::Verifier &v,
|
|||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
default: assert(false); break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!v.EndTable())
|
||||
return false;
|
||||
|
||||
if (!v.EndTable()) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Verify(const reflection::Schema &schema,
|
||||
const reflection::Object &root,
|
||||
const uint8_t *buf,
|
||||
size_t length) {
|
||||
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
|
||||
const uint8_t *buf, size_t length) {
|
||||
Verifier v(buf, length);
|
||||
return VerifyObject(v, schema, root, flatbuffers::GetAnyRoot(buf), true);
|
||||
}
|
||||
|
|
|
@ -57,6 +57,7 @@ bool FileExists(const char *name) {
|
|||
}
|
||||
|
||||
bool DirExists(const char *name) {
|
||||
// clang-format off
|
||||
#ifdef _WIN32
|
||||
#define flatbuffers_stat _stat
|
||||
#define FLATBUFFERS_S_IFDIR _S_IFDIR
|
||||
|
@ -64,6 +65,7 @@ bool DirExists(const char *name) {
|
|||
#define flatbuffers_stat stat
|
||||
#define FLATBUFFERS_S_IFDIR S_IFDIR
|
||||
#endif
|
||||
// clang-format on
|
||||
struct flatbuffers_stat file_info;
|
||||
if (flatbuffers_stat(name, &file_info) != 0) return false;
|
||||
return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0;
|
||||
|
@ -78,8 +80,8 @@ LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
|
|||
FileExistsFunction SetFileExistsFunction(
|
||||
FileExistsFunction file_exists_function) {
|
||||
FileExistsFunction previous_function = g_file_exists_function;
|
||||
g_file_exists_function = file_exists_function ?
|
||||
file_exists_function : FileExistsRaw;
|
||||
g_file_exists_function =
|
||||
file_exists_function ? file_exists_function : FileExistsRaw;
|
||||
return previous_function;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,44 +27,36 @@ std::unique_ptr< MonsterStorage::Stub> MonsterStorage::NewStub(const std::shared
|
|||
}
|
||||
|
||||
MonsterStorage::Stub::Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel)
|
||||
: channel_(channel) , rpcmethod_Store_(MonsterStorage_method_names[0], ::grpc::internal::RpcMethod::NORMAL_RPC, channel)
|
||||
, rpcmethod_Retrieve_(MonsterStorage_method_names[1], ::grpc::internal::RpcMethod::SERVER_STREAMING, channel)
|
||||
: channel_(channel) , rpcmethod_Store_(MonsterStorage_method_names[0], ::grpc::RpcMethod::NORMAL_RPC, channel)
|
||||
, rpcmethod_Retrieve_(MonsterStorage_method_names[1], ::grpc::RpcMethod::SERVER_STREAMING, channel)
|
||||
{}
|
||||
|
||||
::grpc::Status MonsterStorage::Stub::Store(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, flatbuffers::grpc::Message<Stat>* response) {
|
||||
return ::grpc::internal::BlockingUnaryCall(channel_.get(), rpcmethod_Store_, context, request, response);
|
||||
return ::grpc::BlockingUnaryCall(channel_.get(), rpcmethod_Store_, context, request, response);
|
||||
}
|
||||
|
||||
::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>* MonsterStorage::Stub::AsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) {
|
||||
return ::grpc::internal::ClientAsyncResponseReaderFactory< flatbuffers::grpc::Message<Stat>>::Create(channel_.get(), cq, rpcmethod_Store_, context, request, true);
|
||||
}
|
||||
|
||||
::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>* MonsterStorage::Stub::PrepareAsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) {
|
||||
return ::grpc::internal::ClientAsyncResponseReaderFactory< flatbuffers::grpc::Message<Stat>>::Create(channel_.get(), cq, rpcmethod_Store_, context, request, false);
|
||||
return ::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>::Create(channel_.get(), cq, rpcmethod_Store_, context, request);
|
||||
}
|
||||
|
||||
::grpc::ClientReader< flatbuffers::grpc::Message<Monster>>* MonsterStorage::Stub::RetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request) {
|
||||
return ::grpc::internal::ClientReaderFactory< flatbuffers::grpc::Message<Monster>>::Create(channel_.get(), rpcmethod_Retrieve_, context, request);
|
||||
return new ::grpc::ClientReader< flatbuffers::grpc::Message<Monster>>(channel_.get(), rpcmethod_Retrieve_, context, request);
|
||||
}
|
||||
|
||||
::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>* MonsterStorage::Stub::AsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq, void* tag) {
|
||||
return ::grpc::internal::ClientAsyncReaderFactory< flatbuffers::grpc::Message<Monster>>::Create(channel_.get(), cq, rpcmethod_Retrieve_, context, request, true, tag);
|
||||
}
|
||||
|
||||
::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>* MonsterStorage::Stub::PrepareAsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq) {
|
||||
return ::grpc::internal::ClientAsyncReaderFactory< flatbuffers::grpc::Message<Monster>>::Create(channel_.get(), cq, rpcmethod_Retrieve_, context, request, false, nullptr);
|
||||
return ::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>::Create(channel_.get(), cq, rpcmethod_Retrieve_, context, request, tag);
|
||||
}
|
||||
|
||||
MonsterStorage::Service::Service() {
|
||||
AddMethod(new ::grpc::internal::RpcServiceMethod(
|
||||
AddMethod(new ::grpc::RpcServiceMethod(
|
||||
MonsterStorage_method_names[0],
|
||||
::grpc::internal::RpcMethod::NORMAL_RPC,
|
||||
new ::grpc::internal::RpcMethodHandler< MonsterStorage::Service, flatbuffers::grpc::Message<Monster>, flatbuffers::grpc::Message<Stat>>(
|
||||
::grpc::RpcMethod::NORMAL_RPC,
|
||||
new ::grpc::RpcMethodHandler< MonsterStorage::Service, flatbuffers::grpc::Message<Monster>, flatbuffers::grpc::Message<Stat>>(
|
||||
std::mem_fn(&MonsterStorage::Service::Store), this)));
|
||||
AddMethod(new ::grpc::internal::RpcServiceMethod(
|
||||
AddMethod(new ::grpc::RpcServiceMethod(
|
||||
MonsterStorage_method_names[1],
|
||||
::grpc::internal::RpcMethod::SERVER_STREAMING,
|
||||
new ::grpc::internal::ServerStreamingHandler< MonsterStorage::Service, flatbuffers::grpc::Message<Stat>, flatbuffers::grpc::Message<Monster>>(
|
||||
::grpc::RpcMethod::SERVER_STREAMING,
|
||||
new ::grpc::ServerStreamingHandler< MonsterStorage::Service, flatbuffers::grpc::Message<Stat>, flatbuffers::grpc::Message<Monster>>(
|
||||
std::mem_fn(&MonsterStorage::Service::Retrieve), this)));
|
||||
}
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
#ifndef GRPC_monster_5ftest__INCLUDED
|
||||
#define GRPC_monster_5ftest__INCLUDED
|
||||
|
||||
#include "monster_test_generated.h"
|
||||
#include "flatbuffers/grpc.h"
|
||||
#include "monster_test_generated.h"
|
||||
|
||||
#include <grpc++/impl/codegen/async_stream.h>
|
||||
#include <grpc++/impl/codegen/async_unary_call.h>
|
||||
|
@ -20,6 +20,7 @@
|
|||
namespace grpc {
|
||||
class CompletionQueue;
|
||||
class Channel;
|
||||
class RpcService;
|
||||
class ServerCompletionQueue;
|
||||
class ServerContext;
|
||||
} // namespace grpc
|
||||
|
@ -39,24 +40,16 @@ class MonsterStorage final {
|
|||
std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::grpc::Message<Stat>>> AsyncStore(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::grpc::Message<Stat>>>(AsyncStoreRaw(context, request, cq));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::grpc::Message<Stat>>> PrepareAsyncStore(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::grpc::Message<Stat>>>(PrepareAsyncStoreRaw(context, request, cq));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientReaderInterface< flatbuffers::grpc::Message<Monster>>> Retrieve(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request) {
|
||||
return std::unique_ptr< ::grpc::ClientReaderInterface< flatbuffers::grpc::Message<Monster>>>(RetrieveRaw(context, request));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientAsyncReaderInterface< flatbuffers::grpc::Message<Monster>>> AsyncRetrieve(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq, void* tag) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncReaderInterface< flatbuffers::grpc::Message<Monster>>>(AsyncRetrieveRaw(context, request, cq, tag));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientAsyncReaderInterface< flatbuffers::grpc::Message<Monster>>> PrepareAsyncRetrieve(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncReaderInterface< flatbuffers::grpc::Message<Monster>>>(PrepareAsyncRetrieveRaw(context, request, cq));
|
||||
}
|
||||
private:
|
||||
virtual ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::grpc::Message<Stat>>* AsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) = 0;
|
||||
virtual ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::grpc::Message<Stat>>* PrepareAsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) = 0;
|
||||
virtual ::grpc::ClientReaderInterface< flatbuffers::grpc::Message<Monster>>* RetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request) = 0;
|
||||
virtual ::grpc::ClientAsyncReaderInterface< flatbuffers::grpc::Message<Monster>>* AsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq, void* tag) = 0;
|
||||
virtual ::grpc::ClientAsyncReaderInterface< flatbuffers::grpc::Message<Monster>>* PrepareAsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq) = 0;
|
||||
};
|
||||
class Stub final : public StubInterface {
|
||||
public:
|
||||
|
@ -65,28 +58,20 @@ class MonsterStorage final {
|
|||
std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>> AsyncStore(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>>(AsyncStoreRaw(context, request, cq));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>> PrepareAsyncStore(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>>(PrepareAsyncStoreRaw(context, request, cq));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientReader< flatbuffers::grpc::Message<Monster>>> Retrieve(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request) {
|
||||
return std::unique_ptr< ::grpc::ClientReader< flatbuffers::grpc::Message<Monster>>>(RetrieveRaw(context, request));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>> AsyncRetrieve(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq, void* tag) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>>(AsyncRetrieveRaw(context, request, cq, tag));
|
||||
}
|
||||
std::unique_ptr< ::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>> PrepareAsyncRetrieve(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq) {
|
||||
return std::unique_ptr< ::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>>(PrepareAsyncRetrieveRaw(context, request, cq));
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr< ::grpc::ChannelInterface> channel_;
|
||||
::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>* AsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) override;
|
||||
::grpc::ClientAsyncResponseReader< flatbuffers::grpc::Message<Stat>>* PrepareAsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Monster>& request, ::grpc::CompletionQueue* cq) override;
|
||||
::grpc::ClientReader< flatbuffers::grpc::Message<Monster>>* RetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request) override;
|
||||
::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>* AsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq, void* tag) override;
|
||||
::grpc::ClientAsyncReader< flatbuffers::grpc::Message<Monster>>* PrepareAsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::grpc::Message<Stat>& request, ::grpc::CompletionQueue* cq) override;
|
||||
const ::grpc::internal::RpcMethod rpcmethod_Store_;
|
||||
const ::grpc::internal::RpcMethod rpcmethod_Retrieve_;
|
||||
const ::grpc::RpcMethod rpcmethod_Store_;
|
||||
const ::grpc::RpcMethod rpcmethod_Retrieve_;
|
||||
};
|
||||
static std::unique_ptr<Stub> NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options = ::grpc::StubOptions());
|
||||
|
||||
|
@ -179,7 +164,7 @@ class MonsterStorage final {
|
|||
public:
|
||||
WithStreamedUnaryMethod_Store() {
|
||||
::grpc::Service::MarkMethodStreamed(0,
|
||||
new ::grpc::internal::StreamedUnaryHandler< flatbuffers::grpc::Message<Monster>, flatbuffers::grpc::Message<Stat>>(std::bind(&WithStreamedUnaryMethod_Store<BaseClass>::StreamedStore, this, std::placeholders::_1, std::placeholders::_2)));
|
||||
new ::grpc::StreamedUnaryHandler< flatbuffers::grpc::Message<Monster>, flatbuffers::grpc::Message<Stat>>(std::bind(&WithStreamedUnaryMethod_Store<BaseClass>::StreamedStore, this, std::placeholders::_1, std::placeholders::_2)));
|
||||
}
|
||||
~WithStreamedUnaryMethod_Store() override {
|
||||
BaseClassMustBeDerivedFromService(this);
|
||||
|
@ -200,7 +185,7 @@ class MonsterStorage final {
|
|||
public:
|
||||
WithSplitStreamingMethod_Retrieve() {
|
||||
::grpc::Service::MarkMethodStreamed(1,
|
||||
new ::grpc::internal::SplitServerStreamingHandler< flatbuffers::grpc::Message<Stat>, flatbuffers::grpc::Message<Monster>>(std::bind(&WithSplitStreamingMethod_Retrieve<BaseClass>::StreamedRetrieve, this, std::placeholders::_1, std::placeholders::_2)));
|
||||
new ::grpc::SplitServerStreamingHandler< flatbuffers::grpc::Message<Stat>, flatbuffers::grpc::Message<Monster>>(std::bind(&WithSplitStreamingMethod_Retrieve<BaseClass>::StreamedRetrieve, this, std::placeholders::_1, std::placeholders::_2)));
|
||||
}
|
||||
~WithSplitStreamingMethod_Retrieve() override {
|
||||
BaseClassMustBeDerivedFromService(this);
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -18,7 +18,7 @@ export class TableInNestedNS {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -48,8 +48,8 @@ static getRootAsTableInNestedNS(bb:flatbuffers.ByteBuffer, obj?:TableInNestedNS)
|
|||
* @returns {number}
|
||||
*/
|
||||
foo():number {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? this.bb!.readInt32(this.bb_pos + offset) : 0;
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? this.bb.readInt32(this.bb_pos + offset) : 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -57,13 +57,13 @@ foo():number {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_foo(value:number):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeInt32(this.bb_pos + offset, value);
|
||||
this.bb.writeInt32(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -101,7 +101,7 @@ export class StructInNestedNS {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -122,7 +122,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):StructInNestedNS {
|
|||
* @returns {number}
|
||||
*/
|
||||
a():number {
|
||||
return this.bb!.readInt32(this.bb_pos);
|
||||
return this.bb.readInt32(this.bb_pos);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -130,13 +130,13 @@ a():number {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_a(value:number):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 0);
|
||||
var offset = this.bb.__offset(this.bb_pos, 0);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeInt32(this.bb_pos + offset, value);
|
||||
this.bb.writeInt32(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -144,7 +144,7 @@ mutate_a(value:number):boolean {
|
|||
* @returns {number}
|
||||
*/
|
||||
b():number {
|
||||
return this.bb!.readInt32(this.bb_pos + 4);
|
||||
return this.bb.readInt32(this.bb_pos + 4);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -152,13 +152,13 @@ b():number {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_b(value:number):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeInt32(this.bb_pos + offset, value);
|
||||
this.bb.writeInt32(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ export class TableInFirstNS {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -40,16 +40,16 @@ static getRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:TableInFirstNS):T
|
|||
* @returns {NamespaceA.NamespaceB.TableInNestedNS|null}
|
||||
*/
|
||||
fooTable(obj?:NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS):NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {NamespaceA.NamespaceB.EnumInNestedNS}
|
||||
*/
|
||||
fooEnum():NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 6);
|
||||
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb!.readInt8(this.bb_pos + offset)) : NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS.A;
|
||||
var offset = this.bb.__offset(this.bb_pos, 6);
|
||||
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb.readInt8(this.bb_pos + offset)) : NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS.A;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -57,13 +57,13 @@ fooEnum():NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_foo_enum(value:NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 6);
|
||||
var offset = this.bb.__offset(this.bb_pos, 6);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeInt8(this.bb_pos + offset, value);
|
||||
this.bb.writeInt8(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -72,8 +72,8 @@ mutate_foo_enum(value:NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS
|
|||
* @returns {NamespaceA.NamespaceB.StructInNestedNS|null}
|
||||
*/
|
||||
fooStruct(obj?:NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS):NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 8);
|
||||
return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 8);
|
||||
return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -126,7 +126,7 @@ export class TableInC {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -157,8 +157,8 @@ static getRootAsTableInC(bb:flatbuffers.ByteBuffer, obj?:TableInC):TableInC {
|
|||
* @returns {NamespaceA.TableInFirstNS|null}
|
||||
*/
|
||||
referToA1(obj?:NamespaceA.TableInFirstNS):NamespaceA.TableInFirstNS|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NamespaceA.TableInFirstNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NamespaceA.TableInFirstNS).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -166,8 +166,8 @@ referToA1(obj?:NamespaceA.TableInFirstNS):NamespaceA.TableInFirstNS|null {
|
|||
* @returns {NamespaceA.SecondTableInA|null}
|
||||
*/
|
||||
referToA2(obj?:NamespaceA.SecondTableInA):NamespaceA.SecondTableInA|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 6);
|
||||
return offset ? (obj || new NamespaceA.SecondTableInA).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 6);
|
||||
return offset ? (obj || new NamespaceA.SecondTableInA).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -212,7 +212,7 @@ export class SecondTableInA {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -243,8 +243,8 @@ static getRootAsSecondTableInA(bb:flatbuffers.ByteBuffer, obj?:SecondTableInA):S
|
|||
* @returns {NamespaceC.TableInC|null}
|
||||
*/
|
||||
referToC(obj?:NamespaceC.TableInC):NamespaceC.TableInC|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NamespaceC.TableInC).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NamespaceC.TableInC).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
746
tests/test.cpp
746
tests/test.cpp
File diff suppressed because it is too large
Load Diff
|
@ -20,7 +20,7 @@ export class Attacker {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -50,8 +50,8 @@ static getRootAsAttacker(bb:flatbuffers.ByteBuffer, obj?:Attacker):Attacker {
|
|||
* @returns {number}
|
||||
*/
|
||||
swordAttackDamage():number {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? this.bb!.readInt32(this.bb_pos + offset) : 0;
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? this.bb.readInt32(this.bb_pos + offset) : 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -59,13 +59,13 @@ swordAttackDamage():number {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_sword_attack_damage(value:number):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeInt32(this.bb_pos + offset, value);
|
||||
this.bb.writeInt32(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -101,7 +101,7 @@ export class Rapunzel {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -122,7 +122,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):Rapunzel {
|
|||
* @returns {number}
|
||||
*/
|
||||
hairLength():number {
|
||||
return this.bb!.readInt32(this.bb_pos);
|
||||
return this.bb.readInt32(this.bb_pos);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -130,13 +130,13 @@ hairLength():number {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_hair_length(value:number):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 0);
|
||||
var offset = this.bb.__offset(this.bb_pos, 0);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeInt32(this.bb_pos + offset, value);
|
||||
this.bb.writeInt32(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -159,7 +159,7 @@ export class BookReader {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -180,7 +180,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):BookReader {
|
|||
* @returns {number}
|
||||
*/
|
||||
booksRead():number {
|
||||
return this.bb!.readInt32(this.bb_pos);
|
||||
return this.bb.readInt32(this.bb_pos);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -188,13 +188,13 @@ booksRead():number {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_books_read(value:number):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 0);
|
||||
var offset = this.bb.__offset(this.bb_pos, 0);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeInt32(this.bb_pos + offset, value);
|
||||
this.bb.writeInt32(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -217,7 +217,7 @@ export class Movie {
|
|||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb: flatbuffers.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
|
@ -255,8 +255,8 @@ static bufferHasIdentifier(bb:flatbuffers.ByteBuffer):boolean {
|
|||
* @returns {Character}
|
||||
*/
|
||||
mainCharacterType():Character {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? /** @type {Character} */ (this.bb!.readUint8(this.bb_pos + offset)) : Character.NONE;
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? /** @type {Character} */ (this.bb.readUint8(this.bb_pos + offset)) : Character.NONE;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -264,13 +264,13 @@ mainCharacterType():Character {
|
|||
* @returns {boolean}
|
||||
*/
|
||||
mutate_main_character_type(value:Character):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
|
||||
if (offset === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.bb!.writeUint8(this.bb_pos + offset, value);
|
||||
this.bb.writeUint8(this.bb_pos + offset, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -279,8 +279,8 @@ mutate_main_character_type(value:Character):boolean {
|
|||
* @returns {?flatbuffers.Table}
|
||||
*/
|
||||
mainCharacter<T extends flatbuffers.Table>(obj:T):T|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 6);
|
||||
return offset ? this.bb!.__union(obj, this.bb_pos + offset) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 6);
|
||||
return offset ? this.bb.__union(obj, this.bb_pos + offset) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -288,24 +288,24 @@ mainCharacter<T extends flatbuffers.Table>(obj:T):T|null {
|
|||
* @returns {Character}
|
||||
*/
|
||||
charactersType(index: number):Character|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 8);
|
||||
return offset ? /** @type {Character} */ (this.bb!.readUint8(this.bb!.__vector(this.bb_pos + offset) + index)) : /** @type {Character} */ (0);
|
||||
var offset = this.bb.__offset(this.bb_pos, 8);
|
||||
return offset ? /** @type {Character} */ (this.bb.readUint8(this.bb.__vector(this.bb_pos + offset) + index)) : /** @type {Character} */ (0);
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
charactersTypeLength():number {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 8);
|
||||
return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0;
|
||||
var offset = this.bb.__offset(this.bb_pos, 8);
|
||||
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
charactersTypeArray():Uint8Array|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 8);
|
||||
return offset ? new Uint8Array(this.bb!.bytes().buffer, this.bb!.bytes().byteOffset + this.bb!.__vector(this.bb_pos + offset), this.bb!.__vector_len(this.bb_pos + offset)) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 8);
|
||||
return offset ? new Uint8Array(this.bb.bytes().buffer, this.bb.bytes().byteOffset + this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -314,16 +314,16 @@ charactersTypeArray():Uint8Array|null {
|
|||
* @returns {?flatbuffers.Table}
|
||||
*/
|
||||
characters<T extends flatbuffers.Table>(index: number, obj:T):T|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 10);
|
||||
return offset ? this.bb!.__union(obj, this.bb!.__vector(this.bb_pos + offset) + index * 4) : null;
|
||||
var offset = this.bb.__offset(this.bb_pos, 10);
|
||||
return offset ? this.bb.__union(obj, this.bb.__vector(this.bb_pos + offset) + index * 4) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
charactersLength():number {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 10);
|
||||
return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0;
|
||||
var offset = this.bb.__offset(this.bb_pos, 10);
|
||||
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue