/* * 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 "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 class Array { // Array can carry only POD data types (scalars or structs). typedef typename flatbuffers::bool_constant::value> scalar_tag; typedef typename flatbuffers::conditional::type IndirectHelperType; public: typedef uint16_t size_type; typedef typename IndirectHelper::return_type return_type; typedef VectorIterator const_iterator; typedef VectorReverseIterator 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::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 E GetEnum(uoffset_t i) const { return static_cast(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::type GetMutablePointer(uoffset_t i) const { FLATBUFFERS_ASSERT(i < size()); return const_cast(&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(Data()); } T *data() { return reinterpret_cast(Data()); } // Copy data from a span with endian conversion. // If this Array and the span overlap, the behavior is undefined. void CopyFromSpan(flatbuffers::span src) { const auto p1 = reinterpret_cast(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(), 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 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 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 pointer. // This specialization used by idl_gen_text.cpp. template class Array, length> { static_assert(flatbuffers::is_same::value, "unexpected type T"); public: typedef const void *return_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 FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_span(Array &arr) FLATBUFFERS_NOEXCEPT { static_assert( Array::is_span_observable, "wrong type U, only plain struct, LE-scalar, or byte types are allowed"); return span(arr.data(), N); } template FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_span( const Array &arr) FLATBUFFERS_NOEXCEPT { static_assert( Array::is_span_observable, "wrong type U, only plain struct, LE-scalar, or byte types are allowed"); return span(arr.data(), N); } template FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_bytes_span(Array &arr) FLATBUFFERS_NOEXCEPT { static_assert(Array::is_span_observable, "internal error, Array might hold only scalars or structs"); return span(arr.Data(), sizeof(U) * N); } template FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_bytes_span(const Array &arr) FLATBUFFERS_NOEXCEPT { static_assert(Array::is_span_observable, "internal error, Array might hold only scalars or structs"); return span(arr.Data(), sizeof(U) * N); } // Cast a raw T[length] to a raw flatbuffers::Array // without endian conversion. Use with care. // TODO: move these Cast-methods to `internal` namespace. template Array &CastToArray(T (&arr)[length]) { return *reinterpret_cast *>(arr); } template const Array &CastToArray(const T (&arr)[length]) { return *reinterpret_cast *>(arr); } template Array &CastToArrayOfEnum(T (&arr)[length]) { static_assert(sizeof(E) == sizeof(T), "invalid enum type E"); return *reinterpret_cast *>(arr); } template const Array &CastToArrayOfEnum(const T (&arr)[length]) { static_assert(sizeof(E) == sizeof(T), "invalid enum type E"); return *reinterpret_cast *>(arr); } } // namespace flatbuffers #endif // FLATBUFFERS_ARRAY_H_