libosmscout 1.1.1
Loading...
Searching...
No Matches
FeatureReader.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_FEATURE_READER_H
2#define OSMSCOUT_FEATURE_READER_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2018 Tim Teulings
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <limits>
24#include <vector>
25
27#include <osmscout/TypeConfig.h>
28
29namespace osmscout {
30
38 template<class F>
40 {
41 private:
42 std::vector<size_t> lookupTable;
43
44 public:
45 explicit FeatureReader(const TypeConfig& typeConfig);
46
57 bool GetIndex(const FeatureValueBuffer& buffer,
58 size_t& index) const;
59
67 bool IsSet(const FeatureValueBuffer& buffer) const;
68 };
69
70 template<class F>
71 FeatureReader<F>::FeatureReader(const TypeConfig& typeConfig)
72 {
73 FeatureRef feature=typeConfig.GetFeature(F::NAME);
74
75 lookupTable.resize(typeConfig.GetTypeCount(),
76 std::numeric_limits<size_t>::max());
77
78 for (const auto &type : typeConfig.GetTypes()) {
79 size_t index;
80
81 if (type->GetFeature(F::NAME,
82 index)) {
83 lookupTable[type->GetIndex()]=index;
84 }
85 }
86 }
87
88 template<class F>
89 bool FeatureReader<F>::GetIndex(const FeatureValueBuffer& buffer,
90 size_t& index) const
91 {
92 index=lookupTable[buffer.GetType()->GetIndex()];
93
94 return index!=std::numeric_limits<size_t>::max();
95 }
96
97 template<class F>
98 bool FeatureReader<F>::IsSet(const FeatureValueBuffer& buffer) const
99 {
100 size_t index=lookupTable[buffer.GetType()->GetIndex()];
101
102 if (index!=std::numeric_limits<size_t>::max()) {
103 return buffer.HasFeature(index);
104 }
105
106 return false;
107 }
108
114 class OSMSCOUT_API DynamicFeatureReader CLASS_FINAL
115 {
116 private:
117 std::string featureName;
118 std::vector<size_t> lookupTable;
119
120 public:
122 const Feature& feature);
123
124 std::string GetFeatureName() const
125 {
126 return featureName;
127 }
128
129 bool IsSet(const FeatureValueBuffer& buffer) const;
130
132 };
133
141 template<class F, class V>
143 {
144 private:
145 std::vector<size_t> lookupTable;
146
147 static_assert(std::is_base_of<Feature, F>::value, "F have to be subtype of Feature");
148 static_assert(std::is_base_of<FeatureValue, V>::value, "V have to be subtype of FeatureValue");
149
150 public:
151 explicit FeatureValueReader(const TypeConfig& typeConfig);
152
163 bool GetIndex(const FeatureValueBuffer& buffer,
164 size_t& index) const;
165
174 V* GetValue(const FeatureValueBuffer& buffer) const;
175
183 V GetValue(const FeatureValueBuffer& buffer, const V& defaultValue) const;
184 };
185
186 template<class F, class V>
188 {
189 FeatureRef feature=typeConfig.GetFeature(F::NAME);
190
191 assert(feature->HasValue());
192
193 lookupTable.resize(typeConfig.GetTypeCount(),
194 std::numeric_limits<size_t>::max());
195
196 for (const auto &type : typeConfig.GetTypes()) {
197 size_t index;
198
199 if (type->GetFeature(F::NAME,
200 index)) {
201 lookupTable[type->GetIndex()]=index;
202 }
203 }
204 }
205
206 template<class F, class V>
207 bool FeatureValueReader<F,V>::GetIndex(const FeatureValueBuffer& buffer,
208 size_t& index) const
209 {
210 assert(buffer.GetType()->GetIndex() < lookupTable.size());
211 index=lookupTable[buffer.GetType()->GetIndex()];
212
213 return index!=std::numeric_limits<size_t>::max();
214 }
215
216 template<class F, class V>
217 V* FeatureValueReader<F,V>::GetValue(const FeatureValueBuffer& buffer) const
218 {
219 assert(buffer.GetType()->GetIndex()<lookupTable.size());
220 size_t index=lookupTable[buffer.GetType()->GetIndex()];
221
222 if (index!=std::numeric_limits<size_t>::max() &&
223 buffer.HasFeature(index)) {
224 FeatureValue* val=buffer.GetValue(index);
225 // Object returned from Feature::AllocateValue and V have to be the same type!
226 // But it cannot be tested in compile-time, lets do it in runtime assert at least.
227 assert(val==nullptr || dynamic_cast<V*>(val)!=nullptr);
228 return static_cast<V*>(val);
229 }
230
231 return nullptr;
232 }
233
234 template<class F, class V>
235 V FeatureValueReader<F,V>::GetValue(const FeatureValueBuffer& buffer, const V& defaultValue) const
236 {
237 assert(buffer.GetType()->GetIndex() < lookupTable.size());
238 size_t index=lookupTable[buffer.GetType()->GetIndex()];
239
240 if (index!=std::numeric_limits<size_t>::max() &&
241 buffer.HasFeature(index)) {
242 FeatureValue *val = buffer.GetValue(index);
243 // Object returned from Feature::AllocateValue and V have to be the same type!
244 // But it cannot be tested in compile-time, lets do it in runtime assert at least.
245 assert(val == nullptr || dynamic_cast<V*>(val) != nullptr);
246 return *static_cast<V*>(val);
247 }
248
249 return defaultValue;
250 }
251
252 template <class F, class V>
254 {
255 private:
256 std::vector<size_t> lookupTable;
257
258 public:
259 explicit FeatureLabelReader(const TypeConfig& typeConfig);
260
268 std::string GetLabel(const FeatureValueBuffer& buffer) const;
269 };
270
271 template<class F, class V>
273 {
274 FeatureRef feature=typeConfig.GetFeature(F::NAME);
275
276 assert(feature->HasLabel());
277
278 lookupTable.resize(typeConfig.GetTypeCount(),
279 std::numeric_limits<size_t>::max());
280
281 for (const auto &type : typeConfig.GetTypes()) {
282 size_t index;
283
284 if (type->GetFeature(F::NAME,
285 index)) {
286 lookupTable[type->GetIndex()]=index;
287 }
288 }
289 }
290
291 template<class F, class V>
292 std::string FeatureLabelReader<F,V>::GetLabel(const FeatureValueBuffer& buffer) const
293 {
294 size_t index=lookupTable[buffer.GetType()->GetIndex()];
295
296 if (index!=std::numeric_limits<size_t>::max() &&
297 buffer.HasFeature(index)) {
298 auto* value=dynamic_cast<V*>(buffer.GetValue(index));
299
300 if (value!=nullptr) {
301 return value->GetLabel(Locale(), 0);
302 }
303 }
304
305 return "";
306 }
307
311}
312
313#endif
#define OSMSCOUT_API
Definition CoreImportExport.h:45
Definition Area.h:88
DynamicFeatureReader(const TypeConfig &typeConfig, const Feature &feature)
FeatureValue * GetValue(const FeatureValueBuffer &buffer) const
bool IsSet(const FeatureValueBuffer &buffer) const
std::string GetFeatureName() const
Definition FeatureReader.h:124
Vertex2D * buffer
Definition Transformation.h:343
FeatureValueBuffer()=default
Definition TypeFeature.h:99
virtual bool HasValue() const
Definition TypeFeature.h:165
virtual bool HasLabel() const
Definition TypeFeature.h:173
FeatureLabelReader(const TypeConfig &typeConfig)
Definition FeatureReader.h:272
std::string GetLabel(const FeatureValueBuffer &buffer) const
Definition FeatureReader.h:292
FeatureReader(const TypeConfig &typeConfig)
Definition FeatureReader.h:71
bool IsSet(const FeatureValueBuffer &buffer) const
Definition FeatureReader.h:98
bool GetIndex(const FeatureValueBuffer &buffer, size_t &index) const
Definition FeatureReader.h:89
Definition TypeFeature.h:41
FeatureValueReader(const TypeConfig &typeConfig)
Definition FeatureReader.h:187
V GetValue(const FeatureValueBuffer &buffer, const V &defaultValue) const
Definition FeatureReader.h:235
bool GetIndex(const FeatureValueBuffer &buffer, size_t &index) const
Definition FeatureReader.h:207
V * GetValue(const FeatureValueBuffer &buffer) const
Definition FeatureReader.h:217
Definition Area.h:39
std::shared_ptr< Feature > FeatureRef
Definition TypeFeature.h:219