libosmscout  1.1.1
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 
26 #include <osmscout/TypeFeature.h>
27 #include <osmscout/TypeFeatures.h>
28 #include <osmscout/TypeConfig.h>
29 
30 namespace osmscout {
31 
39  template<class F>
41  {
42  private:
43  std::vector<size_t> lookupTable;
44 
45  public:
46  explicit FeatureReader(const TypeConfig& typeConfig);
47 
58  bool GetIndex(const FeatureValueBuffer& buffer,
59  size_t& index) const;
60 
68  bool IsSet(const FeatureValueBuffer& buffer) const;
69  };
70 
71  template<class F>
72  FeatureReader<F>::FeatureReader(const TypeConfig& typeConfig)
73  {
74  FeatureRef feature=typeConfig.GetFeature(F::NAME);
75 
76  lookupTable.resize(typeConfig.GetTypeCount(),
77  std::numeric_limits<size_t>::max());
78 
79  for (const auto &type : typeConfig.GetTypes()) {
80  size_t index;
81 
82  if (type->GetFeature(F::NAME,
83  index)) {
84  lookupTable[type->GetIndex()]=index;
85  }
86  }
87  }
88 
89  template<class F>
90  bool FeatureReader<F>::GetIndex(const FeatureValueBuffer& buffer,
91  size_t& index) const
92  {
93  index=lookupTable[buffer.GetType()->GetIndex()];
94 
95  return index!=std::numeric_limits<size_t>::max();
96  }
97 
98  template<class F>
99  bool FeatureReader<F>::IsSet(const FeatureValueBuffer& buffer) const
100  {
101  size_t index=lookupTable[buffer.GetType()->GetIndex()];
102 
103  if (index!=std::numeric_limits<size_t>::max()) {
104  return buffer.HasFeature(index);
105  }
106 
107  return false;
108  }
109 
115 
121  class OSMSCOUT_API DynamicFeatureReader CLASS_FINAL
122  {
123  private:
124  std::string featureName;
125  std::vector<size_t> lookupTable;
126 
127  public:
128  DynamicFeatureReader(const TypeConfig& typeConfig,
129  const Feature& feature);
130 
131  std::string GetFeatureName() const
132  {
133  return featureName;
134  }
135 
136  bool IsSet(const FeatureValueBuffer& buffer) const;
137 
138  FeatureValue* GetValue(const FeatureValueBuffer& buffer) const;
139  };
140 
148  template<class F, class V>
150  {
151  private:
152  std::vector<size_t> lookupTable;
153 
154  static_assert(std::is_base_of<Feature, F>::value, "F have to be subtype of Feature");
155  static_assert(std::is_base_of<FeatureValue, V>::value, "V have to be subtype of FeatureValue");
156 
157  public:
158  explicit FeatureValueReader(const TypeConfig& typeConfig);
159 
170  bool GetIndex(const FeatureValueBuffer& buffer,
171  size_t& index) const;
172 
181  V* GetValue(const FeatureValueBuffer& buffer) const;
182 
190  V GetValue(const FeatureValueBuffer& buffer, const V& defaultValue) const;
191  };
192 
193  template<class F, class V>
194  FeatureValueReader<F,V>::FeatureValueReader(const TypeConfig& typeConfig)
195  {
196  FeatureRef feature=typeConfig.GetFeature(F::NAME);
197 
198  assert(feature->HasValue());
199 
200  lookupTable.resize(typeConfig.GetTypeCount(),
201  std::numeric_limits<size_t>::max());
202 
203  for (const auto &type : typeConfig.GetTypes()) {
204  size_t index;
205 
206  if (type->GetFeature(F::NAME,
207  index)) {
208  lookupTable[type->GetIndex()]=index;
209  }
210  }
211  }
212 
213  template<class F, class V>
214  bool FeatureValueReader<F,V>::GetIndex(const FeatureValueBuffer& buffer,
215  size_t& index) const
216  {
217  assert(buffer.GetType()->GetIndex() < lookupTable.size());
218  index=lookupTable[buffer.GetType()->GetIndex()];
219 
220  return index!=std::numeric_limits<size_t>::max();
221  }
222 
223  template<class F, class V>
224  V* FeatureValueReader<F,V>::GetValue(const FeatureValueBuffer& buffer) const
225  {
226  assert(buffer.GetType()->GetIndex()<lookupTable.size());
227  size_t index=lookupTable[buffer.GetType()->GetIndex()];
228 
229  if (index!=std::numeric_limits<size_t>::max() &&
230  buffer.HasFeature(index)) {
231  FeatureValue* val=buffer.GetValue(index);
232  // Object returned from Feature::AllocateValue and V have to be the same type!
233  // But it cannot be tested in compile-time, lets do it in runtime assert at least.
234  assert(val==nullptr || dynamic_cast<V*>(val)!=nullptr);
235  return static_cast<V*>(val);
236  }
237 
238  return nullptr;
239  }
240 
241  template<class F, class V>
242  V FeatureValueReader<F,V>::GetValue(const FeatureValueBuffer& buffer, const V& defaultValue) const
243  {
244  assert(buffer.GetType()->GetIndex() < lookupTable.size());
245  size_t index=lookupTable[buffer.GetType()->GetIndex()];
246 
247  if (index!=std::numeric_limits<size_t>::max() &&
248  buffer.HasFeature(index)) {
249  FeatureValue *val = buffer.GetValue(index);
250  // Object returned from Feature::AllocateValue and V have to be the same type!
251  // But it cannot be tested in compile-time, lets do it in runtime assert at least.
252  assert(val == nullptr || dynamic_cast<V*>(val) != nullptr);
253  return *static_cast<V*>(val);
254  }
255 
256  return defaultValue;
257  }
258 
282 
283  template <class F, class V>
285  {
286  private:
287  std::vector<size_t> lookupTable;
288 
289  public:
290  explicit FeatureLabelReader(const TypeConfig& typeConfig);
291 
299  std::string GetLabel(const FeatureValueBuffer& buffer) const;
300  };
301 
302  template<class F, class V>
303  FeatureLabelReader<F,V>::FeatureLabelReader(const TypeConfig& typeConfig)
304  {
305  FeatureRef feature=typeConfig.GetFeature(F::NAME);
306 
307  assert(feature->HasLabel());
308 
309  lookupTable.resize(typeConfig.GetTypeCount(),
310  std::numeric_limits<size_t>::max());
311 
312  for (const auto &type : typeConfig.GetTypes()) {
313  size_t index;
314 
315  if (type->GetFeature(F::NAME,
316  index)) {
317  lookupTable[type->GetIndex()]=index;
318  }
319  }
320  }
321 
322  template<class F, class V>
323  std::string FeatureLabelReader<F,V>::GetLabel(const FeatureValueBuffer& buffer) const
324  {
325  size_t index=lookupTable[buffer.GetType()->GetIndex()];
326 
327  if (index!=std::numeric_limits<size_t>::max() &&
328  buffer.HasFeature(index)) {
329  auto* value=dynamic_cast<V*>(buffer.GetValue(index));
330 
331  if (value!=nullptr) {
332  return value->GetLabel(Locale(), 0);
333  }
334  }
335 
336  return "";
337  }
338 
341 
345 }
346 
347 #endif
FeatureLabelReader(const TypeConfig &typeConfig)
Definition: FeatureReader.h:303
Definition: Area.h:86
Definition: FeatureReader.h:149
V * GetValue(const FeatureValueBuffer &buffer) const
Definition: FeatureReader.h:224
Definition: FeatureReader.h:284
std::string GetLabel(const FeatureValueBuffer &buffer) const
Definition: FeatureReader.h:323
Definition: Area.h:38
FeatureValueReader(const TypeConfig &typeConfig)
Definition: FeatureReader.h:194
Definition: TypeFeature.h:98
Definition: TypeFeature.h:40
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
bool GetIndex(const FeatureValueBuffer &buffer, size_t &index) const
Definition: FeatureReader.h:214
std::string GetFeatureName() const
Definition: FeatureReader.h:131
std::shared_ptr< Feature > FeatureRef
Definition: TypeFeature.h:219
Definition: FeatureReader.h:40
bool GetIndex(const FeatureValueBuffer &buffer, size_t &index) const
Definition: FeatureReader.h:90
FeatureReader(const TypeConfig &typeConfig)
Definition: FeatureReader.h:72
bool IsSet(const FeatureValueBuffer &buffer) const
Definition: FeatureReader.h:99