1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33:03 +04:00

Update FlatBuffers source code to 25.9.23

This commit is contained in:
Dmitry Kurtaev
2025-11-27 18:30:25 +03:00
parent 641a7aa2a6
commit 895be753ac
19 changed files with 817 additions and 641 deletions
+1 -1
View File
@@ -1 +1 @@
Origin: https://github.com/google/flatbuffers/tree/v23.5.9
Origin: https://github.com/google/flatbuffers/tree/v25.9.23
+9 -8
View File
@@ -27,17 +27,15 @@
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;
typedef
typename flatbuffers::conditional<scalar_tag::value, T, const T *>::type
IndirectHelperType;
public:
typedef uint16_t size_type;
typedef typename IndirectHelper<IndirectHelperType>::return_type return_type;
typedef typename IndirectHelper<T>::return_type return_type;
typedef VectorConstIterator<T, return_type, uoffset_t> const_iterator;
typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
@@ -50,7 +48,7 @@ template<typename T, uint16_t length> class Array {
return_type Get(uoffset_t i) const {
FLATBUFFERS_ASSERT(i < size());
return IndirectHelper<IndirectHelperType>::Read(Data(), i);
return IndirectHelper<T>::Read(Data(), i);
}
return_type operator[](uoffset_t i) const { return Get(i); }
@@ -58,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));
}
@@ -134,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
+26 -18
View File
@@ -139,9 +139,9 @@
#endif
#endif // !defined(FLATBUFFERS_LITTLEENDIAN)
#define FLATBUFFERS_VERSION_MAJOR 23
#define FLATBUFFERS_VERSION_MINOR 5
#define FLATBUFFERS_VERSION_REVISION 9
#define FLATBUFFERS_VERSION_MAJOR 25
#define FLATBUFFERS_VERSION_MINOR 9
#define FLATBUFFERS_VERSION_REVISION 23
#define FLATBUFFERS_STRING_EXPAND(X) #X
#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
namespace flatbuffers {
@@ -155,7 +155,7 @@ namespace flatbuffers {
#define FLATBUFFERS_FINAL_CLASS final
#define FLATBUFFERS_OVERRIDE override
#define FLATBUFFERS_EXPLICIT_CPP11 explicit
#define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t
#define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : ::flatbuffers::voffset_t
#else
#define FLATBUFFERS_FINAL_CLASS
#define FLATBUFFERS_OVERRIDE
@@ -279,21 +279,23 @@ namespace flatbuffers {
#endif // !FLATBUFFERS_LOCALE_INDEPENDENT
// Suppress Undefined Behavior Sanitizer (recoverable only). Usage:
// - __suppress_ubsan__("undefined")
// - __suppress_ubsan__("signed-integer-overflow")
// - FLATBUFFERS_SUPPRESS_UBSAN("undefined")
// - FLATBUFFERS_SUPPRESS_UBSAN("signed-integer-overflow")
#if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))
#define __suppress_ubsan__(type) __attribute__((no_sanitize(type)))
#define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize(type)))
#elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
#define __suppress_ubsan__(type) __attribute__((no_sanitize_undefined))
#define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize_undefined))
#else
#define __suppress_ubsan__(type)
#define FLATBUFFERS_SUPPRESS_UBSAN(type)
#endif
namespace flatbuffers {
// This is constexpr function used for checking compile-time constants.
// Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`.
template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) {
return !!t;
}
}
// Enable C++ attribute [[]] if std:c++17 or higher.
#if ((__cplusplus >= 201703L) \
@@ -337,15 +339,15 @@ typedef uint16_t voffset_t;
typedef uintmax_t largest_scalar_t;
// In 32bits, this evaluates to 2GB - 1
#define FLATBUFFERS_MAX_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset_t>::max()
#define FLATBUFFERS_MAX_64_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset64_t>::max()
#define FLATBUFFERS_MAX_BUFFER_SIZE (std::numeric_limits<::flatbuffers::soffset_t>::max)()
#define FLATBUFFERS_MAX_64_BUFFER_SIZE (std::numeric_limits<::flatbuffers::soffset64_t>::max)()
// The minimum size buffer that can be a valid flatbuffer.
// Includes the offset to the root table (uoffset_t), the offset to the vtable
// of the root table (soffset_t), the size of the vtable (uint16_t), and the
// size of the referring table (uint16_t).
#define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(uoffset_t) + sizeof(soffset_t) + \
sizeof(uint16_t) + sizeof(uint16_t)
#define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(::flatbuffers::uoffset_t) + \
sizeof(::flatbuffers::soffset_t) + sizeof(uint16_t) + sizeof(uint16_t)
// We support aligning the contents of buffers up to this size.
#ifndef FLATBUFFERS_MAX_ALIGNMENT
@@ -361,7 +363,6 @@ inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) {
}
#if defined(_MSC_VER)
#pragma warning(disable: 4351) // C4351: new behavior: elements of array ... will be default initialized
#pragma warning(push)
#pragma warning(disable: 4127) // C4127: conditional expression is constant
#endif
@@ -422,7 +423,7 @@ template<typename T> T EndianScalar(T t) {
template<typename T>
// UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
__suppress_ubsan__("alignment")
FLATBUFFERS_SUPPRESS_UBSAN("alignment")
T ReadScalar(const void *p) {
return EndianScalar(*reinterpret_cast<const T *>(p));
}
@@ -436,13 +437,13 @@ T ReadScalar(const void *p) {
template<typename T>
// UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
__suppress_ubsan__("alignment")
FLATBUFFERS_SUPPRESS_UBSAN("alignment")
void WriteScalar(void *p, T t) {
*reinterpret_cast<T *>(p) = EndianScalar(t);
}
template<typename T> struct Offset;
template<typename T> __suppress_ubsan__("alignment") void WriteScalar(void *p, Offset<T> t) {
template<typename T> FLATBUFFERS_SUPPRESS_UBSAN("alignment") void WriteScalar(void *p, Offset<T> t) {
*reinterpret_cast<uoffset_t *>(p) = EndianScalar(t.o);
}
@@ -453,15 +454,22 @@ template<typename T> __suppress_ubsan__("alignment") void WriteScalar(void *p, O
// Computes how many bytes you'd have to pad to be able to write an
// "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in
// memory).
__suppress_ubsan__("unsigned-integer-overflow")
FLATBUFFERS_SUPPRESS_UBSAN("unsigned-integer-overflow")
inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
return ((~buf_size) + 1) & (scalar_size - 1);
}
#if !defined(_MSC_VER)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
// Generic 'operator==' with conditional specialisations.
// T e - new value of a scalar field.
// T def - default of scalar (is known at compile-time).
template<typename T> inline bool IsTheSameAs(T e, T def) { return e == def; }
#if !defined(_MSC_VER)
#pragma GCC diagnostic pop
#endif
#if defined(FLATBUFFERS_NAN_DEFAULTS) && \
defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
+38 -12
View File
@@ -20,12 +20,14 @@
#include <algorithm>
#include "flatbuffers/base.h"
#include "flatbuffers/stl_emulation.h"
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;
@@ -36,8 +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>
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;
@@ -48,6 +56,11 @@ template<typename T = void> struct Offset64 {
bool IsNull() const { return !o; }
};
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.
static_assert(sizeof(Offset<>) == 4, "Offset has wrong size");
static_assert(sizeof(Offset64<>) == 8, "Offset64 has wrong size");
@@ -60,7 +73,8 @@ inline void EndianCheck() {
(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);
@@ -90,7 +104,8 @@ 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> 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);
@@ -135,10 +150,20 @@ struct IndirectHelper<OffsetT<T>> {
};
// For vector of structs.
template<typename T> struct IndirectHelper<const T *> {
typedef const T *return_type;
typedef T *mutable_return_type;
static const size_t element_stride = sizeof(T);
template <typename T>
struct IndirectHelper<
T, typename std::enable_if<
!std::is_scalar<typename std::remove_pointer<T>::type>::value &&
!is_specialisation_of_Offset<T>::value &&
!is_specialisation_of_Offset64<T>::value>::type> {
private:
typedef typename std::remove_pointer<typename std::remove_cv<T>::type>::type
pointee_type;
public:
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) {
// Structs are stored inline, relative to the first struct pointer.
@@ -172,11 +197,11 @@ 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) +
return reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(buf) +
EndianScalar(*reinterpret_cast<uoffset_t*>(buf)));
}
@@ -185,7 +210,8 @@ T *GetMutableSizePrefixedRoot(void *buf) {
return GetMutableRoot<T>(reinterpret_cast<uint8_t*>(buf) + sizeof(SizeT));
}
template<typename T> const T *GetRoot(const void *buf) {
template <typename T>
const T* GetRoot(const void* buf) {
return GetMutableRoot<T>(const_cast<void*>(buf));
}
+2 -1
View File
@@ -27,7 +27,8 @@ 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)
: buf(_buf), len(_len), must_free(false) {}
+8 -1
View File
@@ -80,6 +80,11 @@ class DetachedBuffer {
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(); }
// These may change access mode, leave these at end of public section
FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer& other));
FLATBUFFERS_DELETE_FUNC(
@@ -95,7 +100,9 @@ class DetachedBuffer {
inline void destroy() {
if (buf_) Deallocate(allocator_, buf_, reserved_);
if (own_allocator_ && allocator_) { delete allocator_; }
if (own_allocator_ && allocator_) {
delete allocator_;
}
reset();
}
+102 -66
View File
@@ -45,7 +45,9 @@ inline voffset_t FieldIndexToOffset(voffset_t field_id) {
// Should correspond to what EndTable() below builds up.
const voffset_t fixed_fields =
2 * sizeof(voffset_t); // Vtable size and Object Size.
return fixed_fields + field_id * sizeof(voffset_t);
size_t offset = fixed_fields + field_id * sizeof(voffset_t);
FLATBUFFERS_ASSERT(offset < std::numeric_limits<voffset_t>::max());
return static_cast<voffset_t>(offset);
}
template <typename T, typename Alloc = std::allocator<T>>
@@ -72,7 +74,8 @@ T *data(std::vector<T, Alloc> &v) {
/// `PushElement`/`AddElement`/`EndTable`, or the builtin `CreateString`/
/// `CreateVector` functions. Do this is depth-first order to build up a tree to
/// the root. `Finish()` wraps up the buffer ready for transport.
template<bool Is64Aware = false> class FlatBufferBuilderImpl {
template <bool Is64Aware = false>
class FlatBufferBuilderImpl {
public:
// This switches the size type of the builder, based on if its 64-bit aware
// (uoffset64_t) or not (uoffset_t).
@@ -220,21 +223,13 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// @return Returns a `uint8_t` pointer to the unfinished buffer.
uint8_t* GetCurrentBufferPointer() const { return buf_.data(); }
/// @brief Get the released pointer to the serialized buffer.
/// @warning Do NOT attempt to use this FlatBufferBuilder afterwards!
/// @return A `FlatBuffer` that owns the buffer and its allocator and
/// behaves similar to a `unique_ptr` with a deleter.
FLATBUFFERS_ATTRIBUTE([[deprecated("use Release() instead")]])
DetachedBuffer ReleaseBufferPointer() {
Finished();
return buf_.release();
}
/// @brief Get the released DetachedBuffer.
/// @return A `DetachedBuffer` that owns the buffer and its allocator.
DetachedBuffer Release() {
Finished();
return buf_.release();
DetachedBuffer buffer = buf_.release();
Clear();
return buffer;
}
/// @brief Get the released pointer to the serialized buffer.
@@ -245,10 +240,12 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// @return A raw pointer to the start of the memory block containing
/// the serialized `FlatBuffer`.
/// @remark If the allocator is owned, it gets deleted when the destructor is
/// called..
/// called.
uint8_t* ReleaseRaw(size_t& size, size_t& offset) {
Finished();
return buf_.release_raw(size, offset);
uint8_t* raw = buf_.release_raw(size, offset);
Clear();
return raw;
}
/// @brief get the minimum alignment this buffer needs to be accessed
@@ -304,7 +301,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
void PopBytes(size_t amount) { buf_.pop(amount); }
template<typename T> void AssertScalarT() {
template <typename T>
void AssertScalarT() {
// The code assumes power of 2 sizes and endian-swap-ability.
static_assert(flatbuffers::is_scalar<T>::value, "T must be a scalar type");
}
@@ -330,31 +328,38 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
FieldLoc fl = {off, field};
buf_.scratch_push_small(fl);
num_field_loc++;
if (field > max_voffset_) { max_voffset_ = field; }
if (field > max_voffset_) {
max_voffset_ = field;
}
}
// Like PushElement, but additionally tracks the field this represents.
template<typename T> void AddElement(voffset_t field, T e, T def) {
template <typename T>
void AddElement(voffset_t field, T e, T def) {
// We don't serialize values equal to the default.
if (IsTheSameAs(e, def) && !force_defaults_) return;
TrackField(field, PushElement(e));
}
template<typename T> void AddElement(voffset_t field, T e) {
template <typename T>
void AddElement(voffset_t field, T e) {
TrackField(field, PushElement(e));
}
template<typename T> void AddOffset(voffset_t field, Offset<T> off) {
template <typename T>
void AddOffset(voffset_t field, Offset<T> off) {
if (off.IsNull()) return; // Don't store.
AddElement(field, ReferTo(off.o), static_cast<uoffset_t>(0));
}
template<typename T> void AddOffset(voffset_t field, Offset64<T> off) {
template <typename T>
void AddOffset(voffset_t field, Offset64<T> off) {
if (off.IsNull()) return; // Don't store.
AddElement(field, ReferTo(off.o), static_cast<uoffset64_t>(0));
}
template<typename T> void AddStruct(voffset_t field, const T *structptr) {
template <typename T>
void AddStruct(voffset_t field, const T* structptr) {
if (!structptr) return; // Default, don't store.
Align(AlignOf<T>());
buf_.push_small(*structptr);
@@ -384,12 +389,14 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
return ReferTo(off, GetSize());
}
template<typename T, typename T2> T ReferTo(const T off, const T2 size) {
template <typename T, typename T2>
T ReferTo(const T off, const T2 size) {
FLATBUFFERS_ASSERT(off && off <= size);
return size - off + static_cast<T>(sizeof(T));
}
template<typename T> T ReferTo(const T off, const T size) {
template <typename T>
T ReferTo(const T off, const T size) {
FLATBUFFERS_ASSERT(off && off <= size);
return size - off + static_cast<T>(sizeof(T));
}
@@ -494,7 +501,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
// This checks a required field has been set in a given table that has
// just been constructed.
template<typename T> void Required(Offset<T> table, voffset_t field) {
template <typename T>
void Required(Offset<T> table, voffset_t field) {
auto table_ptr = reinterpret_cast<const Table*>(buf_.data_at(table.o));
bool ok = table_ptr->GetOptionalFieldOffset(field) != 0;
// If this fails, the caller will show what field needs to be set.
@@ -525,7 +533,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
// Aligns such than when "len" bytes are written, an object of type `AlignT`
// can be written after it (forward in the buffer) without padding.
template<typename AlignT> void PreAlign(size_t len) {
template <typename AlignT>
void PreAlign(size_t len) {
AssertScalarT<AlignT>();
PreAlign(len, AlignOf<AlignT>());
}
@@ -588,14 +597,14 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// @brief Store a string in the buffer, which can contain any binary data.
/// @param[in] str A const reference to a std::string like type with support
/// of T::c_str() and T::length() to store in the buffer.
/// of T::data() and T::length() to store in the buffer.
/// @return Returns the offset in the buffer where the string starts.
template <template <typename> class OffsetT = Offset,
// No need to explicitly declare the T type, let the compiler deduce
// it.
int&... ExplicitArgumentBarrier, typename T>
OffsetT<String> CreateString(const T& str) {
return CreateString<OffsetT>(str.c_str(), str.length());
return CreateString<OffsetT>(str.data(), str.length());
}
/// @brief Store a string in the buffer, which can contain any binary data.
@@ -677,7 +686,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
return PushElement<LenT, ReturnT>(static_cast<LenT>(len));
}
template<template<typename> class OffsetT = Offset, typename LenT = uint32_t>
template <template <typename> class OffsetT = Offset,
typename LenT = uint32_t>
void StartVector(size_t len, size_t elemsize, size_t alignment) {
NotNested();
nested = true;
@@ -698,12 +708,27 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
// normally dictate.
// This is useful when storing a nested_flatbuffer in a vector of bytes,
// or when storing SIMD floats, etc.
void ForceVectorAlignment(size_t len, size_t elemsize, size_t alignment) {
void ForceVectorAlignment(const size_t len, const size_t elemsize,
const size_t alignment) {
if (len == 0) return;
FLATBUFFERS_ASSERT(VerifyAlignmentRequirements(alignment));
PreAlign(len * elemsize, alignment);
}
template <bool is_64 = Is64Aware>
typename std::enable_if<is_64, void>::type ForceVectorAlignment64(
const size_t len, const size_t elemsize, const size_t alignment) {
// If you hit this assertion, you are trying to force alignment on a
// vector with offset64 after serializing a 32-bit offset.
FLATBUFFERS_ASSERT(GetSize() == length_of_64_bit_region_);
// Call through.
ForceVectorAlignment(len, elemsize, alignment);
// Update the 64 bit region.
length_of_64_bit_region_ = GetSize();
}
// Similar to ForceVectorAlignment but for String fields.
void ForceStringAlignment(size_t len, size_t alignment) {
if (len == 0) return;
@@ -722,9 +747,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// @param[in] len The number of elements to serialize.
/// @return Returns a typed `TOffset` into the serialized data indicating
/// where the vector is stored.
template<template<typename...> class OffsetT = Offset,
template<typename...> class VectorT = Vector,
int &...ExplicitArgumentBarrier, typename T>
template <typename T, template <typename...> class OffsetT = Offset,
template <typename...> class VectorT = Vector>
OffsetT<VectorT<T>> CreateVector(const T* v, size_t len) {
// The type of the length field in the vector.
typedef typename VectorT<T>::size_type LenT;
@@ -758,7 +782,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// serialize into the buffer as a `vector`.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template<typename T, class C> Offset<Vector<T>> CreateVector(const C &array) {
template <typename T, class C>
Offset<Vector<T>> CreateVector(const C& array) {
return CreateVector(array.data(), array.size());
}
@@ -775,7 +800,9 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
template <typename T>
Offset<Vector<Offset<T>>> CreateVector(const Offset<T>* v, size_t len) {
StartVector<Offset<T>>(len);
for (auto i = len; i > 0;) { PushElement(v[--i]); }
for (auto i = len; i > 0;) {
PushElement(v[--i]);
}
return Offset<Vector<Offset<T>>>(EndVector(len));
}
@@ -793,7 +820,7 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
template <template <typename...> class VectorT = Vector64,
int&... ExplicitArgumentBarrier, typename T>
Offset64<VectorT<T>> CreateVector64(const std::vector<T>& v) {
return CreateVector<Offset64, VectorT>(data(v), v.size());
return CreateVector<T, Offset64, VectorT>(data(v), v.size());
}
// vector<bool> may be implemented using a bit-set, so we can't access it as
@@ -865,7 +892,9 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// where the vector is stored.
template <class It>
Offset<Vector<Offset<String>>> CreateVectorOfStrings(It begin, It end) {
auto size = std::distance(begin, end);
auto distance = std::distance(begin, end);
FLATBUFFERS_ASSERT(distance >= 0);
auto size = static_cast<size_t>(distance);
auto scratch_buffer_usage = size * sizeof(Offset<String>);
// If there is not enough space to store the offsets, there definitely won't
// be enough space to store all the strings. So ensuring space for the
@@ -875,7 +904,7 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
buf_.scratch_push_small(CreateString(*it));
}
StartVector<Offset<String>>(size);
for (auto i = 1; i <= size; i++) {
for (size_t i = 1; i <= size; i++) {
// Note we re-evaluate the buf location each iteration to account for any
// underlying buffer resizing that may occur.
PushElement(*reinterpret_cast<Offset<String>*>(
@@ -899,8 +928,7 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
typedef typename VectorT<T>::size_type LenT;
typedef typename OffsetT<VectorT<const T*>>::offset_type offset_type;
StartVector<OffsetT, LenT>(len * sizeof(T) / AlignOf<T>(), sizeof(T),
AlignOf<T>());
StartVector<OffsetT, LenT>(len, sizeof(T), AlignOf<T>());
if (len > 0) {
PushBytes(reinterpret_cast<const uint8_t*>(v), sizeof(T) * len);
}
@@ -960,9 +988,9 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
return CreateVectorOfStructs<T, OffsetT, VectorT>(data(v), v.size());
}
template<template<typename...> class VectorT = Vector64, int &..., typename T>
Offset64<VectorT<const T *>> CreateVectorOfStructs64(
const std::vector<T> &v) {
template <template <typename...> class VectorT = Vector64, int&...,
typename T>
Offset64<VectorT<const T*>> CreateVectorOfStructs64(const std::vector<T>& v) {
return CreateVectorOfStructs<T, Offset64, VectorT>(data(v), v.size());
}
@@ -981,7 +1009,9 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
const S* v, size_t len, T (*const pack_func)(const S&)) {
FLATBUFFERS_ASSERT(pack_func);
auto structs = StartVectorOfStructs<T>(len);
for (size_t i = 0; i < len; i++) { structs[i] = pack_func(v[i]); }
for (size_t i = 0; i < len; i++) {
structs[i] = pack_func(v[i]);
}
return EndVectorOfStructs<T>(len);
}
@@ -994,8 +1024,7 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template <typename T, typename S>
Offset<Vector<const T *>> CreateVectorOfNativeStructs(const S *v,
size_t len) {
Offset<Vector<const T*>> CreateVectorOfNativeStructs(const S* v, size_t len) {
extern T Pack(const S&);
return CreateVectorOfNativeStructs(v, len, Pack);
}
@@ -1031,7 +1060,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
}
/// @cond FLATBUFFERS_INTERNAL
template<typename T> struct StructKeyComparator {
template <typename T>
struct StructKeyComparator {
bool operator()(const T& a, const T& b) const {
return a.KeyCompareLessThan(&b);
}
@@ -1089,17 +1119,19 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template <typename T, typename S>
Offset<Vector<const T *>> CreateVectorOfSortedNativeStructs(S *v,
size_t len) {
Offset<Vector<const T*>> CreateVectorOfSortedNativeStructs(S* v, size_t len) {
extern T Pack(const S&);
auto structs = StartVectorOfStructs<T>(len);
for (size_t i = 0; i < len; i++) { structs[i] = Pack(v[i]); }
for (size_t i = 0; i < len; i++) {
structs[i] = Pack(v[i]);
}
std::stable_sort(structs, structs + len, StructKeyComparator<T>());
return EndVectorOfStructs<T>(len);
}
/// @cond FLATBUFFERS_INTERNAL
template<typename T> struct TableKeyComparator {
template <typename T>
struct TableKeyComparator {
explicit TableKeyComparator(vector_downward<SizeT>& buf) : buf_(buf) {}
TableKeyComparator(const TableKeyComparator& other) : buf_(other.buf_) {}
bool operator()(const Offset<T>& a, const Offset<T>& b) const {
@@ -1197,12 +1229,15 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
AssertScalarT<T>();
AssertScalarT<U>();
StartVector<T>(len);
for (auto i = len; i > 0;) { PushElement(static_cast<T>(v[--i])); }
for (auto i = len; i > 0;) {
PushElement(static_cast<T>(v[--i]));
}
return Offset<Vector<T>>(EndVector(len));
}
/// @brief Write a struct by itself, typically to be part of a union.
template<typename T> Offset<const T *> CreateStruct(const T &structobj) {
template <typename T>
Offset<const T*> CreateStruct(const T& structobj) {
NotNested();
Align(AlignOf<T>());
buf_.push_small(structobj);
@@ -1245,6 +1280,9 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
FlatBufferBuilderImpl& operator=(const FlatBufferBuilderImpl&);
void Finish(uoffset_t root, const char* file_identifier, bool size_prefix) {
// A buffer can only be finished once. To reuse a builder use `clear()`.
FLATBUFFERS_ASSERT(!finished);
NotNested();
buf_.clear_scratch();
@@ -1264,7 +1302,9 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
kFileIdentifierLength);
}
PushElement(ReferTo(root)); // Location of root.
if (size_prefix) { PushElement(GetSize()); }
if (size_prefix) {
PushElement(GetSize());
}
finished = true;
}
@@ -1302,11 +1342,6 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
// This will remain 0 if no 64-bit offset types are added to the buffer.
size_t length_of_64_bit_region_;
// When true, 64-bit offsets can still be added to the builder. When false,
// only 32-bit offsets can be added, and attempts to add a 64-bit offset will
// raise an assertion. This is typically a compile-time error in ordering the
// serialization of 64-bit offset fields not at the tail of the buffer.
// Ensure objects are not nested.
bool nested;
@@ -1375,8 +1410,7 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
// Must be completed with EndVectorOfStructs().
template <typename T, template <typename> class OffsetT = Offset>
T* StartVectorOfStructs(size_t vector_size) {
StartVector<OffsetT>(vector_size * sizeof(T) / AlignOf<T>(), sizeof(T),
AlignOf<T>());
StartVector<OffsetT>(vector_size, sizeof(T), AlignOf<T>());
return reinterpret_cast<T*>(buf_.make_space(vector_size * sizeof(T)));
}
@@ -1417,8 +1451,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
// Hack to `FlatBufferBuilder` mean `FlatBufferBuilder<false>` or
// `FlatBufferBuilder<>`, where the template < > syntax is required.
typedef FlatBufferBuilderImpl<false> FlatBufferBuilder;
typedef FlatBufferBuilderImpl<true> FlatBufferBuilder64;
using FlatBufferBuilder = FlatBufferBuilderImpl<false>;
using FlatBufferBuilder64 = FlatBufferBuilderImpl<true>;
// These are external due to GCC not allowing them in the class.
// See: https://stackoverflow.com/q/8061456/868247
@@ -1433,7 +1467,8 @@ inline Offset64<String> FlatBufferBuilder64::CreateString(const char *str,
}
// Used to distinguish from real Offsets.
template<typename T = void> struct EmptyOffset {};
template <typename T = void>
struct EmptyOffset {};
// TODO(derekbailey): it would be nice to combine these two methods.
template <>
@@ -1462,8 +1497,9 @@ T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) {
}
template <typename T>
const T *GetTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) {
return GetMutableTemporaryPointer<T>(fbb, offset);
const T* GetTemporaryPointer(const FlatBufferBuilder& fbb, Offset<T> offset) {
return reinterpret_cast<const T*>(fbb.GetCurrentBufferPointer() +
fbb.GetSize() - offset.o);
}
} // namespace flatbuffers
+19 -8
View File
@@ -81,6 +81,17 @@ inline SizeT GetPrefixedSize(const uint8_t *buf) {
return ReadScalar<SizeT>(buf);
}
// Gets the total length of the buffer given a sized prefixed FlatBuffer.
//
// This includes the size of the prefix as well as the buffer:
//
// [size prefix][flatbuffer]
// |---------length--------|
template <typename SizeT = uoffset_t>
inline SizeT GetSizePrefixedBufferLength(const uint8_t* const buf) {
return ReadScalar<SizeT>(buf) + sizeof(SizeT);
}
// Base class for native objects (FlatBuffer data de-serialized into native
// C++ data structures).
// Contains no functionality, purely documentative.
@@ -237,31 +248,31 @@ inline const char *flatbuffers_version_string() {
// clang-format off
#define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\
inline E operator | (E lhs, E rhs){\
inline FLATBUFFERS_CONSTEXPR_CPP11 E operator | (E lhs, E rhs){\
return E(T(lhs) | T(rhs));\
}\
inline E operator & (E lhs, E rhs){\
inline FLATBUFFERS_CONSTEXPR_CPP11 E operator & (E lhs, E rhs){\
return E(T(lhs) & T(rhs));\
}\
inline E operator ^ (E lhs, E rhs){\
inline FLATBUFFERS_CONSTEXPR_CPP11 E operator ^ (E lhs, E rhs){\
return E(T(lhs) ^ T(rhs));\
}\
inline E operator ~ (E lhs){\
inline FLATBUFFERS_CONSTEXPR_CPP11 E operator ~ (E lhs){\
return E(~T(lhs));\
}\
inline E operator |= (E &lhs, E rhs){\
inline FLATBUFFERS_CONSTEXPR_CPP11 E operator |= (E &lhs, E rhs){\
lhs = lhs | rhs;\
return lhs;\
}\
inline E operator &= (E &lhs, E rhs){\
inline FLATBUFFERS_CONSTEXPR_CPP11 E operator &= (E &lhs, E rhs){\
lhs = lhs & rhs;\
return lhs;\
}\
inline E operator ^= (E &lhs, E rhs){\
inline FLATBUFFERS_CONSTEXPR_CPP11 E operator ^= (E &lhs, E rhs){\
lhs = lhs ^ rhs;\
return lhs;\
}\
inline bool operator !(E rhs) \
inline FLATBUFFERS_CONSTEXPR_CPP11 bool operator !(E rhs) \
{\
return !bool(T(rhs)); \
}
+3 -2
View File
@@ -45,7 +45,8 @@
// Testing __cpp_lib_span requires including either <version> or <span>,
// both of which were added in C++20.
// See: https://en.cppreference.com/w/cpp/utility/feature_test
#if defined(__cplusplus) && __cplusplus >= 202002L
#if defined(__cplusplus) && __cplusplus >= 202002L \
|| (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#define FLATBUFFERS_USE_STD_SPAN 1
#endif
#endif // FLATBUFFERS_USE_STD_SPAN
@@ -272,7 +273,7 @@ template<class T, class U>
FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
return static_cast<bool>(lhs) != static_cast<bool>(rhs)
? false
: !static_cast<bool>(lhs) ? false : (*lhs == *rhs);
: !static_cast<bool>(lhs) ? true : (*lhs == *rhs);
}
#endif // FLATBUFFERS_USE_STD_OPTIONAL
+5
View File
@@ -31,6 +31,11 @@ struct String : public Vector<char> {
flatbuffers::string_view string_view() const {
return flatbuffers::string_view(c_str(), size());
}
/* implicit */
operator flatbuffers::string_view() const {
return flatbuffers::string_view(c_str(), size());
}
#endif // FLATBUFFERS_HAS_STRING_VIEW
// clang-format on
+4 -2
View File
@@ -27,11 +27,13 @@ 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]);
}
+12 -6
View File
@@ -42,7 +42,8 @@ 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;
}
@@ -59,15 +60,18 @@ class Table {
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);
return field_offset ? reinterpret_cast<P>(p) : nullptr;
@@ -81,13 +85,15 @@ 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);
+38 -16
View File
@@ -56,12 +56,24 @@ struct VectorIterator {
return data_ == other.data_;
}
bool operator!=(const VectorIterator& other) const {
return data_ != other.data_;
}
bool operator<(const VectorIterator& other) const {
return data_ < other.data_;
}
bool operator!=(const VectorIterator &other) const {
return data_ != other.data_;
bool operator>(const VectorIterator& other) const {
return data_ > other.data_;
}
bool operator<=(const VectorIterator& other) const {
return !(data_ > other.data_);
}
bool operator>=(const VectorIterator& other) const {
return !(data_ < other.data_);
}
difference_type operator-(const VectorIterator& other) const {
@@ -145,14 +157,13 @@ 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,
typedef VectorIterator<T, typename IndirectHelper<T>::mutable_return_type,
uint8_t*, SizeT>
iterator;
typedef VectorConstIterator<T, typename IndirectHelper<T>::return_type,
SizeT>
typedef VectorConstIterator<T, typename IndirectHelper<T>::return_type, SizeT>
const_iterator;
typedef VectorReverseIterator<iterator> reverse_iterator;
typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
@@ -165,14 +176,18 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
SizeT size() const { return EndianScalar(length_); }
// Returns true if the vector is empty.
//
// This just provides another standardized method that is expected of vectors.
bool empty() const { return size() == 0; }
// Deprecated: use size(). Here for backwards compatibility.
FLATBUFFERS_ATTRIBUTE([[deprecated("use size() instead")]])
SizeT Length() const { return size(); }
typedef SizeT size_type;
typedef typename IndirectHelper<T>::return_type return_type;
typedef typename IndirectHelper<T>::mutable_return_type
mutable_return_type;
typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
typedef return_type value_type;
return_type Get(SizeT i) const {
@@ -185,13 +200,15 @@ 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 {
template <typename U>
const U* GetAs(SizeT i) const {
return reinterpret_cast<const U*>(Get(i));
}
@@ -263,7 +280,8 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
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 {
template <typename K>
return_type LookupByKey(K key) const {
void* search_result = std::bsearch(
&key, Data(), size(), IndirectHelper<T>::element_stride, KeyCompare<K>);
@@ -276,7 +294,8 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
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));
}
@@ -293,7 +312,8 @@ template<typename T, typename SizeT = uoffset_t> class Vector {
Vector(const Vector&);
Vector& operator=(const Vector&);
template<typename K> static int KeyCompare(const void *ap, const void *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);
@@ -304,7 +324,8 @@ 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)
@@ -391,7 +412,8 @@ const Vector<Offset<T>> *VectorCast(const Vector<Offset<U>> *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;
}
+19 -9
View File
@@ -17,9 +17,8 @@
#ifndef FLATBUFFERS_VECTOR_DOWNWARD_H_
#define FLATBUFFERS_VECTOR_DOWNWARD_H_
#include <cstdint>
#include <algorithm>
#include <cstdint>
#include "flatbuffers/base.h"
#include "flatbuffers/default_allocator.h"
@@ -33,7 +32,8 @@ 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,
bool own_allocator, size_t buffer_minalign,
@@ -102,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;
}
@@ -143,7 +145,9 @@ 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;
}
@@ -167,7 +171,9 @@ template<typename SizeT = uoffset_t> class vector_downward {
inline SizeT size() const { return size_; }
// The size of the buffer part of the vector that is currently unused.
SizeT unused_buffer_size() const { return static_cast<SizeT>(cur_ - scratch_); }
SizeT unused_buffer_size() const {
return static_cast<SizeT>(cur_ - scratch_);
}
// The size of the scratch part of the vector.
SizeT scratch_size() const { return static_cast<SizeT>(scratch_ - buf_); }
@@ -192,16 +198,20 @@ template<typename SizeT = uoffset_t> class vector_downward {
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); }
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;
}
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;
scratch_ += sizeof(T);
+79 -39
View File
@@ -23,7 +23,8 @@
namespace flatbuffers {
// Helper class to verify the integrity of a FlatBuffer
class Verifier FLATBUFFERS_FINAL_CLASS {
template <bool TrackVerifierBufferSize>
class VerifierTemplate FLATBUFFERS_FINAL_CLASS {
public:
struct Options {
// The maximum nesting of tables and vectors before we call it invalid.
@@ -40,17 +41,18 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
bool assert = false;
};
explicit Verifier(const uint8_t *const buf, const size_t buf_len,
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 Verifier::Options.
Verifier(const uint8_t *const buf, const size_t buf_len,
const uoffset_t max_depth = 64, const uoffset_t max_tables = 1000000,
// Deprecated API, please construct with VerifierTemplate::Options.
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)
: Verifier(buf, buf_len, [&] {
: VerifierTemplate(buf, buf_len, [&] {
Options opts;
opts.max_depth = max_depth;
opts.max_tables = max_tables;
@@ -64,23 +66,23 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
#ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
if (opts_.assert) { FLATBUFFERS_ASSERT(ok); }
#endif
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
if (!ok)
upper_bound_ = 0;
#endif
// clang-format on
if (TrackVerifierBufferSize) {
if (!ok) {
upper_bound_ = 0;
}
}
return ok;
}
// Verify any range within the buffer.
bool Verify(const size_t elem, const size_t elem_len) const {
// clang-format off
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
if (TrackVerifierBufferSize) {
auto upper_bound = elem + elem_len;
if (upper_bound_ < upper_bound)
if (upper_bound_ < upper_bound) {
upper_bound_ = upper_bound;
#endif
// clang-format on
}
}
return Check(elem_len < size_ && elem <= size_ - elem_len);
}
@@ -89,7 +91,8 @@ class Verifier 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));
}
@@ -112,7 +115,8 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
}
// 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);
}
@@ -177,8 +181,8 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
return true;
}
__suppress_ubsan__("unsigned-integer-overflow") bool VerifyTableStart(
const uint8_t *const table) {
FLATBUFFERS_SUPPRESS_UBSAN("unsigned-integer-overflow")
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;
@@ -210,14 +214,14 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
// Call T::Verify, which must be in the generated code for this type.
const auto o = VerifyOffset<uoffset_t>(start);
return Check(o != 0) &&
reinterpret_cast<const T *>(buf_ + start + o)->Verify(*this)
// clang-format off
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
&& GetComputedSize()
#endif
;
// clang-format on
if (!Check(o != 0)) return false;
if (!(reinterpret_cast<const T*>(buf_ + start + o)->Verify(*this))) {
return false;
}
if (TrackVerifierBufferSize) {
if (GetComputedSize() == 0) return false;
}
return true;
}
template <typename T, int&..., typename SizeT>
@@ -232,21 +236,28 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
// If there is a nested buffer, it must be greater than the min size.
if (!Check(buf->size() >= FLATBUFFERS_MIN_BUFFER_SIZE)) return false;
Verifier nested_verifier(buf->data(), buf->size(), opts_);
VerifierTemplate<TrackVerifierBufferSize> nested_verifier(
buf->data(), buf->size(), opts_);
return nested_verifier.VerifyBuffer<T>(identifier);
}
// 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) {
return Verify<SizeT>(0U) &&
Check(ReadScalar<SizeT>(buf_) == size_ - sizeof(SizeT)) &&
// Ensure the prefixed size is within the bounds of the provided
// length.
Check(ReadScalar<SizeT>(buf_) + sizeof(SizeT) <= size_) &&
VerifyBufferFromStart<T>(identifier, sizeof(SizeT));
}
@@ -284,21 +295,27 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
return true;
}
// Returns the message size in bytes
// Returns the message size in bytes.
//
// This should only be called after first calling VerifyBuffer or
// VerifySizePrefixedBuffer.
//
// This method should only be called for VerifierTemplate instances
// where the TrackVerifierBufferSize template parameter is true,
// i.e. for SizeVerifier. For instances where TrackVerifierBufferSize
// is false, this fails at runtime or returns zero.
size_t GetComputedSize() const {
// clang-format off
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
if (TrackVerifierBufferSize) {
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;
#else
// Must turn on FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE for this to work.
}
// Must use SizeVerifier, or (deprecated) turn on
// FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE, for this to work.
(void)upper_bound_;
FLATBUFFERS_ASSERT(false);
return 0;
#endif
// clang-format on
}
std::vector<uint8_t>* GetFlexReuseTracker() { return flex_reuse_tracker_; }
@@ -321,9 +338,32 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
// Specialization for 64-bit offsets.
template <>
inline size_t Verifier::VerifyOffset<uoffset64_t>(const size_t start) const {
template <>
inline size_t VerifierTemplate<false>::VerifyOffset<uoffset64_t>(
const size_t start) const {
return VerifyOffset<uoffset64_t, soffset64_t>(start);
}
template <>
template <>
inline size_t VerifierTemplate<true>::VerifyOffset<uoffset64_t>(
const size_t start) const {
return VerifyOffset<uoffset64_t, soffset64_t>(start);
}
// Instance of VerifierTemplate that supports GetComputedSize().
using SizeVerifier = VerifierTemplate</*TrackVerifierBufferSize = */ true>;
// The FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE build configuration macro is
// deprecated, and should not be defined, since it is easy to misuse in ways
// that result in ODR violations. Rather than using Verifier and defining
// FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE, please use SizeVerifier instead.
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE // Deprecated, see above.
using Verifier = SizeVerifier;
#else
// Instance of VerifierTemplate that is slightly faster, but does not
// support GetComputedSize().
using Verifier = VerifierTemplate</*TrackVerifierBufferSize = */ false>;
#endif
} // namespace flatbuffers
+1 -1
View File
@@ -1,6 +1,6 @@
if(WITH_FLATBUFFERS)
set(HAVE_FLATBUFFERS 1)
set(flatbuffers_VERSION "23.5.9")
set(flatbuffers_VERSION "25.9.23")
ocv_install_3rdparty_licenses(flatbuffers "${OpenCV_SOURCE_DIR}/3rdparty/flatbuffers/LICENSE.txt")
ocv_add_external_target(flatbuffers "${OpenCV_SOURCE_DIR}/3rdparty/flatbuffers/include" "" "HAVE_FLATBUFFERS=1")
set(CUSTOM_STATUS_flatbuffers " Flatbuffers:" "builtin/3rdparty (${flatbuffers_VERSION})")
+3 -3
View File
@@ -8,9 +8,9 @@
// Ensure the included flatbuffers.h is the same version as when this file was
// generated, otherwise it may not be compatible.
static_assert(FLATBUFFERS_VERSION_MAJOR == 23 &&
FLATBUFFERS_VERSION_MINOR == 5 &&
FLATBUFFERS_VERSION_REVISION == 9,
static_assert(FLATBUFFERS_VERSION_MAJOR == 25 &&
FLATBUFFERS_VERSION_MINOR == 9 &&
FLATBUFFERS_VERSION_REVISION == 23,
"Non-compatible flatbuffers version included");
namespace opencv_tflite {