17#ifndef FLATBUFFERS_BUFFER_H_
18#define FLATBUFFERS_BUFFER_H_
28template<
typename T =
void>
struct Offset {
30 typedef uoffset_t offset_type;
34 Offset(
const offset_type _o) : o(_o) {}
35 Offset<> Union()
const {
return o; }
36 bool IsNull()
const {
return !o; }
40template<
typename T =
void>
struct Offset64 {
42 typedef uoffset64_t offset_type;
47 Offset64<> Union()
const {
return o; }
48 bool IsNull()
const {
return !o; }
52static_assert(
sizeof(Offset<>) == 4,
"Offset has wrong size");
53static_assert(
sizeof(Offset64<>) == 8,
"Offset64 has wrong size");
55inline void EndianCheck() {
59 FLATBUFFERS_LITTLEENDIAN);
63template<
typename T> FLATBUFFERS_CONSTEXPR
size_t AlignOf() {
69 return __alignof__(T);
79static inline bool StringLessThan(
const char *a_data, uoffset_t a_size,
80 const char *b_data, uoffset_t b_size) {
81 const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
82 return cmp == 0 ? a_size < b_size : cmp < 0;
93template<
typename T>
struct IndirectHelper {
94 typedef T return_type;
95 typedef T mutable_return_type;
96 static const size_t element_stride =
sizeof(T);
98 static return_type Read(
const uint8_t *
p,
const size_t i) {
99 return EndianScalar((
reinterpret_cast<const T *
>(
p))[i]);
101 static mutable_return_type Read(uint8_t *
p,
const size_t i) {
102 return reinterpret_cast<mutable_return_type
>(
103 Read(
const_cast<const uint8_t *
>(
p), i));
108template<
typename T,
template<
typename>
class OffsetT>
109struct IndirectHelper<OffsetT<T>> {
110 typedef const T *return_type;
111 typedef T *mutable_return_type;
112 typedef typename OffsetT<T>::offset_type offset_type;
113 static const offset_type element_stride =
sizeof(offset_type);
115 static return_type Read(
const uint8_t *
const p,
const offset_type i) {
118 const uint8_t *
const offset_location =
p + i * element_stride;
122 return reinterpret_cast<return_type
>(
123 offset_location + ReadScalar<offset_type>(offset_location));
125 static mutable_return_type Read(uint8_t *
const p,
const offset_type i) {
128 uint8_t *
const offset_location =
p + i * element_stride;
132 return reinterpret_cast<mutable_return_type
>(
133 offset_location + ReadScalar<offset_type>(offset_location));
138template<
typename T>
struct IndirectHelper<const T *> {
139 typedef const T *return_type;
140 typedef T *mutable_return_type;
141 static const size_t element_stride =
sizeof(T);
143 static return_type Read(
const uint8_t *
const p,
const size_t i) {
145 return reinterpret_cast<return_type
>(
p + i * element_stride);
147 static mutable_return_type Read(uint8_t *
const p,
const size_t i) {
149 return reinterpret_cast<mutable_return_type
>(
p + i * element_stride);
160inline const char *GetBufferIdentifier(
const void *buf,
161 bool size_prefixed =
false) {
162 return reinterpret_cast<const char *
>(
buf) +
163 ((size_prefixed) ? 2 *
sizeof(uoffset_t) : sizeof(uoffset_t));
167inline bool BufferHasIdentifier(
const void *buf,
const char *identifier,
168 bool size_prefixed =
false) {
169 return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
170 flatbuffers::kFileIdentifierLength) == 0;
175template<
typename T> T *GetMutableRoot(
void *buf) {
176 if (!buf)
return nullptr;
178 return reinterpret_cast<T *
>(
179 reinterpret_cast<uint8_t *
>(
buf) +
180 EndianScalar(*
reinterpret_cast<uoffset_t *
>(buf)));
183template<
typename T,
typename SizeT = uoffset_t>
184T *GetMutableSizePrefixedRoot(
void *buf) {
185 return GetMutableRoot<T>(
reinterpret_cast<uint8_t *
>(buf) +
sizeof(SizeT));
188template<
typename T>
const T *
GetRoot(
const void *buf) {
189 return GetMutableRoot<T>(
const_cast<void *
>(buf));
192template<
typename T,
typename SizeT = uoffset_t>
193const T *GetSizePrefixedRoot(
const void *buf) {
194 return GetRoot<T>(
reinterpret_cast<const uint8_t *
>(buf) +
sizeof(SizeT));
int Offset(const Dims< 4 > &dims, int i0, int i1, int i2, int i3)
#define FLATBUFFERS_ASSERT
__global uchar * offset(const Image *img, int x, int y)
Reference GetRoot(const uint8_t *buffer, size_t size)