File indexing completed on 2025-08-27 09:30:24
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef FLATBUFFERS_ARRAY_H_
0018 #define FLATBUFFERS_ARRAY_H_
0019
0020 #include <cstdint>
0021 #include <memory>
0022
0023 #include "flatbuffers/base.h"
0024 #include "flatbuffers/stl_emulation.h"
0025 #include "flatbuffers/vector.h"
0026
0027 namespace flatbuffers {
0028
0029
0030 template<typename T, uint16_t length> class Array {
0031
0032 typedef typename flatbuffers::bool_constant<flatbuffers::is_scalar<T>::value>
0033 scalar_tag;
0034 typedef
0035 typename flatbuffers::conditional<scalar_tag::value, T, const T *>::type
0036 IndirectHelperType;
0037
0038 public:
0039 typedef uint16_t size_type;
0040 typedef typename IndirectHelper<IndirectHelperType>::return_type return_type;
0041 typedef VectorConstIterator<T, return_type, uoffset_t> const_iterator;
0042 typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
0043
0044
0045 static FLATBUFFERS_CONSTEXPR bool is_span_observable =
0046 (scalar_tag::value && (FLATBUFFERS_LITTLEENDIAN || sizeof(T) == 1)) ||
0047 !scalar_tag::value;
0048
0049 FLATBUFFERS_CONSTEXPR uint16_t size() const { return length; }
0050
0051 return_type Get(uoffset_t i) const {
0052 FLATBUFFERS_ASSERT(i < size());
0053 return IndirectHelper<IndirectHelperType>::Read(Data(), i);
0054 }
0055
0056 return_type operator[](uoffset_t i) const { return Get(i); }
0057
0058
0059
0060
0061 template<typename E> E GetEnum(uoffset_t i) const {
0062 return static_cast<E>(Get(i));
0063 }
0064
0065 const_iterator begin() const { return const_iterator(Data(), 0); }
0066 const_iterator end() const { return const_iterator(Data(), size()); }
0067
0068 const_reverse_iterator rbegin() const {
0069 return const_reverse_iterator(end());
0070 }
0071 const_reverse_iterator rend() const {
0072 return const_reverse_iterator(begin());
0073 }
0074
0075 const_iterator cbegin() const { return begin(); }
0076 const_iterator cend() const { return end(); }
0077
0078 const_reverse_iterator crbegin() const { return rbegin(); }
0079 const_reverse_iterator crend() const { return rend(); }
0080
0081
0082
0083
0084
0085
0086 typename flatbuffers::conditional<scalar_tag::value, void, T *>::type
0087 GetMutablePointer(uoffset_t i) const {
0088 FLATBUFFERS_ASSERT(i < size());
0089 return const_cast<T *>(&data()[i]);
0090 }
0091
0092
0093 void Mutate(uoffset_t i, const T &val) { MutateImpl(scalar_tag(), i, val); }
0094
0095
0096 const uint8_t *Data() const { return data_; }
0097
0098 uint8_t *Data() { return data_; }
0099
0100
0101 const T *data() const { return reinterpret_cast<const T *>(Data()); }
0102 T *data() { return reinterpret_cast<T *>(Data()); }
0103
0104
0105
0106 void CopyFromSpan(flatbuffers::span<const T, length> src) {
0107 const auto p1 = reinterpret_cast<const uint8_t *>(src.data());
0108 const auto p2 = Data();
0109 FLATBUFFERS_ASSERT(!(p1 >= p2 && p1 < (p2 + length)) &&
0110 !(p2 >= p1 && p2 < (p1 + length)));
0111 (void)p1;
0112 (void)p2;
0113 CopyFromSpanImpl(flatbuffers::bool_constant<is_span_observable>(), src);
0114 }
0115
0116 protected:
0117 void MutateImpl(flatbuffers::true_type, uoffset_t i, const T &val) {
0118 FLATBUFFERS_ASSERT(i < size());
0119 WriteScalar(data() + i, val);
0120 }
0121
0122 void MutateImpl(flatbuffers::false_type, uoffset_t i, const T &val) {
0123 *(GetMutablePointer(i)) = val;
0124 }
0125
0126 void CopyFromSpanImpl(flatbuffers::true_type,
0127 flatbuffers::span<const T, length> src) {
0128
0129
0130
0131 std::memcpy(data(), src.data(), length * sizeof(T));
0132 }
0133
0134
0135 void CopyFromSpanImpl(flatbuffers::false_type,
0136 flatbuffers::span<const T, length> src) {
0137 for (size_type k = 0; k < length; k++) { Mutate(k, src[k]); }
0138 }
0139
0140
0141
0142
0143
0144
0145 #if defined(__cpp_constexpr)
0146 constexpr Array();
0147 #else
0148 Array();
0149 #endif
0150
0151 uint8_t data_[length * sizeof(T)];
0152
0153 private:
0154
0155
0156 Array(const Array &);
0157 Array &operator=(const Array &);
0158 };
0159
0160
0161
0162 template<typename T, uint16_t length, template<typename> class OffsetT>
0163 class Array<OffsetT<T>, length> {
0164 static_assert(flatbuffers::is_same<T, void>::value, "unexpected type T");
0165
0166 public:
0167 typedef const void *return_type;
0168 typedef uint16_t size_type;
0169
0170 const uint8_t *Data() const { return data_; }
0171
0172
0173 return_type operator[](uoffset_t) const {
0174 FLATBUFFERS_ASSERT(false);
0175 return nullptr;
0176 }
0177
0178 private:
0179
0180 Array();
0181 Array(const Array &);
0182 Array &operator=(const Array &);
0183
0184 uint8_t data_[1];
0185 };
0186
0187 template<class U, uint16_t N>
0188 FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U, N> make_span(Array<U, N> &arr)
0189 FLATBUFFERS_NOEXCEPT {
0190 static_assert(
0191 Array<U, N>::is_span_observable,
0192 "wrong type U, only plain struct, LE-scalar, or byte types are allowed");
0193 return span<U, N>(arr.data(), N);
0194 }
0195
0196 template<class U, uint16_t N>
0197 FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const U, N> make_span(
0198 const Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
0199 static_assert(
0200 Array<U, N>::is_span_observable,
0201 "wrong type U, only plain struct, LE-scalar, or byte types are allowed");
0202 return span<const U, N>(arr.data(), N);
0203 }
0204
0205 template<class U, uint16_t N>
0206 FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<uint8_t, sizeof(U) * N>
0207 make_bytes_span(Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
0208 static_assert(Array<U, N>::is_span_observable,
0209 "internal error, Array<T> might hold only scalars or structs");
0210 return span<uint8_t, sizeof(U) * N>(arr.Data(), sizeof(U) * N);
0211 }
0212
0213 template<class U, uint16_t N>
0214 FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const uint8_t, sizeof(U) * N>
0215 make_bytes_span(const Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
0216 static_assert(Array<U, N>::is_span_observable,
0217 "internal error, Array<T> might hold only scalars or structs");
0218 return span<const uint8_t, sizeof(U) * N>(arr.Data(), sizeof(U) * N);
0219 }
0220
0221
0222
0223
0224 template<typename T, uint16_t length>
0225 Array<T, length> &CastToArray(T (&arr)[length]) {
0226 return *reinterpret_cast<Array<T, length> *>(arr);
0227 }
0228
0229 template<typename T, uint16_t length>
0230 const Array<T, length> &CastToArray(const T (&arr)[length]) {
0231 return *reinterpret_cast<const Array<T, length> *>(arr);
0232 }
0233
0234 template<typename E, typename T, uint16_t length>
0235 Array<E, length> &CastToArrayOfEnum(T (&arr)[length]) {
0236 static_assert(sizeof(E) == sizeof(T), "invalid enum type E");
0237 return *reinterpret_cast<Array<E, length> *>(arr);
0238 }
0239
0240 template<typename E, typename T, uint16_t length>
0241 const Array<E, length> &CastToArrayOfEnum(const T (&arr)[length]) {
0242 static_assert(sizeof(E) == sizeof(T), "invalid enum type E");
0243 return *reinterpret_cast<const Array<E, length> *>(arr);
0244 }
0245
0246 template<typename T, uint16_t length>
0247 bool operator==(const Array<T, length> &lhs,
0248 const Array<T, length> &rhs) noexcept {
0249 return std::addressof(lhs) == std::addressof(rhs) ||
0250 (lhs.size() == rhs.size() &&
0251 std::memcmp(lhs.Data(), rhs.Data(), rhs.size() * sizeof(T)) == 0);
0252 }
0253
0254 }
0255
0256 #endif