mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
@@ -81,7 +81,7 @@ if(APPLE)
|
||||
"${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -Wall -pedantic -Werror -Wextra")
|
||||
elseif(CMAKE_COMPILER_IS_GNUCXX)
|
||||
set(CMAKE_CXX_FLAGS
|
||||
"${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow")
|
||||
"${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow -Wunused-result -Werror=unused-result")
|
||||
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
||||
set(CMAKE_CXX_FLAGS
|
||||
"${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -Wall -pedantic -Werror -Wextra")
|
||||
|
||||
@@ -1187,6 +1187,18 @@ class Table {
|
||||
uint8_t data_[1];
|
||||
};
|
||||
|
||||
// Helper function to test if a field is present, using any of the field
|
||||
// enums in the generated code.
|
||||
// `table` must be a generated table type. Since this is a template parameter,
|
||||
// this is not typechecked to be a subclass of Table, so beware!
|
||||
// Note: this function will return false for fields equal to the default
|
||||
// value, since they're not stored in the buffer (unless force_defaults was
|
||||
// used).
|
||||
template<typename T> bool IsFieldPresent(const T *table, voffset_t field) {
|
||||
// Cast, since Table is a private baseclass of any table types.
|
||||
return reinterpret_cast<const Table *>(table)->CheckField(field);
|
||||
}
|
||||
|
||||
// Utility function for reverse lookups on the EnumNames*() functions
|
||||
// (in the generated C++ code)
|
||||
// names must be NULL terminated.
|
||||
|
||||
@@ -340,6 +340,46 @@ struct IDLOptions {
|
||||
lang(IDLOptions::kJava) {}
|
||||
};
|
||||
|
||||
// A way to make error propagation less error prone by requiring values to be
|
||||
// checked.
|
||||
// Once you create a value of this type you must either:
|
||||
// - Call Check() on it.
|
||||
// - Copy or assign it to another value.
|
||||
// Failure to do so leads to an assert.
|
||||
// This guarantees that this as return value cannot be ignored.
|
||||
class CheckedError {
|
||||
public:
|
||||
explicit CheckedError(bool error)
|
||||
: is_error_(error), has_been_checked_(false) {}
|
||||
|
||||
CheckedError &operator=(const CheckedError &other) {
|
||||
is_error_ = other.is_error_;
|
||||
has_been_checked_ = false;
|
||||
other.has_been_checked_ = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CheckedError(const CheckedError &other) {
|
||||
*this = other; // Use assignment operator.
|
||||
}
|
||||
|
||||
~CheckedError() { assert(has_been_checked_); }
|
||||
|
||||
bool Check() { has_been_checked_ = true; return is_error_; }
|
||||
|
||||
private:
|
||||
bool is_error_;
|
||||
mutable bool has_been_checked_;
|
||||
};
|
||||
|
||||
// Additionally, in GCC we can get these errors statically, for additional
|
||||
// assurance:
|
||||
#ifdef __GNUC__
|
||||
#define CHECKED_ERROR CheckedError __attribute__((warn_unused_result))
|
||||
#else
|
||||
#define CHECKED_ERROR CheckedError
|
||||
#endif
|
||||
|
||||
class Parser {
|
||||
public:
|
||||
explicit Parser(const IDLOptions &options = IDLOptions())
|
||||
@@ -395,44 +435,51 @@ class Parser {
|
||||
// See reflection/reflection.fbs
|
||||
void Serialize();
|
||||
|
||||
private:
|
||||
int64_t ParseHexNum(int nibbles);
|
||||
void Next();
|
||||
bool IsNext(int t);
|
||||
void Expect(int t);
|
||||
CHECKED_ERROR CheckBitsFit(int64_t val, size_t bits);
|
||||
|
||||
private:
|
||||
CHECKED_ERROR Error(const std::string &msg);
|
||||
CHECKED_ERROR ParseHexNum(int nibbles, int64_t *val);
|
||||
CHECKED_ERROR Next();
|
||||
bool Is(int t);
|
||||
CHECKED_ERROR Expect(int t);
|
||||
std::string TokenToStringId(int t);
|
||||
EnumDef *LookupEnum(const std::string &id);
|
||||
void ParseNamespacing(std::string *id, std::string *last);
|
||||
void ParseTypeIdent(Type &type);
|
||||
void ParseType(Type &type);
|
||||
FieldDef &AddField(StructDef &struct_def,
|
||||
const std::string &name,
|
||||
const Type &type);
|
||||
void ParseField(StructDef &struct_def);
|
||||
void ParseAnyValue(Value &val, FieldDef *field, size_t parent_fieldn);
|
||||
uoffset_t ParseTable(const StructDef &struct_def, std::string *value);
|
||||
CHECKED_ERROR ParseNamespacing(std::string *id, std::string *last);
|
||||
CHECKED_ERROR ParseTypeIdent(Type &type);
|
||||
CHECKED_ERROR ParseType(Type &type);
|
||||
CHECKED_ERROR AddField(StructDef &struct_def, const std::string &name,
|
||||
const Type &type, FieldDef **dest);
|
||||
CHECKED_ERROR ParseField(StructDef &struct_def);
|
||||
CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field, size_t parent_fieldn);
|
||||
CHECKED_ERROR ParseTable(const StructDef &struct_def, std::string *value,
|
||||
uoffset_t *ovalue);
|
||||
void SerializeStruct(const StructDef &struct_def, const Value &val);
|
||||
void AddVector(bool sortbysize, int count);
|
||||
uoffset_t ParseVector(const Type &type);
|
||||
void ParseMetaData(Definition &def);
|
||||
bool TryTypedValue(int dtoken, bool check, Value &e, BaseType req);
|
||||
void ParseHash(Value &e, FieldDef* field);
|
||||
void ParseSingleValue(Value &e);
|
||||
int64_t ParseIntegerFromString(Type &type);
|
||||
CHECKED_ERROR ParseVector(const Type &type, uoffset_t *ovalue);
|
||||
CHECKED_ERROR ParseMetaData(Definition &def);
|
||||
CHECKED_ERROR TryTypedValue(int dtoken, bool check, Value &e, BaseType req,
|
||||
bool *destmatch);
|
||||
CHECKED_ERROR ParseHash(Value &e, FieldDef* field);
|
||||
CHECKED_ERROR ParseSingleValue(Value &e);
|
||||
CHECKED_ERROR ParseIntegerFromString(Type &type, int64_t *result);
|
||||
StructDef *LookupCreateStruct(const std::string &name,
|
||||
bool create_if_new = true,
|
||||
bool definition = false);
|
||||
EnumDef &ParseEnum(bool is_union);
|
||||
void ParseNamespace();
|
||||
StructDef &StartStruct(const std::string &name);
|
||||
void ParseDecl();
|
||||
void ParseProtoFields(StructDef *struct_def, bool isextend,
|
||||
bool inside_oneof);
|
||||
void ParseProtoOption();
|
||||
void ParseProtoKey();
|
||||
void ParseProtoDecl();
|
||||
void ParseProtoCurliesOrIdent();
|
||||
Type ParseTypeFromProtoType();
|
||||
CHECKED_ERROR ParseEnum(bool is_union, EnumDef **dest);
|
||||
CHECKED_ERROR ParseNamespace();
|
||||
CHECKED_ERROR StartStruct(const std::string &name, StructDef **dest);
|
||||
CHECKED_ERROR ParseDecl();
|
||||
CHECKED_ERROR ParseProtoFields(StructDef *struct_def, bool isextend,
|
||||
bool inside_oneof);
|
||||
CHECKED_ERROR ParseProtoOption();
|
||||
CHECKED_ERROR ParseProtoKey();
|
||||
CHECKED_ERROR ParseProtoDecl();
|
||||
CHECKED_ERROR ParseProtoCurliesOrIdent();
|
||||
CHECKED_ERROR ParseTypeFromProtoType(Type *type);
|
||||
|
||||
CHECKED_ERROR DoParse(const char *_source, const char **include_paths,
|
||||
const char *source_filename);
|
||||
|
||||
public:
|
||||
SymbolTable<StructDef> structs_;
|
||||
@@ -454,7 +501,7 @@ class Parser {
|
||||
const char *source_, *cursor_;
|
||||
int line_; // the current line being parsed
|
||||
int token_;
|
||||
std::string files_being_parsed_;
|
||||
std::string file_being_parsed_;
|
||||
|
||||
std::string attribute_;
|
||||
std::vector<std::string> doc_comment_;
|
||||
|
||||
1133
src/idl_parser.cpp
1133
src/idl_parser.cpp
File diff suppressed because it is too large
Load Diff
@@ -287,14 +287,17 @@ void SetString(const reflection::Schema &schema, const std::string &val,
|
||||
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 start = static_cast<uoffset_t>(reinterpret_cast<const uint8_t *>(str) -
|
||||
flatbuf->data() +
|
||||
sizeof(uoffset_t));
|
||||
auto str_start = static_cast<uoffset_t>(
|
||||
reinterpret_cast<const uint8_t *>(str) - flatbuf->data());
|
||||
auto start = str_start + sizeof(uoffset_t);
|
||||
if (delta) {
|
||||
// Clear the old string, since we don't want parts of it remaining.
|
||||
memset(flatbuf->data() + start, 0, str->Length());
|
||||
// Different size, we must expand (or contract).
|
||||
ResizeContext(schema, start, delta, flatbuf, root_table);
|
||||
// Set the new length.
|
||||
WriteScalar(flatbuf->data() + str_start,
|
||||
static_cast<uoffset_t>(val.size()));
|
||||
}
|
||||
// Copy new data. Safe because we created the right amount of space.
|
||||
memcpy(flatbuf->data() + start, val.c_str(), val.size() + 1);
|
||||
|
||||
@@ -1,2 +1,17 @@
|
||||
..\flatc.exe -c -j -n -g -b -p --php -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
|
||||
..\flatc.exe -b --schema monster_test.fbs
|
||||
:: Copyright 2015 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.
|
||||
|
||||
..\flatc.exe --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
|
||||
..\flatc.exe --cpp --java --csharp --go --binary --python --js --php --gen-mutable -o namespace_test namespace_test\namespace_test1.fbs namespace_test\namespace_test2.fbs
|
||||
..\flatc.exe --binary --schema monster_test.fbs
|
||||
|
||||
@@ -1,2 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright 2015 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.
|
||||
|
||||
../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
|
||||
../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
|
||||
../flatc --binary --schema monster_test.fbs
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# automatically generated, do not modify
|
||||
|
||||
# namespace: NamespaceB
|
||||
|
||||
class EnumInNestedNS(object):
|
||||
A = 0
|
||||
B = 1
|
||||
C = 2
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace NamespaceA.NamespaceB
|
||||
{
|
||||
|
||||
using System;
|
||||
using FlatBuffers;
|
||||
|
||||
public sealed class StructInNestedNS : Struct {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# automatically generated, do not modify
|
||||
|
||||
# namespace: NamespaceB
|
||||
|
||||
import flatbuffers
|
||||
|
||||
class StructInNestedNS(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
# StructInNestedNS
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# StructInNestedNS
|
||||
def A(self): return self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(0))
|
||||
# StructInNestedNS
|
||||
def B(self): return self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(4))
|
||||
|
||||
def CreateStructInNestedNS(builder, a, b):
|
||||
builder.Prep(4, 8)
|
||||
builder.PrependInt32(b)
|
||||
builder.PrependInt32(a)
|
||||
return builder.Offset()
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace NamespaceA.NamespaceB
|
||||
{
|
||||
|
||||
using System;
|
||||
using FlatBuffers;
|
||||
|
||||
public sealed class TableInNestedNS : Table {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# automatically generated, do not modify
|
||||
|
||||
# namespace: NamespaceB
|
||||
|
||||
import flatbuffers
|
||||
|
||||
class TableInNestedNS(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
# TableInNestedNS
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# TableInNestedNS
|
||||
def Foo(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
|
||||
if o != 0:
|
||||
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
|
||||
return 0
|
||||
|
||||
def TableInNestedNSStart(builder): builder.StartObject(1)
|
||||
def TableInNestedNSAddFoo(builder, foo): builder.PrependInt32Slot(0, foo, 0)
|
||||
def TableInNestedNSEnd(builder): return builder.EndObject()
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace NamespaceA
|
||||
{
|
||||
|
||||
using System;
|
||||
using FlatBuffers;
|
||||
|
||||
public sealed class TableInFirstNS : Table {
|
||||
|
||||
47
tests/namespace_test/NamespaceA/TableInFirstNS.py
Normal file
47
tests/namespace_test/NamespaceA/TableInFirstNS.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# automatically generated, do not modify
|
||||
|
||||
# namespace: NamespaceA
|
||||
|
||||
import flatbuffers
|
||||
|
||||
class TableInFirstNS(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
# TableInFirstNS
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# TableInFirstNS
|
||||
def FooTable(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
|
||||
if o != 0:
|
||||
x = self._tab.Indirect(o + self._tab.Pos)
|
||||
from .TableInNestedNS import TableInNestedNS
|
||||
obj = TableInNestedNS()
|
||||
obj.Init(self._tab.Bytes, x)
|
||||
return obj
|
||||
return None
|
||||
|
||||
# TableInFirstNS
|
||||
def FooEnum(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
|
||||
if o != 0:
|
||||
return self._tab.Get(flatbuffers.number_types.Int8Flags, o + self._tab.Pos)
|
||||
return 0
|
||||
|
||||
# TableInFirstNS
|
||||
def FooStruct(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8))
|
||||
if o != 0:
|
||||
x = o + self._tab.Pos
|
||||
from .StructInNestedNS import StructInNestedNS
|
||||
obj = StructInNestedNS()
|
||||
obj.Init(self._tab.Bytes, x)
|
||||
return obj
|
||||
return None
|
||||
|
||||
def TableInFirstNSStart(builder): builder.StartObject(3)
|
||||
def TableInFirstNSAddFooTable(builder, fooTable): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(fooTable), 0)
|
||||
def TableInFirstNSAddFooEnum(builder, fooEnum): builder.PrependInt8Slot(1, fooEnum, 0)
|
||||
def TableInFirstNSAddFooStruct(builder, fooStruct): builder.PrependStructSlot(2, flatbuffers.number_types.UOffsetTFlags.py_type(fooStruct), 0)
|
||||
def TableInFirstNSEnd(builder): return builder.EndObject()
|
||||
0
tests/namespace_test/NamespaceA/__init__.py
Normal file
0
tests/namespace_test/NamespaceA/__init__.py
Normal file
@@ -42,11 +42,14 @@ MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS {
|
||||
STRUCT_END(StructInNestedNS, 8);
|
||||
|
||||
struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
int32_t foo() const { return GetField<int32_t>(4, 0); }
|
||||
bool mutate_foo(int32_t _foo) { return SetField(4, _foo); }
|
||||
enum {
|
||||
VT_FOO = 4,
|
||||
};
|
||||
int32_t foo() const { return GetField<int32_t>(VT_FOO, 0); }
|
||||
bool mutate_foo(int32_t _foo) { return SetField(VT_FOO, _foo); }
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<int32_t>(verifier, 4 /* foo */) &&
|
||||
VerifyField<int32_t>(verifier, VT_FOO) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
@@ -54,7 +57,7 @@ struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
struct TableInNestedNSBuilder {
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_foo(int32_t foo) { fbb_.AddElement<int32_t>(4, foo, 0); }
|
||||
void add_foo(int32_t foo) { fbb_.AddElement<int32_t>(TableInNestedNS::VT_FOO, foo, 0); }
|
||||
TableInNestedNSBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
TableInNestedNSBuilder &operator=(const TableInNestedNSBuilder &);
|
||||
flatbuffers::Offset<TableInNestedNS> Finish() {
|
||||
|
||||
143
tests/namespace_test/namespace_test1_generated.js
Normal file
143
tests/namespace_test/namespace_test1_generated.js
Normal file
@@ -0,0 +1,143 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/**
|
||||
* @const
|
||||
*/
|
||||
var NamespaceA = NamespaceA || {};
|
||||
|
||||
/**
|
||||
* @const
|
||||
*/
|
||||
NamespaceA.NamespaceB = NamespaceA.NamespaceB || {};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
*/
|
||||
NamespaceA.NamespaceB.EnumInNestedNS = {
|
||||
A: 0,
|
||||
B: 1,
|
||||
C: 2
|
||||
};
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
NamespaceA.NamespaceB.TableInNestedNS = function() {
|
||||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
this.bb = null;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.bb_pos = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {number} i
|
||||
* @param {flatbuffers.ByteBuffer} bb
|
||||
* @returns {NamespaceA.NamespaceB.TableInNestedNS}
|
||||
*/
|
||||
NamespaceA.NamespaceB.TableInNestedNS.prototype.__init = function(i, bb) {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.ByteBuffer} bb
|
||||
* @param {NamespaceA.NamespaceB.TableInNestedNS=} obj
|
||||
* @returns {NamespaceA.NamespaceB.TableInNestedNS}
|
||||
*/
|
||||
NamespaceA.NamespaceB.TableInNestedNS.getRootAsTableInNestedNS = function(bb, obj) {
|
||||
return (obj || new NamespaceA.NamespaceB.TableInNestedNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
NamespaceA.NamespaceB.TableInNestedNS.prototype.foo = function() {
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? this.bb.readInt32(this.bb_pos + offset) : 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
*/
|
||||
NamespaceA.NamespaceB.TableInNestedNS.startTableInNestedNS = function(builder) {
|
||||
builder.startObject(1);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
* @param {number} foo
|
||||
*/
|
||||
NamespaceA.NamespaceB.TableInNestedNS.addFoo = function(builder, foo) {
|
||||
builder.addFieldInt32(0, foo, 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
* @returns {flatbuffers.Offset}
|
||||
*/
|
||||
NamespaceA.NamespaceB.TableInNestedNS.endTableInNestedNS = function(builder) {
|
||||
var offset = builder.endObject();
|
||||
return offset;
|
||||
};
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
NamespaceA.NamespaceB.StructInNestedNS = function() {
|
||||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
this.bb = null;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.bb_pos = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {number} i
|
||||
* @param {flatbuffers.ByteBuffer} bb
|
||||
* @returns {NamespaceA.NamespaceB.StructInNestedNS}
|
||||
*/
|
||||
NamespaceA.NamespaceB.StructInNestedNS.prototype.__init = function(i, bb) {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
NamespaceA.NamespaceB.StructInNestedNS.prototype.a = function() {
|
||||
return this.bb.readInt32(this.bb_pos);
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
NamespaceA.NamespaceB.StructInNestedNS.prototype.b = function() {
|
||||
return this.bb.readInt32(this.bb_pos + 4);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
* @param {number} a
|
||||
* @param {number} b
|
||||
* @returns {flatbuffers.Offset}
|
||||
*/
|
||||
NamespaceA.NamespaceB.StructInNestedNS.createStructInNestedNS = function(builder, a, b) {
|
||||
builder.prep(4, 8);
|
||||
builder.writeInt32(b);
|
||||
builder.writeInt32(a);
|
||||
return builder.offset();
|
||||
};
|
||||
|
||||
// Exports for Node.js and RequireJS
|
||||
this.NamespaceA = NamespaceA;
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
#include "namespace_test1_generated.h"
|
||||
|
||||
namespace NamespaceA {
|
||||
namespace NamespaceB {
|
||||
struct TableInNestedNS;
|
||||
@@ -17,18 +19,23 @@ namespace NamespaceA {
|
||||
struct TableInFirstNS;
|
||||
|
||||
struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
const NamespaceA::NamespaceB::TableInNestedNS *foo_table() const { return GetPointer<const NamespaceA::NamespaceB::TableInNestedNS *>(4); }
|
||||
NamespaceA::NamespaceB::TableInNestedNS *mutable_foo_table() { return GetPointer<NamespaceA::NamespaceB::TableInNestedNS *>(4); }
|
||||
NamespaceA::NamespaceB::EnumInNestedNS foo_enum() const { return static_cast<NamespaceA::NamespaceB::EnumInNestedNS>(GetField<int8_t>(6, 0)); }
|
||||
bool mutate_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS _foo_enum) { return SetField(6, static_cast<int8_t>(_foo_enum)); }
|
||||
const NamespaceA::NamespaceB::StructInNestedNS *foo_struct() const { return GetStruct<const NamespaceA::NamespaceB::StructInNestedNS *>(8); }
|
||||
NamespaceA::NamespaceB::StructInNestedNS *mutable_foo_struct() { return GetStruct<NamespaceA::NamespaceB::StructInNestedNS *>(8); }
|
||||
enum {
|
||||
VT_FOO_TABLE = 4,
|
||||
VT_FOO_ENUM = 6,
|
||||
VT_FOO_STRUCT = 8,
|
||||
};
|
||||
const NamespaceA::NamespaceB::TableInNestedNS *foo_table() const { return GetPointer<const NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE); }
|
||||
NamespaceA::NamespaceB::TableInNestedNS *mutable_foo_table() { return GetPointer<NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE); }
|
||||
NamespaceA::NamespaceB::EnumInNestedNS foo_enum() const { return static_cast<NamespaceA::NamespaceB::EnumInNestedNS>(GetField<int8_t>(VT_FOO_ENUM, 0)); }
|
||||
bool mutate_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS _foo_enum) { return SetField(VT_FOO_ENUM, static_cast<int8_t>(_foo_enum)); }
|
||||
const NamespaceA::NamespaceB::StructInNestedNS *foo_struct() const { return GetStruct<const NamespaceA::NamespaceB::StructInNestedNS *>(VT_FOO_STRUCT); }
|
||||
NamespaceA::NamespaceB::StructInNestedNS *mutable_foo_struct() { return GetStruct<NamespaceA::NamespaceB::StructInNestedNS *>(VT_FOO_STRUCT); }
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 4 /* foo_table */) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, VT_FOO_TABLE) &&
|
||||
verifier.VerifyTable(foo_table()) &&
|
||||
VerifyField<int8_t>(verifier, 6 /* foo_enum */) &&
|
||||
VerifyField<NamespaceA::NamespaceB::StructInNestedNS>(verifier, 8 /* foo_struct */) &&
|
||||
VerifyField<int8_t>(verifier, VT_FOO_ENUM) &&
|
||||
VerifyField<NamespaceA::NamespaceB::StructInNestedNS>(verifier, VT_FOO_STRUCT) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
@@ -36,9 +43,9 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
struct TableInFirstNSBuilder {
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_foo_table(flatbuffers::Offset<NamespaceA::NamespaceB::TableInNestedNS> foo_table) { fbb_.AddOffset(4, foo_table); }
|
||||
void add_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS foo_enum) { fbb_.AddElement<int8_t>(6, static_cast<int8_t>(foo_enum), 0); }
|
||||
void add_foo_struct(const NamespaceA::NamespaceB::StructInNestedNS *foo_struct) { fbb_.AddStruct(8, foo_struct); }
|
||||
void add_foo_table(flatbuffers::Offset<NamespaceA::NamespaceB::TableInNestedNS> foo_table) { fbb_.AddOffset(TableInFirstNS::VT_FOO_TABLE, foo_table); }
|
||||
void add_foo_enum(NamespaceA::NamespaceB::EnumInNestedNS foo_enum) { fbb_.AddElement<int8_t>(TableInFirstNS::VT_FOO_ENUM, static_cast<int8_t>(foo_enum), 0); }
|
||||
void add_foo_struct(const NamespaceA::NamespaceB::StructInNestedNS *foo_struct) { fbb_.AddStruct(TableInFirstNS::VT_FOO_STRUCT, foo_struct); }
|
||||
TableInFirstNSBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
TableInFirstNSBuilder &operator=(const TableInFirstNSBuilder &);
|
||||
flatbuffers::Offset<TableInFirstNS> Finish() {
|
||||
|
||||
115
tests/namespace_test/namespace_test2_generated.js
Normal file
115
tests/namespace_test/namespace_test2_generated.js
Normal file
@@ -0,0 +1,115 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/**
|
||||
* @const
|
||||
*/
|
||||
var NamespaceA = NamespaceA || {};
|
||||
|
||||
/**
|
||||
* @const
|
||||
*/
|
||||
NamespaceA.NamespaceB = NamespaceA.NamespaceB || {};
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
NamespaceA.TableInFirstNS = function() {
|
||||
/**
|
||||
* @type {flatbuffers.ByteBuffer}
|
||||
*/
|
||||
this.bb = null;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.bb_pos = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {number} i
|
||||
* @param {flatbuffers.ByteBuffer} bb
|
||||
* @returns {NamespaceA.TableInFirstNS}
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.prototype.__init = function(i, bb) {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.ByteBuffer} bb
|
||||
* @param {NamespaceA.TableInFirstNS=} obj
|
||||
* @returns {NamespaceA.TableInFirstNS}
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.getRootAsTableInFirstNS = function(bb, obj) {
|
||||
return (obj || new NamespaceA.TableInFirstNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {NamespaceA.NamespaceB.TableInNestedNS=} obj
|
||||
* @returns {NamespaceA.NamespaceB.TableInNestedNS}
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.prototype.fooTable = function(obj) {
|
||||
var offset = this.bb.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {NamespaceA.NamespaceB.EnumInNestedNS}
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.prototype.fooEnum = function() {
|
||||
var offset = this.bb.__offset(this.bb_pos, 6);
|
||||
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb.readInt8(this.bb_pos + offset)) : NamespaceA.NamespaceB.EnumInNestedNS.A;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {NamespaceA.NamespaceB.StructInNestedNS=} obj
|
||||
* @returns {NamespaceA.NamespaceB.StructInNestedNS}
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.prototype.fooStruct = function(obj) {
|
||||
var offset = this.bb.__offset(this.bb_pos, 8);
|
||||
return offset ? (obj || new NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.startTableInFirstNS = function(builder) {
|
||||
builder.startObject(3);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
* @param {flatbuffers.Offset} fooTableOffset
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.addFooTable = function(builder, fooTableOffset) {
|
||||
builder.addFieldOffset(0, fooTableOffset, 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
* @param {NamespaceA.NamespaceB.EnumInNestedNS} fooEnum
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.addFooEnum = function(builder, fooEnum) {
|
||||
builder.addFieldInt8(1, fooEnum, NamespaceA.NamespaceB.EnumInNestedNS.A);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
* @param {flatbuffers.Offset} fooStructOffset
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.addFooStruct = function(builder, fooStructOffset) {
|
||||
builder.addFieldStruct(2, fooStructOffset, 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {flatbuffers.Builder} builder
|
||||
* @returns {flatbuffers.Offset}
|
||||
*/
|
||||
NamespaceA.TableInFirstNS.endTableInFirstNS = function(builder) {
|
||||
var offset = builder.endObject();
|
||||
return offset;
|
||||
};
|
||||
|
||||
// Exports for Node.js and RequireJS
|
||||
this.NamespaceA = NamespaceA;
|
||||
@@ -21,6 +21,8 @@
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#include "monster_test_generated.h"
|
||||
#include "namespace_test/namespace_test1_generated.h"
|
||||
#include "namespace_test/namespace_test2_generated.h"
|
||||
|
||||
#include <random>
|
||||
|
||||
@@ -219,6 +221,10 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length) {
|
||||
for (auto it = tests->begin(); it != tests->end(); ++it) {
|
||||
TEST_EQ(it->a() == 10 || it->a() == 30, true); // Just testing iterators.
|
||||
}
|
||||
|
||||
// Checking for presence of fields:
|
||||
TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_HP), true);
|
||||
TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_MANA), false);
|
||||
}
|
||||
|
||||
// Change a FlatBuffer in-place, after it has been constructed.
|
||||
@@ -398,6 +404,11 @@ void ReflectionTest(uint8_t *flatbuf, size_t length) {
|
||||
rtestarrayofstring->MutateOffset(2, string_ptr);
|
||||
TEST_EQ_STR(rtestarrayofstring->Get(0)->c_str(), "bob");
|
||||
TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank");
|
||||
// Test integrity of all resize operations above.
|
||||
flatbuffers::Verifier resize_verifier(
|
||||
reinterpret_cast<const uint8_t *>(resizingbuf.data()),
|
||||
resizingbuf.size());
|
||||
TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
|
||||
// As an additional test, also set it on the name field.
|
||||
// Note: unlike the name change above, this just overwrites the offset,
|
||||
// rather than changing the string in-place.
|
||||
|
||||
Reference in New Issue
Block a user