/* * 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_BUFFER_H_ #define FLATBUFFERS_BUFFER_H_ #include <algorithm> #include "flatbuffers/base.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 { … }; // Wrapper for uoffset64_t Offsets. template<typename T = void> struct Offset64 { … }; // Litmus check for ensuring the Offsets are the expected size. static_assert …; static_assert …; inline void EndianCheck() { … } template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() { … } // Lexicographically compare two strings (possibly containing nulls), and // return true if the first is less than the second. static inline bool StringLessThan(const char *a_data, uoffset_t a_size, const char *b_data, uoffset_t b_size) { … } // When we read serialized data from memory, in the case of most scalars, // we want to just read T, but in the case of Offset, we want to actually // perform the indirection and return a pointer. // The template specialization below does just that. // It is wrapped in a struct since function templates can't overload on the // 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 { … }; // For vector of Offsets. IndirectHelper<OffsetT<T>>; // For vector of structs. IndirectHelper<const T *>; /// @brief Get a pointer to the file_identifier section of the buffer. /// @return Returns a const char pointer to the start of the file_identifier /// characters in the buffer. The returned char * has length /// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'. /// This function is UNDEFINED for FlatBuffers whose schema does not include /// a file_identifier (likely points at padding or the start of a the root /// vtable). inline const char *GetBufferIdentifier(const void *buf, bool size_prefixed = false) { … } // Helper to see if the identifier in a buffer has the expected value. inline bool BufferHasIdentifier(const void *buf, const char *identifier, bool size_prefixed = false) { … } /// @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, typename SizeT = uoffset_t> T *GetMutableSizePrefixedRoot(void *buf) { … } template<typename T> const T *GetRoot(const void *buf) { … } template<typename T, typename SizeT = uoffset_t> const T *GetSizePrefixedRoot(const void *buf) { … } } // namespace flatbuffers #endif // FLATBUFFERS_BUFFER_H_