forked from BigfootDev/flatbuffers
* First working hack of adding 64-bit. Don't judge :) * Made vector_downward work on 64 bit types * vector_downward uses size_t, added offset64 to reflection * cleaned up adding offset64 in parser * Add C++ testing skeleton for 64-bit * working test for CreateVector64 * working >2 GiB buffers * support for large strings * simplified CreateString<> to just provide the offset type * generalize CreateVector template * update test_64.afb due to upstream format change * Added Vector64 type, which is just an alias for vector ATM * Switch to Offset64 for Vector64 * Update for reflection bfbs output change * Starting to add support for vector64 type in C++ * made a generic CreateVector that can handle different offsets and vector types * Support for 32-vector with 64-addressing * Vector64 basic builder + tests working * basic support for json vector64 support * renamed fields in test_64bit.fbs to better reflect their use * working C++ vector64 builder * Apply --annotate-sparse-vector to 64-bit tests * Enable Vector64 for --annotate-sparse-vectors * Merged from upstream * Add `near_string` field for testing 32-bit offsets alongside * keep track of where the 32-bit and 64-bit regions are for flatbufferbuilder * move template<> outside class body for GCC * update run.sh to build and run tests * basic assertion for adding 64-bit offset at the wrong time * started to separate `FlatBufferBuilder` into two classes, 1 64-bit aware, the other not * add test for nested flatbuffer vector64, fix bug in alignment of big vectors * fixed CreateDirect method by iterating by Offset64 first * internal refactoring of flatbufferbuilder * block not supported languages in the parser from using 64-bit * evolution tests for adding a vector64 field * conformity tests for adding/removing offset64 attributes * ensure test is for a big buffer * add parser error tests for `offset64` and `vector64` attributes * add missing static that GCC only complains about * remove stdint-uintn.h header that gets automatically added * move 64-bit CalculateOffset internal * fixed return size of EndVector * various fixes on windows * add SizeT to vector_downward * minimze range of size changes in vector and builder * reworked how tracking if 64-offsets are added * Add ReturnT to EndVector * small cleanups * remove need for second Array definition * combine IndirectHelpers into one definition * started support for vector of struct * Support for 32/64-vectors of structs + Offset64 * small cleanups * add verification for vector64 * add sized prefix for 64-bit buffers * add fuzzer for 64-bit * add example of adding many vectors using a wrapper table * run the new -bfbs-gen-embed logic on the 64-bit tests * remove run.sh and fix cmakelist issue * fixed bazel rules * fixed some PR comments * add 64-bit tests to cmakelist
257 lines
9.0 KiB
C++
257 lines
9.0 KiB
C++
/*
|
|
* Copyright 2021 Google Inc. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#ifndef FLATBUFFERS_ARRAY_H_
|
|
#define FLATBUFFERS_ARRAY_H_
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
|
|
#include "flatbuffers/base.h"
|
|
#include "flatbuffers/stl_emulation.h"
|
|
#include "flatbuffers/vector.h"
|
|
|
|
namespace flatbuffers {
|
|
|
|
// This is used as a helper type for accessing arrays.
|
|
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 VectorConstIterator<T, return_type, uoffset_t> const_iterator;
|
|
typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
|
|
|
|
// If T is a LE-scalar or a struct (!scalar_tag::value).
|
|
static FLATBUFFERS_CONSTEXPR bool is_span_observable =
|
|
(scalar_tag::value && (FLATBUFFERS_LITTLEENDIAN || sizeof(T) == 1)) ||
|
|
!scalar_tag::value;
|
|
|
|
FLATBUFFERS_CONSTEXPR uint16_t size() const { return length; }
|
|
|
|
return_type Get(uoffset_t i) const {
|
|
FLATBUFFERS_ASSERT(i < size());
|
|
return IndirectHelper<IndirectHelperType>::Read(Data(), i);
|
|
}
|
|
|
|
return_type operator[](uoffset_t i) const { return Get(i); }
|
|
|
|
// 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 {
|
|
return static_cast<E>(Get(i));
|
|
}
|
|
|
|
const_iterator begin() const { return const_iterator(Data(), 0); }
|
|
const_iterator end() const { return const_iterator(Data(), size()); }
|
|
|
|
const_reverse_iterator rbegin() const {
|
|
return const_reverse_iterator(end());
|
|
}
|
|
const_reverse_iterator rend() const {
|
|
return const_reverse_iterator(begin());
|
|
}
|
|
|
|
const_iterator cbegin() const { return begin(); }
|
|
const_iterator cend() const { return end(); }
|
|
|
|
const_reverse_iterator crbegin() const { return rbegin(); }
|
|
const_reverse_iterator crend() const { return rend(); }
|
|
|
|
// Get a mutable pointer to elements inside this array.
|
|
// This method used to mutate arrays of structs followed by a @p Mutate
|
|
// 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
|
|
GetMutablePointer(uoffset_t i) const {
|
|
FLATBUFFERS_ASSERT(i < size());
|
|
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); }
|
|
|
|
// The raw data in little endian format. Use with care.
|
|
const uint8_t *Data() const { 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()); }
|
|
|
|
// 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 p2 = Data();
|
|
FLATBUFFERS_ASSERT(!(p1 >= p2 && p1 < (p2 + length)) &&
|
|
!(p2 >= p1 && p2 < (p1 + length)));
|
|
(void)p1;
|
|
(void)p2;
|
|
CopyFromSpanImpl(flatbuffers::bool_constant<is_span_observable>(), src);
|
|
}
|
|
|
|
protected:
|
|
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) {
|
|
*(GetMutablePointer(i)) = val;
|
|
}
|
|
|
|
void CopyFromSpanImpl(flatbuffers::true_type,
|
|
flatbuffers::span<const T, length> src) {
|
|
// Use std::memcpy() instead of std::copy() to avoid performance degradation
|
|
// due to aliasing if T is char or unsigned char.
|
|
// The size is known at compile time, so memcpy would be inlined.
|
|
std::memcpy(data(), src.data(), length * sizeof(T));
|
|
}
|
|
|
|
// 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]); }
|
|
}
|
|
|
|
// This class is only used to access pre-existing data. Don't ever
|
|
// try to construct these manually.
|
|
// 'constexpr' allows us to use 'size()' at compile time.
|
|
// @note Must not use 'FLATBUFFERS_CONSTEXPR' here, as const is not allowed on
|
|
// a constructor.
|
|
#if defined(__cpp_constexpr)
|
|
constexpr Array();
|
|
#else
|
|
Array();
|
|
#endif
|
|
|
|
uint8_t data_[length * sizeof(T)];
|
|
|
|
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 &);
|
|
};
|
|
|
|
// 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>
|
|
class Array<OffsetT<T>, length> {
|
|
static_assert(flatbuffers::is_same<T, void>::value, "unexpected type T");
|
|
|
|
public:
|
|
typedef const void *return_type;
|
|
typedef uint16_t size_type;
|
|
|
|
const uint8_t *Data() const { return data_; }
|
|
|
|
// Make idl_gen_text.cpp::PrintContainer happy.
|
|
return_type operator[](uoffset_t) const {
|
|
FLATBUFFERS_ASSERT(false);
|
|
return nullptr;
|
|
}
|
|
|
|
private:
|
|
// This class is only used to access pre-existing data.
|
|
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)
|
|
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<U, N>(arr.data(), N);
|
|
}
|
|
|
|
template<class U, uint16_t N>
|
|
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const U, N> make_span(
|
|
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>
|
|
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<uint8_t, sizeof(U) * N>
|
|
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>
|
|
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const uint8_t, sizeof(U) * N>
|
|
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);
|
|
}
|
|
|
|
// 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>
|
|
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]) {
|
|
static_assert(sizeof(E) == sizeof(T), "invalid enum type E");
|
|
return *reinterpret_cast<Array<E, length> *>(arr);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
} // namespace flatbuffers
|
|
|
|
#endif // FLATBUFFERS_ARRAY_H_
|