/src/MapServer/src/flatgeobuf/include/flatbuffers/buffer.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2021 Google Inc. All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #ifndef FLATBUFFERS_BUFFER_H_ |
18 | | #define FLATBUFFERS_BUFFER_H_ |
19 | | |
20 | | #include "flatbuffers/base.h" |
21 | | |
22 | | namespace mapserver { |
23 | | namespace flatbuffers { |
24 | | |
25 | | // Wrapper for uoffset_t to allow safe template specialization. |
26 | | // Value is allowed to be 0 to indicate a null object (see e.g. AddOffset). |
27 | | template<typename T> struct Offset { |
28 | | uoffset_t o; |
29 | | Offset() : o(0) {} |
30 | | Offset(uoffset_t _o) : o(_o) {} |
31 | | Offset<void> Union() const { return Offset<void>(o); } |
32 | 0 | bool IsNull() const { return !o; }Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::flatbuffers::String>::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::flatbuffers::Vector<double> >::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Column> > >::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Crs>::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::flatbuffers::Vector<unsigned int> >::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::flatbuffers::Vector<unsigned long> >::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Geometry> > >::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Geometry>::IsNull() const Unexecuted instantiation: mapserver::flatbuffers::Offset<mapserver::flatbuffers::Vector<unsigned char> >::IsNull() const |
33 | | }; |
34 | | |
35 | 0 | inline void EndianCheck() { |
36 | 0 | int endiantest = 1; |
37 | | // If this fails, see FLATBUFFERS_LITTLEENDIAN above. |
38 | 0 | FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) == |
39 | 0 | FLATBUFFERS_LITTLEENDIAN); |
40 | 0 | (void)endiantest; |
41 | 0 | } |
42 | | |
43 | 0 | template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() { |
44 | 0 | // clang-format off |
45 | 0 | #ifdef _MSC_VER |
46 | 0 | return __alignof(T); |
47 | 0 | #else |
48 | 0 | #ifndef alignof |
49 | 0 | return __alignof__(T); |
50 | 0 | #else |
51 | 0 | return alignof(T); |
52 | 0 | #endif |
53 | 0 | #endif |
54 | 0 | // clang-format on |
55 | 0 | } |
56 | | |
57 | | // Lexicographically compare two strings (possibly containing nulls), and |
58 | | // return true if the first is less than the second. |
59 | | static inline bool StringLessThan(const char *a_data, uoffset_t a_size, |
60 | 0 | const char *b_data, uoffset_t b_size) { |
61 | 0 | const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size)); |
62 | 0 | return cmp == 0 ? a_size < b_size : cmp < 0; |
63 | 0 | } Unexecuted instantiation: flatgeobuf_c.cpp:mapserver::flatbuffers::StringLessThan(char const*, unsigned int, char const*, unsigned int) Unexecuted instantiation: geometryreader.cpp:mapserver::flatbuffers::StringLessThan(char const*, unsigned int, char const*, unsigned int) Unexecuted instantiation: packedrtree.cpp:mapserver::flatbuffers::StringLessThan(char const*, unsigned int, char const*, unsigned int) |
64 | | |
65 | | // When we read serialized data from memory, in the case of most scalars, |
66 | | // we want to just read T, but in the case of Offset, we want to actually |
67 | | // perform the indirection and return a pointer. |
68 | | // The template specialization below does just that. |
69 | | // It is wrapped in a struct since function templates can't overload on the |
70 | | // return type like this. |
71 | | // The typedef is for the convenience of callers of this function |
72 | | // (avoiding the need for a trailing return decltype) |
73 | | template<typename T> struct IndirectHelper { |
74 | | typedef T return_type; |
75 | | typedef T mutable_return_type; |
76 | | static const size_t element_stride = sizeof(T); |
77 | 0 | static return_type Read(const uint8_t *p, uoffset_t i) { |
78 | 0 | return EndianScalar((reinterpret_cast<const T *>(p))[i]); |
79 | 0 | } Unexecuted instantiation: mapserver::flatbuffers::IndirectHelper<double>::Read(unsigned char const*, unsigned int) Unexecuted instantiation: mapserver::flatbuffers::IndirectHelper<unsigned int>::Read(unsigned char const*, unsigned int) |
80 | | }; |
81 | | template<typename T> struct IndirectHelper<Offset<T>> { |
82 | | typedef const T *return_type; |
83 | | typedef T *mutable_return_type; |
84 | | static const size_t element_stride = sizeof(uoffset_t); |
85 | 0 | static return_type Read(const uint8_t *p, uoffset_t i) { |
86 | 0 | p += i * sizeof(uoffset_t); |
87 | 0 | return reinterpret_cast<return_type>(p + ReadScalar<uoffset_t>(p)); |
88 | 0 | } Unexecuted instantiation: mapserver::flatbuffers::IndirectHelper<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Column> >::Read(unsigned char const*, unsigned int) Unexecuted instantiation: mapserver::flatbuffers::IndirectHelper<mapserver::flatbuffers::Offset<mapserver::flatbuffers::String> >::Read(unsigned char const*, unsigned int) Unexecuted instantiation: mapserver::flatbuffers::IndirectHelper<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Geometry> >::Read(unsigned char const*, unsigned int) |
89 | | }; |
90 | | template<typename T> struct IndirectHelper<const T *> { |
91 | | typedef const T *return_type; |
92 | | typedef T *mutable_return_type; |
93 | | static const size_t element_stride = sizeof(T); |
94 | | static return_type Read(const uint8_t *p, uoffset_t i) { |
95 | | return reinterpret_cast<const T *>(p + i * sizeof(T)); |
96 | | } |
97 | | }; |
98 | | |
99 | | /// @brief Get a pointer to the the file_identifier section of the buffer. |
100 | | /// @return Returns a const char pointer to the start of the file_identifier |
101 | | /// characters in the buffer. The returned char * has length |
102 | | /// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'. |
103 | | /// This function is UNDEFINED for FlatBuffers whose schema does not include |
104 | | /// a file_identifier (likely points at padding or the start of a the root |
105 | | /// vtable). |
106 | | inline const char *GetBufferIdentifier(const void *buf, |
107 | 0 | bool size_prefixed = false) { |
108 | 0 | return reinterpret_cast<const char *>(buf) + |
109 | 0 | ((size_prefixed) ? 2 * sizeof(uoffset_t) : sizeof(uoffset_t)); |
110 | 0 | } |
111 | | |
112 | | // Helper to see if the identifier in a buffer has the expected value. |
113 | | inline bool BufferHasIdentifier(const void *buf, const char *identifier, |
114 | 0 | bool size_prefixed = false) { |
115 | 0 | return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier, |
116 | 0 | flatbuffers::kFileIdentifierLength) == 0; |
117 | 0 | } |
118 | | |
119 | | /// @cond FLATBUFFERS_INTERNAL |
120 | | // Helpers to get a typed pointer to the root object contained in the buffer. |
121 | 0 | template<typename T> T *GetMutableRoot(void *buf) { |
122 | 0 | EndianCheck(); |
123 | 0 | return reinterpret_cast<T *>( |
124 | 0 | reinterpret_cast<uint8_t *>(buf) + |
125 | 0 | EndianScalar(*reinterpret_cast<uoffset_t *>(buf))); |
126 | 0 | } Unexecuted instantiation: mapserver::FlatGeobuf::Feature* mapserver::flatbuffers::GetMutableRoot<mapserver::FlatGeobuf::Feature>(void*) Unexecuted instantiation: mapserver::FlatGeobuf::Header* mapserver::flatbuffers::GetMutableRoot<mapserver::FlatGeobuf::Header>(void*) |
127 | | |
128 | | template<typename T> T *GetMutableSizePrefixedRoot(void *buf) { |
129 | | return GetMutableRoot<T>(reinterpret_cast<uint8_t *>(buf) + |
130 | | sizeof(uoffset_t)); |
131 | | } |
132 | | |
133 | 0 | template<typename T> const T *GetRoot(const void *buf) { |
134 | 0 | return GetMutableRoot<T>(const_cast<void *>(buf)); |
135 | 0 | } Unexecuted instantiation: mapserver::FlatGeobuf::Feature const* mapserver::flatbuffers::GetRoot<mapserver::FlatGeobuf::Feature>(void const*) Unexecuted instantiation: mapserver::FlatGeobuf::Header const* mapserver::flatbuffers::GetRoot<mapserver::FlatGeobuf::Header>(void const*) |
136 | | |
137 | 0 | template<typename T> const T *GetSizePrefixedRoot(const void *buf) { |
138 | 0 | return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(uoffset_t)); |
139 | 0 | } Unexecuted instantiation: mapserver::FlatGeobuf::Header const* mapserver::flatbuffers::GetSizePrefixedRoot<mapserver::FlatGeobuf::Header>(void const*) Unexecuted instantiation: mapserver::FlatGeobuf::Feature const* mapserver::flatbuffers::GetSizePrefixedRoot<mapserver::FlatGeobuf::Feature>(void const*) |
140 | | |
141 | | } // namespace flatbuffers |
142 | | } // namespace mapserver |
143 | | |
144 | | #endif // FLATBUFFERS_BUFFER_H_ |