mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-20 19:57:32 +00:00
Use the Google Style for clang-format without exceptions (#8706)
This reduces the friction when merging from github and google repos by using the exact same clang style guide. MARKDOWN=true
This commit is contained in:
@@ -28,21 +28,21 @@ class Allocator {
|
||||
virtual ~Allocator() {}
|
||||
|
||||
// Allocate `size` bytes of memory.
|
||||
virtual uint8_t *allocate(size_t size) = 0;
|
||||
virtual uint8_t* allocate(size_t size) = 0;
|
||||
|
||||
// Deallocate `size` bytes of memory at `p` allocated by this allocator.
|
||||
virtual void deallocate(uint8_t *p, size_t size) = 0;
|
||||
virtual void deallocate(uint8_t* p, size_t size) = 0;
|
||||
|
||||
// Reallocate `new_size` bytes of memory, replacing the old region of size
|
||||
// `old_size` at `p`. In contrast to a normal realloc, this grows downwards,
|
||||
// and is intended specifcally for `vector_downward` use.
|
||||
// `in_use_back` and `in_use_front` indicate how much of `old_size` is
|
||||
// actually in use at each end, and needs to be copied.
|
||||
virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
|
||||
virtual uint8_t* reallocate_downward(uint8_t* old_p, size_t old_size,
|
||||
size_t new_size, size_t in_use_back,
|
||||
size_t in_use_front) {
|
||||
FLATBUFFERS_ASSERT(new_size > old_size); // vector_downward only grows
|
||||
uint8_t *new_p = allocate(new_size);
|
||||
uint8_t* new_p = allocate(new_size);
|
||||
memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
|
||||
in_use_front);
|
||||
deallocate(old_p, old_size);
|
||||
@@ -54,7 +54,7 @@ class Allocator {
|
||||
// to `new_p` of `new_size`. Only memory of size `in_use_front` and
|
||||
// `in_use_back` will be copied from the front and back of the old memory
|
||||
// allocation.
|
||||
void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p,
|
||||
void memcpy_downward(uint8_t* old_p, size_t old_size, uint8_t* new_p,
|
||||
size_t new_size, size_t in_use_back,
|
||||
size_t in_use_front) {
|
||||
memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back,
|
||||
|
||||
@@ -27,7 +27,8 @@
|
||||
namespace flatbuffers {
|
||||
|
||||
// This is used as a helper type for accessing arrays.
|
||||
template<typename T, uint16_t length> class Array {
|
||||
template <typename T, uint16_t length>
|
||||
class Array {
|
||||
// Array<T> can carry only POD data types (scalars or structs).
|
||||
typedef typename flatbuffers::bool_constant<flatbuffers::is_scalar<T>::value>
|
||||
scalar_tag;
|
||||
@@ -55,7 +56,8 @@ template<typename T, uint16_t length> class Array {
|
||||
// If this is a Vector of enums, T will be its storage type, not the enum
|
||||
// type. This function makes it convenient to retrieve value with enum
|
||||
// type E.
|
||||
template<typename E> E GetEnum(uoffset_t i) const {
|
||||
template <typename E>
|
||||
E GetEnum(uoffset_t i) const {
|
||||
return static_cast<E>(Get(i));
|
||||
}
|
||||
|
||||
@@ -80,28 +82,28 @@ template<typename T, uint16_t length> class Array {
|
||||
// operation. For primitive types use @p Mutate directly.
|
||||
// @warning Assignments and reads to/from the dereferenced pointer are not
|
||||
// automatically converted to the correct endianness.
|
||||
typename flatbuffers::conditional<scalar_tag::value, void, T *>::type
|
||||
typename flatbuffers::conditional<scalar_tag::value, void, T*>::type
|
||||
GetMutablePointer(uoffset_t i) const {
|
||||
FLATBUFFERS_ASSERT(i < size());
|
||||
return const_cast<T *>(&data()[i]);
|
||||
return const_cast<T*>(&data()[i]);
|
||||
}
|
||||
|
||||
// Change elements if you have a non-const pointer to this object.
|
||||
void Mutate(uoffset_t i, const T &val) { MutateImpl(scalar_tag(), i, val); }
|
||||
void Mutate(uoffset_t i, const T& val) { MutateImpl(scalar_tag(), i, val); }
|
||||
|
||||
// The raw data in little endian format. Use with care.
|
||||
const uint8_t *Data() const { return data_; }
|
||||
const uint8_t* Data() const { return data_; }
|
||||
|
||||
uint8_t *Data() { return data_; }
|
||||
uint8_t* Data() { return data_; }
|
||||
|
||||
// Similarly, but typed, much like std::vector::data
|
||||
const T *data() const { return reinterpret_cast<const T *>(Data()); }
|
||||
T *data() { return reinterpret_cast<T *>(Data()); }
|
||||
const T* data() const { return reinterpret_cast<const T*>(Data()); }
|
||||
T* data() { return reinterpret_cast<T*>(Data()); }
|
||||
|
||||
// Copy data from a span with endian conversion.
|
||||
// If this Array and the span overlap, the behavior is undefined.
|
||||
void CopyFromSpan(flatbuffers::span<const T, length> src) {
|
||||
const auto p1 = reinterpret_cast<const uint8_t *>(src.data());
|
||||
const auto p1 = reinterpret_cast<const uint8_t*>(src.data());
|
||||
const auto p2 = Data();
|
||||
FLATBUFFERS_ASSERT(!(p1 >= p2 && p1 < (p2 + length)) &&
|
||||
!(p2 >= p1 && p2 < (p1 + length)));
|
||||
@@ -111,12 +113,12 @@ template<typename T, uint16_t length> class Array {
|
||||
}
|
||||
|
||||
protected:
|
||||
void MutateImpl(flatbuffers::true_type, uoffset_t i, const T &val) {
|
||||
void MutateImpl(flatbuffers::true_type, uoffset_t i, const T& val) {
|
||||
FLATBUFFERS_ASSERT(i < size());
|
||||
WriteScalar(data() + i, val);
|
||||
}
|
||||
|
||||
void MutateImpl(flatbuffers::false_type, uoffset_t i, const T &val) {
|
||||
void MutateImpl(flatbuffers::false_type, uoffset_t i, const T& val) {
|
||||
*(GetMutablePointer(i)) = val;
|
||||
}
|
||||
|
||||
@@ -131,7 +133,9 @@ template<typename T, uint16_t length> class Array {
|
||||
// Copy data from flatbuffers::span with endian conversion.
|
||||
void CopyFromSpanImpl(flatbuffers::false_type,
|
||||
flatbuffers::span<const T, length> src) {
|
||||
for (size_type k = 0; k < length; k++) { Mutate(k, src[k]); }
|
||||
for (size_type k = 0; k < length; k++) {
|
||||
Mutate(k, src[k]);
|
||||
}
|
||||
}
|
||||
|
||||
// This class is only used to access pre-existing data. Don't ever
|
||||
@@ -150,21 +154,21 @@ template<typename T, uint16_t length> class Array {
|
||||
private:
|
||||
// This class is a pointer. Copying will therefore create an invalid object.
|
||||
// Private and unimplemented copy constructor.
|
||||
Array(const Array &);
|
||||
Array &operator=(const Array &);
|
||||
Array(const Array&);
|
||||
Array& operator=(const Array&);
|
||||
};
|
||||
|
||||
// Specialization for Array[struct] with access using Offset<void> pointer.
|
||||
// This specialization used by idl_gen_text.cpp.
|
||||
template<typename T, uint16_t length, template<typename> class OffsetT>
|
||||
template <typename T, uint16_t length, template <typename> class OffsetT>
|
||||
class Array<OffsetT<T>, length> {
|
||||
static_assert(flatbuffers::is_same<T, void>::value, "unexpected type T");
|
||||
|
||||
public:
|
||||
typedef const void *return_type;
|
||||
typedef const void* return_type;
|
||||
typedef uint16_t size_type;
|
||||
|
||||
const uint8_t *Data() const { return data_; }
|
||||
const uint8_t* Data() const { return data_; }
|
||||
|
||||
// Make idl_gen_text.cpp::PrintContainer happy.
|
||||
return_type operator[](uoffset_t) const {
|
||||
@@ -175,14 +179,14 @@ class Array<OffsetT<T>, length> {
|
||||
private:
|
||||
// This class is only used to access pre-existing data.
|
||||
Array();
|
||||
Array(const Array &);
|
||||
Array &operator=(const Array &);
|
||||
Array(const Array&);
|
||||
Array& operator=(const Array&);
|
||||
|
||||
uint8_t data_[1];
|
||||
};
|
||||
|
||||
template<class U, uint16_t N>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U, N> make_span(Array<U, N> &arr)
|
||||
template <class U, uint16_t N>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U, N> make_span(Array<U, N>& arr)
|
||||
FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(
|
||||
Array<U, N>::is_span_observable,
|
||||
@@ -190,26 +194,26 @@ FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U, N> make_span(Array<U, N> &arr)
|
||||
return span<U, N>(arr.data(), N);
|
||||
}
|
||||
|
||||
template<class U, uint16_t N>
|
||||
template <class U, uint16_t N>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const U, N> make_span(
|
||||
const Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
|
||||
const Array<U, N>& arr) FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(
|
||||
Array<U, N>::is_span_observable,
|
||||
"wrong type U, only plain struct, LE-scalar, or byte types are allowed");
|
||||
return span<const U, N>(arr.data(), N);
|
||||
}
|
||||
|
||||
template<class U, uint16_t N>
|
||||
template <class U, uint16_t N>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<uint8_t, sizeof(U) * N>
|
||||
make_bytes_span(Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
|
||||
make_bytes_span(Array<U, N>& arr) FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Array<U, N>::is_span_observable,
|
||||
"internal error, Array<T> might hold only scalars or structs");
|
||||
return span<uint8_t, sizeof(U) * N>(arr.Data(), sizeof(U) * N);
|
||||
}
|
||||
|
||||
template<class U, uint16_t N>
|
||||
template <class U, uint16_t N>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const uint8_t, sizeof(U) * N>
|
||||
make_bytes_span(const Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
|
||||
make_bytes_span(const Array<U, N>& arr) FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Array<U, N>::is_span_observable,
|
||||
"internal error, Array<T> might hold only scalars or structs");
|
||||
return span<const uint8_t, sizeof(U) * N>(arr.Data(), sizeof(U) * N);
|
||||
@@ -218,31 +222,31 @@ make_bytes_span(const Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
|
||||
// Cast a raw T[length] to a raw flatbuffers::Array<T, length>
|
||||
// without endian conversion. Use with care.
|
||||
// TODO: move these Cast-methods to `internal` namespace.
|
||||
template<typename T, uint16_t length>
|
||||
Array<T, length> &CastToArray(T (&arr)[length]) {
|
||||
return *reinterpret_cast<Array<T, length> *>(arr);
|
||||
template <typename T, uint16_t length>
|
||||
Array<T, length>& CastToArray(T (&arr)[length]) {
|
||||
return *reinterpret_cast<Array<T, length>*>(arr);
|
||||
}
|
||||
|
||||
template<typename T, uint16_t length>
|
||||
const Array<T, length> &CastToArray(const T (&arr)[length]) {
|
||||
return *reinterpret_cast<const Array<T, length> *>(arr);
|
||||
template <typename T, uint16_t length>
|
||||
const Array<T, length>& CastToArray(const T (&arr)[length]) {
|
||||
return *reinterpret_cast<const Array<T, length>*>(arr);
|
||||
}
|
||||
|
||||
template<typename E, typename T, uint16_t length>
|
||||
Array<E, length> &CastToArrayOfEnum(T (&arr)[length]) {
|
||||
template <typename E, typename T, uint16_t length>
|
||||
Array<E, length>& CastToArrayOfEnum(T (&arr)[length]) {
|
||||
static_assert(sizeof(E) == sizeof(T), "invalid enum type E");
|
||||
return *reinterpret_cast<Array<E, length> *>(arr);
|
||||
return *reinterpret_cast<Array<E, length>*>(arr);
|
||||
}
|
||||
|
||||
template<typename E, typename T, uint16_t length>
|
||||
const Array<E, length> &CastToArrayOfEnum(const T (&arr)[length]) {
|
||||
template <typename E, typename T, uint16_t length>
|
||||
const Array<E, length>& CastToArrayOfEnum(const T (&arr)[length]) {
|
||||
static_assert(sizeof(E) == sizeof(T), "invalid enum type E");
|
||||
return *reinterpret_cast<const Array<E, length> *>(arr);
|
||||
return *reinterpret_cast<const Array<E, length>*>(arr);
|
||||
}
|
||||
|
||||
template<typename T, uint16_t length>
|
||||
bool operator==(const Array<T, length> &lhs,
|
||||
const Array<T, length> &rhs) noexcept {
|
||||
template <typename T, uint16_t length>
|
||||
bool operator==(const Array<T, length>& lhs,
|
||||
const Array<T, length>& rhs) noexcept {
|
||||
return std::addressof(lhs) == std::addressof(rhs) ||
|
||||
(lhs.size() == rhs.size() &&
|
||||
std::memcmp(lhs.Data(), rhs.Data(), rhs.size() * sizeof(T)) == 0);
|
||||
|
||||
@@ -26,7 +26,8 @@ namespace flatbuffers {
|
||||
|
||||
// Wrapper for uoffset_t to allow safe template specialization.
|
||||
// Value is allowed to be 0 to indicate a null object (see e.g. AddOffset).
|
||||
template<typename T = void> struct Offset {
|
||||
template <typename T = void>
|
||||
struct Offset {
|
||||
// The type of offset to use.
|
||||
typedef uoffset_t offset_type;
|
||||
|
||||
@@ -37,12 +38,14 @@ template<typename T = void> struct Offset {
|
||||
bool IsNull() const { return !o; }
|
||||
};
|
||||
|
||||
template<typename T> struct is_specialisation_of_Offset : false_type {};
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
struct is_specialisation_of_Offset : false_type {};
|
||||
template <typename T>
|
||||
struct is_specialisation_of_Offset<Offset<T>> : true_type {};
|
||||
|
||||
// Wrapper for uoffset64_t Offsets.
|
||||
template<typename T = void> struct Offset64 {
|
||||
template <typename T = void>
|
||||
struct Offset64 {
|
||||
// The type of offset to use.
|
||||
typedef uoffset64_t offset_type;
|
||||
|
||||
@@ -53,8 +56,9 @@ template<typename T = void> struct Offset64 {
|
||||
bool IsNull() const { return !o; }
|
||||
};
|
||||
|
||||
template<typename T> struct is_specialisation_of_Offset64 : false_type {};
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
struct is_specialisation_of_Offset64 : false_type {};
|
||||
template <typename T>
|
||||
struct is_specialisation_of_Offset64<Offset64<T>> : true_type {};
|
||||
|
||||
// Litmus check for ensuring the Offsets are the expected size.
|
||||
@@ -64,12 +68,13 @@ static_assert(sizeof(Offset64<>) == 8, "Offset64 has wrong size");
|
||||
inline void EndianCheck() {
|
||||
int endiantest = 1;
|
||||
// If this fails, see FLATBUFFERS_LITTLEENDIAN above.
|
||||
FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
|
||||
FLATBUFFERS_ASSERT(*reinterpret_cast<char*>(&endiantest) ==
|
||||
FLATBUFFERS_LITTLEENDIAN);
|
||||
(void)endiantest;
|
||||
}
|
||||
|
||||
template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() {
|
||||
template <typename T>
|
||||
FLATBUFFERS_CONSTEXPR size_t AlignOf() {
|
||||
// clang-format off
|
||||
#ifdef _MSC_VER
|
||||
return __alignof(T);
|
||||
@@ -85,8 +90,8 @@ template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() {
|
||||
|
||||
// Lexicographically compare two strings (possibly containing nulls), and
|
||||
// return true if the first is less than the second.
|
||||
static inline bool StringLessThan(const char *a_data, uoffset_t a_size,
|
||||
const char *b_data, uoffset_t b_size) {
|
||||
static inline bool StringLessThan(const char* a_data, uoffset_t a_size,
|
||||
const char* b_data, uoffset_t b_size) {
|
||||
const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
|
||||
return cmp == 0 ? a_size < b_size : cmp < 0;
|
||||
}
|
||||
@@ -99,42 +104,43 @@ static inline bool StringLessThan(const char *a_data, uoffset_t a_size,
|
||||
// return type like this.
|
||||
// The typedef is for the convenience of callers of this function
|
||||
// (avoiding the need for a trailing return decltype)
|
||||
template<typename T, typename Enable = void> struct IndirectHelper {
|
||||
template <typename T, typename Enable = void>
|
||||
struct IndirectHelper {
|
||||
typedef T return_type;
|
||||
typedef T mutable_return_type;
|
||||
static const size_t element_stride = sizeof(T);
|
||||
|
||||
static return_type Read(const uint8_t *p, const size_t i) {
|
||||
return EndianScalar((reinterpret_cast<const T *>(p))[i]);
|
||||
static return_type Read(const uint8_t* p, const size_t i) {
|
||||
return EndianScalar((reinterpret_cast<const T*>(p))[i]);
|
||||
}
|
||||
static mutable_return_type Read(uint8_t *p, const size_t i) {
|
||||
static mutable_return_type Read(uint8_t* p, const size_t i) {
|
||||
return reinterpret_cast<mutable_return_type>(
|
||||
Read(const_cast<const uint8_t *>(p), i));
|
||||
Read(const_cast<const uint8_t*>(p), i));
|
||||
}
|
||||
};
|
||||
|
||||
// For vector of Offsets.
|
||||
template<typename T, template<typename> class OffsetT>
|
||||
template <typename T, template <typename> class OffsetT>
|
||||
struct IndirectHelper<OffsetT<T>> {
|
||||
typedef const T *return_type;
|
||||
typedef T *mutable_return_type;
|
||||
typedef const T* return_type;
|
||||
typedef T* mutable_return_type;
|
||||
typedef typename OffsetT<T>::offset_type offset_type;
|
||||
static const offset_type element_stride = sizeof(offset_type);
|
||||
|
||||
static return_type Read(const uint8_t *const p, const offset_type i) {
|
||||
static return_type Read(const uint8_t* const p, const offset_type i) {
|
||||
// Offsets are relative to themselves, so first update the pointer to
|
||||
// point to the offset location.
|
||||
const uint8_t *const offset_location = p + i * element_stride;
|
||||
const uint8_t* const offset_location = p + i * element_stride;
|
||||
|
||||
// Then read the scalar value of the offset (which may be 32 or 64-bits) and
|
||||
// then determine the relative location from the offset location.
|
||||
return reinterpret_cast<return_type>(
|
||||
offset_location + ReadScalar<offset_type>(offset_location));
|
||||
}
|
||||
static mutable_return_type Read(uint8_t *const p, const offset_type i) {
|
||||
static mutable_return_type Read(uint8_t* const p, const offset_type i) {
|
||||
// Offsets are relative to themselves, so first update the pointer to
|
||||
// point to the offset location.
|
||||
uint8_t *const offset_location = p + i * element_stride;
|
||||
uint8_t* const offset_location = p + i * element_stride;
|
||||
|
||||
// Then read the scalar value of the offset (which may be 32 or 64-bits) and
|
||||
// then determine the relative location from the offset location.
|
||||
@@ -144,7 +150,7 @@ struct IndirectHelper<OffsetT<T>> {
|
||||
};
|
||||
|
||||
// For vector of structs.
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
struct IndirectHelper<
|
||||
T, typename std::enable_if<
|
||||
!std::is_scalar<typename std::remove_pointer<T>::type>::value &&
|
||||
@@ -155,15 +161,15 @@ struct IndirectHelper<
|
||||
pointee_type;
|
||||
|
||||
public:
|
||||
typedef const pointee_type *return_type;
|
||||
typedef pointee_type *mutable_return_type;
|
||||
typedef const pointee_type* return_type;
|
||||
typedef pointee_type* mutable_return_type;
|
||||
static const size_t element_stride = sizeof(pointee_type);
|
||||
|
||||
static return_type Read(const uint8_t *const p, const size_t i) {
|
||||
static return_type Read(const uint8_t* const p, const size_t i) {
|
||||
// Structs are stored inline, relative to the first struct pointer.
|
||||
return reinterpret_cast<return_type>(p + i * element_stride);
|
||||
}
|
||||
static mutable_return_type Read(uint8_t *const p, const size_t i) {
|
||||
static mutable_return_type Read(uint8_t* const p, const size_t i) {
|
||||
// Structs are stored inline, relative to the first struct pointer.
|
||||
return reinterpret_cast<mutable_return_type>(p + i * element_stride);
|
||||
}
|
||||
@@ -176,14 +182,14 @@ struct IndirectHelper<
|
||||
/// This function is UNDEFINED for FlatBuffers whose schema does not include
|
||||
/// a file_identifier (likely points at padding or the start of a the root
|
||||
/// vtable).
|
||||
inline const char *GetBufferIdentifier(const void *buf,
|
||||
inline const char* GetBufferIdentifier(const void* buf,
|
||||
bool size_prefixed = false) {
|
||||
return reinterpret_cast<const char *>(buf) +
|
||||
return reinterpret_cast<const char*>(buf) +
|
||||
((size_prefixed) ? 2 * sizeof(uoffset_t) : sizeof(uoffset_t));
|
||||
}
|
||||
|
||||
// Helper to see if the identifier in a buffer has the expected value.
|
||||
inline bool BufferHasIdentifier(const void *buf, const char *identifier,
|
||||
inline bool BufferHasIdentifier(const void* buf, const char* identifier,
|
||||
bool size_prefixed = false) {
|
||||
return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
|
||||
flatbuffers::kFileIdentifierLength) == 0;
|
||||
@@ -191,26 +197,27 @@ inline bool BufferHasIdentifier(const void *buf, const char *identifier,
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
// Helpers to get a typed pointer to the root object contained in the buffer.
|
||||
template<typename T> T *GetMutableRoot(void *buf) {
|
||||
template <typename T>
|
||||
T* GetMutableRoot(void* buf) {
|
||||
if (!buf) return nullptr;
|
||||
EndianCheck();
|
||||
return reinterpret_cast<T *>(
|
||||
reinterpret_cast<uint8_t *>(buf) +
|
||||
EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
|
||||
return reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(buf) +
|
||||
EndianScalar(*reinterpret_cast<uoffset_t*>(buf)));
|
||||
}
|
||||
|
||||
template<typename T, typename SizeT = uoffset_t>
|
||||
T *GetMutableSizePrefixedRoot(void *buf) {
|
||||
return GetMutableRoot<T>(reinterpret_cast<uint8_t *>(buf) + sizeof(SizeT));
|
||||
template <typename T, typename SizeT = uoffset_t>
|
||||
T* GetMutableSizePrefixedRoot(void* buf) {
|
||||
return GetMutableRoot<T>(reinterpret_cast<uint8_t*>(buf) + sizeof(SizeT));
|
||||
}
|
||||
|
||||
template<typename T> const T *GetRoot(const void *buf) {
|
||||
return GetMutableRoot<T>(const_cast<void *>(buf));
|
||||
template <typename T>
|
||||
const T* GetRoot(const void* buf) {
|
||||
return GetMutableRoot<T>(const_cast<void*>(buf));
|
||||
}
|
||||
|
||||
template<typename T, typename SizeT = uoffset_t>
|
||||
const T *GetSizePrefixedRoot(const void *buf) {
|
||||
return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(SizeT));
|
||||
template <typename T, typename SizeT = uoffset_t>
|
||||
const T* GetSizePrefixedRoot(const void* buf) {
|
||||
return GetRoot<T>(reinterpret_cast<const uint8_t*>(buf) + sizeof(SizeT));
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -27,23 +27,24 @@ namespace flatbuffers {
|
||||
// A BufferRef does not own its buffer.
|
||||
struct BufferRefBase {}; // for std::is_base_of
|
||||
|
||||
template<typename T> struct BufferRef : BufferRefBase {
|
||||
template <typename T>
|
||||
struct BufferRef : BufferRefBase {
|
||||
BufferRef() : buf(nullptr), len(0), must_free(false) {}
|
||||
BufferRef(uint8_t *_buf, uoffset_t _len)
|
||||
BufferRef(uint8_t* _buf, uoffset_t _len)
|
||||
: buf(_buf), len(_len), must_free(false) {}
|
||||
|
||||
~BufferRef() {
|
||||
if (must_free) free(buf);
|
||||
}
|
||||
|
||||
const T *GetRoot() const { return flatbuffers::GetRoot<T>(buf); }
|
||||
const T* GetRoot() const { return flatbuffers::GetRoot<T>(buf); }
|
||||
|
||||
bool Verify() {
|
||||
Verifier verifier(buf, len);
|
||||
return verifier.VerifyBuffer<T>(nullptr);
|
||||
}
|
||||
|
||||
uint8_t *buf;
|
||||
uint8_t* buf;
|
||||
uoffset_t len;
|
||||
bool must_free;
|
||||
};
|
||||
|
||||
@@ -45,13 +45,13 @@ class CodeGenerator {
|
||||
// Generate code from the provided `parser`.
|
||||
//
|
||||
// DEPRECATED: prefer using the other overload of GenerateCode for bfbs.
|
||||
virtual Status GenerateCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) = 0;
|
||||
virtual Status GenerateCode(const Parser& parser, const std::string& path,
|
||||
const std::string& filename) = 0;
|
||||
|
||||
// Generate code from the provided `parser` and place it in the output.
|
||||
virtual Status GenerateCodeString(const Parser &parser,
|
||||
const std::string &filename,
|
||||
std::string &output) {
|
||||
virtual Status GenerateCodeString(const Parser& parser,
|
||||
const std::string& filename,
|
||||
std::string& output) {
|
||||
(void)parser;
|
||||
(void)filename;
|
||||
(void)output;
|
||||
@@ -60,18 +60,18 @@ class CodeGenerator {
|
||||
|
||||
// Generate code from the provided `buffer` of given `length`. The buffer is a
|
||||
// serialized reflection.fbs.
|
||||
virtual Status GenerateCode(const uint8_t *buffer, int64_t length,
|
||||
const CodeGenOptions &options) = 0;
|
||||
virtual Status GenerateCode(const uint8_t* buffer, int64_t length,
|
||||
const CodeGenOptions& options) = 0;
|
||||
|
||||
virtual Status GenerateMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &filename,
|
||||
std::string &output) = 0;
|
||||
virtual Status GenerateMakeRule(const Parser& parser, const std::string& path,
|
||||
const std::string& filename,
|
||||
std::string& output) = 0;
|
||||
|
||||
virtual Status GenerateGrpcCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) = 0;
|
||||
virtual Status GenerateGrpcCode(const Parser& parser, const std::string& path,
|
||||
const std::string& filename) = 0;
|
||||
|
||||
virtual Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) = 0;
|
||||
virtual Status GenerateRootFile(const Parser& parser,
|
||||
const std::string& path) = 0;
|
||||
|
||||
virtual bool IsSchemaOnly() const = 0;
|
||||
|
||||
@@ -88,8 +88,8 @@ class CodeGenerator {
|
||||
|
||||
private:
|
||||
// Copying is not supported.
|
||||
CodeGenerator(const CodeGenerator &) = delete;
|
||||
CodeGenerator &operator=(const CodeGenerator &) = delete;
|
||||
CodeGenerator(const CodeGenerator&) = delete;
|
||||
CodeGenerator& operator=(const CodeGenerator&) = delete;
|
||||
};
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -51,11 +51,11 @@ 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;
|
||||
}
|
||||
|
||||
std::string GetValue(const std::string &key) const {
|
||||
std::string GetValue(const std::string& key) const {
|
||||
const auto it = value_map_.find(key);
|
||||
return it == value_map_.end() ? "" : it->second;
|
||||
}
|
||||
@@ -76,7 +76,7 @@ class CodeWriter {
|
||||
if (cur_ident_lvl_) cur_ident_lvl_--;
|
||||
}
|
||||
|
||||
void SetPadding(const std::string &padding) { pad_ = padding; }
|
||||
void SetPadding(const std::string& padding) { pad_ = padding; }
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> value_map_;
|
||||
@@ -86,24 +86,24 @@ class CodeWriter {
|
||||
bool ignore_ident_;
|
||||
|
||||
// Add ident padding (tab or space) based on ident level
|
||||
void AppendIdent(std::stringstream &stream);
|
||||
void AppendIdent(std::stringstream& stream);
|
||||
};
|
||||
|
||||
class BaseGenerator {
|
||||
public:
|
||||
virtual bool generate() = 0;
|
||||
|
||||
static std::string NamespaceDir(const Parser &parser, const std::string &path,
|
||||
const Namespace &ns,
|
||||
static std::string NamespaceDir(const Parser& parser, const std::string& path,
|
||||
const Namespace& ns,
|
||||
const bool dasherize = false);
|
||||
|
||||
std::string GeneratedFileName(const std::string &path,
|
||||
const std::string &file_name,
|
||||
const IDLOptions &options) const;
|
||||
std::string GeneratedFileName(const std::string& path,
|
||||
const std::string& file_name,
|
||||
const IDLOptions& options) const;
|
||||
|
||||
protected:
|
||||
BaseGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name, std::string qualifying_start,
|
||||
BaseGenerator(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name, std::string qualifying_start,
|
||||
std::string qualifying_separator, std::string default_extension)
|
||||
: parser_(parser),
|
||||
path_(path),
|
||||
@@ -114,84 +114,84 @@ class BaseGenerator {
|
||||
virtual ~BaseGenerator() {}
|
||||
|
||||
// No copy/assign.
|
||||
BaseGenerator &operator=(const BaseGenerator &);
|
||||
BaseGenerator(const BaseGenerator &);
|
||||
BaseGenerator& operator=(const BaseGenerator&);
|
||||
BaseGenerator(const BaseGenerator&);
|
||||
|
||||
std::string NamespaceDir(const Namespace &ns,
|
||||
std::string NamespaceDir(const Namespace& ns,
|
||||
const bool dasherize = false) const;
|
||||
|
||||
static const char *FlatBuffersGeneratedWarning();
|
||||
static const char* FlatBuffersGeneratedWarning();
|
||||
|
||||
static std::string FullNamespace(const char *separator, const Namespace &ns);
|
||||
static std::string FullNamespace(const char* separator, const Namespace& ns);
|
||||
|
||||
static std::string LastNamespacePart(const Namespace &ns);
|
||||
static std::string LastNamespacePart(const Namespace& ns);
|
||||
|
||||
// tracks the current namespace for early exit in WrapInNameSpace
|
||||
// c++, java and csharp returns a different namespace from
|
||||
// the following default (no early exit, always fully qualify),
|
||||
// which works for js and php
|
||||
virtual const Namespace *CurrentNameSpace() const { return nullptr; }
|
||||
virtual const Namespace* CurrentNameSpace() const { return nullptr; }
|
||||
|
||||
// Ensure that a type is prefixed with its namespace even within
|
||||
// its own namespace to avoid conflict between generated method
|
||||
// names and similarly named classes or structs
|
||||
std::string WrapInNameSpace(const Namespace *ns,
|
||||
const std::string &name) const;
|
||||
std::string WrapInNameSpace(const Namespace* ns,
|
||||
const std::string& name) const;
|
||||
|
||||
std::string WrapInNameSpace(const Definition &def,
|
||||
const std::string &suffix = "") const;
|
||||
std::string WrapInNameSpace(const Definition& def,
|
||||
const std::string& suffix = "") const;
|
||||
|
||||
std::string GetNameSpace(const Definition &def) const;
|
||||
std::string GetNameSpace(const Definition& def) const;
|
||||
|
||||
const Parser &parser_;
|
||||
const std::string &path_;
|
||||
const std::string &file_name_;
|
||||
const Parser& parser_;
|
||||
const std::string& path_;
|
||||
const std::string& file_name_;
|
||||
const std::string qualifying_start_;
|
||||
const std::string qualifying_separator_;
|
||||
const std::string default_extension_;
|
||||
};
|
||||
|
||||
struct CommentConfig {
|
||||
const char *first_line;
|
||||
const char *content_line_prefix;
|
||||
const char *last_line;
|
||||
const char* first_line;
|
||||
const char* content_line_prefix;
|
||||
const char* last_line;
|
||||
};
|
||||
|
||||
extern void GenComment(const std::vector<std::string> &dc,
|
||||
std::string *code_ptr, const CommentConfig *config,
|
||||
const char *prefix = "");
|
||||
extern void GenComment(const std::vector<std::string>& dc,
|
||||
std::string* code_ptr, const CommentConfig* config,
|
||||
const char* prefix = "");
|
||||
|
||||
class FloatConstantGenerator {
|
||||
public:
|
||||
virtual ~FloatConstantGenerator() {}
|
||||
std::string GenFloatConstant(const FieldDef &field) const;
|
||||
std::string GenFloatConstant(const FieldDef& field) const;
|
||||
|
||||
private:
|
||||
virtual std::string Value(double v, const std::string &src) const = 0;
|
||||
virtual std::string Value(double v, const std::string& src) const = 0;
|
||||
virtual std::string Inf(double v) const = 0;
|
||||
virtual std::string NaN(double v) const = 0;
|
||||
|
||||
virtual std::string Value(float v, const std::string &src) const = 0;
|
||||
virtual std::string Value(float v, const std::string& src) const = 0;
|
||||
virtual std::string Inf(float v) const = 0;
|
||||
virtual std::string NaN(float v) const = 0;
|
||||
|
||||
template<typename T>
|
||||
std::string GenFloatConstantImpl(const FieldDef &field) const;
|
||||
template <typename T>
|
||||
std::string GenFloatConstantImpl(const FieldDef& field) const;
|
||||
};
|
||||
|
||||
class SimpleFloatConstantGenerator : public FloatConstantGenerator {
|
||||
public:
|
||||
SimpleFloatConstantGenerator(const char *nan_number,
|
||||
const char *pos_inf_number,
|
||||
const char *neg_inf_number);
|
||||
SimpleFloatConstantGenerator(const char* nan_number,
|
||||
const char* pos_inf_number,
|
||||
const char* neg_inf_number);
|
||||
|
||||
private:
|
||||
std::string Value(double v,
|
||||
const std::string &src) const FLATBUFFERS_OVERRIDE;
|
||||
const std::string& src) const FLATBUFFERS_OVERRIDE;
|
||||
std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
|
||||
std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
|
||||
|
||||
std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
|
||||
std::string Value(float v, const std::string& src) const FLATBUFFERS_OVERRIDE;
|
||||
std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
|
||||
std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
|
||||
|
||||
@@ -203,24 +203,24 @@ class SimpleFloatConstantGenerator : public FloatConstantGenerator {
|
||||
// C++, C#, Java like generator.
|
||||
class TypedFloatConstantGenerator : public FloatConstantGenerator {
|
||||
public:
|
||||
TypedFloatConstantGenerator(const char *double_prefix,
|
||||
const char *single_prefix, const char *nan_number,
|
||||
const char *pos_inf_number,
|
||||
const char *neg_inf_number = "");
|
||||
TypedFloatConstantGenerator(const char* double_prefix,
|
||||
const char* single_prefix, const char* nan_number,
|
||||
const char* pos_inf_number,
|
||||
const char* neg_inf_number = "");
|
||||
|
||||
private:
|
||||
std::string Value(double v,
|
||||
const std::string &src) const FLATBUFFERS_OVERRIDE;
|
||||
const std::string& src) const FLATBUFFERS_OVERRIDE;
|
||||
std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
|
||||
|
||||
std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
|
||||
|
||||
std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
|
||||
std::string Value(float v, const std::string& src) const FLATBUFFERS_OVERRIDE;
|
||||
std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
|
||||
std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
|
||||
|
||||
std::string MakeNaN(const std::string &prefix) const;
|
||||
std::string MakeInf(bool neg, const std::string &prefix) const;
|
||||
std::string MakeNaN(const std::string& prefix) const;
|
||||
std::string MakeInf(bool neg, const std::string& prefix) const;
|
||||
|
||||
const std::string double_prefix_;
|
||||
const std::string single_prefix_;
|
||||
@@ -229,9 +229,9 @@ class TypedFloatConstantGenerator : public FloatConstantGenerator {
|
||||
const std::string neg_inf_number_;
|
||||
};
|
||||
|
||||
std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
std::string JavaCSharpMakeRule(const bool java, const Parser& parser,
|
||||
const std::string& path,
|
||||
const std::string& file_name);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
||||
@@ -25,32 +25,32 @@ namespace flatbuffers {
|
||||
// DefaultAllocator uses new/delete to allocate memory regions
|
||||
class DefaultAllocator : public Allocator {
|
||||
public:
|
||||
uint8_t *allocate(size_t size) FLATBUFFERS_OVERRIDE {
|
||||
uint8_t* allocate(size_t size) FLATBUFFERS_OVERRIDE {
|
||||
return new uint8_t[size];
|
||||
}
|
||||
|
||||
void deallocate(uint8_t *p, size_t) FLATBUFFERS_OVERRIDE { delete[] p; }
|
||||
void deallocate(uint8_t* p, size_t) FLATBUFFERS_OVERRIDE { delete[] p; }
|
||||
|
||||
static void dealloc(void *p, size_t) { delete[] static_cast<uint8_t *>(p); }
|
||||
static void dealloc(void* p, size_t) { delete[] static_cast<uint8_t*>(p); }
|
||||
};
|
||||
|
||||
// These functions allow for a null allocator to mean use the default allocator,
|
||||
// as used by DetachedBuffer and vector_downward below.
|
||||
// This is to avoid having a statically or dynamically allocated default
|
||||
// allocator, or having to move it between the classes that may own it.
|
||||
inline uint8_t *Allocate(Allocator *allocator, size_t size) {
|
||||
inline uint8_t* Allocate(Allocator* allocator, size_t size) {
|
||||
return allocator ? allocator->allocate(size)
|
||||
: DefaultAllocator().allocate(size);
|
||||
}
|
||||
|
||||
inline void Deallocate(Allocator *allocator, uint8_t *p, size_t size) {
|
||||
inline void Deallocate(Allocator* allocator, uint8_t* p, size_t size) {
|
||||
if (allocator)
|
||||
allocator->deallocate(p, size);
|
||||
else
|
||||
DefaultAllocator().deallocate(p, size);
|
||||
}
|
||||
|
||||
inline uint8_t *ReallocateDownward(Allocator *allocator, uint8_t *old_p,
|
||||
inline uint8_t* ReallocateDownward(Allocator* allocator, uint8_t* old_p,
|
||||
size_t old_size, size_t new_size,
|
||||
size_t in_use_back, size_t in_use_front) {
|
||||
return allocator ? allocator->reallocate_downward(old_p, old_size, new_size,
|
||||
|
||||
@@ -36,8 +36,8 @@ class DetachedBuffer {
|
||||
cur_(nullptr),
|
||||
size_(0) {}
|
||||
|
||||
DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf,
|
||||
size_t reserved, uint8_t *cur, size_t sz)
|
||||
DetachedBuffer(Allocator* allocator, bool own_allocator, uint8_t* buf,
|
||||
size_t reserved, uint8_t* cur, size_t sz)
|
||||
: allocator_(allocator),
|
||||
own_allocator_(own_allocator),
|
||||
buf_(buf),
|
||||
@@ -45,7 +45,7 @@ class DetachedBuffer {
|
||||
cur_(cur),
|
||||
size_(sz) {}
|
||||
|
||||
DetachedBuffer(DetachedBuffer &&other) noexcept
|
||||
DetachedBuffer(DetachedBuffer&& other) noexcept
|
||||
: allocator_(other.allocator_),
|
||||
own_allocator_(other.own_allocator_),
|
||||
buf_(other.buf_),
|
||||
@@ -55,7 +55,7 @@ class DetachedBuffer {
|
||||
other.reset();
|
||||
}
|
||||
|
||||
DetachedBuffer &operator=(DetachedBuffer &&other) noexcept {
|
||||
DetachedBuffer& operator=(DetachedBuffer&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
|
||||
destroy();
|
||||
@@ -74,33 +74,35 @@ class DetachedBuffer {
|
||||
|
||||
~DetachedBuffer() { destroy(); }
|
||||
|
||||
const uint8_t *data() const { return cur_; }
|
||||
const uint8_t* data() const { return cur_; }
|
||||
|
||||
uint8_t *data() { return cur_; }
|
||||
uint8_t* data() { return cur_; }
|
||||
|
||||
size_t size() const { return size_; }
|
||||
|
||||
uint8_t *begin() { return data(); }
|
||||
const uint8_t *begin() const { return data(); }
|
||||
uint8_t *end() { return data() + size(); }
|
||||
const uint8_t *end() const { return data() + size(); }
|
||||
uint8_t* begin() { return data(); }
|
||||
const uint8_t* begin() const { return data(); }
|
||||
uint8_t* end() { return data() + size(); }
|
||||
const uint8_t* end() const { return data() + size(); }
|
||||
|
||||
// These may change access mode, leave these at end of public section
|
||||
FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer &other));
|
||||
FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer& other));
|
||||
FLATBUFFERS_DELETE_FUNC(
|
||||
DetachedBuffer &operator=(const DetachedBuffer &other));
|
||||
DetachedBuffer& operator=(const DetachedBuffer& other));
|
||||
|
||||
protected:
|
||||
Allocator *allocator_;
|
||||
Allocator* allocator_;
|
||||
bool own_allocator_;
|
||||
uint8_t *buf_;
|
||||
uint8_t* buf_;
|
||||
size_t reserved_;
|
||||
uint8_t *cur_;
|
||||
uint8_t* cur_;
|
||||
size_t size_;
|
||||
|
||||
inline void destroy() {
|
||||
if (buf_) Deallocate(allocator_, buf_, reserved_);
|
||||
if (own_allocator_ && allocator_) { delete allocator_; }
|
||||
if (own_allocator_ && allocator_) {
|
||||
delete allocator_;
|
||||
}
|
||||
reset();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,16 +31,16 @@ class FileManager {
|
||||
FileManager() = default;
|
||||
virtual ~FileManager() = default;
|
||||
|
||||
virtual bool SaveFile(const std::string &absolute_file_name,
|
||||
const std::string &content) = 0;
|
||||
virtual bool SaveFile(const std::string& absolute_file_name,
|
||||
const std::string& content) = 0;
|
||||
|
||||
virtual bool LoadFile(const std::string &absolute_file_name,
|
||||
std::string *buf) = 0;
|
||||
virtual bool LoadFile(const std::string& absolute_file_name,
|
||||
std::string* buf) = 0;
|
||||
|
||||
private:
|
||||
// Copying is not supported.
|
||||
FileManager(const FileManager &) = delete;
|
||||
FileManager &operator=(const FileManager &) = delete;
|
||||
FileManager(const FileManager&) = delete;
|
||||
FileManager& operator=(const FileManager&) = delete;
|
||||
};
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -41,14 +41,14 @@ namespace flatbuffers {
|
||||
/// it is the opposite transformation of GetRoot().
|
||||
/// This may be useful if you want to pass on a root and have the recipient
|
||||
/// delete the buffer afterwards.
|
||||
inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
|
||||
auto table = reinterpret_cast<const Table *>(root);
|
||||
inline const uint8_t* GetBufferStartFromRootPointer(const void* root) {
|
||||
auto table = reinterpret_cast<const Table*>(root);
|
||||
auto vtable = table->GetVTable();
|
||||
// Either the vtable is before the root or after the root.
|
||||
auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
|
||||
auto start = (std::min)(vtable, reinterpret_cast<const uint8_t*>(root));
|
||||
// Align to at least sizeof(uoffset_t).
|
||||
start = reinterpret_cast<const uint8_t *>(reinterpret_cast<uintptr_t>(start) &
|
||||
~(sizeof(uoffset_t) - 1));
|
||||
start = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(start) &
|
||||
~(sizeof(uoffset_t) - 1));
|
||||
// Additionally, there may be a file_identifier in the buffer, and the root
|
||||
// offset. The buffer may have been aligned to any size between
|
||||
// sizeof(uoffset_t) and FLATBUFFERS_MAX_ALIGNMENT (see "force_align").
|
||||
@@ -64,7 +64,7 @@ inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
|
||||
possible_roots; possible_roots--) {
|
||||
start -= sizeof(uoffset_t);
|
||||
if (ReadScalar<uoffset_t>(start) + start ==
|
||||
reinterpret_cast<const uint8_t *>(root))
|
||||
reinterpret_cast<const uint8_t*>(root))
|
||||
return start;
|
||||
}
|
||||
// We didn't find the root, either the "root" passed isn't really a root,
|
||||
@@ -76,8 +76,8 @@ inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
|
||||
}
|
||||
|
||||
/// @brief This return the prefixed size of a FlatBuffer.
|
||||
template<typename SizeT = uoffset_t>
|
||||
inline SizeT GetPrefixedSize(const uint8_t *buf) {
|
||||
template <typename SizeT = uoffset_t>
|
||||
inline SizeT GetPrefixedSize(const uint8_t* buf) {
|
||||
return ReadScalar<SizeT>(buf);
|
||||
}
|
||||
|
||||
@@ -87,8 +87,8 @@ inline SizeT GetPrefixedSize(const uint8_t *buf) {
|
||||
//
|
||||
// [size prefix][flatbuffer]
|
||||
// |---------length--------|
|
||||
template<typename SizeT = uoffset_t>
|
||||
inline SizeT GetSizePrefixedBufferLength(const uint8_t *const buf) {
|
||||
template <typename SizeT = uoffset_t>
|
||||
inline SizeT GetSizePrefixedBufferLength(const uint8_t* const buf) {
|
||||
return ReadScalar<SizeT>(buf) + sizeof(SizeT);
|
||||
}
|
||||
|
||||
@@ -106,9 +106,9 @@ struct NativeTable {};
|
||||
/// if you wish. The resolver does the opposite lookup, for when the object
|
||||
/// is being serialized again.
|
||||
typedef uint64_t hash_value_t;
|
||||
typedef std::function<void(void **pointer_adr, hash_value_t hash)>
|
||||
typedef std::function<void(void** pointer_adr, hash_value_t hash)>
|
||||
resolver_function_t;
|
||||
typedef std::function<hash_value_t(void *pointer)> rehasher_function_t;
|
||||
typedef std::function<hash_value_t(void* pointer)> rehasher_function_t;
|
||||
|
||||
// Helper function to test if a field is present, using any of the field
|
||||
// enums in the generated code.
|
||||
@@ -117,18 +117,18 @@ typedef std::function<hash_value_t(void *pointer)> rehasher_function_t;
|
||||
// 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, typename T::FlatBuffersVTableOffset field) {
|
||||
template <typename T>
|
||||
bool IsFieldPresent(const T* table, typename T::FlatBuffersVTableOffset field) {
|
||||
// Cast, since Table is a private baseclass of any table types.
|
||||
return reinterpret_cast<const Table *>(table)->CheckField(
|
||||
return reinterpret_cast<const Table*>(table)->CheckField(
|
||||
static_cast<voffset_t>(field));
|
||||
}
|
||||
|
||||
// Utility function for reverse lookups on the EnumNames*() functions
|
||||
// (in the generated C++ code)
|
||||
// names must be NULL terminated.
|
||||
inline int LookupEnum(const char **names, const char *name) {
|
||||
for (const char **p = names; *p; p++)
|
||||
inline int LookupEnum(const char** names, const char* name) {
|
||||
for (const char** p = names; *p; p++)
|
||||
if (!strcmp(*p, name)) return static_cast<int>(p - names);
|
||||
return -1;
|
||||
}
|
||||
@@ -227,20 +227,20 @@ static_assert(sizeof(TypeCode) == 2, "TypeCode");
|
||||
struct TypeTable;
|
||||
|
||||
// Signature of the static method present in each type.
|
||||
typedef const TypeTable *(*TypeFunction)();
|
||||
typedef const TypeTable* (*TypeFunction)();
|
||||
|
||||
struct TypeTable {
|
||||
SequenceType st;
|
||||
size_t num_elems; // of type_codes, values, names (but not type_refs).
|
||||
const TypeCode *type_codes; // num_elems count
|
||||
const TypeFunction *type_refs; // less than num_elems entries (see TypeCode).
|
||||
const int16_t *array_sizes; // less than num_elems entries (see TypeCode).
|
||||
const int64_t *values; // Only set for non-consecutive enum/union or structs.
|
||||
const char *const *names; // Only set if compiled with --reflect-names.
|
||||
const TypeCode* type_codes; // num_elems count
|
||||
const TypeFunction* type_refs; // less than num_elems entries (see TypeCode).
|
||||
const int16_t* array_sizes; // less than num_elems entries (see TypeCode).
|
||||
const int64_t* values; // Only set for non-consecutive enum/union or structs.
|
||||
const char* const* names; // Only set if compiled with --reflect-names.
|
||||
};
|
||||
|
||||
// String which identifies the current version of FlatBuffers.
|
||||
inline const char *flatbuffers_version_string() {
|
||||
inline const char* flatbuffers_version_string() {
|
||||
return "FlatBuffers " FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
|
||||
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
|
||||
|
||||
@@ -30,8 +30,8 @@
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
extern void LogCompilerWarn(const std::string &warn);
|
||||
extern void LogCompilerError(const std::string &err);
|
||||
extern void LogCompilerWarn(const std::string& warn);
|
||||
extern void LogCompilerError(const std::string& err);
|
||||
|
||||
struct FlatCOptions {
|
||||
IDLOptions opts;
|
||||
@@ -43,8 +43,8 @@ struct FlatCOptions {
|
||||
std::vector<std::string> filenames;
|
||||
|
||||
std::list<std::string> include_directories_storage;
|
||||
std::vector<const char *> include_directories;
|
||||
std::vector<const char *> conform_include_directories;
|
||||
std::vector<const char*> include_directories;
|
||||
std::vector<const char*> conform_include_directories;
|
||||
std::vector<bool> generator_enabled;
|
||||
size_t binary_files_from = std::numeric_limits<size_t>::max();
|
||||
std::string conform_to_schema;
|
||||
@@ -70,10 +70,10 @@ struct FlatCOption {
|
||||
|
||||
class FlatCompiler {
|
||||
public:
|
||||
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.
|
||||
@@ -84,42 +84,42 @@ class FlatCompiler {
|
||||
ErrorFn error_fn;
|
||||
};
|
||||
|
||||
explicit FlatCompiler(const InitParams ¶ms) : params_(params) {}
|
||||
explicit FlatCompiler(const InitParams& params) : params_(params) {}
|
||||
|
||||
bool RegisterCodeGenerator(const FlatCOption &option,
|
||||
bool RegisterCodeGenerator(const FlatCOption& option,
|
||||
std::shared_ptr<CodeGenerator> code_generator);
|
||||
|
||||
int Compile(const FlatCOptions &options);
|
||||
int Compile(const FlatCOptions& options);
|
||||
|
||||
std::string GetShortUsageString(const std::string &program_name) const;
|
||||
std::string GetUsageString(const std::string &program_name) const;
|
||||
std::string GetShortUsageString(const std::string& program_name) const;
|
||||
std::string GetUsageString(const std::string& program_name) const;
|
||||
|
||||
// Parse the FlatC options from command line arguments.
|
||||
FlatCOptions ParseFromCommandLineArguments(int argc, const char **argv);
|
||||
FlatCOptions ParseFromCommandLineArguments(int argc, const char** argv);
|
||||
|
||||
private:
|
||||
void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
|
||||
const std::string &contents,
|
||||
const std::vector<const char *> &include_directories) const;
|
||||
void ParseFile(flatbuffers::Parser& parser, const std::string& filename,
|
||||
const std::string& contents,
|
||||
const std::vector<const char*>& include_directories) const;
|
||||
|
||||
void LoadBinarySchema(Parser &parser, const std::string &filename,
|
||||
const std::string &contents);
|
||||
void LoadBinarySchema(Parser& parser, const std::string& filename,
|
||||
const std::string& contents);
|
||||
|
||||
void Warn(const std::string &warn, bool show_exe_name = true) const;
|
||||
void Warn(const std::string& warn, bool show_exe_name = true) const;
|
||||
|
||||
void Error(const std::string &err, bool usage = true,
|
||||
void Error(const std::string& err, bool usage = true,
|
||||
bool show_exe_name = true) const;
|
||||
|
||||
void AnnotateBinaries(const uint8_t *binary_schema,
|
||||
void AnnotateBinaries(const uint8_t* binary_schema,
|
||||
uint64_t binary_schema_size,
|
||||
const FlatCOptions &options);
|
||||
const FlatCOptions& options);
|
||||
|
||||
void ValidateOptions(const FlatCOptions &options);
|
||||
void ValidateOptions(const FlatCOptions& options);
|
||||
|
||||
Parser GetConformParser(const FlatCOptions &options);
|
||||
Parser GetConformParser(const FlatCOptions& options);
|
||||
|
||||
std::unique_ptr<Parser> GenerateCode(const FlatCOptions &options,
|
||||
Parser &conform_parser);
|
||||
std::unique_ptr<Parser> GenerateCode(const FlatCOptions& options,
|
||||
Parser& conform_parser);
|
||||
|
||||
std::map<std::string, std::shared_ptr<CodeGenerator>> code_generators_;
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ namespace flexbuffers {
|
||||
|
||||
// Verifies the `nested` flexbuffer within a flatbuffer vector is valid.
|
||||
inline bool VerifyNestedFlexBuffer(
|
||||
const flatbuffers::Vector<uint8_t> *const nested,
|
||||
flatbuffers::Verifier &verifier) {
|
||||
const flatbuffers::Vector<uint8_t>* const nested,
|
||||
flatbuffers::Verifier& verifier) {
|
||||
if (!nested) return true;
|
||||
return verifier.Check(flexbuffers::VerifyBuffer(
|
||||
nested->data(), nested->size(), verifier.GetFlexReuseTracker()));
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -30,23 +30,24 @@ 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() {}
|
||||
|
||||
Message(::grpc::Slice slice) : slice_(slice) {}
|
||||
|
||||
Message &operator=(const Message &other) = delete;
|
||||
Message& operator=(const Message& other) = delete;
|
||||
|
||||
Message(Message &&other) = default;
|
||||
Message(Message&& other) = default;
|
||||
|
||||
Message(const Message &other) = delete;
|
||||
Message(const Message& other) = delete;
|
||||
|
||||
Message &operator=(Message &&other) = default;
|
||||
Message& operator=(Message&& other) = default;
|
||||
|
||||
const uint8_t *mutable_data() const { return slice_.begin(); }
|
||||
const uint8_t* mutable_data() const { return slice_.begin(); }
|
||||
|
||||
const uint8_t *data() const { return slice_.begin(); }
|
||||
const uint8_t* data() const { return slice_.begin(); }
|
||||
|
||||
size_t size() const { return slice_.size(); }
|
||||
|
||||
@@ -55,12 +56,12 @@ template<class T> class Message {
|
||||
return verifier.VerifyBuffer<T>(nullptr);
|
||||
}
|
||||
|
||||
T *GetMutableRoot() { return flatbuffers::GetMutableRoot<T>(mutable_data()); }
|
||||
T* GetMutableRoot() { return flatbuffers::GetMutableRoot<T>(mutable_data()); }
|
||||
|
||||
const T *GetRoot() const { return flatbuffers::GetRoot<T>(data()); }
|
||||
const T* GetRoot() const { return flatbuffers::GetRoot<T>(data()); }
|
||||
|
||||
// This is only intended for serializer use, or if you know what you're doing
|
||||
const ::grpc::Slice &BorrowSlice() const { return slice_; }
|
||||
const ::grpc::Slice& BorrowSlice() const { return slice_; }
|
||||
|
||||
private:
|
||||
::grpc::Slice slice_;
|
||||
@@ -75,41 +76,41 @@ class SliceAllocator : public Allocator {
|
||||
public:
|
||||
SliceAllocator() {}
|
||||
|
||||
SliceAllocator(const SliceAllocator &other) = delete;
|
||||
SliceAllocator &operator=(const SliceAllocator &other) = delete;
|
||||
SliceAllocator(const SliceAllocator& other) = delete;
|
||||
SliceAllocator& operator=(const SliceAllocator& other) = delete;
|
||||
|
||||
SliceAllocator(SliceAllocator &&other) {
|
||||
SliceAllocator(SliceAllocator&& other) {
|
||||
// default-construct and swap idiom
|
||||
swap(other);
|
||||
}
|
||||
|
||||
SliceAllocator &operator=(SliceAllocator &&other) {
|
||||
SliceAllocator& operator=(SliceAllocator&& other) {
|
||||
// move-construct and swap idiom
|
||||
SliceAllocator temp(std::move(other));
|
||||
swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(SliceAllocator &other) {
|
||||
void swap(SliceAllocator& other) {
|
||||
using std::swap;
|
||||
swap(slice_, other.slice_);
|
||||
}
|
||||
|
||||
virtual ~SliceAllocator() {}
|
||||
|
||||
virtual uint8_t *allocate(size_t size) override {
|
||||
virtual uint8_t* allocate(size_t size) override {
|
||||
FLATBUFFERS_ASSERT(slice_.size() == 0);
|
||||
slice_ = ::grpc::Slice(size);
|
||||
return const_cast<uint8_t *>(slice_.begin());
|
||||
return const_cast<uint8_t*>(slice_.begin());
|
||||
}
|
||||
|
||||
virtual void deallocate(uint8_t *p, size_t size) override {
|
||||
virtual void deallocate(uint8_t* p, size_t size) override {
|
||||
FLATBUFFERS_ASSERT(p == slice_.begin());
|
||||
FLATBUFFERS_ASSERT(size == slice_.size());
|
||||
slice_ = ::grpc::Slice();
|
||||
}
|
||||
|
||||
virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
|
||||
virtual uint8_t* reallocate_downward(uint8_t* old_p, size_t old_size,
|
||||
size_t new_size, size_t in_use_back,
|
||||
size_t in_use_front) override {
|
||||
FLATBUFFERS_ASSERT(old_p == slice_.begin());
|
||||
@@ -117,15 +118,15 @@ class SliceAllocator : public Allocator {
|
||||
FLATBUFFERS_ASSERT(new_size > old_size);
|
||||
::grpc::Slice old_slice = slice_;
|
||||
::grpc::Slice new_slice = ::grpc::Slice(new_size);
|
||||
uint8_t *new_p = const_cast<uint8_t *>(new_slice.begin());
|
||||
uint8_t* new_p = const_cast<uint8_t*>(new_slice.begin());
|
||||
memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
|
||||
in_use_front);
|
||||
slice_ = new_slice;
|
||||
return const_cast<uint8_t *>(slice_.begin());
|
||||
return const_cast<uint8_t*>(slice_.begin());
|
||||
}
|
||||
|
||||
private:
|
||||
::grpc::Slice &get_slice(uint8_t *p, size_t size) {
|
||||
::grpc::Slice& get_slice(uint8_t* p, size_t size) {
|
||||
FLATBUFFERS_ASSERT(p == slice_.begin());
|
||||
FLATBUFFERS_ASSERT(size == slice_.size());
|
||||
return slice_;
|
||||
@@ -153,24 +154,24 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
explicit MessageBuilder(uoffset_t initial_size = 1024)
|
||||
: FlatBufferBuilder(initial_size, &slice_allocator_, false) {}
|
||||
|
||||
MessageBuilder(const MessageBuilder &other) = delete;
|
||||
MessageBuilder &operator=(const MessageBuilder &other) = delete;
|
||||
MessageBuilder(const MessageBuilder& other) = delete;
|
||||
MessageBuilder& operator=(const MessageBuilder& other) = delete;
|
||||
|
||||
MessageBuilder(MessageBuilder &&other)
|
||||
MessageBuilder(MessageBuilder&& other)
|
||||
: FlatBufferBuilder(1024, &slice_allocator_, false) {
|
||||
// Default construct and swap idiom.
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
/// Create a MessageBuilder from a FlatBufferBuilder.
|
||||
explicit MessageBuilder(FlatBufferBuilder &&src,
|
||||
void (*dealloc)(void *,
|
||||
explicit MessageBuilder(FlatBufferBuilder&& src,
|
||||
void (*dealloc)(void*,
|
||||
size_t) = &DefaultAllocator::dealloc)
|
||||
: FlatBufferBuilder(1024, &slice_allocator_, false) {
|
||||
src.Swap(*this);
|
||||
src.SwapBufAllocator(*this);
|
||||
if (buf_.capacity()) {
|
||||
uint8_t *buf = buf_.scratch_data(); // pointer to memory
|
||||
uint8_t* buf = buf_.scratch_data(); // pointer to memory
|
||||
size_t capacity = buf_.capacity(); // size of memory
|
||||
slice_allocator_.slice_ = ::grpc::Slice(buf, capacity, dealloc);
|
||||
} else {
|
||||
@@ -181,21 +182,21 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
/// Move-assign a FlatBufferBuilder to a MessageBuilder.
|
||||
/// Only FlatBufferBuilder with default allocator (basically, nullptr) is
|
||||
/// supported.
|
||||
MessageBuilder &operator=(FlatBufferBuilder &&src) {
|
||||
MessageBuilder& operator=(FlatBufferBuilder&& src) {
|
||||
// Move construct a temporary and swap
|
||||
MessageBuilder temp(std::move(src));
|
||||
Swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
MessageBuilder &operator=(MessageBuilder &&other) {
|
||||
MessageBuilder& operator=(MessageBuilder&& other) {
|
||||
// Move construct a temporary and swap
|
||||
MessageBuilder temp(std::move(other));
|
||||
Swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Swap(MessageBuilder &other) {
|
||||
void Swap(MessageBuilder& other) {
|
||||
slice_allocator_.swap(other.slice_allocator_);
|
||||
FlatBufferBuilder::Swap(other);
|
||||
// After swapping the FlatBufferBuilder, we swap back the allocator, which
|
||||
@@ -209,8 +210,8 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
// Releases the ownership of the buffer pointer.
|
||||
// Returns the size, offset, and the original grpc_slice that
|
||||
// allocated the buffer. Also see grpc_slice_unref().
|
||||
uint8_t *ReleaseRaw(size_t &size, size_t &offset, ::grpc::Slice &slice) {
|
||||
uint8_t *buf = FlatBufferBuilder::ReleaseRaw(size, offset);
|
||||
uint8_t* ReleaseRaw(size_t& size, size_t& offset, ::grpc::Slice& slice) {
|
||||
uint8_t* buf = FlatBufferBuilder::ReleaseRaw(size, offset);
|
||||
slice = slice_allocator_.slice_;
|
||||
slice_allocator_.slice_ = ::grpc::Slice();
|
||||
return buf;
|
||||
@@ -221,7 +222,8 @@ 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_.scratch_data(); // pointer to memory
|
||||
auto buf_size = buf_.capacity(); // size of memory
|
||||
auto msg_data = buf_.data(); // pointer to msg
|
||||
@@ -243,7 +245,8 @@ 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;
|
||||
@@ -258,10 +261,11 @@ 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,
|
||||
ByteBuffer *buffer, bool *own_buffer) {
|
||||
static grpc::Status Serialize(const flatbuffers::grpc::Message<T>& msg,
|
||||
ByteBuffer* buffer, bool* own_buffer) {
|
||||
// Package the single slice into a `ByteBuffer`,
|
||||
// incrementing the refcount in the process.
|
||||
*buffer = ByteBuffer(&msg.BorrowSlice(), 1);
|
||||
@@ -270,8 +274,8 @@ template<class T> class SerializationTraits<flatbuffers::grpc::Message<T>> {
|
||||
}
|
||||
|
||||
// Deserialize by pulling the
|
||||
static grpc::Status Deserialize(ByteBuffer *buf,
|
||||
flatbuffers::grpc::Message<T> *msg) {
|
||||
static grpc::Status Deserialize(ByteBuffer* buf,
|
||||
flatbuffers::grpc::Message<T>* msg) {
|
||||
Slice slice;
|
||||
if (!buf->TrySingleSlice(&slice).ok()) {
|
||||
if (!buf->DumpToSingleSlice(&slice).ok()) {
|
||||
|
||||
@@ -24,73 +24,81 @@
|
||||
|
||||
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) {
|
||||
for (const char* c = input; *c; ++c) {
|
||||
hash *= FnvTraits<T>::kFnvPrime;
|
||||
hash ^= static_cast<unsigned char>(*c);
|
||||
}
|
||||
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) {
|
||||
for (const char* c = input; *c; ++c) {
|
||||
hash ^= static_cast<unsigned char>(*c);
|
||||
hash *= FnvTraits<T>::kFnvPrime;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
template<> inline uint16_t HashFnv1<uint16_t>(const char *input) {
|
||||
template <>
|
||||
inline uint16_t HashFnv1<uint16_t>(const char* input) {
|
||||
uint32_t hash = HashFnv1<uint32_t>(input);
|
||||
return (hash >> 16) ^ (hash & 0xffff);
|
||||
}
|
||||
|
||||
template<> inline uint16_t HashFnv1a<uint16_t>(const char *input) {
|
||||
template <>
|
||||
inline uint16_t HashFnv1a<uint16_t>(const char* input) {
|
||||
uint32_t hash = HashFnv1a<uint32_t>(input);
|
||||
return (hash >> 16) ^ (hash & 0xffff);
|
||||
}
|
||||
|
||||
template<typename T> struct NamedHashFunction {
|
||||
const char *name;
|
||||
template <typename T>
|
||||
struct NamedHashFunction {
|
||||
const char* name;
|
||||
|
||||
typedef T (*HashFunction)(const char *);
|
||||
typedef T (*HashFunction)(const char*);
|
||||
HashFunction function;
|
||||
};
|
||||
|
||||
const NamedHashFunction<uint16_t> kHashFunctions16[] = {
|
||||
{ "fnv1_16", HashFnv1<uint16_t> },
|
||||
{ "fnv1a_16", HashFnv1a<uint16_t> },
|
||||
{"fnv1_16", HashFnv1<uint16_t>},
|
||||
{"fnv1a_16", HashFnv1a<uint16_t>},
|
||||
};
|
||||
|
||||
const NamedHashFunction<uint32_t> kHashFunctions32[] = {
|
||||
{ "fnv1_32", HashFnv1<uint32_t> },
|
||||
{ "fnv1a_32", HashFnv1a<uint32_t> },
|
||||
{"fnv1_32", HashFnv1<uint32_t>},
|
||||
{"fnv1a_32", HashFnv1a<uint32_t>},
|
||||
};
|
||||
|
||||
const NamedHashFunction<uint64_t> kHashFunctions64[] = {
|
||||
{ "fnv1_64", HashFnv1<uint64_t> },
|
||||
{ "fnv1a_64", HashFnv1a<uint64_t> },
|
||||
{"fnv1_64", HashFnv1<uint64_t>},
|
||||
{"fnv1a_64", HashFnv1a<uint64_t>},
|
||||
};
|
||||
|
||||
inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(
|
||||
const char *name) {
|
||||
const char* name) {
|
||||
std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
if (std::strcmp(name, kHashFunctions16[i].name) == 0) {
|
||||
@@ -101,7 +109,7 @@ inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(
|
||||
}
|
||||
|
||||
inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
|
||||
const char *name) {
|
||||
const char* name) {
|
||||
std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
if (std::strcmp(name, kHashFunctions32[i].name) == 0) {
|
||||
@@ -112,7 +120,7 @@ inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
|
||||
}
|
||||
|
||||
inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(
|
||||
const char *name) {
|
||||
const char* name) {
|
||||
std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
if (std::strcmp(name, kHashFunctions64[i].name) == 0) {
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
// Limits maximum depth of nested objects.
|
||||
// Prevents stack overflow while parse scheme, or json, or flexbuffer.
|
||||
#if !defined(FLATBUFFERS_MAX_PARSING_DEPTH)
|
||||
# define FLATBUFFERS_MAX_PARSING_DEPTH 64
|
||||
#define FLATBUFFERS_MAX_PARSING_DEPTH 64
|
||||
#endif
|
||||
|
||||
namespace flatbuffers {
|
||||
@@ -192,15 +192,15 @@ 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, uint16_t _fixed_length = 0)
|
||||
explicit Type(BaseType _base_type = BASE_TYPE_NONE, StructDef* _sd = nullptr,
|
||||
EnumDef* _ed = nullptr, uint16_t _fixed_length = 0)
|
||||
: base_type(_base_type),
|
||||
element(BASE_TYPE_NONE),
|
||||
struct_def(_sd),
|
||||
enum_def(_ed),
|
||||
fixed_length(_fixed_length) {}
|
||||
|
||||
bool operator==(const Type &o) const {
|
||||
bool operator==(const Type& o) const {
|
||||
return base_type == o.base_type && element == o.element &&
|
||||
struct_def == o.struct_def && enum_def == o.enum_def;
|
||||
}
|
||||
@@ -209,15 +209,15 @@ struct Type {
|
||||
return Type(element, struct_def, enum_def, fixed_length);
|
||||
}
|
||||
|
||||
Offset<reflection::Type> Serialize(FlatBufferBuilder *builder) const;
|
||||
Offset<reflection::Type> Serialize(FlatBufferBuilder* builder) const;
|
||||
|
||||
bool Deserialize(const Parser &parser, const reflection::Type *type);
|
||||
bool Deserialize(const Parser& parser, const reflection::Type* type);
|
||||
|
||||
BaseType base_type;
|
||||
BaseType element; // only set if t == BASE_TYPE_VECTOR or
|
||||
// BASE_TYPE_VECTOR64
|
||||
StructDef *struct_def; // only set if t or element == BASE_TYPE_STRUCT
|
||||
EnumDef *enum_def; // set if t == BASE_TYPE_UNION / BASE_TYPE_UTYPE,
|
||||
StructDef* struct_def; // only set if t or element == BASE_TYPE_STRUCT
|
||||
EnumDef* enum_def; // set if t == BASE_TYPE_UNION / BASE_TYPE_UTYPE,
|
||||
// or for an integral type derived from an enum.
|
||||
uint16_t fixed_length; // only set if t == BASE_TYPE_ARRAY
|
||||
};
|
||||
@@ -234,13 +234,16 @@ struct Value {
|
||||
|
||||
// Helper class that retains the original order of a set of identifiers and
|
||||
// also provides quick lookup.
|
||||
template<typename T> class SymbolTable {
|
||||
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) {
|
||||
bool Add(const std::string& name, T* e) {
|
||||
vec.emplace_back(e);
|
||||
auto it = dict.find(name);
|
||||
if (it != dict.end()) return true;
|
||||
@@ -248,7 +251,7 @@ template<typename T> class SymbolTable {
|
||||
return false;
|
||||
}
|
||||
|
||||
void Move(const std::string &oldname, const std::string &newname) {
|
||||
void Move(const std::string& oldname, const std::string& newname) {
|
||||
auto it = dict.find(oldname);
|
||||
if (it != dict.end()) {
|
||||
auto obj = it->second;
|
||||
@@ -259,14 +262,14 @@ template<typename T> class SymbolTable {
|
||||
}
|
||||
}
|
||||
|
||||
T *Lookup(const std::string &name) const {
|
||||
T* Lookup(const std::string& name) const {
|
||||
auto it = dict.find(name);
|
||||
return it == dict.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
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.
|
||||
@@ -277,14 +280,14 @@ struct Namespace {
|
||||
// which has a full namespaced descriptor.
|
||||
// With max_components you can request less than the number of components
|
||||
// the current namespace has.
|
||||
std::string GetFullyQualifiedName(const std::string &name,
|
||||
std::string GetFullyQualifiedName(const std::string& name,
|
||||
size_t max_components = 1000) const;
|
||||
|
||||
std::vector<std::string> components;
|
||||
size_t from_table; // Part of the namespace corresponds to a message/table.
|
||||
};
|
||||
|
||||
inline bool operator<(const Namespace &a, const Namespace &b) {
|
||||
inline bool operator<(const Namespace& a, const Namespace& b) {
|
||||
size_t min_size = std::min(a.components.size(), b.components.size());
|
||||
for (size_t i = 0; i < min_size; ++i) {
|
||||
if (a.components[i] != b.components[i])
|
||||
@@ -305,23 +308,23 @@ struct Definition {
|
||||
|
||||
flatbuffers::Offset<
|
||||
flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
|
||||
SerializeAttributes(FlatBufferBuilder *builder, const Parser &parser) const;
|
||||
SerializeAttributes(FlatBufferBuilder* builder, const Parser& parser) const;
|
||||
|
||||
bool DeserializeAttributes(Parser &parser,
|
||||
const Vector<Offset<reflection::KeyValue>> *attrs);
|
||||
bool DeserializeAttributes(Parser& parser,
|
||||
const Vector<Offset<reflection::KeyValue>>* attrs);
|
||||
|
||||
std::string name;
|
||||
std::string file;
|
||||
std::vector<std::string> doc_comment;
|
||||
SymbolTable<Value> attributes;
|
||||
bool generated; // did we already output code for this definition?
|
||||
Namespace *defined_namespace; // Where it was defined.
|
||||
Namespace* defined_namespace; // Where it was defined.
|
||||
|
||||
// For use with Serialize()
|
||||
uoffset_t serialized_location;
|
||||
int index; // Inside the vector it is stored.
|
||||
int refcount;
|
||||
const std::string *declaration_file;
|
||||
const std::string* declaration_file;
|
||||
};
|
||||
|
||||
struct FieldDef : public Definition {
|
||||
@@ -337,17 +340,14 @@ struct FieldDef : public Definition {
|
||||
padding(0),
|
||||
sibling_union_field(nullptr) {}
|
||||
|
||||
Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
|
||||
const Parser &parser) const;
|
||||
Offset<reflection::Field> Serialize(FlatBufferBuilder* builder, uint16_t id,
|
||||
const Parser& parser) const;
|
||||
|
||||
bool Deserialize(Parser &parser, const reflection::Field *field);
|
||||
bool Deserialize(Parser& parser, const reflection::Field* field);
|
||||
|
||||
|
||||
bool IsScalarOptional() const {
|
||||
return IsScalar() && IsOptional();
|
||||
}
|
||||
bool IsScalarOptional() const { return IsScalar() && IsOptional(); }
|
||||
bool IsScalar() const {
|
||||
return ::flatbuffers::IsScalar(value.type.base_type);
|
||||
return ::flatbuffers::IsScalar(value.type.base_type);
|
||||
}
|
||||
bool IsOptional() const { return presence == kOptional; }
|
||||
bool IsRequired() const { return presence == kRequired; }
|
||||
@@ -383,14 +383,14 @@ struct FieldDef : public Definition {
|
||||
}
|
||||
Presence presence;
|
||||
|
||||
StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
|
||||
StructDef* nested_flatbuffer; // This field contains nested FlatBuffer data.
|
||||
size_t padding; // Bytes to always pad after this field.
|
||||
|
||||
// sibling_union_field is always set to nullptr. The only exception is
|
||||
// when FieldDef is a union field or an union type field. Therefore,
|
||||
// sibling_union_field on a union field points to the union type field
|
||||
// and vice-versa.
|
||||
FieldDef *sibling_union_field;
|
||||
FieldDef* sibling_union_field;
|
||||
};
|
||||
|
||||
struct StructDef : public Definition {
|
||||
@@ -408,10 +408,10 @@ struct StructDef : public Definition {
|
||||
if (fields.vec.size()) fields.vec.back()->padding = padding;
|
||||
}
|
||||
|
||||
Offset<reflection::Object> Serialize(FlatBufferBuilder *builder,
|
||||
const Parser &parser) const;
|
||||
Offset<reflection::Object> Serialize(FlatBufferBuilder* builder,
|
||||
const Parser& parser) const;
|
||||
|
||||
bool Deserialize(Parser &parser, const reflection::Object *object);
|
||||
bool Deserialize(Parser& parser, const reflection::Object* object);
|
||||
|
||||
SymbolTable<FieldDef> fields;
|
||||
|
||||
@@ -430,17 +430,17 @@ struct EnumDef;
|
||||
struct EnumValBuilder;
|
||||
|
||||
struct EnumVal {
|
||||
Offset<reflection::EnumVal> Serialize(FlatBufferBuilder *builder,
|
||||
const Parser &parser) const;
|
||||
Offset<reflection::EnumVal> Serialize(FlatBufferBuilder* builder,
|
||||
const Parser& parser) const;
|
||||
|
||||
bool Deserialize(Parser &parser, const reflection::EnumVal *val);
|
||||
bool Deserialize(Parser& parser, const reflection::EnumVal* val);
|
||||
|
||||
flatbuffers::Offset<
|
||||
flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
|
||||
SerializeAttributes(FlatBufferBuilder *builder, const Parser &parser) const;
|
||||
SerializeAttributes(FlatBufferBuilder* builder, const Parser& parser) const;
|
||||
|
||||
bool DeserializeAttributes(Parser &parser,
|
||||
const Vector<Offset<reflection::KeyValue>> *attrs);
|
||||
bool DeserializeAttributes(Parser& parser,
|
||||
const Vector<Offset<reflection::KeyValue>>* attrs);
|
||||
|
||||
uint64_t GetAsUInt64() const { return static_cast<uint64_t>(value); }
|
||||
int64_t GetAsInt64() const { return value; }
|
||||
@@ -455,9 +455,9 @@ struct EnumVal {
|
||||
private:
|
||||
friend EnumDef;
|
||||
friend EnumValBuilder;
|
||||
friend bool operator==(const EnumVal &lhs, const EnumVal &rhs);
|
||||
friend bool operator==(const EnumVal& lhs, const EnumVal& rhs);
|
||||
|
||||
EnumVal(const std::string &_name, int64_t _val) : name(_name), value(_val) {}
|
||||
EnumVal(const std::string& _name, int64_t _val) : name(_name), value(_val) {}
|
||||
EnumVal() : value(0) {}
|
||||
|
||||
int64_t value;
|
||||
@@ -466,37 +466,38 @@ struct EnumVal {
|
||||
struct EnumDef : public Definition {
|
||||
EnumDef() : is_union(false), uses_multiple_type_instances(false) {}
|
||||
|
||||
Offset<reflection::Enum> Serialize(FlatBufferBuilder *builder,
|
||||
const Parser &parser) const;
|
||||
Offset<reflection::Enum> Serialize(FlatBufferBuilder* builder,
|
||||
const Parser& parser) const;
|
||||
|
||||
bool Deserialize(Parser &parser, const reflection::Enum *values);
|
||||
bool Deserialize(Parser& parser, const reflection::Enum* values);
|
||||
|
||||
template<typename T> void ChangeEnumValue(EnumVal *ev, T new_val);
|
||||
template <typename T>
|
||||
void ChangeEnumValue(EnumVal* ev, T new_val);
|
||||
void SortByValue();
|
||||
void RemoveDuplicates();
|
||||
|
||||
std::string AllFlags() const;
|
||||
const EnumVal *MinValue() const;
|
||||
const EnumVal *MaxValue() const;
|
||||
const EnumVal* MinValue() const;
|
||||
const EnumVal* MaxValue() const;
|
||||
// Returns the number of integer steps from v1 to v2.
|
||||
uint64_t Distance(const EnumVal *v1, const EnumVal *v2) const;
|
||||
uint64_t Distance(const EnumVal* v1, const EnumVal* v2) const;
|
||||
// Returns the number of integer steps from Min to Max.
|
||||
uint64_t Distance() const { return Distance(MinValue(), MaxValue()); }
|
||||
|
||||
EnumVal *ReverseLookup(int64_t enum_idx,
|
||||
EnumVal* ReverseLookup(int64_t enum_idx,
|
||||
bool skip_union_default = false) const;
|
||||
EnumVal *FindByValue(const std::string &constant) const;
|
||||
EnumVal* FindByValue(const std::string& constant) const;
|
||||
|
||||
std::string ToString(const EnumVal &ev) const {
|
||||
std::string ToString(const EnumVal& ev) const {
|
||||
return IsUInt64() ? NumToString(ev.GetAsUInt64())
|
||||
: NumToString(ev.GetAsInt64());
|
||||
}
|
||||
|
||||
size_t size() const { return vals.vec.size(); }
|
||||
|
||||
const std::vector<EnumVal *> &Vals() const { return vals.vec; }
|
||||
const std::vector<EnumVal*>& Vals() const { return vals.vec; }
|
||||
|
||||
const EnumVal *Lookup(const std::string &enum_name) const {
|
||||
const EnumVal* Lookup(const std::string& enum_name) const {
|
||||
return vals.Lookup(enum_name);
|
||||
}
|
||||
|
||||
@@ -515,53 +516,53 @@ struct EnumDef : public Definition {
|
||||
SymbolTable<EnumVal> vals;
|
||||
};
|
||||
|
||||
inline bool IsString(const Type &type) {
|
||||
inline bool IsString(const Type& type) {
|
||||
return type.base_type == BASE_TYPE_STRING;
|
||||
}
|
||||
|
||||
inline bool IsStruct(const Type &type) {
|
||||
inline bool IsStruct(const Type& type) {
|
||||
return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed;
|
||||
}
|
||||
|
||||
inline bool IsIncompleteStruct(const Type &type) {
|
||||
inline bool IsIncompleteStruct(const Type& type) {
|
||||
return type.base_type == BASE_TYPE_STRUCT && type.struct_def->predecl;
|
||||
}
|
||||
|
||||
inline bool IsTable(const Type &type) {
|
||||
inline bool IsTable(const Type& type) {
|
||||
return type.base_type == BASE_TYPE_STRUCT && !type.struct_def->fixed;
|
||||
}
|
||||
|
||||
inline bool IsUnion(const Type &type) {
|
||||
inline bool IsUnion(const Type& type) {
|
||||
return type.enum_def != nullptr && type.enum_def->is_union;
|
||||
}
|
||||
|
||||
inline bool IsUnionType(const Type &type) {
|
||||
inline bool IsUnionType(const Type& type) {
|
||||
return IsUnion(type) && IsInteger(type.base_type);
|
||||
}
|
||||
|
||||
inline bool IsVector(const Type &type) { return IsVector(type.base_type); }
|
||||
inline bool IsVector(const Type& type) { return IsVector(type.base_type); }
|
||||
|
||||
inline bool IsVectorOfStruct(const Type &type) {
|
||||
inline bool IsVectorOfStruct(const Type& type) {
|
||||
return IsVector(type) && IsStruct(type.VectorType());
|
||||
}
|
||||
|
||||
inline bool IsVectorOfTable(const Type &type) {
|
||||
inline bool IsVectorOfTable(const Type& type) {
|
||||
return IsVector(type) && IsTable(type.VectorType());
|
||||
}
|
||||
|
||||
inline bool IsArray(const Type &type) {
|
||||
inline bool IsArray(const Type& type) {
|
||||
return type.base_type == BASE_TYPE_ARRAY;
|
||||
}
|
||||
|
||||
inline bool IsSeries(const Type &type) {
|
||||
inline bool IsSeries(const Type& type) {
|
||||
return IsVector(type) || IsArray(type);
|
||||
}
|
||||
|
||||
inline bool IsEnum(const Type &type) {
|
||||
inline bool IsEnum(const Type& type) {
|
||||
return type.enum_def != nullptr && IsInteger(type.base_type);
|
||||
}
|
||||
|
||||
inline size_t InlineSize(const Type &type) {
|
||||
inline size_t InlineSize(const Type& type) {
|
||||
return IsStruct(type)
|
||||
? type.struct_def->bytesize
|
||||
: (IsArray(type)
|
||||
@@ -569,7 +570,7 @@ inline size_t InlineSize(const Type &type) {
|
||||
: SizeOf(type.base_type));
|
||||
}
|
||||
|
||||
inline size_t InlineAlignment(const Type &type) {
|
||||
inline size_t InlineAlignment(const Type& type) {
|
||||
if (IsStruct(type)) {
|
||||
return type.struct_def->minalign;
|
||||
} else if (IsArray(type)) {
|
||||
@@ -579,14 +580,14 @@ inline size_t InlineAlignment(const Type &type) {
|
||||
return SizeOf(type.base_type);
|
||||
}
|
||||
}
|
||||
inline bool operator==(const EnumVal &lhs, const EnumVal &rhs) {
|
||||
inline bool operator==(const EnumVal& lhs, const EnumVal& rhs) {
|
||||
return lhs.value == rhs.value;
|
||||
}
|
||||
inline bool operator!=(const EnumVal &lhs, const EnumVal &rhs) {
|
||||
inline bool operator!=(const EnumVal& lhs, const EnumVal& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
inline bool EqualByName(const Type &a, const Type &b) {
|
||||
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 != nullptr && b.struct_def != nullptr &&
|
||||
@@ -597,18 +598,18 @@ inline bool EqualByName(const Type &a, const Type &b) {
|
||||
}
|
||||
|
||||
struct RPCCall : public Definition {
|
||||
Offset<reflection::RPCCall> Serialize(FlatBufferBuilder *builder,
|
||||
const Parser &parser) const;
|
||||
Offset<reflection::RPCCall> Serialize(FlatBufferBuilder* builder,
|
||||
const Parser& parser) const;
|
||||
|
||||
bool Deserialize(Parser &parser, const reflection::RPCCall *call);
|
||||
bool Deserialize(Parser& parser, const reflection::RPCCall* call);
|
||||
|
||||
StructDef *request, *response;
|
||||
};
|
||||
|
||||
struct ServiceDef : public Definition {
|
||||
Offset<reflection::Service> Serialize(FlatBufferBuilder *builder,
|
||||
const Parser &parser) const;
|
||||
bool Deserialize(Parser &parser, const reflection::Service *service);
|
||||
Offset<reflection::Service> Serialize(FlatBufferBuilder* builder,
|
||||
const Parser& parser) const;
|
||||
bool Deserialize(Parser& parser, const reflection::Service* service);
|
||||
|
||||
SymbolTable<RPCCall> calls;
|
||||
};
|
||||
@@ -627,7 +628,7 @@ struct IncludedFile {
|
||||
};
|
||||
|
||||
// Since IncludedFile is contained within a std::set, need to provide ordering.
|
||||
inline bool operator<(const IncludedFile &a, const IncludedFile &b) {
|
||||
inline bool operator<(const IncludedFile& a, const IncludedFile& b) {
|
||||
return a.filename < b.filename;
|
||||
}
|
||||
|
||||
@@ -711,7 +712,7 @@ struct IDLOptions {
|
||||
/********************************** Python **********************************/
|
||||
bool python_no_type_prefix_suffix;
|
||||
bool python_typing;
|
||||
bool python_decode_obj_api_strings=false;
|
||||
bool python_decode_obj_api_strings = false;
|
||||
|
||||
// The target Python version. Can be one of the following:
|
||||
// - "0"
|
||||
@@ -881,7 +882,7 @@ struct ParserState {
|
||||
attr_is_trivial_ascii_string_(true) {}
|
||||
|
||||
protected:
|
||||
void ResetState(const char *source) {
|
||||
void ResetState(const char* source) {
|
||||
prev_cursor_ = source;
|
||||
cursor_ = source;
|
||||
line_ = 0;
|
||||
@@ -898,9 +899,9 @@ struct ParserState {
|
||||
return static_cast<int64_t>(cursor_ - line_start_);
|
||||
}
|
||||
|
||||
const char *prev_cursor_;
|
||||
const char *cursor_;
|
||||
const char *line_start_;
|
||||
const char* prev_cursor_;
|
||||
const char* cursor_;
|
||||
const char* line_start_;
|
||||
int line_; // the current line being parsed
|
||||
int token_;
|
||||
|
||||
@@ -924,14 +925,14 @@ class CheckedError {
|
||||
explicit CheckedError(bool error)
|
||||
: is_error_(error), has_been_checked_(false) {}
|
||||
|
||||
CheckedError &operator=(const CheckedError &other) {
|
||||
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) {
|
||||
CheckedError(const CheckedError& other) {
|
||||
*this = other; // Use assignment operator.
|
||||
}
|
||||
|
||||
@@ -960,7 +961,7 @@ class CheckedError {
|
||||
|
||||
class Parser : public ParserState {
|
||||
public:
|
||||
explicit Parser(const IDLOptions &options = IDLOptions())
|
||||
explicit Parser(const IDLOptions& options = IDLOptions())
|
||||
: current_namespace_(nullptr),
|
||||
empty_namespace_(nullptr),
|
||||
flex_builder_(256, flexbuffers::BUILDER_FLAG_SHARE_ALL),
|
||||
@@ -972,7 +973,9 @@ class Parser : public ParserState {
|
||||
source_(nullptr),
|
||||
anonymous_counter_(0),
|
||||
parse_depth_counter_(0) {
|
||||
if (opts.force_defaults) { builder_.ForceDefaults(true); }
|
||||
if (opts.force_defaults) {
|
||||
builder_.ForceDefaults(true);
|
||||
}
|
||||
// Start out with the empty namespace being current.
|
||||
empty_namespace_ = new Namespace();
|
||||
namespaces_.push_back(empty_namespace_);
|
||||
@@ -1012,11 +1015,11 @@ class Parser : public ParserState {
|
||||
}
|
||||
|
||||
// Copying is not allowed
|
||||
Parser(const Parser &) = delete;
|
||||
Parser &operator=(const Parser &) = delete;
|
||||
Parser(const Parser&) = delete;
|
||||
Parser& operator=(const Parser&) = delete;
|
||||
|
||||
Parser(Parser &&) = default;
|
||||
Parser &operator=(Parser &&) = default;
|
||||
Parser(Parser&&) = default;
|
||||
Parser& operator=(Parser&&) = default;
|
||||
|
||||
~Parser() {
|
||||
for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
|
||||
@@ -1035,16 +1038,16 @@ class Parser : public ParserState {
|
||||
// supply its name in source_filename.
|
||||
// All paths specified in this call must be in posix format, if you accept
|
||||
// paths from user input, please call PosixPath on them first.
|
||||
bool Parse(const char *_source, const char **include_paths = nullptr,
|
||||
const char *source_filename = nullptr);
|
||||
bool Parse(const char* _source, const char** include_paths = nullptr,
|
||||
const char* source_filename = nullptr);
|
||||
|
||||
bool ParseJson(const char *json, const char *json_filename = nullptr);
|
||||
bool ParseJson(const char* json, const char* json_filename = nullptr);
|
||||
|
||||
// Returns the number of characters were consumed when parsing a JSON string.
|
||||
std::ptrdiff_t BytesConsumed() const;
|
||||
|
||||
// Set the root type. May override the one set in the schema.
|
||||
bool SetRootType(const char *name);
|
||||
bool SetRootType(const char* name);
|
||||
|
||||
// Mark all definitions as already having code generated.
|
||||
void MarkGenerated();
|
||||
@@ -1052,41 +1055,41 @@ class Parser : public ParserState {
|
||||
// Get the files recursively included by the given file. The returned
|
||||
// container will have at least the given file.
|
||||
std::set<std::string> GetIncludedFilesRecursive(
|
||||
const std::string &file_name) const;
|
||||
const std::string& file_name) const;
|
||||
|
||||
// Fills builder_ with a binary version of the schema parsed.
|
||||
// See reflection/reflection.fbs
|
||||
void Serialize();
|
||||
|
||||
// Deserialize a schema buffer
|
||||
bool Deserialize(const uint8_t *buf, const size_t size);
|
||||
bool Deserialize(const uint8_t* buf, const size_t size);
|
||||
|
||||
// Fills internal structure as if the schema passed had been loaded by parsing
|
||||
// with Parse except that included filenames will not be populated.
|
||||
bool Deserialize(const reflection::Schema *schema);
|
||||
bool Deserialize(const reflection::Schema* schema);
|
||||
|
||||
Type *DeserializeType(const reflection::Type *type);
|
||||
Type* DeserializeType(const reflection::Type* type);
|
||||
|
||||
// Checks that the schema represented by this parser is a safe evolution
|
||||
// of the schema provided. Returns non-empty error on any problems.
|
||||
std::string ConformTo(const Parser &base);
|
||||
std::string ConformTo(const Parser& base);
|
||||
|
||||
// Similar to Parse(), but now only accepts JSON to be parsed into a
|
||||
// FlexBuffer.
|
||||
bool ParseFlexBuffer(const char *source, const char *source_filename,
|
||||
flexbuffers::Builder *builder);
|
||||
bool ParseFlexBuffer(const char* source, const char* source_filename,
|
||||
flexbuffers::Builder* builder);
|
||||
|
||||
StructDef *LookupStruct(const std::string &id) const;
|
||||
StructDef *LookupStructThruParentNamespaces(const std::string &id) const;
|
||||
StructDef* LookupStruct(const std::string& id) const;
|
||||
StructDef* LookupStructThruParentNamespaces(const std::string& id) const;
|
||||
|
||||
std::string UnqualifiedName(const std::string &fullQualifiedName);
|
||||
std::string UnqualifiedName(const std::string& fullQualifiedName);
|
||||
|
||||
FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
|
||||
FLATBUFFERS_CHECKED_ERROR Error(const std::string& msg);
|
||||
|
||||
// @brief Verify that any of 'opts.lang_to_generate' supports Optional scalars
|
||||
// in a schema.
|
||||
// @param opts Options used to parce a schema and generate code.
|
||||
static bool SupportsOptionalScalars(const flatbuffers::IDLOptions &opts);
|
||||
static bool SupportsOptionalScalars(const flatbuffers::IDLOptions& opts);
|
||||
|
||||
// Get the set of included files that are directly referenced by the file
|
||||
// being parsed. This does not include files that are transitively included by
|
||||
@@ -1096,101 +1099,101 @@ class Parser : public ParserState {
|
||||
private:
|
||||
class ParseDepthGuard;
|
||||
|
||||
void Message(const std::string &msg);
|
||||
void Warning(const std::string &msg);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t *val);
|
||||
void Message(const std::string& msg);
|
||||
void Warning(const std::string& msg);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t* val);
|
||||
FLATBUFFERS_CHECKED_ERROR Next();
|
||||
FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark();
|
||||
bool Is(int t) const;
|
||||
bool IsIdent(const char *id) const;
|
||||
bool IsIdent(const char* id) const;
|
||||
FLATBUFFERS_CHECKED_ERROR Expect(int t);
|
||||
std::string TokenToStringId(int t) const;
|
||||
EnumDef *LookupEnum(const std::string &id);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseNamespacing(std::string *id,
|
||||
std::string *last);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTypeIdent(Type &type);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseType(Type &type);
|
||||
FLATBUFFERS_CHECKED_ERROR AddField(StructDef &struct_def,
|
||||
const std::string &name, const Type &type,
|
||||
FieldDef **dest);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseField(StructDef &struct_def);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseString(Value &val, bool use_string_pooling);
|
||||
EnumDef* LookupEnum(const std::string& id);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseNamespacing(std::string* id,
|
||||
std::string* last);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTypeIdent(Type& type);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseType(Type& type);
|
||||
FLATBUFFERS_CHECKED_ERROR AddField(StructDef& struct_def,
|
||||
const std::string& name, const Type& type,
|
||||
FieldDef** dest);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseField(StructDef& struct_def);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseString(Value& val, bool use_string_pooling);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseComma();
|
||||
FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field,
|
||||
FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value& val, FieldDef* field,
|
||||
size_t parent_fieldn,
|
||||
const StructDef *parent_struct_def,
|
||||
const StructDef* parent_struct_def,
|
||||
size_t count,
|
||||
bool inside_vector = false);
|
||||
template<typename F>
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTableDelimiters(size_t &fieldn,
|
||||
const StructDef *struct_def,
|
||||
template <typename F>
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTableDelimiters(size_t& fieldn,
|
||||
const StructDef* struct_def,
|
||||
F body);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTable(const StructDef &struct_def,
|
||||
std::string *value, uoffset_t *ovalue);
|
||||
void SerializeStruct(const StructDef &struct_def, const Value &val);
|
||||
void SerializeStruct(FlatBufferBuilder &builder, const StructDef &struct_def,
|
||||
const Value &val);
|
||||
template<typename F>
|
||||
FLATBUFFERS_CHECKED_ERROR ParseVectorDelimiters(size_t &count, F body);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseVector(const Type &type, uoffset_t *ovalue,
|
||||
FieldDef *field, size_t fieldn);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseArray(Value &array);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTable(const StructDef& struct_def,
|
||||
std::string* value, uoffset_t* ovalue);
|
||||
void SerializeStruct(const StructDef& struct_def, const Value& val);
|
||||
void SerializeStruct(FlatBufferBuilder& builder, const StructDef& struct_def,
|
||||
const Value& val);
|
||||
template <typename F>
|
||||
FLATBUFFERS_CHECKED_ERROR ParseVectorDelimiters(size_t& count, F body);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseVector(const Type& type, uoffset_t* ovalue,
|
||||
FieldDef* field, size_t fieldn);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseArray(Value& array);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseNestedFlatbuffer(
|
||||
Value &val, FieldDef *field, size_t fieldn,
|
||||
const StructDef *parent_struct_def);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseMetaData(SymbolTable<Value> *attributes);
|
||||
FLATBUFFERS_CHECKED_ERROR TryTypedValue(const std::string *name, int dtoken,
|
||||
bool check, Value &e, BaseType req,
|
||||
bool *destmatch);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseHash(Value &e, FieldDef *field);
|
||||
Value& val, FieldDef* field, size_t fieldn,
|
||||
const StructDef* parent_struct_def);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseMetaData(SymbolTable<Value>* attributes);
|
||||
FLATBUFFERS_CHECKED_ERROR TryTypedValue(const std::string* name, int dtoken,
|
||||
bool check, Value& e, BaseType req,
|
||||
bool* destmatch);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseHash(Value& e, FieldDef* field);
|
||||
FLATBUFFERS_CHECKED_ERROR TokenError();
|
||||
FLATBUFFERS_CHECKED_ERROR ParseSingleValue(const std::string *name, Value &e,
|
||||
FLATBUFFERS_CHECKED_ERROR ParseSingleValue(const std::string* name, Value& e,
|
||||
bool check_now);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseFunction(const std::string *name, Value &e);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(const Type &type,
|
||||
std::string *result);
|
||||
StructDef *LookupCreateStruct(const std::string &name,
|
||||
FLATBUFFERS_CHECKED_ERROR ParseFunction(const std::string* name, Value& e);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(const Type& type,
|
||||
std::string* result);
|
||||
StructDef* LookupCreateStruct(const std::string& name,
|
||||
bool create_if_new = true,
|
||||
bool definition = false);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseEnum(bool is_union, EnumDef **dest,
|
||||
const char *filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseEnum(bool is_union, EnumDef** dest,
|
||||
const char* filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseNamespace();
|
||||
FLATBUFFERS_CHECKED_ERROR StartStruct(const std::string &name,
|
||||
StructDef **dest);
|
||||
FLATBUFFERS_CHECKED_ERROR StartEnum(const std::string &name, bool is_union,
|
||||
EnumDef **dest);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseDecl(const char *filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseService(const char *filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoFields(StructDef *struct_def,
|
||||
FLATBUFFERS_CHECKED_ERROR StartStruct(const std::string& name,
|
||||
StructDef** dest);
|
||||
FLATBUFFERS_CHECKED_ERROR StartEnum(const std::string& name, bool is_union,
|
||||
EnumDef** dest);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseDecl(const char* filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseService(const char* filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoFields(StructDef* struct_def,
|
||||
bool isextend, bool inside_oneof);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoMapField(StructDef *struct_def);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoMapField(StructDef* struct_def);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoOption();
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoKey();
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoDecl();
|
||||
FLATBUFFERS_CHECKED_ERROR ParseProtoCurliesOrIdent();
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTypeFromProtoType(Type *type);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseTypeFromProtoType(Type* type);
|
||||
FLATBUFFERS_CHECKED_ERROR SkipAnyJsonValue();
|
||||
FLATBUFFERS_CHECKED_ERROR ParseFlexBufferNumericConstant(
|
||||
flexbuffers::Builder *builder);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseFlexBufferValue(flexbuffers::Builder *builder);
|
||||
FLATBUFFERS_CHECKED_ERROR StartParseFile(const char *source,
|
||||
const char *source_filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseRoot(const char *_source,
|
||||
const char **include_paths,
|
||||
const char *source_filename);
|
||||
flexbuffers::Builder* builder);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseFlexBufferValue(flexbuffers::Builder* builder);
|
||||
FLATBUFFERS_CHECKED_ERROR StartParseFile(const char* source,
|
||||
const char* source_filename);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseRoot(const char* _source,
|
||||
const char** include_paths,
|
||||
const char* source_filename);
|
||||
FLATBUFFERS_CHECKED_ERROR CheckPrivateLeak();
|
||||
FLATBUFFERS_CHECKED_ERROR CheckPrivatelyLeakedFields(
|
||||
const Definition &def, const Definition &value_type);
|
||||
FLATBUFFERS_CHECKED_ERROR DoParse(const char *_source,
|
||||
const char **include_paths,
|
||||
const char *source_filename,
|
||||
const char *include_filename);
|
||||
const Definition& def, const Definition& value_type);
|
||||
FLATBUFFERS_CHECKED_ERROR DoParse(const char* _source,
|
||||
const char** include_paths,
|
||||
const char* source_filename,
|
||||
const char* include_filename);
|
||||
FLATBUFFERS_CHECKED_ERROR DoParseJson();
|
||||
FLATBUFFERS_CHECKED_ERROR CheckClash(std::vector<FieldDef *> &fields,
|
||||
StructDef *struct_def,
|
||||
const char *suffix, BaseType baseType);
|
||||
FLATBUFFERS_CHECKED_ERROR CheckClash(std::vector<FieldDef*>& fields,
|
||||
StructDef* struct_def,
|
||||
const char* suffix, BaseType baseType);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseAlignAttribute(
|
||||
const std::string &align_constant, size_t min_align, size_t *align);
|
||||
const std::string& align_constant, size_t min_align, size_t* align);
|
||||
|
||||
bool SupportsAdvancedUnionFeatures() const;
|
||||
bool SupportsAdvancedArrayFeatures() const;
|
||||
@@ -1198,27 +1201,28 @@ class Parser : public ParserState {
|
||||
bool SupportsDefaultVectorsAndStrings() const;
|
||||
bool Supports64BitOffsets() const;
|
||||
bool SupportsUnionUnderlyingType() const;
|
||||
Namespace *UniqueNamespace(Namespace *ns);
|
||||
Namespace* UniqueNamespace(Namespace* ns);
|
||||
|
||||
FLATBUFFERS_CHECKED_ERROR RecurseError();
|
||||
template<typename F> CheckedError Recurse(F f);
|
||||
template <typename F>
|
||||
CheckedError Recurse(F f);
|
||||
|
||||
const std::string &GetPooledString(const std::string &s) const;
|
||||
const std::string& GetPooledString(const std::string& s) const;
|
||||
|
||||
public:
|
||||
SymbolTable<Type> types_;
|
||||
SymbolTable<StructDef> structs_;
|
||||
SymbolTable<EnumDef> enums_;
|
||||
SymbolTable<ServiceDef> services_;
|
||||
std::vector<Namespace *> namespaces_;
|
||||
Namespace *current_namespace_;
|
||||
Namespace *empty_namespace_;
|
||||
std::vector<Namespace*> namespaces_;
|
||||
Namespace* current_namespace_;
|
||||
Namespace* empty_namespace_;
|
||||
std::string error_; // User readable error_ if Parse() == false
|
||||
|
||||
FlatBufferBuilder builder_; // any data contained in the file
|
||||
flexbuffers::Builder flex_builder_;
|
||||
flexbuffers::Reference flex_root_;
|
||||
StructDef *root_struct_def_;
|
||||
StructDef* root_struct_def_;
|
||||
std::string file_identifier_;
|
||||
std::string file_extension_;
|
||||
|
||||
@@ -1237,9 +1241,9 @@ class Parser : public ParserState {
|
||||
std::string file_being_parsed_;
|
||||
|
||||
private:
|
||||
const char *source_;
|
||||
const char* source_;
|
||||
|
||||
std::vector<std::pair<Value, FieldDef *>> field_stack_;
|
||||
std::vector<std::pair<Value, FieldDef*>> field_stack_;
|
||||
|
||||
// TODO(cneo): Refactor parser to use string_cache more often to save
|
||||
// on memory usage.
|
||||
@@ -1260,51 +1264,50 @@ class Parser : public ParserState {
|
||||
// These functions return nullptr on success, or an error string,
|
||||
// which may happen if the flatbuffer cannot be encoded in JSON (e.g.,
|
||||
// it contains non-UTF-8 byte arrays in String values).
|
||||
extern bool GenerateTextFromTable(const Parser &parser,
|
||||
const void *table,
|
||||
const std::string &tablename,
|
||||
std::string *text);
|
||||
extern const char *GenerateText(const Parser &parser, const void *flatbuffer,
|
||||
std::string *text);
|
||||
extern const char *GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
extern bool GenerateTextFromTable(const Parser& parser, const void* table,
|
||||
const std::string& tablename,
|
||||
std::string* text);
|
||||
extern const char* GenerateText(const Parser& parser, const void* flatbuffer,
|
||||
std::string* text);
|
||||
extern const char* GenerateTextFile(const Parser& parser,
|
||||
const std::string& path,
|
||||
const std::string& file_name);
|
||||
|
||||
extern const char *GenTextFromTable(const Parser &parser, const void *table,
|
||||
const std::string &tablename,
|
||||
std::string *text);
|
||||
extern const char *GenText(const Parser &parser, const void *flatbuffer,
|
||||
std::string *text);
|
||||
extern const char *GenTextFile(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name);
|
||||
extern const char* GenTextFromTable(const Parser& parser, const void* table,
|
||||
const std::string& tablename,
|
||||
std::string* text);
|
||||
extern const char* GenText(const Parser& parser, const void* flatbuffer,
|
||||
std::string* text);
|
||||
extern const char* GenTextFile(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name);
|
||||
|
||||
// Generate GRPC Cpp interfaces.
|
||||
// See idl_gen_grpc.cpp.
|
||||
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);
|
||||
|
||||
// Generate GRPC Go interfaces.
|
||||
// See idl_gen_grpc.cpp.
|
||||
bool GenerateGoGRPC(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name);
|
||||
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, const std::string &path,
|
||||
const std::string &file_name);
|
||||
bool GenerateJavaGRPC(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name);
|
||||
|
||||
// Generate GRPC Python interfaces.
|
||||
// See idl_gen_grpc.cpp.
|
||||
bool GeneratePythonGRPC(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name);
|
||||
bool GeneratePythonGRPC(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name);
|
||||
|
||||
// Generate GRPC Swift interfaces.
|
||||
// See idl_gen_grpc.cpp.
|
||||
extern bool GenerateSwiftGRPC(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name);
|
||||
extern bool GenerateSwiftGRPC(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name);
|
||||
|
||||
extern bool GenerateTSGRPC(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name);
|
||||
extern bool GenerateTSGRPC(const Parser& parser, const std::string& path,
|
||||
const std::string& file_name);
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_IDL_H_
|
||||
|
||||
@@ -42,61 +42,70 @@ struct IterationVisitor {
|
||||
// If not present, val == nullptr. set_idx is the index of all set fields.
|
||||
virtual void Field(size_t /*field_idx*/, size_t /*set_idx*/,
|
||||
ElementaryType /*type*/, bool /*is_vector*/,
|
||||
const TypeTable * /*type_table*/, const char * /*name*/,
|
||||
const uint8_t * /*val*/) {}
|
||||
const TypeTable* /*type_table*/, const char* /*name*/,
|
||||
const uint8_t* /*val*/) {}
|
||||
// Called for a value that is actually present, after a field, or as part
|
||||
// of a vector.
|
||||
virtual void UType(uint8_t, const char *) {}
|
||||
virtual void UType(uint8_t, const char*) {}
|
||||
virtual void Bool(bool) {}
|
||||
virtual void Char(int8_t, const char *) {}
|
||||
virtual void UChar(uint8_t, const char *) {}
|
||||
virtual void Short(int16_t, const char *) {}
|
||||
virtual void UShort(uint16_t, const char *) {}
|
||||
virtual void Int(int32_t, const char *) {}
|
||||
virtual void UInt(uint32_t, const char *) {}
|
||||
virtual void Char(int8_t, const char*) {}
|
||||
virtual void UChar(uint8_t, const char*) {}
|
||||
virtual void Short(int16_t, const char*) {}
|
||||
virtual void UShort(uint16_t, const char*) {}
|
||||
virtual void Int(int32_t, const char*) {}
|
||||
virtual void UInt(uint32_t, const char*) {}
|
||||
virtual void Long(int64_t) {}
|
||||
virtual void ULong(uint64_t) {}
|
||||
virtual void Float(float) {}
|
||||
virtual void Double(double) {}
|
||||
virtual void String(const String *) {}
|
||||
virtual void Unknown(const uint8_t *) {} // From a future version.
|
||||
virtual void String(const String*) {}
|
||||
virtual void Unknown(const uint8_t*) {} // From a future version.
|
||||
// These mark the scope of a vector.
|
||||
virtual void StartVector() {}
|
||||
virtual void EndVector() {}
|
||||
virtual void Element(size_t /*i*/, ElementaryType /*type*/,
|
||||
const TypeTable * /*type_table*/,
|
||||
const uint8_t * /*val*/) {}
|
||||
const TypeTable* /*type_table*/,
|
||||
const uint8_t* /*val*/) {}
|
||||
virtual ~IterationVisitor() {}
|
||||
};
|
||||
|
||||
inline size_t InlineSize(ElementaryType type, const TypeTable *type_table) {
|
||||
inline size_t InlineSize(ElementaryType type, const TypeTable* type_table) {
|
||||
switch (type) {
|
||||
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_UNION:
|
||||
return 4;
|
||||
case ST_STRUCT:
|
||||
return static_cast<size_t>(type_table->values[type_table->num_elems]);
|
||||
default: FLATBUFFERS_ASSERT(false); return 1;
|
||||
default:
|
||||
FLATBUFFERS_ASSERT(false);
|
||||
return 1;
|
||||
}
|
||||
default: FLATBUFFERS_ASSERT(false); return 1;
|
||||
default:
|
||||
FLATBUFFERS_ASSERT(false);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
inline int64_t LookupEnum(int64_t enum_val, const int64_t *values,
|
||||
inline int64_t LookupEnum(int64_t enum_val, const int64_t* values,
|
||||
size_t num_values) {
|
||||
if (!values) return enum_val;
|
||||
for (size_t i = 0; i < num_values; i++) {
|
||||
@@ -105,7 +114,8 @@ inline int64_t LookupEnum(int64_t enum_val, const int64_t *values,
|
||||
return -1; // Unknown enum value.
|
||||
}
|
||||
|
||||
template<typename T> const char *EnumName(T tval, const TypeTable *type_table) {
|
||||
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<int64_t>(tval), type_table->values,
|
||||
type_table->num_elems);
|
||||
@@ -115,12 +125,12 @@ template<typename T> const char *EnumName(T tval, const TypeTable *type_table) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void IterateObject(const uint8_t *obj, const TypeTable *type_table,
|
||||
IterationVisitor *visitor);
|
||||
void IterateObject(const uint8_t* obj, const TypeTable* type_table,
|
||||
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) {
|
||||
inline void IterateValue(ElementaryType type, const uint8_t* val,
|
||||
const TypeTable* type_table, const uint8_t* prev_val,
|
||||
soffset_t vector_index, IterationVisitor* visitor) {
|
||||
switch (type) {
|
||||
case ET_UTYPE: {
|
||||
auto tval = ReadScalar<uint8_t>(val);
|
||||
@@ -179,7 +189,7 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
|
||||
}
|
||||
case ET_STRING: {
|
||||
val += ReadScalar<uoffset_t>(val);
|
||||
visitor->String(reinterpret_cast<const String *>(val));
|
||||
visitor->String(reinterpret_cast<const String*>(val));
|
||||
break;
|
||||
}
|
||||
case ET_SEQUENCE: {
|
||||
@@ -188,13 +198,15 @@ 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);
|
||||
FLATBUFFERS_ASSERT(prev_val);
|
||||
auto union_type = *prev_val; // Always a uint8_t.
|
||||
if (vector_index >= 0) {
|
||||
auto type_vec = reinterpret_cast<const Vector<uint8_t> *>(prev_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 =
|
||||
@@ -209,16 +221,19 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
|
||||
break;
|
||||
}
|
||||
case ET_STRING:
|
||||
visitor->String(reinterpret_cast<const String *>(val));
|
||||
visitor->String(reinterpret_cast<const String*>(val));
|
||||
break;
|
||||
default: visitor->Unknown(val);
|
||||
default:
|
||||
visitor->Unknown(val);
|
||||
}
|
||||
} else {
|
||||
visitor->Unknown(val);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ST_ENUM: FLATBUFFERS_ASSERT(false); break;
|
||||
case ST_ENUM:
|
||||
FLATBUFFERS_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -229,10 +244,10 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
|
||||
}
|
||||
}
|
||||
|
||||
inline void IterateObject(const uint8_t *obj, const TypeTable *type_table,
|
||||
IterationVisitor *visitor) {
|
||||
inline void IterateObject(const uint8_t* obj, const TypeTable* type_table,
|
||||
IterationVisitor* visitor) {
|
||||
visitor->StartSequence();
|
||||
const uint8_t *prev_val = nullptr;
|
||||
const uint8_t* prev_val = nullptr;
|
||||
size_t set_idx = 0;
|
||||
size_t array_idx = 0;
|
||||
for (size_t i = 0; i < type_table->num_elems; i++) {
|
||||
@@ -240,12 +255,14 @@ inline void IterateObject(const uint8_t *obj, const TypeTable *type_table,
|
||||
auto type = static_cast<ElementaryType>(type_code.base_type);
|
||||
auto is_repeating = type_code.is_repeating != 0;
|
||||
auto ref_idx = type_code.sequence_ref;
|
||||
const TypeTable *ref = nullptr;
|
||||
if (ref_idx >= 0) { ref = type_table->type_refs[ref_idx](); }
|
||||
const TypeTable* ref = nullptr;
|
||||
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;
|
||||
const uint8_t* val = nullptr;
|
||||
if (type_table->st == ST_TABLE) {
|
||||
val = reinterpret_cast<const Table *>(obj)->GetAddressOf(
|
||||
val = reinterpret_cast<const Table*>(obj)->GetAddressOf(
|
||||
FieldIndexToOffset(static_cast<voffset_t>(i)));
|
||||
} else {
|
||||
val = obj + type_table->values[i];
|
||||
@@ -259,7 +276,7 @@ inline void IterateObject(const uint8_t *obj, const TypeTable *type_table,
|
||||
if (type_table->st == ST_TABLE) {
|
||||
// variable length vector
|
||||
val += ReadScalar<uoffset_t>(val);
|
||||
auto vec = reinterpret_cast<const Vector<uint8_t> *>(val);
|
||||
auto vec = reinterpret_cast<const Vector<uint8_t>*>(val);
|
||||
elem_ptr = vec->Data();
|
||||
size = vec->size();
|
||||
} else {
|
||||
@@ -284,9 +301,9 @@ inline void IterateObject(const uint8_t *obj, const TypeTable *type_table,
|
||||
visitor->EndSequence();
|
||||
}
|
||||
|
||||
inline void IterateFlatBuffer(const uint8_t *buffer,
|
||||
const TypeTable *type_table,
|
||||
IterationVisitor *callback) {
|
||||
inline void IterateFlatBuffer(const uint8_t* buffer,
|
||||
const TypeTable* type_table,
|
||||
IterationVisitor* callback) {
|
||||
IterateObject(GetRoot<uint8_t>(buffer), type_table, callback);
|
||||
}
|
||||
|
||||
@@ -315,7 +332,9 @@ struct ToStringVisitor : public IterationVisitor {
|
||||
vector_delimited(true) {}
|
||||
|
||||
void append_indent() {
|
||||
for (size_t i = 0; i < indent_level; i++) { s += in; }
|
||||
for (size_t i = 0; i < indent_level; i++) {
|
||||
s += in;
|
||||
}
|
||||
}
|
||||
|
||||
void StartSequence() {
|
||||
@@ -330,8 +349,8 @@ struct ToStringVisitor : public IterationVisitor {
|
||||
s += "}";
|
||||
}
|
||||
void Field(size_t /*field_idx*/, size_t set_idx, ElementaryType /*type*/,
|
||||
bool /*is_vector*/, const TypeTable * /*type_table*/,
|
||||
const char *name, const uint8_t *val) {
|
||||
bool /*is_vector*/, const TypeTable* /*type_table*/,
|
||||
const char* name, const uint8_t* val) {
|
||||
if (!val) return;
|
||||
if (set_idx) {
|
||||
s += ",";
|
||||
@@ -345,7 +364,8 @@ struct ToStringVisitor : public IterationVisitor {
|
||||
s += ": ";
|
||||
}
|
||||
}
|
||||
template<typename T> void Named(T x, const char *name) {
|
||||
template <typename T>
|
||||
void Named(T x, const char* name) {
|
||||
if (name) {
|
||||
if (q) s += "\"";
|
||||
s += name;
|
||||
@@ -354,22 +374,22 @@ struct ToStringVisitor : public IterationVisitor {
|
||||
s += NumToString(x);
|
||||
}
|
||||
}
|
||||
void UType(uint8_t x, const char *name) { Named(x, name); }
|
||||
void UType(uint8_t x, const char* name) { Named(x, name); }
|
||||
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 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 String(const struct String *str) {
|
||||
void String(const struct String* str) {
|
||||
EscapeString(str->c_str(), str->size(), &s, true, false);
|
||||
}
|
||||
void Unknown(const uint8_t *) { s += "(?)"; }
|
||||
void Unknown(const uint8_t*) { s += "(?)"; }
|
||||
void StartVector() {
|
||||
s += "[";
|
||||
if (vector_delimited) {
|
||||
@@ -391,7 +411,7 @@ struct ToStringVisitor : public IterationVisitor {
|
||||
s += "]";
|
||||
}
|
||||
void Element(size_t i, ElementaryType /*type*/,
|
||||
const TypeTable * /*type_table*/, const uint8_t * /*val*/) {
|
||||
const TypeTable* /*type_table*/, const uint8_t* /*val*/) {
|
||||
if (i) {
|
||||
s += ",";
|
||||
if (vector_delimited) {
|
||||
@@ -404,11 +424,11 @@ struct ToStringVisitor : public IterationVisitor {
|
||||
}
|
||||
};
|
||||
|
||||
inline std::string FlatBufferToString(const uint8_t *buffer,
|
||||
const TypeTable *type_table,
|
||||
inline std::string FlatBufferToString(const uint8_t* buffer,
|
||||
const TypeTable* type_table,
|
||||
bool multi_line = false,
|
||||
bool vector_delimited = true,
|
||||
const std::string &indent = "",
|
||||
const std::string& indent = "",
|
||||
bool quotes = false) {
|
||||
ToStringVisitor tostring_visitor(multi_line ? "\n" : " ", quotes, indent,
|
||||
vector_delimited);
|
||||
|
||||
@@ -47,28 +47,28 @@ inline bool IsLong(reflection::BaseType t) {
|
||||
inline size_t GetTypeSize(reflection::BaseType base_type) {
|
||||
// This needs to correspond to the BaseType enum.
|
||||
static size_t sizes[] = {
|
||||
0, // None
|
||||
1, // UType
|
||||
1, // Bool
|
||||
1, // Byte
|
||||
1, // UByte
|
||||
2, // Short
|
||||
2, // UShort
|
||||
4, // Int
|
||||
4, // UInt
|
||||
8, // Long
|
||||
8, // ULong
|
||||
4, // Float
|
||||
8, // Double
|
||||
4, // String
|
||||
4, // Vector
|
||||
4, // Obj
|
||||
4, // Union
|
||||
0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds
|
||||
// errors.
|
||||
8, // Vector64
|
||||
0, // None
|
||||
1, // UType
|
||||
1, // Bool
|
||||
1, // Byte
|
||||
1, // UByte
|
||||
2, // Short
|
||||
2, // UShort
|
||||
4, // Int
|
||||
4, // UInt
|
||||
8, // Long
|
||||
8, // ULong
|
||||
4, // Float
|
||||
8, // Double
|
||||
4, // String
|
||||
4, // Vector
|
||||
4, // Obj
|
||||
4, // Union
|
||||
0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds
|
||||
// errors.
|
||||
8, // Vector64
|
||||
|
||||
0 // MaxBaseType. This must be kept the last entry in this array.
|
||||
0 // MaxBaseType. This must be kept the last entry in this array.
|
||||
};
|
||||
static_assert(sizeof(sizes) / sizeof(size_t) == reflection::MaxBaseType + 1,
|
||||
"Size of sizes[] array does not match the count of BaseType "
|
||||
@@ -79,7 +79,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,
|
||||
const reflection::Schema &schema) {
|
||||
const reflection::Schema& schema) {
|
||||
if (base_type == reflection::Obj &&
|
||||
schema.objects()->Get(type_index)->is_struct()) {
|
||||
return schema.objects()->Get(type_index)->bytesize();
|
||||
@@ -89,119 +89,121 @@ inline size_t GetTypeSizeInline(reflection::BaseType base_type, int type_index,
|
||||
}
|
||||
|
||||
// Get the root, regardless of what type it is.
|
||||
inline Table *GetAnyRoot(uint8_t *const flatbuf) {
|
||||
inline Table* GetAnyRoot(uint8_t* const flatbuf) {
|
||||
return GetMutableRoot<Table>(flatbuf);
|
||||
}
|
||||
|
||||
inline const Table *GetAnyRoot(const uint8_t *const flatbuf) {
|
||||
inline const Table* GetAnyRoot(const uint8_t* const flatbuf) {
|
||||
return GetRoot<Table>(flatbuf);
|
||||
}
|
||||
|
||||
inline Table *GetAnySizePrefixedRoot(uint8_t *const flatbuf) {
|
||||
inline Table* GetAnySizePrefixedRoot(uint8_t* const flatbuf) {
|
||||
return GetMutableSizePrefixedRoot<Table>(flatbuf);
|
||||
}
|
||||
|
||||
inline const Table *GetAnySizePrefixedRoot(const uint8_t *const flatbuf) {
|
||||
inline const Table* GetAnySizePrefixedRoot(const uint8_t* const flatbuf) {
|
||||
return GetSizePrefixedRoot<Table>(flatbuf);
|
||||
}
|
||||
|
||||
// Get a field's default, if you know it's an integer, and its exact type.
|
||||
template<typename T> T GetFieldDefaultI(const reflection::Field &field) {
|
||||
template <typename T>
|
||||
T GetFieldDefaultI(const reflection::Field& field) {
|
||||
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
|
||||
return static_cast<T>(field.default_integer());
|
||||
}
|
||||
|
||||
// Get a field's default, if you know it's floating point and its exact type.
|
||||
template<typename T> T GetFieldDefaultF(const reflection::Field &field) {
|
||||
template <typename T>
|
||||
T GetFieldDefaultF(const reflection::Field& field) {
|
||||
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
|
||||
return static_cast<T>(field.default_real());
|
||||
}
|
||||
|
||||
// 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) {
|
||||
FLATBUFFERS_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) {
|
||||
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
|
||||
return table.GetField<T>(field.offset(),
|
||||
static_cast<T>(field.default_real()));
|
||||
}
|
||||
|
||||
// Get a field, if you know it's a string.
|
||||
inline const String *GetFieldS(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
inline const String* GetFieldS(const Table& table,
|
||||
const reflection::Field& field) {
|
||||
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::String);
|
||||
return table.GetPointer<const String *>(field.offset());
|
||||
return table.GetPointer<const String*>(field.offset());
|
||||
}
|
||||
|
||||
// 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) {
|
||||
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Vector &&
|
||||
sizeof(T) == GetTypeSize(field.type()->element()));
|
||||
return table.GetPointer<Vector<T> *>(field.offset());
|
||||
return table.GetPointer<Vector<T>*>(field.offset());
|
||||
}
|
||||
|
||||
// Get a field, if you know it's a vector, generically.
|
||||
// To actually access elements, use the return value together with
|
||||
// field.type()->element() in any of GetAnyVectorElemI below etc.
|
||||
inline VectorOfAny *GetFieldAnyV(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
return table.GetPointer<VectorOfAny *>(field.offset());
|
||||
inline VectorOfAny* GetFieldAnyV(const Table& table,
|
||||
const reflection::Field& field) {
|
||||
return table.GetPointer<VectorOfAny*>(field.offset());
|
||||
}
|
||||
|
||||
// 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) {
|
||||
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj ||
|
||||
field.type()->base_type() == reflection::Union);
|
||||
return table.GetPointer<Table *>(field.offset());
|
||||
return table.GetPointer<Table*>(field.offset());
|
||||
}
|
||||
|
||||
// Get a field, if you know it's a struct.
|
||||
inline const Struct *GetFieldStruct(const Table &table,
|
||||
const reflection::Field &field) {
|
||||
inline const Struct* GetFieldStruct(const Table& table,
|
||||
const reflection::Field& field) {
|
||||
// TODO: This does NOT check if the field is a table or struct, but we'd need
|
||||
// access to the schema to check the is_struct flag.
|
||||
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj);
|
||||
return table.GetStruct<const Struct *>(field.offset());
|
||||
return table.GetStruct<const Struct*>(field.offset());
|
||||
}
|
||||
|
||||
// Get a structure's field, if you know it's a struct.
|
||||
inline const Struct *GetFieldStruct(const Struct &structure,
|
||||
const reflection::Field &field) {
|
||||
inline const Struct* GetFieldStruct(const Struct& structure,
|
||||
const reflection::Field& field) {
|
||||
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj);
|
||||
return structure.GetStruct<const Struct *>(field.offset());
|
||||
return structure.GetStruct<const Struct*>(field.offset());
|
||||
}
|
||||
|
||||
// Raw helper functions used below: get any value in memory as a 64bit int, a
|
||||
// double or a string.
|
||||
// All scalars get static_cast to an int64_t, strings use strtoull, every other
|
||||
// data type returns 0.
|
||||
int64_t GetAnyValueI(reflection::BaseType type, const uint8_t *data);
|
||||
int64_t GetAnyValueI(reflection::BaseType type, const uint8_t* data);
|
||||
// All scalars static cast to double, strings use strtod, every other data
|
||||
// type is 0.0.
|
||||
double GetAnyValueF(reflection::BaseType type, const uint8_t *data);
|
||||
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);
|
||||
std::string GetAnyValueS(reflection::BaseType type, const uint8_t* data,
|
||||
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,
|
||||
const reflection::Field &field) {
|
||||
inline int64_t GetAnyFieldI(const Table& table,
|
||||
const reflection::Field& field) {
|
||||
auto field_ptr = table.GetAddressOf(field.offset());
|
||||
return field_ptr ? GetAnyValueI(field.type()->base_type(), field_ptr)
|
||||
: field.default_integer();
|
||||
}
|
||||
|
||||
// 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();
|
||||
@@ -210,9 +212,9 @@ inline double GetAnyFieldF(const Table &table, const reflection::Field &field) {
|
||||
// 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.
|
||||
inline std::string GetAnyFieldS(const Table &table,
|
||||
const reflection::Field &field,
|
||||
const reflection::Schema *schema) {
|
||||
inline std::string GetAnyFieldS(const Table& table,
|
||||
const reflection::Field& field,
|
||||
const reflection::Schema* schema) {
|
||||
auto field_ptr = table.GetAddressOf(field.offset());
|
||||
return field_ptr ? GetAnyValueS(field.type()->base_type(), field_ptr, schema,
|
||||
field.type()->index())
|
||||
@@ -220,38 +222,38 @@ 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()));
|
||||
}
|
||||
|
||||
// Get any struct field as a string, regardless of what type it is.
|
||||
inline std::string GetAnyFieldS(const Struct &st,
|
||||
const reflection::Field &field) {
|
||||
inline std::string GetAnyFieldS(const Struct& st,
|
||||
const reflection::Field& field) {
|
||||
return GetAnyValueS(field.type()->base_type(),
|
||||
st.GetAddressOf(field.offset()), nullptr, -1);
|
||||
}
|
||||
|
||||
// Get any vector element as a 64bit int, regardless of what type it is.
|
||||
inline int64_t GetAnyVectorElemI(const VectorOfAny *vec,
|
||||
inline int64_t GetAnyVectorElemI(const VectorOfAny* vec,
|
||||
reflection::BaseType elem_type, size_t i) {
|
||||
return GetAnyValueI(elem_type, vec->Data() + GetTypeSize(elem_type) * i);
|
||||
}
|
||||
|
||||
// Get any vector element as a double, regardless of what type it is.
|
||||
inline double GetAnyVectorElemF(const VectorOfAny *vec,
|
||||
inline double GetAnyVectorElemF(const VectorOfAny* vec,
|
||||
reflection::BaseType elem_type, size_t i) {
|
||||
return GetAnyValueF(elem_type, vec->Data() + GetTypeSize(elem_type) * i);
|
||||
}
|
||||
|
||||
// Get any vector element as a string, regardless of what type it is.
|
||||
inline std::string GetAnyVectorElemS(const VectorOfAny *vec,
|
||||
inline std::string GetAnyVectorElemS(const VectorOfAny* vec,
|
||||
reflection::BaseType elem_type, size_t i) {
|
||||
return GetAnyValueS(elem_type, vec->Data() + GetTypeSize(elem_type) * i,
|
||||
nullptr, -1);
|
||||
@@ -260,10 +262,10 @@ 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 reinterpret_cast<T *>(elem_ptr + ReadScalar<uoffset_t>(elem_ptr));
|
||||
return reinterpret_cast<T*>(elem_ptr + ReadScalar<uoffset_t>(elem_ptr));
|
||||
}
|
||||
|
||||
// Get the inline-address of a vector element. Useful for Structs (pass Struct
|
||||
@@ -271,37 +273,39 @@ T *GetAnyVectorElemPointer(const VectorOfAny *vec, size_t i) {
|
||||
// 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,
|
||||
template <typename T>
|
||||
T* GetAnyVectorElemAddressOf(const VectorOfAny* vec, size_t i,
|
||||
size_t elem_size) {
|
||||
return reinterpret_cast<T *>(vec->Data() + elem_size * i);
|
||||
return reinterpret_cast<T*>(vec->Data() + elem_size * i);
|
||||
}
|
||||
|
||||
// Similarly, for elements of tables.
|
||||
template<typename T>
|
||||
T *GetAnyFieldAddressOf(const Table &table, const reflection::Field &field) {
|
||||
return reinterpret_cast<T *>(table.GetAddressOf(field.offset()));
|
||||
template <typename T>
|
||||
T* GetAnyFieldAddressOf(const Table& table, const reflection::Field& field) {
|
||||
return reinterpret_cast<T*>(table.GetAddressOf(field.offset()));
|
||||
}
|
||||
|
||||
// Similarly, for elements of structs.
|
||||
template<typename T>
|
||||
T *GetAnyFieldAddressOf(const Struct &st, const reflection::Field &field) {
|
||||
return reinterpret_cast<T *>(st.GetAddressOf(field.offset()));
|
||||
template <typename T>
|
||||
T* GetAnyFieldAddressOf(const Struct& st, const reflection::Field& field) {
|
||||
return reinterpret_cast<T*>(st.GetAddressOf(field.offset()));
|
||||
}
|
||||
|
||||
// Loop over all the fields of the provided `object` and call `func` on each one
|
||||
// in increasing order by their field->id(). If `reverse` is true, `func` is
|
||||
// called in descending order
|
||||
void ForAllFields(const reflection::Object *object, bool reverse,
|
||||
std::function<void(const reflection::Field *)> func);
|
||||
void ForAllFields(const reflection::Object* object, bool reverse,
|
||||
std::function<void(const reflection::Field*)> func);
|
||||
|
||||
// ------------------------- 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;
|
||||
}
|
||||
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(type));
|
||||
T def;
|
||||
if (IsInteger(type)) {
|
||||
@@ -317,12 +321,12 @@ bool SetField(Table *table, const reflection::Field &field, T val) {
|
||||
// double or a string.
|
||||
// These work for all scalar values, but do nothing for other data types.
|
||||
// To set a string, see SetString below.
|
||||
void SetAnyValueI(reflection::BaseType type, uint8_t *data, int64_t val);
|
||||
void SetAnyValueF(reflection::BaseType type, uint8_t *data, double val);
|
||||
void SetAnyValueS(reflection::BaseType type, uint8_t *data, const char *val);
|
||||
void SetAnyValueI(reflection::BaseType type, uint8_t* data, int64_t val);
|
||||
void SetAnyValueF(reflection::BaseType type, uint8_t* data, double val);
|
||||
void SetAnyValueS(reflection::BaseType type, uint8_t* data, const char* val);
|
||||
|
||||
// Set any table field as a 64bit int, regardless of type what it is.
|
||||
inline bool SetAnyFieldI(Table *table, const reflection::Field &field,
|
||||
inline bool SetAnyFieldI(Table* table, const reflection::Field& field,
|
||||
int64_t val) {
|
||||
auto field_ptr = table->GetAddressOf(field.offset());
|
||||
if (!field_ptr) return val == GetFieldDefaultI<int64_t>(field);
|
||||
@@ -331,7 +335,7 @@ inline bool SetAnyFieldI(Table *table, const reflection::Field &field,
|
||||
}
|
||||
|
||||
// Set any table field as a double, regardless of what type it is.
|
||||
inline bool SetAnyFieldF(Table *table, const reflection::Field &field,
|
||||
inline bool SetAnyFieldF(Table* table, const reflection::Field& field,
|
||||
double val) {
|
||||
auto field_ptr = table->GetAddressOf(field.offset());
|
||||
if (!field_ptr) return val == GetFieldDefaultF<double>(field);
|
||||
@@ -340,8 +344,8 @@ 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) {
|
||||
inline bool SetAnyFieldS(Table* table, const reflection::Field& field,
|
||||
const char* val) {
|
||||
auto field_ptr = table->GetAddressOf(field.offset());
|
||||
if (!field_ptr) return false;
|
||||
SetAnyValueS(field.type()->base_type(), field_ptr, val);
|
||||
@@ -349,41 +353,41 @@ inline bool SetAnyFieldS(Table *table, const reflection::Field &field,
|
||||
}
|
||||
|
||||
// Set any struct field as a 64bit int, regardless of type what it is.
|
||||
inline void SetAnyFieldI(Struct *st, const reflection::Field &field,
|
||||
inline void SetAnyFieldI(Struct* st, const reflection::Field& field,
|
||||
int64_t val) {
|
||||
SetAnyValueI(field.type()->base_type(), st->GetAddressOf(field.offset()),
|
||||
val);
|
||||
}
|
||||
|
||||
// Set any struct field as a double, regardless of type what it is.
|
||||
inline void SetAnyFieldF(Struct *st, const reflection::Field &field,
|
||||
inline void SetAnyFieldF(Struct* st, const reflection::Field& field,
|
||||
double val) {
|
||||
SetAnyValueF(field.type()->base_type(), st->GetAddressOf(field.offset()),
|
||||
val);
|
||||
}
|
||||
|
||||
// Set any struct field as a string, regardless of type what it is.
|
||||
inline void SetAnyFieldS(Struct *st, const reflection::Field &field,
|
||||
const char *val) {
|
||||
inline void SetAnyFieldS(Struct* st, const reflection::Field& field,
|
||||
const char* val) {
|
||||
SetAnyValueS(field.type()->base_type(), st->GetAddressOf(field.offset()),
|
||||
val);
|
||||
}
|
||||
|
||||
// Set any vector element as a 64bit int, regardless of type what it is.
|
||||
inline void SetAnyVectorElemI(VectorOfAny *vec, reflection::BaseType elem_type,
|
||||
inline void SetAnyVectorElemI(VectorOfAny* vec, reflection::BaseType elem_type,
|
||||
size_t i, int64_t val) {
|
||||
SetAnyValueI(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
|
||||
}
|
||||
|
||||
// Set any vector element as a double, regardless of type what it is.
|
||||
inline void SetAnyVectorElemF(VectorOfAny *vec, reflection::BaseType elem_type,
|
||||
inline void SetAnyVectorElemF(VectorOfAny* vec, reflection::BaseType elem_type,
|
||||
size_t i, double val) {
|
||||
SetAnyValueF(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
|
||||
}
|
||||
|
||||
// Set any vector element as a string, regardless of type what it is.
|
||||
inline void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type,
|
||||
size_t i, const char *val) {
|
||||
inline void SetAnyVectorElemS(VectorOfAny* vec, reflection::BaseType elem_type,
|
||||
size_t i, const char* val) {
|
||||
SetAnyValueS(elem_type, vec->Data() + GetTypeSize(elem_type) * i, val);
|
||||
}
|
||||
|
||||
@@ -391,36 +395,37 @@ inline void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type,
|
||||
|
||||
// "smart" pointer for use with resizing vectors: turns a pointer inside
|
||||
// a vector into a relative offset, such that it is not affected by resizes.
|
||||
template<typename T, typename U> class pointer_inside_vector {
|
||||
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 *>(vec.data())),
|
||||
pointer_inside_vector(T* ptr, std::vector<U>& vec)
|
||||
: offset_(reinterpret_cast<uint8_t*>(ptr) -
|
||||
reinterpret_cast<uint8_t*>(vec.data())),
|
||||
vec_(vec) {}
|
||||
|
||||
T *operator*() const {
|
||||
return reinterpret_cast<T *>(reinterpret_cast<uint8_t *>(vec_.data()) +
|
||||
offset_);
|
||||
T* operator*() const {
|
||||
return reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(vec_.data()) +
|
||||
offset_);
|
||||
}
|
||||
T *operator->() const { return operator*(); }
|
||||
T* operator->() const { return operator*(); }
|
||||
|
||||
private:
|
||||
size_t offset_;
|
||||
std::vector<U> &vec_;
|
||||
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);
|
||||
}
|
||||
|
||||
inline const char *UnionTypeFieldSuffix() { return "_type"; }
|
||||
inline const char* UnionTypeFieldSuffix() { return "_type"; }
|
||||
|
||||
// Helper to figure out the actual table type a union refers to.
|
||||
inline const reflection::Object &GetUnionType(
|
||||
const reflection::Schema &schema, const reflection::Object &parent,
|
||||
const reflection::Field &unionfield, const Table &table) {
|
||||
inline const reflection::Object& GetUnionType(
|
||||
const reflection::Schema& schema, const reflection::Object& parent,
|
||||
const reflection::Field& unionfield, const Table& table) {
|
||||
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(
|
||||
@@ -436,27 +441,27 @@ inline const reflection::Object &GetUnionType(
|
||||
// "str" must live inside "flatbuf" and may be invalidated after this call.
|
||||
// If your FlatBuffer's root table is not the schema's root table, you should
|
||||
// pass in your root_table type as well.
|
||||
void SetString(const reflection::Schema &schema, const std::string &val,
|
||||
const String *str, std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table = nullptr);
|
||||
void SetString(const reflection::Schema& schema, const std::string& val,
|
||||
const String* str, std::vector<uint8_t>* flatbuf,
|
||||
const reflection::Object* root_table = nullptr);
|
||||
|
||||
// Resizes a flatbuffers::Vector inside a FlatBuffer. FlatBuffer must
|
||||
// live inside a std::vector so we can resize the buffer if needed.
|
||||
// "vec" must live inside "flatbuf" and may be invalidated after this call.
|
||||
// If your FlatBuffer's root table is not the schema's root table, you should
|
||||
// pass in your root_table type as well.
|
||||
uint8_t *ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize,
|
||||
const VectorOfAny *vec, uoffset_t num_elems,
|
||||
uoffset_t elem_size, std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table = nullptr);
|
||||
uint8_t* ResizeAnyVector(const reflection::Schema& schema, uoffset_t newsize,
|
||||
const VectorOfAny* vec, uoffset_t num_elems,
|
||||
uoffset_t elem_size, std::vector<uint8_t>* flatbuf,
|
||||
const reflection::Object* root_table = nullptr);
|
||||
|
||||
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) {
|
||||
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(),
|
||||
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++) {
|
||||
@@ -465,7 +470,7 @@ void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val,
|
||||
if (is_scalar) {
|
||||
WriteScalar(loc, val);
|
||||
} else { // struct
|
||||
*reinterpret_cast<T *>(loc) = val;
|
||||
*reinterpret_cast<T*>(loc) = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -478,11 +483,11 @@ void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val,
|
||||
// existing one.
|
||||
// The return value can now be set using Vector::MutateOffset or SetFieldT
|
||||
// below.
|
||||
const uint8_t *AddFlatBuffer(std::vector<uint8_t> &flatbuf,
|
||||
const uint8_t *newbuf, size_t newlen);
|
||||
const uint8_t* AddFlatBuffer(std::vector<uint8_t>& flatbuf,
|
||||
const uint8_t* newbuf, size_t newlen);
|
||||
|
||||
inline bool SetFieldT(Table *table, const reflection::Field &field,
|
||||
const uint8_t *val) {
|
||||
inline bool SetFieldT(Table* table, const reflection::Field& field,
|
||||
const uint8_t* val) {
|
||||
FLATBUFFERS_ASSERT(sizeof(uoffset_t) ==
|
||||
GetTypeSize(field.type()->base_type()));
|
||||
return table->SetPointer(field.offset(), val);
|
||||
@@ -499,22 +504,22 @@ inline bool SetFieldT(Table *table, const reflection::Field &field,
|
||||
// DAG, the copy will be a tree instead (with duplicates). Strings can be
|
||||
// shared however, by passing true for use_string_pooling.
|
||||
|
||||
Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
||||
const reflection::Schema &schema,
|
||||
const reflection::Object &objectdef,
|
||||
const Table &table,
|
||||
bool use_string_pooling = false);
|
||||
Offset<const Table*> CopyTable(FlatBufferBuilder& fbb,
|
||||
const reflection::Schema& schema,
|
||||
const reflection::Object& objectdef,
|
||||
const Table& table,
|
||||
bool use_string_pooling = false);
|
||||
|
||||
// Verifies the provided flatbuffer using reflection.
|
||||
// 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, uoffset_t max_depth = 64,
|
||||
bool Verify(const reflection::Schema& schema, const reflection::Object& root,
|
||||
const uint8_t* buf, size_t length, uoffset_t max_depth = 64,
|
||||
uoffset_t max_tables = 1000000);
|
||||
|
||||
bool VerifySizePrefixed(const reflection::Schema &schema,
|
||||
const reflection::Object &root, const uint8_t *buf,
|
||||
bool VerifySizePrefixed(const reflection::Schema& schema,
|
||||
const reflection::Object& root, const uint8_t* buf,
|
||||
size_t length, uoffset_t max_depth = 64,
|
||||
uoffset_t max_tables = 1000000);
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ class Registry {
|
||||
public:
|
||||
// Call this for all schemas that may be in use. The identifier has
|
||||
// a function in the generated code, e.g. MonsterIdentifier().
|
||||
void Register(const char *file_identifier, const char *schema_path) {
|
||||
void Register(const char* file_identifier, const char* schema_path) {
|
||||
Schema schema;
|
||||
schema.path_ = schema_path;
|
||||
schemas_[file_identifier] = schema;
|
||||
@@ -38,7 +38,7 @@ 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) + kFileIdentifierLength) {
|
||||
@@ -46,7 +46,7 @@ class Registry {
|
||||
return false;
|
||||
}
|
||||
std::string ident(
|
||||
reinterpret_cast<const char *>(flatbuf) + sizeof(uoffset_t),
|
||||
reinterpret_cast<const char*>(flatbuf) + sizeof(uoffset_t),
|
||||
kFileIdentifierLength);
|
||||
// Load and parse the schema.
|
||||
Parser parser;
|
||||
@@ -64,8 +64,8 @@ class Registry {
|
||||
// Converts a binary buffer to text using one of the schemas in the registry,
|
||||
// use the file_identifier to indicate which.
|
||||
// If DetachedBuffer::data() is null then parsing failed.
|
||||
DetachedBuffer TextToFlatBuffer(const char *text,
|
||||
const char *file_identifier) {
|
||||
DetachedBuffer TextToFlatBuffer(const char* text,
|
||||
const char* file_identifier) {
|
||||
// Load and parse the schema.
|
||||
Parser parser;
|
||||
if (!LoadSchema(file_identifier, &parser)) return DetachedBuffer();
|
||||
@@ -79,17 +79,17 @@ class Registry {
|
||||
}
|
||||
|
||||
// Modify any parsing / output options used by the other functions.
|
||||
void SetOptions(const IDLOptions &opts) { opts_ = opts; }
|
||||
void SetOptions(const IDLOptions& opts) { opts_ = opts; }
|
||||
|
||||
// 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_; }
|
||||
const std::string& GetLastError() { return lasterror_; }
|
||||
|
||||
private:
|
||||
bool LoadSchema(const std::string &ident, Parser *parser) {
|
||||
bool LoadSchema(const std::string& ident, Parser* parser) {
|
||||
// Find the schema, if not, exit.
|
||||
auto it = schemas_.find(ident);
|
||||
if (it == schemas_.end()) {
|
||||
@@ -97,7 +97,7 @@ class Registry {
|
||||
lasterror_ = "identifier for this buffer not in the registry";
|
||||
return false;
|
||||
}
|
||||
auto &schema = it->second;
|
||||
auto& schema = it->second;
|
||||
// Load the schema from disk. If not, exit.
|
||||
std::string schematext;
|
||||
if (!LoadFile(schema.path_.c_str(), false, &schematext)) {
|
||||
@@ -121,7 +121,7 @@ class Registry {
|
||||
|
||||
std::string lasterror_;
|
||||
IDLOptions opts_;
|
||||
std::vector<const char *> include_paths_;
|
||||
std::vector<const char*> include_paths_;
|
||||
std::map<std::string, Schema> schemas_;
|
||||
};
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
namespace flatbuffers {
|
||||
|
||||
struct String : public Vector<char> {
|
||||
const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
|
||||
const char* c_str() const { return reinterpret_cast<const char*>(Data()); }
|
||||
std::string str() const { return std::string(c_str(), size()); }
|
||||
|
||||
// clang-format off
|
||||
@@ -39,27 +39,27 @@ struct String : public Vector<char> {
|
||||
#endif // FLATBUFFERS_HAS_STRING_VIEW
|
||||
// clang-format on
|
||||
|
||||
bool operator<(const String &o) const {
|
||||
bool operator<(const String& o) const {
|
||||
return StringLessThan(this->data(), this->size(), o.data(), o.size());
|
||||
}
|
||||
};
|
||||
|
||||
// Convenience function to get std::string from a String returning an empty
|
||||
// string on null pointer.
|
||||
static inline std::string GetString(const String *str) {
|
||||
static inline std::string GetString(const String* str) {
|
||||
return str ? str->str() : "";
|
||||
}
|
||||
|
||||
// Convenience function to get char* from a String returning an empty string on
|
||||
// null pointer.
|
||||
static inline const char *GetCstring(const String *str) {
|
||||
static inline const char* GetCstring(const String* str) {
|
||||
return str ? str->c_str() : "";
|
||||
}
|
||||
|
||||
#ifdef FLATBUFFERS_HAS_STRING_VIEW
|
||||
// Convenience function to get string_view from a String returning an empty
|
||||
// string_view on null pointer.
|
||||
static inline flatbuffers::string_view GetStringView(const String *str) {
|
||||
static inline flatbuffers::string_view GetStringView(const String* str) {
|
||||
return str ? str->string_view() : flatbuffers::string_view();
|
||||
}
|
||||
#endif // FLATBUFFERS_HAS_STRING_VIEW
|
||||
|
||||
@@ -27,23 +27,25 @@ namespace flatbuffers {
|
||||
|
||||
class Struct FLATBUFFERS_FINAL_CLASS {
|
||||
public:
|
||||
template<typename T> T GetField(uoffset_t o) const {
|
||||
template <typename T>
|
||||
T GetField(uoffset_t o) const {
|
||||
return ReadScalar<T>(&data_[o]);
|
||||
}
|
||||
|
||||
template<typename T> T GetStruct(uoffset_t o) const {
|
||||
template <typename T>
|
||||
T GetStruct(uoffset_t o) const {
|
||||
return reinterpret_cast<T>(&data_[o]);
|
||||
}
|
||||
|
||||
const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; }
|
||||
uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; }
|
||||
const uint8_t* GetAddressOf(uoffset_t o) const { return &data_[o]; }
|
||||
uint8_t* GetAddressOf(uoffset_t o) { return &data_[o]; }
|
||||
|
||||
private:
|
||||
// private constructor & copy constructor: you obtain instances of this
|
||||
// class by pointing to existing data only
|
||||
Struct();
|
||||
Struct(const Struct &);
|
||||
Struct &operator=(const Struct &);
|
||||
Struct(const Struct&);
|
||||
Struct& operator=(const Struct&);
|
||||
|
||||
uint8_t data_[1];
|
||||
};
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace flatbuffers {
|
||||
// omitted and added at will, but uses an extra indirection to read.
|
||||
class Table {
|
||||
public:
|
||||
const uint8_t *GetVTable() const {
|
||||
const uint8_t* GetVTable() const {
|
||||
return data_ - ReadScalar<soffset_t>(data_);
|
||||
}
|
||||
|
||||
@@ -42,38 +42,42 @@ class Table {
|
||||
return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
|
||||
}
|
||||
|
||||
template<typename T> T GetField(voffset_t field, T defaultval) const {
|
||||
template <typename T>
|
||||
T GetField(voffset_t field, T defaultval) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
|
||||
}
|
||||
|
||||
template<typename P, typename OffsetSize = uoffset_t>
|
||||
template <typename P, typename OffsetSize = uoffset_t>
|
||||
P GetPointer(voffset_t field) {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
auto p = data_ + field_offset;
|
||||
return field_offset ? reinterpret_cast<P>(p + ReadScalar<OffsetSize>(p))
|
||||
: nullptr;
|
||||
}
|
||||
template<typename P, typename OffsetSize = uoffset_t>
|
||||
template <typename P, typename OffsetSize = uoffset_t>
|
||||
P GetPointer(voffset_t field) const {
|
||||
return const_cast<Table *>(this)->GetPointer<P, OffsetSize>(field);
|
||||
return const_cast<Table*>(this)->GetPointer<P, OffsetSize>(field);
|
||||
}
|
||||
|
||||
template<typename P> P GetPointer64(voffset_t field) {
|
||||
template <typename P>
|
||||
P GetPointer64(voffset_t field) {
|
||||
return GetPointer<P, uoffset64_t>(field);
|
||||
}
|
||||
|
||||
template<typename P> P GetPointer64(voffset_t field) const {
|
||||
template <typename P>
|
||||
P GetPointer64(voffset_t field) const {
|
||||
return GetPointer<P, uoffset64_t>(field);
|
||||
}
|
||||
|
||||
template<typename P> P GetStruct(voffset_t field) const {
|
||||
template <typename P>
|
||||
P GetStruct(voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
auto p = const_cast<uint8_t *>(data_ + field_offset);
|
||||
auto p = const_cast<uint8_t*>(data_ + field_offset);
|
||||
return field_offset ? reinterpret_cast<P>(p) : nullptr;
|
||||
}
|
||||
|
||||
template<typename Raw, typename Face>
|
||||
template <typename Raw, typename Face>
|
||||
flatbuffers::Optional<Face> GetOptional(voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
auto p = data_ + field_offset;
|
||||
@@ -81,20 +85,22 @@ class Table {
|
||||
: Optional<Face>();
|
||||
}
|
||||
|
||||
template<typename T> bool SetField(voffset_t field, T val, T def) {
|
||||
template <typename T>
|
||||
bool SetField(voffset_t field, T val, T def) {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
if (!field_offset) return IsTheSameAs(val, def);
|
||||
WriteScalar(data_ + field_offset, val);
|
||||
return true;
|
||||
}
|
||||
template<typename T> bool SetField(voffset_t field, T val) {
|
||||
template <typename T>
|
||||
bool SetField(voffset_t field, T val) {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
if (!field_offset) return false;
|
||||
WriteScalar(data_ + field_offset, val);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SetPointer(voffset_t field, const uint8_t *val) {
|
||||
bool SetPointer(voffset_t field, const uint8_t* val) {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
if (!field_offset) return false;
|
||||
WriteScalar(data_ + field_offset,
|
||||
@@ -102,12 +108,12 @@ class Table {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *GetAddressOf(voffset_t field) {
|
||||
uint8_t* GetAddressOf(voffset_t field) {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return field_offset ? data_ + field_offset : nullptr;
|
||||
}
|
||||
const uint8_t *GetAddressOf(voffset_t field) const {
|
||||
return const_cast<Table *>(this)->GetAddressOf(field);
|
||||
const uint8_t* GetAddressOf(voffset_t field) const {
|
||||
return const_cast<Table*>(this)->GetAddressOf(field);
|
||||
}
|
||||
|
||||
bool CheckField(voffset_t field) const {
|
||||
@@ -116,13 +122,13 @@ class Table {
|
||||
|
||||
// Verify the vtable of this table.
|
||||
// Call this once per table, followed by VerifyField once per field.
|
||||
bool VerifyTableStart(Verifier &verifier) const {
|
||||
bool VerifyTableStart(Verifier& verifier) const {
|
||||
return verifier.VerifyTableStart(data_);
|
||||
}
|
||||
|
||||
// Verify a particular field.
|
||||
template<typename T>
|
||||
bool VerifyField(const Verifier &verifier, voffset_t field,
|
||||
template <typename T>
|
||||
bool VerifyField(const Verifier& verifier, voffset_t field,
|
||||
size_t align) const {
|
||||
// Calling GetOptionalFieldOffset should be safe now thanks to
|
||||
// VerifyTable().
|
||||
@@ -132,8 +138,8 @@ class Table {
|
||||
}
|
||||
|
||||
// VerifyField for required fields.
|
||||
template<typename T>
|
||||
bool VerifyFieldRequired(const Verifier &verifier, voffset_t field,
|
||||
template <typename T>
|
||||
bool VerifyFieldRequired(const Verifier& verifier, voffset_t field,
|
||||
size_t align) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return verifier.Check(field_offset != 0) &&
|
||||
@@ -141,24 +147,24 @@ class Table {
|
||||
}
|
||||
|
||||
// Versions for offsets.
|
||||
template<typename OffsetT = uoffset_t>
|
||||
bool VerifyOffset(const Verifier &verifier, voffset_t field) const {
|
||||
template <typename OffsetT = uoffset_t>
|
||||
bool VerifyOffset(const Verifier& verifier, voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return !field_offset || verifier.VerifyOffset<OffsetT>(data_, field_offset);
|
||||
}
|
||||
|
||||
template<typename OffsetT = uoffset_t>
|
||||
bool VerifyOffsetRequired(const Verifier &verifier, voffset_t field) const {
|
||||
template <typename OffsetT = uoffset_t>
|
||||
bool VerifyOffsetRequired(const Verifier& verifier, voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
return verifier.Check(field_offset != 0) &&
|
||||
verifier.VerifyOffset<OffsetT>(data_, field_offset);
|
||||
}
|
||||
|
||||
bool VerifyOffset64(const Verifier &verifier, voffset_t field) const {
|
||||
bool VerifyOffset64(const Verifier& verifier, voffset_t field) const {
|
||||
return VerifyOffset<uoffset64_t>(verifier, field);
|
||||
}
|
||||
|
||||
bool VerifyOffset64Required(const Verifier &verifier, voffset_t field) const {
|
||||
bool VerifyOffset64Required(const Verifier& verifier, voffset_t field) const {
|
||||
return VerifyOffsetRequired<uoffset64_t>(verifier, field);
|
||||
}
|
||||
|
||||
@@ -166,15 +172,15 @@ class Table {
|
||||
// private constructor & copy constructor: you obtain instances of this
|
||||
// class by pointing to existing data only
|
||||
Table();
|
||||
Table(const Table &other);
|
||||
Table &operator=(const Table &);
|
||||
Table(const Table& other);
|
||||
Table& operator=(const Table&);
|
||||
|
||||
uint8_t data_[1];
|
||||
};
|
||||
|
||||
// This specialization allows avoiding warnings like:
|
||||
// MSVC C4800: type: forcing value to bool 'true' or 'false'.
|
||||
template<>
|
||||
template <>
|
||||
inline flatbuffers::Optional<bool> Table::GetOptional<uint8_t, bool>(
|
||||
voffset_t field) const {
|
||||
auto field_offset = GetOptionalFieldOffset(field);
|
||||
|
||||
@@ -24,11 +24,11 @@
|
||||
#include "flatbuffers/stl_emulation.h"
|
||||
|
||||
#ifndef FLATBUFFERS_PREFER_PRINTF
|
||||
# include <iomanip>
|
||||
# include <sstream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#else // FLATBUFFERS_PREFER_PRINTF
|
||||
# include <float.h>
|
||||
# include <stdio.h>
|
||||
#include <float.h>
|
||||
#include <stdio.h>
|
||||
#endif // FLATBUFFERS_PREFER_PRINTF
|
||||
|
||||
#include <cmath>
|
||||
@@ -90,7 +90,8 @@ inline char CharToLower(char c) {
|
||||
// @end-locale-independent functions for ASCII character set
|
||||
|
||||
#ifdef FLATBUFFERS_PREFER_PRINTF
|
||||
template<typename T> size_t IntToDigitCount(T t) {
|
||||
template <typename T>
|
||||
size_t IntToDigitCount(T t) {
|
||||
size_t digit_count = 0;
|
||||
// Count the sign for negative numbers
|
||||
if (t < 0) digit_count++;
|
||||
@@ -105,19 +106,20 @@ template<typename T> size_t IntToDigitCount(T t) {
|
||||
return digit_count;
|
||||
}
|
||||
|
||||
template<typename T> size_t NumToStringWidth(T t, int precision = 0) {
|
||||
template <typename T>
|
||||
size_t NumToStringWidth(T t, int precision = 0) {
|
||||
size_t string_width = IntToDigitCount(t);
|
||||
// Count the dot for floating point numbers
|
||||
if (precision) string_width += (precision + 1);
|
||||
return string_width;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::string NumToStringImplWrapper(T t, const char *fmt, int precision = 0) {
|
||||
template <typename T>
|
||||
std::string NumToStringImplWrapper(T t, const char* fmt, int precision = 0) {
|
||||
size_t string_width = NumToStringWidth(t, precision);
|
||||
std::string s(string_width, 0x00);
|
||||
// Allow snprintf to use std::string trailing null to detect buffer overflow
|
||||
snprintf(const_cast<char *>(s.data()), (s.size() + 1), fmt, string_width, t);
|
||||
snprintf(const_cast<char*>(s.data()), (s.size() + 1), fmt, string_width, t);
|
||||
return s;
|
||||
}
|
||||
#endif // FLATBUFFERS_PREFER_PRINTF
|
||||
@@ -125,7 +127,8 @@ std::string NumToStringImplWrapper(T t, const char *fmt, int precision = 0) {
|
||||
// Convert an integer or floating point value to a string.
|
||||
// In contrast to std::stringstream, "char" values are
|
||||
// converted to a string of digits, and we don't use scientific notation.
|
||||
template<typename T> std::string NumToString(T t) {
|
||||
template <typename T>
|
||||
std::string NumToString(T t) {
|
||||
// clang-format off
|
||||
|
||||
#ifndef FLATBUFFERS_PREFER_PRINTF
|
||||
@@ -139,18 +142,22 @@ template<typename T> std::string NumToString(T t) {
|
||||
// clang-format on
|
||||
}
|
||||
// Avoid char types used as character data.
|
||||
template<> inline std::string NumToString<signed char>(signed char t) {
|
||||
template <>
|
||||
inline std::string NumToString<signed char>(signed char t) {
|
||||
return NumToString(static_cast<int>(t));
|
||||
}
|
||||
template<> inline std::string NumToString<unsigned char>(unsigned char t) {
|
||||
template <>
|
||||
inline std::string NumToString<unsigned char>(unsigned char t) {
|
||||
return NumToString(static_cast<int>(t));
|
||||
}
|
||||
template<> inline std::string NumToString<char>(char t) {
|
||||
template <>
|
||||
inline std::string NumToString<char>(char t) {
|
||||
return NumToString(static_cast<int>(t));
|
||||
}
|
||||
|
||||
// Special versions for floats/doubles.
|
||||
template<typename T> std::string FloatToString(T t, int precision) {
|
||||
template <typename T>
|
||||
std::string FloatToString(T t, int precision) {
|
||||
// clang-format off
|
||||
|
||||
#ifndef FLATBUFFERS_PREFER_PRINTF
|
||||
@@ -177,10 +184,12 @@ template<typename T> std::string FloatToString(T t, int precision) {
|
||||
return s;
|
||||
}
|
||||
|
||||
template<> inline std::string NumToString<double>(double t) {
|
||||
template <>
|
||||
inline std::string NumToString<double>(double t) {
|
||||
return FloatToString(t, 12);
|
||||
}
|
||||
template<> inline std::string NumToString<float>(float t) {
|
||||
template <>
|
||||
inline std::string NumToString<float>(float t) {
|
||||
return FloatToString(t, 6);
|
||||
}
|
||||
|
||||
@@ -279,8 +288,8 @@ inline void strtoval_impl(float *val, const char *str, char **endptr) {
|
||||
// - If full string conversion can't be performed, 0 is returned.
|
||||
// - If the converted value falls out of range of corresponding return type, a
|
||||
// range error occurs. In this case value MAX(T)/MIN(T) is returned.
|
||||
template<typename T>
|
||||
inline bool StringToIntegerImpl(T *val, const char *const str,
|
||||
template <typename T>
|
||||
inline bool StringToIntegerImpl(T* val, const char* const str,
|
||||
const int base = 0,
|
||||
const bool check_errno = true) {
|
||||
// T is int64_t or uint64_T
|
||||
@@ -295,7 +304,7 @@ inline bool StringToIntegerImpl(T *val, const char *const str,
|
||||
} else {
|
||||
if (check_errno) errno = 0; // clear thread-local errno
|
||||
auto endptr = str;
|
||||
strtoval_impl(val, str, const_cast<char **>(&endptr), base);
|
||||
strtoval_impl(val, str, const_cast<char**>(&endptr), base);
|
||||
if ((*endptr != '\0') || (endptr == str)) {
|
||||
*val = 0; // erase partial result
|
||||
return false; // invalid string
|
||||
@@ -306,15 +315,17 @@ inline bool StringToIntegerImpl(T *val, const char *const str,
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline bool StringToFloatImpl(T *val, const char *const str) {
|
||||
template <typename T>
|
||||
inline bool StringToFloatImpl(T* val, const char* const str) {
|
||||
// Type T must be either float or double.
|
||||
FLATBUFFERS_ASSERT(str && val);
|
||||
auto end = str;
|
||||
strtoval_impl(val, str, const_cast<char **>(&end));
|
||||
strtoval_impl(val, str, const_cast<char**>(&end));
|
||||
auto done = (end != str) && (*end == '\0');
|
||||
if (!done) *val = 0; // erase partial result
|
||||
if (done && std::isnan(*val)) { *val = std::numeric_limits<T>::quiet_NaN(); }
|
||||
if (done && std::isnan(*val)) {
|
||||
*val = std::numeric_limits<T>::quiet_NaN();
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
@@ -324,7 +335,8 @@ inline bool StringToFloatImpl(T *val, const char *const str) {
|
||||
// - If full string conversion can't be performed, 0 is returned.
|
||||
// - If the converted value falls out of range of corresponding return type, a
|
||||
// range error occurs. In this case value MAX(T)/MIN(T) is returned.
|
||||
template<typename T> inline bool StringToNumber(const char *s, T *val) {
|
||||
template <typename T>
|
||||
inline bool StringToNumber(const char* s, T* val) {
|
||||
// Assert on `unsigned long` and `signed long` on LP64.
|
||||
// If it is necessary, it could be solved with flatbuffers::enable_if<B,T>.
|
||||
static_assert(sizeof(T) < sizeof(int64_t), "unexpected type T");
|
||||
@@ -351,12 +363,13 @@ template<typename T> inline bool StringToNumber(const char *s, T *val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
template<> inline bool StringToNumber<int64_t>(const char *str, int64_t *val) {
|
||||
template <>
|
||||
inline bool StringToNumber<int64_t>(const char* str, int64_t* val) {
|
||||
return StringToIntegerImpl(val, str);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool StringToNumber<uint64_t>(const char *str, uint64_t *val) {
|
||||
template <>
|
||||
inline bool StringToNumber<uint64_t>(const char* str, uint64_t* val) {
|
||||
if (!StringToIntegerImpl(val, str)) return false;
|
||||
// The strtoull accepts negative numbers:
|
||||
// If the minus sign was part of the input sequence, the numeric value
|
||||
@@ -377,39 +390,41 @@ inline bool StringToNumber<uint64_t>(const char *str, uint64_t *val) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> inline bool StringToNumber(const char *s, float *val) {
|
||||
template <>
|
||||
inline bool StringToNumber(const char* s, float* val) {
|
||||
return StringToFloatImpl(val, s);
|
||||
}
|
||||
|
||||
template<> inline bool StringToNumber(const char *s, double *val) {
|
||||
template <>
|
||||
inline bool StringToNumber(const char* s, double* val) {
|
||||
return StringToFloatImpl(val, s);
|
||||
}
|
||||
|
||||
inline int64_t StringToInt(const char *s, int base = 10) {
|
||||
inline int64_t StringToInt(const char* s, int base = 10) {
|
||||
int64_t val;
|
||||
return StringToIntegerImpl(&val, s, base) ? val : 0;
|
||||
}
|
||||
|
||||
inline uint64_t StringToUInt(const char *s, int base = 10) {
|
||||
inline uint64_t StringToUInt(const char* s, int base = 10) {
|
||||
uint64_t val;
|
||||
return StringToIntegerImpl(&val, s, base) ? val : 0;
|
||||
}
|
||||
|
||||
inline bool StringIsFlatbufferNan(const std::string &s) {
|
||||
inline bool StringIsFlatbufferNan(const std::string& s) {
|
||||
return s == "nan" || s == "+nan" || s == "-nan";
|
||||
}
|
||||
|
||||
inline bool StringIsFlatbufferPositiveInfinity(const std::string &s) {
|
||||
inline bool StringIsFlatbufferPositiveInfinity(const std::string& s) {
|
||||
return s == "inf" || s == "+inf" || s == "infinity" || s == "+infinity";
|
||||
}
|
||||
|
||||
inline bool StringIsFlatbufferNegativeInfinity(const std::string &s) {
|
||||
inline bool StringIsFlatbufferNegativeInfinity(const std::string& s) {
|
||||
return s == "-inf" || s == "-infinity";
|
||||
}
|
||||
|
||||
typedef bool (*LoadFileFunction)(const char *filename, bool binary,
|
||||
std::string *dest);
|
||||
typedef bool (*FileExistsFunction)(const char *filename);
|
||||
typedef bool (*LoadFileFunction)(const char* filename, bool binary,
|
||||
std::string* dest);
|
||||
typedef bool (*FileExistsFunction)(const char* filename);
|
||||
|
||||
LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function);
|
||||
|
||||
@@ -417,29 +432,29 @@ FileExistsFunction SetFileExistsFunction(
|
||||
FileExistsFunction file_exists_function);
|
||||
|
||||
// Check if file "name" exists.
|
||||
bool FileExists(const char *name);
|
||||
bool FileExists(const char* name);
|
||||
|
||||
// Check if "name" exists and it is also a directory.
|
||||
bool DirExists(const char *name);
|
||||
bool DirExists(const char* name);
|
||||
|
||||
// Load file "name" into "buf" returning true if successful
|
||||
// false otherwise. If "binary" is false data is read
|
||||
// using ifstream's text mode, otherwise data is read with
|
||||
// no transcoding.
|
||||
bool LoadFile(const char *name, bool binary, std::string *buf);
|
||||
bool LoadFile(const char* name, bool binary, std::string* buf);
|
||||
|
||||
// Save data "buf" of length "len" bytes into a file
|
||||
// "name" returning true if successful, false otherwise.
|
||||
// If "binary" is false data is written using ifstream's
|
||||
// text mode, otherwise data is written with no
|
||||
// transcoding.
|
||||
bool SaveFile(const char *name, const char *buf, size_t len, bool binary);
|
||||
bool SaveFile(const char* name, const char* buf, size_t len, bool binary);
|
||||
|
||||
// Save data "buf" into file "name" returning true if
|
||||
// successful, false otherwise. If "binary" is false
|
||||
// data is written using ifstream's text mode, otherwise
|
||||
// data is written with no transcoding.
|
||||
inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
|
||||
inline bool SaveFile(const char* name, const std::string& buf, bool binary) {
|
||||
return SaveFile(name, buf.c_str(), buf.size(), binary);
|
||||
}
|
||||
|
||||
@@ -452,51 +467,50 @@ inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
|
||||
FLATBUFFERS_CONSTEXPR char kPathSeparator = '/';
|
||||
|
||||
// Returns the path with the extension, if any, removed.
|
||||
std::string StripExtension(const std::string &filepath);
|
||||
std::string StripExtension(const std::string& filepath);
|
||||
|
||||
// Returns the extension, if any.
|
||||
std::string GetExtension(const std::string &filepath);
|
||||
std::string GetExtension(const std::string& filepath);
|
||||
|
||||
// Return the last component of the path, after the last separator.
|
||||
std::string StripPath(const std::string &filepath);
|
||||
std::string StripPath(const std::string& filepath);
|
||||
|
||||
// Strip the last component of the path + separator.
|
||||
std::string StripFileName(const std::string &filepath);
|
||||
std::string StripFileName(const std::string& filepath);
|
||||
|
||||
std::string StripPrefix(const std::string &filepath,
|
||||
const std::string &prefix_to_remove);
|
||||
std::string StripPrefix(const std::string& filepath,
|
||||
const std::string& prefix_to_remove);
|
||||
|
||||
// Concatenates a path with a filename, regardless of whether the path
|
||||
// ends in a separator or not.
|
||||
std::string ConCatPathFileName(const std::string &path,
|
||||
const std::string &filename);
|
||||
std::string ConCatPathFileName(const std::string& path,
|
||||
const std::string& filename);
|
||||
|
||||
// Replaces any '\\' separators with '/'
|
||||
std::string PosixPath(const char *path);
|
||||
std::string PosixPath(const std::string &path);
|
||||
std::string PosixPath(const char* path);
|
||||
std::string PosixPath(const std::string& path);
|
||||
|
||||
// This function ensure a directory exists, by recursively
|
||||
// creating dirs for any parts of the path that don't exist yet.
|
||||
void EnsureDirExists(const std::string &filepath);
|
||||
void EnsureDirExists(const std::string& filepath);
|
||||
|
||||
// Obtains the relative or absolute path.
|
||||
std::string FilePath(const std::string &project,
|
||||
const std::string &filePath,
|
||||
std::string FilePath(const std::string& project, const std::string& filePath,
|
||||
bool absolute);
|
||||
|
||||
// Obtains the absolute path from any other path.
|
||||
// Returns the input path if the absolute path couldn't be resolved.
|
||||
std::string AbsolutePath(const std::string &filepath);
|
||||
std::string AbsolutePath(const std::string& filepath);
|
||||
|
||||
// Returns files relative to the --project_root path, prefixed with `//`.
|
||||
std::string RelativeToRootPath(const std::string &project,
|
||||
const std::string &filepath);
|
||||
std::string RelativeToRootPath(const std::string& project,
|
||||
const std::string& filepath);
|
||||
|
||||
// To and from UTF-8 unicode conversion functions
|
||||
|
||||
// Convert a unicode code point into a UTF-8 representation by appending it
|
||||
// to a string. Returns the number of bytes generated.
|
||||
inline int ToUTF8(uint32_t ucc, std::string *out) {
|
||||
inline int ToUTF8(uint32_t ucc, std::string* out) {
|
||||
FLATBUFFERS_ASSERT(!(ucc & 0x80000000)); // Top bit can't be set.
|
||||
// 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8
|
||||
for (int i = 0; i < 6; i++) {
|
||||
@@ -524,7 +538,7 @@ inline int ToUTF8(uint32_t ucc, std::string *out) {
|
||||
// advanced past all bytes parsed.
|
||||
// returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in
|
||||
// this case).
|
||||
inline int FromUTF8(const char **in) {
|
||||
inline int FromUTF8(const char** in) {
|
||||
int len = 0;
|
||||
// Count leading 1 bits.
|
||||
for (int mask = 0x80; mask >= 0x04; mask >>= 1) {
|
||||
@@ -538,7 +552,9 @@ inline int FromUTF8(const char **in) {
|
||||
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++) {
|
||||
@@ -548,20 +564,28 @@ 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;
|
||||
@@ -596,26 +620,40 @@ inline std::string WordWrap(const std::string in, size_t max_length,
|
||||
}
|
||||
#endif // !FLATBUFFERS_PREFER_PRINTF
|
||||
|
||||
inline bool EscapeString(const char *s, size_t length, std::string *_text,
|
||||
inline bool EscapeString(const char* s, size_t length, std::string* _text,
|
||||
bool allow_non_utf8, bool natural_utf8) {
|
||||
std::string &text = *_text;
|
||||
std::string& text = *_text;
|
||||
text += "\"";
|
||||
for (uoffset_t i = 0; i < length; i++) {
|
||||
char c = s[i];
|
||||
switch (c) {
|
||||
case '\n': text += "\\n"; break;
|
||||
case '\t': text += "\\t"; break;
|
||||
case '\r': text += "\\r"; break;
|
||||
case '\b': text += "\\b"; break;
|
||||
case '\f': text += "\\f"; break;
|
||||
case '\"': text += "\\\""; break;
|
||||
case '\\': text += "\\\\"; break;
|
||||
case '\n':
|
||||
text += "\\n";
|
||||
break;
|
||||
case '\t':
|
||||
text += "\\t";
|
||||
break;
|
||||
case '\r':
|
||||
text += "\\r";
|
||||
break;
|
||||
case '\b':
|
||||
text += "\\b";
|
||||
break;
|
||||
case '\f':
|
||||
text += "\\f";
|
||||
break;
|
||||
case '\"':
|
||||
text += "\\\"";
|
||||
break;
|
||||
case '\\':
|
||||
text += "\\\\";
|
||||
break;
|
||||
default:
|
||||
if (c >= ' ' && c <= '~') {
|
||||
text += c;
|
||||
} else {
|
||||
// Not printable ASCII data. Let's see if it's valid UTF-8 first:
|
||||
const char *utf8 = s + i;
|
||||
const char* utf8 = s + i;
|
||||
int ucc = FromUTF8(&utf8);
|
||||
if (ucc < 0) {
|
||||
if (allow_non_utf8) {
|
||||
@@ -666,19 +704,21 @@ inline bool EscapeString(const char *s, size_t length, std::string *_text,
|
||||
return true;
|
||||
}
|
||||
|
||||
inline std::string BufferToHexText(const void *buffer, size_t buffer_size,
|
||||
inline std::string BufferToHexText(const void* buffer, size_t buffer_size,
|
||||
size_t max_length,
|
||||
const std::string &wrapped_line_prefix,
|
||||
const std::string &wrapped_line_suffix) {
|
||||
const std::string& wrapped_line_prefix,
|
||||
const std::string& wrapped_line_suffix) {
|
||||
std::string text = wrapped_line_prefix;
|
||||
size_t start_offset = 0;
|
||||
const char *s = reinterpret_cast<const char *>(buffer);
|
||||
const char* s = reinterpret_cast<const char*>(buffer);
|
||||
for (size_t i = 0; s && i < buffer_size; i++) {
|
||||
// Last iteration or do we have more?
|
||||
bool have_more = i + 1 < buffer_size;
|
||||
text += "0x";
|
||||
text += IntToStringHex(static_cast<uint8_t>(s[i]), 2);
|
||||
if (have_more) { text += ','; }
|
||||
if (have_more) {
|
||||
text += ',';
|
||||
}
|
||||
// If we have more to process and we reached max_length
|
||||
if (have_more &&
|
||||
text.size() + wrapped_line_suffix.size() >= start_offset + max_length) {
|
||||
@@ -693,17 +733,17 @@ inline std::string BufferToHexText(const void *buffer, size_t buffer_size,
|
||||
}
|
||||
|
||||
// Remove paired quotes in a string: "text"|'text' -> text.
|
||||
std::string RemoveStringQuotes(const std::string &s);
|
||||
std::string RemoveStringQuotes(const std::string& s);
|
||||
|
||||
// Change th global C-locale to locale with name <locale_name>.
|
||||
// Returns an actual locale name in <_value>, useful if locale_name is "" or
|
||||
// null.
|
||||
bool SetGlobalTestLocale(const char *locale_name,
|
||||
std::string *_value = nullptr);
|
||||
bool SetGlobalTestLocale(const char* locale_name,
|
||||
std::string* _value = nullptr);
|
||||
|
||||
// Read (or test) a value of environment variable.
|
||||
bool ReadEnvironmentVariable(const char *var_name,
|
||||
std::string *_value = nullptr);
|
||||
bool ReadEnvironmentVariable(const char* var_name,
|
||||
std::string* _value = nullptr);
|
||||
|
||||
enum class Case {
|
||||
kUnknown = 0,
|
||||
@@ -729,7 +769,7 @@ enum class Case {
|
||||
|
||||
// Convert the `input` string of case `input_case` to the specified
|
||||
// `output_case`.
|
||||
std::string ConvertCase(const std::string &input, Case output_case,
|
||||
std::string ConvertCase(const std::string& input, Case output_case,
|
||||
Case input_case = Case::kSnake);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -27,56 +27,56 @@ struct String;
|
||||
|
||||
// An STL compatible iterator implementation for Vector below, effectively
|
||||
// calling Get() for every element.
|
||||
template<typename T, typename IT, typename Data = uint8_t *,
|
||||
typename SizeT = uoffset_t>
|
||||
template <typename T, typename IT, typename Data = uint8_t*,
|
||||
typename SizeT = uoffset_t>
|
||||
struct VectorIterator {
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
typedef IT value_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef IT *pointer;
|
||||
typedef IT &reference;
|
||||
typedef IT* pointer;
|
||||
typedef IT& reference;
|
||||
|
||||
static const SizeT element_stride = IndirectHelper<T>::element_stride;
|
||||
|
||||
VectorIterator(Data data, SizeT i) : data_(data + element_stride * i) {}
|
||||
VectorIterator(const VectorIterator &other) : data_(other.data_) {}
|
||||
VectorIterator(const VectorIterator& other) : data_(other.data_) {}
|
||||
VectorIterator() : data_(nullptr) {}
|
||||
|
||||
VectorIterator &operator=(const VectorIterator &other) {
|
||||
VectorIterator& operator=(const VectorIterator& other) {
|
||||
data_ = other.data_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VectorIterator &operator=(VectorIterator &&other) {
|
||||
VectorIterator& operator=(VectorIterator&& other) {
|
||||
data_ = other.data_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const VectorIterator &other) const {
|
||||
bool operator==(const VectorIterator& other) const {
|
||||
return data_ == other.data_;
|
||||
}
|
||||
|
||||
bool operator!=(const VectorIterator &other) const {
|
||||
bool operator!=(const VectorIterator& other) const {
|
||||
return data_ != other.data_;
|
||||
}
|
||||
|
||||
bool operator<(const VectorIterator &other) const {
|
||||
bool operator<(const VectorIterator& other) const {
|
||||
return data_ < other.data_;
|
||||
}
|
||||
|
||||
bool operator>(const VectorIterator &other) const {
|
||||
bool operator>(const VectorIterator& other) const {
|
||||
return data_ > other.data_;
|
||||
}
|
||||
|
||||
bool operator<=(const VectorIterator &other) const {
|
||||
bool operator<=(const VectorIterator& other) const {
|
||||
return !(data_ > other.data_);
|
||||
}
|
||||
|
||||
bool operator>=(const VectorIterator &other) const {
|
||||
bool operator>=(const VectorIterator& other) const {
|
||||
return !(data_ < other.data_);
|
||||
}
|
||||
|
||||
difference_type operator-(const VectorIterator &other) const {
|
||||
difference_type operator-(const VectorIterator& other) const {
|
||||
return (data_ - other.data_) / element_stride;
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ struct VectorIterator {
|
||||
// `pointer operator->()`.
|
||||
IT operator->() const { return IndirectHelper<T>::Read(data_, 0); }
|
||||
|
||||
VectorIterator &operator++() {
|
||||
VectorIterator& operator++() {
|
||||
data_ += element_stride;
|
||||
return *this;
|
||||
}
|
||||
@@ -99,16 +99,16 @@ struct VectorIterator {
|
||||
return temp;
|
||||
}
|
||||
|
||||
VectorIterator operator+(const SizeT &offset) const {
|
||||
VectorIterator operator+(const SizeT& offset) const {
|
||||
return VectorIterator(data_ + offset * element_stride, 0);
|
||||
}
|
||||
|
||||
VectorIterator &operator+=(const SizeT &offset) {
|
||||
VectorIterator& operator+=(const SizeT& offset) {
|
||||
data_ += offset * element_stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VectorIterator &operator--() {
|
||||
VectorIterator& operator--() {
|
||||
data_ -= element_stride;
|
||||
return *this;
|
||||
}
|
||||
@@ -119,11 +119,11 @@ struct VectorIterator {
|
||||
return temp;
|
||||
}
|
||||
|
||||
VectorIterator operator-(const SizeT &offset) const {
|
||||
VectorIterator operator-(const SizeT& offset) const {
|
||||
return VectorIterator(data_ - offset * element_stride, 0);
|
||||
}
|
||||
|
||||
VectorIterator &operator-=(const SizeT &offset) {
|
||||
VectorIterator& operator-=(const SizeT& offset) {
|
||||
data_ -= offset * element_stride;
|
||||
return *this;
|
||||
}
|
||||
@@ -132,10 +132,10 @@ struct VectorIterator {
|
||||
Data data_;
|
||||
};
|
||||
|
||||
template<typename T, typename IT, typename SizeT = uoffset_t>
|
||||
using VectorConstIterator = VectorIterator<T, IT, const uint8_t *, SizeT>;
|
||||
template <typename T, typename IT, typename SizeT = uoffset_t>
|
||||
using VectorConstIterator = VectorIterator<T, IT, const uint8_t*, SizeT>;
|
||||
|
||||
template<typename Iterator>
|
||||
template <typename Iterator>
|
||||
struct VectorReverseIterator : public std::reverse_iterator<Iterator> {
|
||||
explicit VectorReverseIterator(Iterator iter)
|
||||
: std::reverse_iterator<Iterator>(iter) {}
|
||||
@@ -157,10 +157,11 @@ struct VectorReverseIterator : public std::reverse_iterator<Iterator> {
|
||||
|
||||
// This is used as a helper type for accessing vectors.
|
||||
// Vector::data() assumes the vector elements start after the length field.
|
||||
template<typename T, typename SizeT = uoffset_t> class Vector {
|
||||
template <typename T, typename SizeT = uoffset_t>
|
||||
class Vector {
|
||||
public:
|
||||
typedef VectorIterator<T, typename IndirectHelper<T>::mutable_return_type,
|
||||
uint8_t *, SizeT>
|
||||
uint8_t*, SizeT>
|
||||
iterator;
|
||||
typedef VectorConstIterator<T, typename IndirectHelper<T>::return_type, SizeT>
|
||||
const_iterator;
|
||||
@@ -199,24 +200,26 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
|
||||
// If this is a Vector of enums, T will be its storage type, not the enum
|
||||
// type. This function makes it convenient to retrieve value with enum
|
||||
// type E.
|
||||
template<typename E> E GetEnum(SizeT i) const {
|
||||
template <typename E>
|
||||
E GetEnum(SizeT i) const {
|
||||
return static_cast<E>(Get(i));
|
||||
}
|
||||
|
||||
// If this a vector of unions, this does the cast for you. There's no check
|
||||
// to make sure this is the right type!
|
||||
template<typename U> const U *GetAs(SizeT i) const {
|
||||
return reinterpret_cast<const U *>(Get(i));
|
||||
template <typename U>
|
||||
const U* GetAs(SizeT i) const {
|
||||
return reinterpret_cast<const U*>(Get(i));
|
||||
}
|
||||
|
||||
// If this a vector of unions, this does the cast for you. There's no check
|
||||
// to make sure this is actually a string!
|
||||
const String *GetAsString(SizeT i) const {
|
||||
return reinterpret_cast<const String *>(Get(i));
|
||||
const String* GetAsString(SizeT i) const {
|
||||
return reinterpret_cast<const String*>(Get(i));
|
||||
}
|
||||
|
||||
const void *GetStructFromOffset(size_t o) const {
|
||||
return reinterpret_cast<const void *>(Data() + o);
|
||||
const void* GetStructFromOffset(size_t o) const {
|
||||
return reinterpret_cast<const void*>(Data() + o);
|
||||
}
|
||||
|
||||
iterator begin() { return iterator(Data(), 0); }
|
||||
@@ -245,7 +248,7 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
|
||||
|
||||
// Change elements if you have a non-const pointer to this object.
|
||||
// Scalars only. See reflection.h, and the documentation.
|
||||
void Mutate(SizeT i, const T &val) {
|
||||
void Mutate(SizeT i, const T& val) {
|
||||
FLATBUFFERS_ASSERT(i < size());
|
||||
WriteScalar(data() + i, val);
|
||||
}
|
||||
@@ -253,7 +256,7 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
|
||||
// Change an element of a vector of tables (or strings).
|
||||
// "val" points to the new table/string, as you can obtain from
|
||||
// e.g. reflection::AddFlatBuffer().
|
||||
void MutateOffset(SizeT i, const uint8_t *val) {
|
||||
void MutateOffset(SizeT i, const uint8_t* val) {
|
||||
FLATBUFFERS_ASSERT(i < size());
|
||||
static_assert(sizeof(T) == sizeof(SizeT), "Unrelated types");
|
||||
WriteScalar(data() + i,
|
||||
@@ -267,30 +270,32 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
|
||||
}
|
||||
|
||||
// The raw data in little endian format. Use with care.
|
||||
const uint8_t *Data() const {
|
||||
return reinterpret_cast<const uint8_t *>(&length_ + 1);
|
||||
const uint8_t* Data() const {
|
||||
return reinterpret_cast<const uint8_t*>(&length_ + 1);
|
||||
}
|
||||
|
||||
uint8_t *Data() { return reinterpret_cast<uint8_t *>(&length_ + 1); }
|
||||
uint8_t* Data() { return reinterpret_cast<uint8_t*>(&length_ + 1); }
|
||||
|
||||
// Similarly, but typed, much like std::vector::data
|
||||
const T *data() const { return reinterpret_cast<const T *>(Data()); }
|
||||
T *data() { return reinterpret_cast<T *>(Data()); }
|
||||
const T* data() const { return reinterpret_cast<const T*>(Data()); }
|
||||
T* data() { return reinterpret_cast<T*>(Data()); }
|
||||
|
||||
template<typename K> return_type LookupByKey(K key) const {
|
||||
void *search_result = std::bsearch(
|
||||
template <typename K>
|
||||
return_type LookupByKey(K key) const {
|
||||
void* search_result = std::bsearch(
|
||||
&key, Data(), size(), IndirectHelper<T>::element_stride, KeyCompare<K>);
|
||||
|
||||
if (!search_result) {
|
||||
return nullptr; // Key not found.
|
||||
}
|
||||
|
||||
const uint8_t *element = reinterpret_cast<const uint8_t *>(search_result);
|
||||
const uint8_t* element = reinterpret_cast<const uint8_t*>(search_result);
|
||||
|
||||
return IndirectHelper<T>::Read(element, 0);
|
||||
}
|
||||
|
||||
template<typename K> mutable_return_type MutableLookupByKey(K key) {
|
||||
template <typename K>
|
||||
mutable_return_type MutableLookupByKey(K key) {
|
||||
return const_cast<mutable_return_type>(LookupByKey(key));
|
||||
}
|
||||
|
||||
@@ -304,12 +309,13 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
|
||||
private:
|
||||
// This class is a pointer. Copying will therefore create an invalid object.
|
||||
// Private and unimplemented copy constructor.
|
||||
Vector(const Vector &);
|
||||
Vector &operator=(const Vector &);
|
||||
Vector(const Vector&);
|
||||
Vector& operator=(const Vector&);
|
||||
|
||||
template<typename K> static int KeyCompare(const void *ap, const void *bp) {
|
||||
const K *key = reinterpret_cast<const K *>(ap);
|
||||
const uint8_t *data = reinterpret_cast<const uint8_t *>(bp);
|
||||
template <typename K>
|
||||
static int KeyCompare(const void* ap, const void* bp) {
|
||||
const K* key = reinterpret_cast<const K*>(ap);
|
||||
const uint8_t* data = reinterpret_cast<const uint8_t*>(bp);
|
||||
auto table = IndirectHelper<T>::Read(data, 0);
|
||||
|
||||
// std::bsearch compares with the operands transposed, so we negate the
|
||||
@@ -318,35 +324,36 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> using Vector64 = Vector<T, uoffset64_t>;
|
||||
template <typename T>
|
||||
using Vector64 = Vector<T, uoffset64_t>;
|
||||
|
||||
template<class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U> make_span(Vector<U> &vec)
|
||||
template <class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U> make_span(Vector<U>& vec)
|
||||
FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Vector<U>::is_span_observable,
|
||||
"wrong type U, only LE-scalar, or byte types are allowed");
|
||||
return span<U>(vec.data(), vec.size());
|
||||
}
|
||||
|
||||
template<class U>
|
||||
template <class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const U> make_span(
|
||||
const Vector<U> &vec) FLATBUFFERS_NOEXCEPT {
|
||||
const Vector<U>& vec) FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Vector<U>::is_span_observable,
|
||||
"wrong type U, only LE-scalar, or byte types are allowed");
|
||||
return span<const U>(vec.data(), vec.size());
|
||||
}
|
||||
|
||||
template<class U>
|
||||
template <class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<uint8_t> make_bytes_span(
|
||||
Vector<U> &vec) FLATBUFFERS_NOEXCEPT {
|
||||
Vector<U>& vec) FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Vector<U>::scalar_tag::value,
|
||||
"wrong type U, only LE-scalar, or byte types are allowed");
|
||||
return span<uint8_t>(vec.Data(), vec.size() * sizeof(U));
|
||||
}
|
||||
|
||||
template<class U>
|
||||
template <class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const uint8_t> make_bytes_span(
|
||||
const Vector<U> &vec) FLATBUFFERS_NOEXCEPT {
|
||||
const Vector<U>& vec) FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Vector<U>::scalar_tag::value,
|
||||
"wrong type U, only LE-scalar, or byte types are allowed");
|
||||
return span<const uint8_t>(vec.Data(), vec.size() * sizeof(U));
|
||||
@@ -354,17 +361,17 @@ FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const uint8_t> make_bytes_span(
|
||||
|
||||
// Convenient helper functions to get a span of any vector, regardless
|
||||
// of whether it is null or not (the field is not set).
|
||||
template<class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U> make_span(Vector<U> *ptr)
|
||||
template <class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U> make_span(Vector<U>* ptr)
|
||||
FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Vector<U>::is_span_observable,
|
||||
"wrong type U, only LE-scalar, or byte types are allowed");
|
||||
return ptr ? make_span(*ptr) : span<U>();
|
||||
}
|
||||
|
||||
template<class U>
|
||||
template <class U>
|
||||
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const U> make_span(
|
||||
const Vector<U> *ptr) FLATBUFFERS_NOEXCEPT {
|
||||
const Vector<U>* ptr) FLATBUFFERS_NOEXCEPT {
|
||||
static_assert(Vector<U>::is_span_observable,
|
||||
"wrong type U, only LE-scalar, or byte types are allowed");
|
||||
return ptr ? make_span(*ptr) : span<const U>();
|
||||
@@ -376,10 +383,10 @@ class VectorOfAny {
|
||||
public:
|
||||
uoffset_t size() const { return EndianScalar(length_); }
|
||||
|
||||
const uint8_t *Data() const {
|
||||
return reinterpret_cast<const uint8_t *>(&length_ + 1);
|
||||
const uint8_t* Data() const {
|
||||
return reinterpret_cast<const uint8_t*>(&length_ + 1);
|
||||
}
|
||||
uint8_t *Data() { return reinterpret_cast<uint8_t *>(&length_ + 1); }
|
||||
uint8_t* Data() { return reinterpret_cast<uint8_t*>(&length_ + 1); }
|
||||
|
||||
protected:
|
||||
VectorOfAny();
|
||||
@@ -387,25 +394,26 @@ class VectorOfAny {
|
||||
uoffset_t length_;
|
||||
|
||||
private:
|
||||
VectorOfAny(const VectorOfAny &);
|
||||
VectorOfAny &operator=(const VectorOfAny &);
|
||||
VectorOfAny(const VectorOfAny&);
|
||||
VectorOfAny& operator=(const VectorOfAny&);
|
||||
};
|
||||
|
||||
template<typename T, typename U>
|
||||
Vector<Offset<T>> *VectorCast(Vector<Offset<U>> *ptr) {
|
||||
template <typename T, typename U>
|
||||
Vector<Offset<T>>* VectorCast(Vector<Offset<U>>* ptr) {
|
||||
static_assert(std::is_base_of<T, U>::value, "Unrelated types");
|
||||
return reinterpret_cast<Vector<Offset<T>> *>(ptr);
|
||||
return reinterpret_cast<Vector<Offset<T>>*>(ptr);
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
const Vector<Offset<T>> *VectorCast(const Vector<Offset<U>> *ptr) {
|
||||
template <typename T, typename U>
|
||||
const Vector<Offset<T>>* VectorCast(const Vector<Offset<U>>* ptr) {
|
||||
static_assert(std::is_base_of<T, U>::value, "Unrelated types");
|
||||
return reinterpret_cast<const Vector<Offset<T>> *>(ptr);
|
||||
return reinterpret_cast<const Vector<Offset<T>>*>(ptr);
|
||||
}
|
||||
|
||||
// Convenient helper function to get the length of any vector, regardless
|
||||
// of whether it is null or not (the field is not set).
|
||||
template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
|
||||
template <typename T>
|
||||
static inline size_t VectorLength(const Vector<T>* v) {
|
||||
return v ? v->size() : 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,9 +32,10 @@ namespace flatbuffers {
|
||||
// Since this vector leaves the lower part unused, we support a "scratch-pad"
|
||||
// that can be stored there for temporary data, to share the allocated space.
|
||||
// Essentially, this supports 2 std::vectors in a single buffer.
|
||||
template<typename SizeT = uoffset_t> class vector_downward {
|
||||
template <typename SizeT = uoffset_t>
|
||||
class vector_downward {
|
||||
public:
|
||||
explicit vector_downward(size_t initial_size, Allocator *allocator,
|
||||
explicit vector_downward(size_t initial_size, Allocator* allocator,
|
||||
bool own_allocator, size_t buffer_minalign,
|
||||
const SizeT max_size = FLATBUFFERS_MAX_BUFFER_SIZE)
|
||||
: allocator_(allocator),
|
||||
@@ -48,7 +49,7 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
cur_(nullptr),
|
||||
scratch_(nullptr) {}
|
||||
|
||||
vector_downward(vector_downward &&other) noexcept
|
||||
vector_downward(vector_downward&& other) noexcept
|
||||
// clang-format on
|
||||
: allocator_(other.allocator_),
|
||||
own_allocator_(other.own_allocator_),
|
||||
@@ -70,7 +71,7 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
other.scratch_ = nullptr;
|
||||
}
|
||||
|
||||
vector_downward &operator=(vector_downward &&other) noexcept {
|
||||
vector_downward& operator=(vector_downward&& other) noexcept {
|
||||
// Move construct a temporary and swap idiom
|
||||
vector_downward temp(std::move(other));
|
||||
swap(temp);
|
||||
@@ -101,7 +102,9 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
void clear_scratch() { scratch_ = buf_; }
|
||||
|
||||
void clear_allocator() {
|
||||
if (own_allocator_ && allocator_) { delete allocator_; }
|
||||
if (own_allocator_ && allocator_) {
|
||||
delete allocator_;
|
||||
}
|
||||
allocator_ = nullptr;
|
||||
own_allocator_ = false;
|
||||
}
|
||||
@@ -112,8 +115,8 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
}
|
||||
|
||||
// Relinquish the pointer to the caller.
|
||||
uint8_t *release_raw(size_t &allocated_bytes, size_t &offset) {
|
||||
auto *buf = buf_;
|
||||
uint8_t* release_raw(size_t& allocated_bytes, size_t& offset) {
|
||||
auto* buf = buf_;
|
||||
allocated_bytes = reserved_;
|
||||
offset = vector_downward::offset();
|
||||
|
||||
@@ -142,12 +145,14 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_);
|
||||
// If the length is larger than the unused part of the buffer, we need to
|
||||
// grow.
|
||||
if (len > unused_buffer_size()) { reallocate(len); }
|
||||
if (len > unused_buffer_size()) {
|
||||
reallocate(len);
|
||||
}
|
||||
FLATBUFFERS_ASSERT(size() < max_size_);
|
||||
return len;
|
||||
}
|
||||
|
||||
inline uint8_t *make_space(size_t len) {
|
||||
inline uint8_t* make_space(size_t len) {
|
||||
if (len) {
|
||||
ensure_space(len);
|
||||
cur_ -= len;
|
||||
@@ -157,7 +162,7 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
}
|
||||
|
||||
// Returns nullptr if using the DefaultAllocator.
|
||||
Allocator *get_custom_allocator() { return allocator_; }
|
||||
Allocator* get_custom_allocator() { return allocator_; }
|
||||
|
||||
// The current offset into the buffer.
|
||||
size_t offset() const { return cur_ - buf_; }
|
||||
@@ -175,36 +180,40 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
|
||||
size_t capacity() const { return reserved_; }
|
||||
|
||||
uint8_t *data() const {
|
||||
uint8_t* data() const {
|
||||
FLATBUFFERS_ASSERT(cur_);
|
||||
return cur_;
|
||||
}
|
||||
|
||||
uint8_t *scratch_data() const {
|
||||
uint8_t* scratch_data() const {
|
||||
FLATBUFFERS_ASSERT(buf_);
|
||||
return buf_;
|
||||
}
|
||||
|
||||
uint8_t *scratch_end() const {
|
||||
uint8_t* scratch_end() const {
|
||||
FLATBUFFERS_ASSERT(scratch_);
|
||||
return scratch_;
|
||||
}
|
||||
|
||||
uint8_t *data_at(size_t offset) const { return buf_ + reserved_ - offset; }
|
||||
uint8_t* data_at(size_t offset) const { return buf_ + reserved_ - offset; }
|
||||
|
||||
void push(const uint8_t *bytes, size_t num) {
|
||||
if (num > 0) { memcpy(make_space(num), bytes, num); }
|
||||
void push(const uint8_t* bytes, size_t num) {
|
||||
if (num > 0) {
|
||||
memcpy(make_space(num), bytes, num);
|
||||
}
|
||||
}
|
||||
|
||||
// Specialized version of push() that avoids memcpy call for small data.
|
||||
template<typename T> void push_small(const T &little_endian_t) {
|
||||
template <typename T>
|
||||
void push_small(const T& little_endian_t) {
|
||||
make_space(sizeof(T));
|
||||
*reinterpret_cast<T *>(cur_) = little_endian_t;
|
||||
*reinterpret_cast<T*>(cur_) = little_endian_t;
|
||||
}
|
||||
|
||||
template<typename T> void scratch_push_small(const T &t) {
|
||||
template <typename T>
|
||||
void scratch_push_small(const T& t) {
|
||||
ensure_space(sizeof(T));
|
||||
*reinterpret_cast<T *>(scratch_) = t;
|
||||
*reinterpret_cast<T*>(scratch_) = t;
|
||||
scratch_ += sizeof(T);
|
||||
}
|
||||
|
||||
@@ -228,7 +237,7 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
|
||||
void scratch_pop(size_t bytes_to_remove) { scratch_ -= bytes_to_remove; }
|
||||
|
||||
void swap(vector_downward &other) {
|
||||
void swap(vector_downward& other) {
|
||||
using std::swap;
|
||||
swap(allocator_, other.allocator_);
|
||||
swap(own_allocator_, other.own_allocator_);
|
||||
@@ -242,7 +251,7 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
swap(scratch_, other.scratch_);
|
||||
}
|
||||
|
||||
void swap_allocator(vector_downward &other) {
|
||||
void swap_allocator(vector_downward& other) {
|
||||
using std::swap;
|
||||
swap(allocator_, other.allocator_);
|
||||
swap(own_allocator_, other.own_allocator_);
|
||||
@@ -250,10 +259,10 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
|
||||
private:
|
||||
// You shouldn't really be copying instances of this class.
|
||||
FLATBUFFERS_DELETE_FUNC(vector_downward(const vector_downward &));
|
||||
FLATBUFFERS_DELETE_FUNC(vector_downward &operator=(const vector_downward &));
|
||||
FLATBUFFERS_DELETE_FUNC(vector_downward(const vector_downward&));
|
||||
FLATBUFFERS_DELETE_FUNC(vector_downward& operator=(const vector_downward&));
|
||||
|
||||
Allocator *allocator_;
|
||||
Allocator* allocator_;
|
||||
bool own_allocator_;
|
||||
size_t initial_size_;
|
||||
|
||||
@@ -262,9 +271,9 @@ template<typename SizeT = uoffset_t> class vector_downward {
|
||||
size_t buffer_minalign_;
|
||||
size_t reserved_;
|
||||
SizeT size_;
|
||||
uint8_t *buf_;
|
||||
uint8_t *cur_; // Points at location between empty (below) and used (above).
|
||||
uint8_t *scratch_; // Points to the end of the scratchpad in use.
|
||||
uint8_t* buf_;
|
||||
uint8_t* cur_; // Points at location between empty (below) and used (above).
|
||||
uint8_t* scratch_; // Points to the end of the scratchpad in use.
|
||||
|
||||
void reallocate(size_t len) {
|
||||
auto old_reserved = reserved_;
|
||||
|
||||
@@ -41,14 +41,14 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
bool assert = false;
|
||||
};
|
||||
|
||||
explicit VerifierTemplate(const uint8_t *const buf, const size_t buf_len,
|
||||
const Options &opts)
|
||||
explicit VerifierTemplate(const uint8_t* const buf, const size_t buf_len,
|
||||
const Options& opts)
|
||||
: buf_(buf), size_(buf_len), opts_(opts) {
|
||||
FLATBUFFERS_ASSERT(size_ < opts.max_size);
|
||||
}
|
||||
|
||||
// Deprecated API, please construct with VerifierTemplate::Options.
|
||||
VerifierTemplate(const uint8_t *const buf, const size_t buf_len,
|
||||
VerifierTemplate(const uint8_t* const buf, const size_t buf_len,
|
||||
const uoffset_t max_depth = 64,
|
||||
const uoffset_t max_tables = 1000000,
|
||||
const bool check_alignment = true)
|
||||
@@ -80,7 +80,7 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
if (TrackVerifierBufferSize) {
|
||||
auto upper_bound = elem + elem_len;
|
||||
if (upper_bound_ < upper_bound) {
|
||||
upper_bound_ = upper_bound;
|
||||
upper_bound_ = upper_bound;
|
||||
}
|
||||
}
|
||||
return Check(elem_len < size_ && elem <= size_ - elem_len);
|
||||
@@ -91,59 +91,61 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
}
|
||||
|
||||
// Verify a range indicated by sizeof(T).
|
||||
template<typename T> bool Verify(const size_t elem) const {
|
||||
template <typename T>
|
||||
bool Verify(const size_t elem) const {
|
||||
return VerifyAlignment(elem, sizeof(T)) && Verify(elem, sizeof(T));
|
||||
}
|
||||
|
||||
bool VerifyFromPointer(const uint8_t *const p, const size_t len) {
|
||||
bool VerifyFromPointer(const uint8_t* const p, const size_t len) {
|
||||
return Verify(static_cast<size_t>(p - buf_), len);
|
||||
}
|
||||
|
||||
// Verify relative to a known-good base pointer.
|
||||
bool VerifyFieldStruct(const uint8_t *const base, const voffset_t elem_off,
|
||||
bool VerifyFieldStruct(const uint8_t* const base, const voffset_t elem_off,
|
||||
const size_t elem_len, const size_t align) const {
|
||||
const auto f = static_cast<size_t>(base - buf_) + elem_off;
|
||||
return VerifyAlignment(f, align) && Verify(f, elem_len);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool VerifyField(const uint8_t *const base, const voffset_t elem_off,
|
||||
template <typename T>
|
||||
bool VerifyField(const uint8_t* const base, const voffset_t elem_off,
|
||||
const size_t align) const {
|
||||
const auto f = static_cast<size_t>(base - buf_) + elem_off;
|
||||
return VerifyAlignment(f, align) && Verify(f, sizeof(T));
|
||||
}
|
||||
|
||||
// Verify a pointer (may be NULL) of a table type.
|
||||
template<typename T> bool VerifyTable(const T *const table) {
|
||||
template <typename T>
|
||||
bool VerifyTable(const T* const table) {
|
||||
return !table || table->Verify(*this);
|
||||
}
|
||||
|
||||
// Verify a pointer (may be NULL) of any vector type.
|
||||
template<int &..., typename T, typename LenT>
|
||||
bool VerifyVector(const Vector<T, LenT> *const vec) const {
|
||||
template <int&..., typename T, typename LenT>
|
||||
bool VerifyVector(const Vector<T, LenT>* const vec) const {
|
||||
return !vec || VerifyVectorOrString<LenT>(
|
||||
reinterpret_cast<const uint8_t *>(vec), sizeof(T));
|
||||
reinterpret_cast<const uint8_t*>(vec), sizeof(T));
|
||||
}
|
||||
|
||||
// Verify a pointer (may be NULL) of a vector to struct.
|
||||
template<int &..., typename T, typename LenT>
|
||||
bool VerifyVector(const Vector<const T *, LenT> *const vec) const {
|
||||
return VerifyVector(reinterpret_cast<const Vector<T, LenT> *>(vec));
|
||||
template <int&..., typename T, typename LenT>
|
||||
bool VerifyVector(const Vector<const T*, LenT>* const vec) const {
|
||||
return VerifyVector(reinterpret_cast<const Vector<T, LenT>*>(vec));
|
||||
}
|
||||
|
||||
// Verify a pointer (may be NULL) to string.
|
||||
bool VerifyString(const String *const str) const {
|
||||
bool VerifyString(const String* const str) const {
|
||||
size_t end;
|
||||
return !str || (VerifyVectorOrString<uoffset_t>(
|
||||
reinterpret_cast<const uint8_t *>(str), 1, &end) &&
|
||||
reinterpret_cast<const uint8_t*>(str), 1, &end) &&
|
||||
Verify(end, 1) && // Must have terminator
|
||||
Check(buf_[end] == '\0')); // Terminating byte must be 0.
|
||||
}
|
||||
|
||||
// Common code between vectors and strings.
|
||||
template<typename LenT = uoffset_t>
|
||||
bool VerifyVectorOrString(const uint8_t *const vec, const size_t elem_size,
|
||||
size_t *const end = nullptr) const {
|
||||
template <typename LenT = uoffset_t>
|
||||
bool VerifyVectorOrString(const uint8_t* const vec, const size_t elem_size,
|
||||
size_t* const end = nullptr) const {
|
||||
const auto vec_offset = static_cast<size_t>(vec - buf_);
|
||||
// Check we can read the size field.
|
||||
if (!Verify<LenT>(vec_offset)) return false;
|
||||
@@ -159,7 +161,7 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
}
|
||||
|
||||
// Special case for string contents, after the above has been called.
|
||||
bool VerifyVectorOfStrings(const Vector<Offset<String>> *const vec) const {
|
||||
bool VerifyVectorOfStrings(const Vector<Offset<String>>* const vec) const {
|
||||
if (vec) {
|
||||
for (uoffset_t i = 0; i < vec->size(); i++) {
|
||||
if (!VerifyString(vec->Get(i))) return false;
|
||||
@@ -169,8 +171,8 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
}
|
||||
|
||||
// Special case for table contents, after the above has been called.
|
||||
template<typename T>
|
||||
bool VerifyVectorOfTables(const Vector<Offset<T>> *const vec) {
|
||||
template <typename T>
|
||||
bool VerifyVectorOfTables(const Vector<Offset<T>>* const vec) {
|
||||
if (vec) {
|
||||
for (uoffset_t i = 0; i < vec->size(); i++) {
|
||||
if (!vec->Get(i)->Verify(*this)) return false;
|
||||
@@ -180,7 +182,7 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
}
|
||||
|
||||
FLATBUFFERS_SUPPRESS_UBSAN("unsigned-integer-overflow")
|
||||
bool VerifyTableStart(const uint8_t *const table) {
|
||||
bool VerifyTableStart(const uint8_t* const table) {
|
||||
// Check the vtable offset.
|
||||
const auto tableo = static_cast<size_t>(table - buf_);
|
||||
if (!Verify<soffset_t>(tableo)) return false;
|
||||
@@ -197,8 +199,8 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
return Check((vsize & 1) == 0) && Verify(vtableo, vsize);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool VerifyBufferFromStart(const char *const identifier, const size_t start) {
|
||||
template <typename T>
|
||||
bool VerifyBufferFromStart(const char* const identifier, const size_t start) {
|
||||
// Buffers have to be of some size to be valid. The reason it is a runtime
|
||||
// check instead of static_assert, is that nested flatbuffers go through
|
||||
// this call and their size is determined at runtime.
|
||||
@@ -213,7 +215,7 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
// Call T::Verify, which must be in the generated code for this type.
|
||||
const auto o = VerifyOffset<uoffset_t>(start);
|
||||
if (!Check(o != 0)) return false;
|
||||
if (!(reinterpret_cast<const T *>(buf_ + start + o)->Verify(*this))) {
|
||||
if (!(reinterpret_cast<const T*>(buf_ + start + o)->Verify(*this))) {
|
||||
return false;
|
||||
}
|
||||
if (TrackVerifierBufferSize) {
|
||||
@@ -222,9 +224,9 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T, int &..., typename SizeT>
|
||||
bool VerifyNestedFlatBuffer(const Vector<uint8_t, SizeT> *const buf,
|
||||
const char *const identifier) {
|
||||
template <typename T, int&..., typename SizeT>
|
||||
bool VerifyNestedFlatBuffer(const Vector<uint8_t, SizeT>* const buf,
|
||||
const char* const identifier) {
|
||||
// Caller opted out of this.
|
||||
if (!opts_.check_nested_flatbuffers) return true;
|
||||
|
||||
@@ -240,14 +242,18 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
}
|
||||
|
||||
// Verify this whole buffer, starting with root type T.
|
||||
template<typename T> bool VerifyBuffer() { return VerifyBuffer<T>(nullptr); }
|
||||
template <typename T>
|
||||
bool VerifyBuffer() {
|
||||
return VerifyBuffer<T>(nullptr);
|
||||
}
|
||||
|
||||
template<typename T> bool VerifyBuffer(const char *const identifier) {
|
||||
template <typename T>
|
||||
bool VerifyBuffer(const char* const identifier) {
|
||||
return VerifyBufferFromStart<T>(identifier, 0);
|
||||
}
|
||||
|
||||
template<typename T, typename SizeT = uoffset_t>
|
||||
bool VerifySizePrefixedBuffer(const char *const identifier) {
|
||||
template <typename T, typename SizeT = uoffset_t>
|
||||
bool VerifySizePrefixedBuffer(const char* const identifier) {
|
||||
return Verify<SizeT>(0U) &&
|
||||
// Ensure the prefixed size is within the bounds of the provided
|
||||
// length.
|
||||
@@ -255,7 +261,7 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
VerifyBufferFromStart<T>(identifier, sizeof(SizeT));
|
||||
}
|
||||
|
||||
template<typename OffsetT = uoffset_t, typename SOffsetT = soffset_t>
|
||||
template <typename OffsetT = uoffset_t, typename SOffsetT = soffset_t>
|
||||
size_t VerifyOffset(const size_t start) const {
|
||||
if (!Verify<OffsetT>(start)) return 0;
|
||||
const auto o = ReadScalar<OffsetT>(buf_ + start);
|
||||
@@ -269,8 +275,8 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
return o;
|
||||
}
|
||||
|
||||
template<typename OffsetT = uoffset_t>
|
||||
size_t VerifyOffset(const uint8_t *const base, const voffset_t start) const {
|
||||
template <typename OffsetT = uoffset_t>
|
||||
size_t VerifyOffset(const uint8_t* const base, const voffset_t start) const {
|
||||
return VerifyOffset<OffsetT>(static_cast<size_t>(base - buf_) + start);
|
||||
}
|
||||
|
||||
@@ -303,7 +309,7 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
uintptr_t size = upper_bound_;
|
||||
// Align the size to uoffset_t
|
||||
size = (size - 1 + sizeof(uoffset_t)) & ~(sizeof(uoffset_t) - 1);
|
||||
return (size > size_) ? 0 : size;
|
||||
return (size > size_) ? 0 : size;
|
||||
}
|
||||
// Must use SizeVerifier, or (deprecated) turn on
|
||||
// FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE, for this to work.
|
||||
@@ -312,14 +318,14 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> *GetFlexReuseTracker() { return flex_reuse_tracker_; }
|
||||
std::vector<uint8_t>* GetFlexReuseTracker() { return flex_reuse_tracker_; }
|
||||
|
||||
void SetFlexReuseTracker(std::vector<uint8_t> *const rt) {
|
||||
void SetFlexReuseTracker(std::vector<uint8_t>* const rt) {
|
||||
flex_reuse_tracker_ = rt;
|
||||
}
|
||||
|
||||
private:
|
||||
const uint8_t *buf_;
|
||||
const uint8_t* buf_;
|
||||
const size_t size_;
|
||||
const Options opts_;
|
||||
|
||||
@@ -327,18 +333,18 @@ class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
|
||||
|
||||
uoffset_t depth_ = 0;
|
||||
uoffset_t num_tables_ = 0;
|
||||
std::vector<uint8_t> *flex_reuse_tracker_ = nullptr;
|
||||
std::vector<uint8_t>* flex_reuse_tracker_ = nullptr;
|
||||
};
|
||||
|
||||
// Specialization for 64-bit offsets.
|
||||
template<>
|
||||
template<>
|
||||
template <>
|
||||
template <>
|
||||
inline size_t VerifierTemplate<false>::VerifyOffset<uoffset64_t>(
|
||||
const size_t start) const {
|
||||
return VerifyOffset<uoffset64_t, soffset64_t>(start);
|
||||
}
|
||||
template<>
|
||||
template<>
|
||||
template <>
|
||||
template <>
|
||||
inline size_t VerifierTemplate<true>::VerifyOffset<uoffset64_t>(
|
||||
const size_t start) const {
|
||||
return VerifyOffset<uoffset64_t, soffset64_t>(start);
|
||||
|
||||
Reference in New Issue
Block a user