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_BUFFER_H_
0018 #define FLATBUFFERS_BUFFER_H_
0019
0020 #include <algorithm>
0021
0022 #include "flatbuffers/base.h"
0023
0024 namespace flatbuffers {
0025
0026
0027
0028 template<typename T = void> struct Offset {
0029
0030 typedef uoffset_t offset_type;
0031
0032 offset_type o;
0033 Offset() : o(0) {}
0034 Offset(const offset_type _o) : o(_o) {}
0035 Offset<> Union() const { return o; }
0036 bool IsNull() const { return !o; }
0037 };
0038
0039
0040 template<typename T = void> struct Offset64 {
0041
0042 typedef uoffset64_t offset_type;
0043
0044 offset_type o;
0045 Offset64() : o(0) {}
0046 Offset64(const offset_type offset) : o(offset) {}
0047 Offset64<> Union() const { return o; }
0048 bool IsNull() const { return !o; }
0049 };
0050
0051
0052 static_assert(sizeof(Offset<>) == 4, "Offset has wrong size");
0053 static_assert(sizeof(Offset64<>) == 8, "Offset64 has wrong size");
0054
0055 inline void EndianCheck() {
0056 int endiantest = 1;
0057
0058 FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
0059 FLATBUFFERS_LITTLEENDIAN);
0060 (void)endiantest;
0061 }
0062
0063 template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() {
0064
0065 #ifdef _MSC_VER
0066 return __alignof(T);
0067 #else
0068 #ifndef alignof
0069 return __alignof__(T);
0070 #else
0071 return alignof(T);
0072 #endif
0073 #endif
0074
0075 }
0076
0077
0078
0079 static inline bool StringLessThan(const char *a_data, uoffset_t a_size,
0080 const char *b_data, uoffset_t b_size) {
0081 const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
0082 return cmp == 0 ? a_size < b_size : cmp < 0;
0083 }
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093 template<typename T> struct IndirectHelper {
0094 typedef T return_type;
0095 typedef T mutable_return_type;
0096 static const size_t element_stride = sizeof(T);
0097
0098 static return_type Read(const uint8_t *p, const size_t i) {
0099 return EndianScalar((reinterpret_cast<const T *>(p))[i]);
0100 }
0101 static mutable_return_type Read(uint8_t *p, const size_t i) {
0102 return reinterpret_cast<mutable_return_type>(
0103 Read(const_cast<const uint8_t *>(p), i));
0104 }
0105 };
0106
0107
0108 template<typename T, template<typename> class OffsetT>
0109 struct IndirectHelper<OffsetT<T>> {
0110 typedef const T *return_type;
0111 typedef T *mutable_return_type;
0112 typedef typename OffsetT<T>::offset_type offset_type;
0113 static const offset_type element_stride = sizeof(offset_type);
0114
0115 static return_type Read(const uint8_t *const p, const offset_type i) {
0116
0117
0118 const uint8_t *const offset_location = p + i * element_stride;
0119
0120
0121
0122 return reinterpret_cast<return_type>(
0123 offset_location + ReadScalar<offset_type>(offset_location));
0124 }
0125 static mutable_return_type Read(uint8_t *const p, const offset_type i) {
0126
0127
0128 uint8_t *const offset_location = p + i * element_stride;
0129
0130
0131
0132 return reinterpret_cast<mutable_return_type>(
0133 offset_location + ReadScalar<offset_type>(offset_location));
0134 }
0135 };
0136
0137
0138 template<typename T> struct IndirectHelper<const T *> {
0139 typedef const T *return_type;
0140 typedef T *mutable_return_type;
0141 static const size_t element_stride = sizeof(T);
0142
0143 static return_type Read(const uint8_t *const p, const size_t i) {
0144
0145 return reinterpret_cast<return_type>(p + i * element_stride);
0146 }
0147 static mutable_return_type Read(uint8_t *const p, const size_t i) {
0148
0149 return reinterpret_cast<mutable_return_type>(p + i * element_stride);
0150 }
0151 };
0152
0153
0154
0155
0156
0157
0158
0159
0160 inline const char *GetBufferIdentifier(const void *buf,
0161 bool size_prefixed = false) {
0162 return reinterpret_cast<const char *>(buf) +
0163 ((size_prefixed) ? 2 * sizeof(uoffset_t) : sizeof(uoffset_t));
0164 }
0165
0166
0167 inline bool BufferHasIdentifier(const void *buf, const char *identifier,
0168 bool size_prefixed = false) {
0169 return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
0170 flatbuffers::kFileIdentifierLength) == 0;
0171 }
0172
0173
0174
0175 template<typename T> T *GetMutableRoot(void *buf) {
0176 if (!buf) return nullptr;
0177 EndianCheck();
0178 return reinterpret_cast<T *>(
0179 reinterpret_cast<uint8_t *>(buf) +
0180 EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
0181 }
0182
0183 template<typename T, typename SizeT = uoffset_t>
0184 T *GetMutableSizePrefixedRoot(void *buf) {
0185 return GetMutableRoot<T>(reinterpret_cast<uint8_t *>(buf) + sizeof(SizeT));
0186 }
0187
0188 template<typename T> const T *GetRoot(const void *buf) {
0189 return GetMutableRoot<T>(const_cast<void *>(buf));
0190 }
0191
0192 template<typename T, typename SizeT = uoffset_t>
0193 const T *GetSizePrefixedRoot(const void *buf) {
0194 return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(SizeT));
0195 }
0196
0197 }
0198
0199 #endif