Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:30:24

0001 /*
0002  * Copyright 2021 Google Inc. All rights reserved.
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *     http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
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 // This is used as a helper type for accessing arrays.
0030 template<typename T, uint16_t length> class Array {
0031   // Array<T> can carry only POD data types (scalars or structs).
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   // If T is a LE-scalar or a struct (!scalar_tag::value).
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   // If this is a Vector of enums, T will be its storage type, not the enum
0059   // type. This function makes it convenient to retrieve value with enum
0060   // type E.
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   // Get a mutable pointer to elements inside this array.
0082   // This method used to mutate arrays of structs followed by a @p Mutate
0083   // operation. For primitive types use @p Mutate directly.
0084   // @warning Assignments and reads to/from the dereferenced pointer are not
0085   //  automatically converted to the correct endianness.
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   // Change elements if you have a non-const pointer to this object.
0093   void Mutate(uoffset_t i, const T &val) { MutateImpl(scalar_tag(), i, val); }
0094 
0095   // The raw data in little endian format. Use with care.
0096   const uint8_t *Data() const { return data_; }
0097 
0098   uint8_t *Data() { return data_; }
0099 
0100   // Similarly, but typed, much like std::vector::data
0101   const T *data() const { return reinterpret_cast<const T *>(Data()); }
0102   T *data() { return reinterpret_cast<T *>(Data()); }
0103 
0104   // Copy data from a span with endian conversion.
0105   // If this Array and the span overlap, the behavior is undefined.
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     // Use std::memcpy() instead of std::copy() to avoid performance degradation
0129     // due to aliasing if T is char or unsigned char.
0130     // The size is known at compile time, so memcpy would be inlined.
0131     std::memcpy(data(), src.data(), length * sizeof(T));
0132   }
0133 
0134   // Copy data from flatbuffers::span with endian conversion.
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   // This class is only used to access pre-existing data. Don't ever
0141   // try to construct these manually.
0142   // 'constexpr' allows us to use 'size()' at compile time.
0143   // @note Must not use 'FLATBUFFERS_CONSTEXPR' here, as const is not allowed on
0144   //  a constructor.
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   // This class is a pointer. Copying will therefore create an invalid object.
0155   // Private and unimplemented copy constructor.
0156   Array(const Array &);
0157   Array &operator=(const Array &);
0158 };
0159 
0160 // Specialization for Array[struct] with access using Offset<void> pointer.
0161 // This specialization used by idl_gen_text.cpp.
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   // Make idl_gen_text.cpp::PrintContainer happy.
0173   return_type operator[](uoffset_t) const {
0174     FLATBUFFERS_ASSERT(false);
0175     return nullptr;
0176   }
0177 
0178  private:
0179   // This class is only used to access pre-existing data.
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 // Cast a raw T[length] to a raw flatbuffers::Array<T, length>
0222 // without endian conversion. Use with care.
0223 // TODO: move these Cast-methods to `internal` namespace.
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 }  // namespace flatbuffers
0255 
0256 #endif  // FLATBUFFERS_ARRAY_H_