/src/MapServer/src/flatgeobuf/include/flatbuffers/table.h
Line | Count | Source (jump to first uncovered line) |
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_TABLE_H_ |
18 | | #define FLATBUFFERS_TABLE_H_ |
19 | | |
20 | | #include "flatbuffers/base.h" |
21 | | #include "flatbuffers/verifier.h" |
22 | | |
23 | | namespace mapserver { |
24 | | namespace flatbuffers { |
25 | | |
26 | | // "tables" use an offset table (possibly shared) that allows fields to be |
27 | | // omitted and added at will, but uses an extra indirection to read. |
28 | | class Table { |
29 | | public: |
30 | 0 | const uint8_t *GetVTable() const { |
31 | 0 | return data_ - ReadScalar<soffset_t>(data_); |
32 | 0 | } |
33 | | |
34 | | // This gets the field offset for any of the functions below it, or 0 |
35 | | // if the field was not present. |
36 | 0 | voffset_t GetOptionalFieldOffset(voffset_t field) const { |
37 | | // The vtable offset is always at the start. |
38 | 0 | auto vtable = GetVTable(); |
39 | | // The first element is the size of the vtable (fields + type id + itself). |
40 | 0 | auto vtsize = ReadScalar<voffset_t>(vtable); |
41 | | // If the field we're accessing is outside the vtable, we're reading older |
42 | | // data, so it's the same as if the offset was 0 (not present). |
43 | 0 | return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0; |
44 | 0 | } |
45 | | |
46 | 0 | template<typename T> T GetField(voffset_t field, T defaultval) const { |
47 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
48 | 0 | return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval; |
49 | 0 | } Unexecuted instantiation: unsigned char mapserver::flatbuffers::Table::GetField<unsigned char>(unsigned short, unsigned char) const Unexecuted instantiation: unsigned long mapserver::flatbuffers::Table::GetField<unsigned long>(unsigned short, unsigned long) const Unexecuted instantiation: unsigned short mapserver::flatbuffers::Table::GetField<unsigned short>(unsigned short, unsigned short) const Unexecuted instantiation: int mapserver::flatbuffers::Table::GetField<int>(unsigned short, int) const |
50 | | |
51 | 0 | template<typename P> P GetPointer(voffset_t field) { |
52 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
53 | 0 | auto p = data_ + field_offset; |
54 | 0 | return field_offset ? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p)) |
55 | 0 | : nullptr; |
56 | 0 | } Unexecuted instantiation: mapserver::FlatGeobuf::Geometry const* mapserver::flatbuffers::Table::GetPointer<mapserver::FlatGeobuf::Geometry const*>(unsigned short) Unexecuted instantiation: mapserver::flatbuffers::Vector<unsigned char> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<unsigned char> const*>(unsigned short) Unexecuted instantiation: mapserver::flatbuffers::Vector<double> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<double> const*>(unsigned short) Unexecuted instantiation: mapserver::FlatGeobuf::Crs const* mapserver::flatbuffers::Table::GetPointer<mapserver::FlatGeobuf::Crs const*>(unsigned short) Unexecuted instantiation: mapserver::flatbuffers::String const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::String const*>(unsigned short) Unexecuted instantiation: mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Column> > const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Column> > const*>(unsigned short) Unexecuted instantiation: mapserver::flatbuffers::Vector<unsigned int> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<unsigned int> const*>(unsigned short) Unexecuted instantiation: mapserver::flatbuffers::Vector<unsigned long> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<unsigned long> const*>(unsigned short) Unexecuted instantiation: mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Geometry> > const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Geometry> > const*>(unsigned short) |
57 | 0 | template<typename P> P GetPointer(voffset_t field) const { |
58 | 0 | return const_cast<Table *>(this)->GetPointer<P>(field); |
59 | 0 | } Unexecuted instantiation: mapserver::FlatGeobuf::Geometry const* mapserver::flatbuffers::Table::GetPointer<mapserver::FlatGeobuf::Geometry const*>(unsigned short) const Unexecuted instantiation: mapserver::flatbuffers::Vector<unsigned char> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<unsigned char> const*>(unsigned short) const Unexecuted instantiation: mapserver::flatbuffers::Vector<double> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<double> const*>(unsigned short) const Unexecuted instantiation: mapserver::FlatGeobuf::Crs const* mapserver::flatbuffers::Table::GetPointer<mapserver::FlatGeobuf::Crs const*>(unsigned short) const Unexecuted instantiation: mapserver::flatbuffers::String const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::String const*>(unsigned short) const Unexecuted instantiation: mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Column> > const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Column> > const*>(unsigned short) const Unexecuted instantiation: mapserver::flatbuffers::Vector<unsigned int> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<unsigned int> const*>(unsigned short) const Unexecuted instantiation: mapserver::flatbuffers::Vector<unsigned long> const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<unsigned long> const*>(unsigned short) const Unexecuted instantiation: mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Geometry> > const* mapserver::flatbuffers::Table::GetPointer<mapserver::flatbuffers::Vector<mapserver::flatbuffers::Offset<mapserver::FlatGeobuf::Geometry> > const*>(unsigned short) const |
60 | | |
61 | | template<typename P> P GetStruct(voffset_t field) const { |
62 | | auto field_offset = GetOptionalFieldOffset(field); |
63 | | auto p = const_cast<uint8_t *>(data_ + field_offset); |
64 | | return field_offset ? reinterpret_cast<P>(p) : nullptr; |
65 | | } |
66 | | |
67 | | template<typename Raw, typename Face> |
68 | | flatbuffers::Optional<Face> GetOptional(voffset_t field) const { |
69 | | auto field_offset = GetOptionalFieldOffset(field); |
70 | | auto p = data_ + field_offset; |
71 | | return field_offset ? Optional<Face>(static_cast<Face>(ReadScalar<Raw>(p))) |
72 | | : Optional<Face>(); |
73 | | } |
74 | | |
75 | | template<typename T> bool SetField(voffset_t field, T val, T def) { |
76 | | auto field_offset = GetOptionalFieldOffset(field); |
77 | | if (!field_offset) return IsTheSameAs(val, def); |
78 | | WriteScalar(data_ + field_offset, val); |
79 | | return true; |
80 | | } |
81 | | template<typename T> bool SetField(voffset_t field, T val) { |
82 | | auto field_offset = GetOptionalFieldOffset(field); |
83 | | if (!field_offset) return false; |
84 | | WriteScalar(data_ + field_offset, val); |
85 | | return true; |
86 | | } |
87 | | |
88 | 0 | bool SetPointer(voffset_t field, const uint8_t *val) { |
89 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
90 | 0 | if (!field_offset) return false; |
91 | 0 | WriteScalar(data_ + field_offset, |
92 | 0 | static_cast<uoffset_t>(val - (data_ + field_offset))); |
93 | 0 | return true; |
94 | 0 | } |
95 | | |
96 | 0 | uint8_t *GetAddressOf(voffset_t field) { |
97 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
98 | 0 | return field_offset ? data_ + field_offset : nullptr; |
99 | 0 | } |
100 | 0 | const uint8_t *GetAddressOf(voffset_t field) const { |
101 | 0 | return const_cast<Table *>(this)->GetAddressOf(field); |
102 | 0 | } |
103 | | |
104 | 0 | bool CheckField(voffset_t field) const { |
105 | 0 | return GetOptionalFieldOffset(field) != 0; |
106 | 0 | } |
107 | | |
108 | | // Verify the vtable of this table. |
109 | | // Call this once per table, followed by VerifyField once per field. |
110 | 0 | bool VerifyTableStart(Verifier &verifier) const { |
111 | 0 | return verifier.VerifyTableStart(data_); |
112 | 0 | } |
113 | | |
114 | | // Verify a particular field. |
115 | | template<typename T> |
116 | | bool VerifyField(const Verifier &verifier, voffset_t field, |
117 | 0 | size_t align) const { |
118 | 0 | // Calling GetOptionalFieldOffset should be safe now thanks to |
119 | 0 | // VerifyTable(). |
120 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
121 | 0 | // Check the actual field. |
122 | 0 | return !field_offset || verifier.VerifyField<T>(data_, field_offset, align); |
123 | 0 | } Unexecuted instantiation: bool mapserver::flatbuffers::Table::VerifyField<unsigned char>(mapserver::flatbuffers::Verifier const&, unsigned short, unsigned long) const Unexecuted instantiation: bool mapserver::flatbuffers::Table::VerifyField<int>(mapserver::flatbuffers::Verifier const&, unsigned short, unsigned long) const Unexecuted instantiation: bool mapserver::flatbuffers::Table::VerifyField<unsigned long>(mapserver::flatbuffers::Verifier const&, unsigned short, unsigned long) const Unexecuted instantiation: bool mapserver::flatbuffers::Table::VerifyField<unsigned short>(mapserver::flatbuffers::Verifier const&, unsigned short, unsigned long) const |
124 | | |
125 | | // VerifyField for required fields. |
126 | | template<typename T> |
127 | | bool VerifyFieldRequired(const Verifier &verifier, voffset_t field, |
128 | | size_t align) const { |
129 | | auto field_offset = GetOptionalFieldOffset(field); |
130 | | return verifier.Check(field_offset != 0) && |
131 | | verifier.VerifyField<T>(data_, field_offset, align); |
132 | | } |
133 | | |
134 | | // Versions for offsets. |
135 | 0 | bool VerifyOffset(const Verifier &verifier, voffset_t field) const { |
136 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
137 | 0 | return !field_offset || verifier.VerifyOffset(data_, field_offset); |
138 | 0 | } |
139 | | |
140 | 0 | bool VerifyOffsetRequired(const Verifier &verifier, voffset_t field) const { |
141 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
142 | 0 | return verifier.Check(field_offset != 0) && |
143 | 0 | verifier.VerifyOffset(data_, field_offset); |
144 | 0 | } |
145 | | |
146 | | private: |
147 | | // private constructor & copy constructor: you obtain instances of this |
148 | | // class by pointing to existing data only |
149 | | Table(); |
150 | | Table(const Table &other); |
151 | | Table &operator=(const Table &); |
152 | | |
153 | | uint8_t data_[1]; |
154 | | }; |
155 | | |
156 | | // This specialization allows avoiding warnings like: |
157 | | // MSVC C4800: type: forcing value to bool 'true' or 'false'. |
158 | | template<> |
159 | | inline flatbuffers::Optional<bool> Table::GetOptional<uint8_t, bool>( |
160 | 0 | voffset_t field) const { |
161 | 0 | auto field_offset = GetOptionalFieldOffset(field); |
162 | 0 | auto p = data_ + field_offset; |
163 | 0 | return field_offset ? Optional<bool>(ReadScalar<uint8_t>(p) != 0) |
164 | 0 | : Optional<bool>(); |
165 | 0 | } |
166 | | |
167 | | } // namespace flatbuffers |
168 | | } // namespace mapserver |
169 | | |
170 | | #endif // FLATBUFFERS_TABLE_H_ |