Libosmium  2.11.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
map.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_INDEX_MAP_HPP
2 #define OSMIUM_INDEX_MAP_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <algorithm>
37 #include <cstddef>
38 #include <functional>
39 #include <map>
40 #include <memory>
41 #include <stdexcept>
42 #include <string>
43 #include <type_traits>
44 #include <vector>
45 
47 #include <osmium/util/string.hpp>
48 
49 namespace osmium {
50 
51  struct map_factory_error : public std::runtime_error {
52 
53  explicit map_factory_error(const char* message) :
54  std::runtime_error(message) {
55  }
56 
57  explicit map_factory_error(const std::string& message) :
58  std::runtime_error(message) {
59  }
60 
61  }; // struct map_factory_error
62 
63  namespace index {
64 
68  namespace map {
69 
96  template <typename TId, typename TValue>
97  class Map {
98 
99  "TId template parameter for class Map must be unsigned integral type");
100 
101  Map(const Map&) = delete;
102  Map& operator=(const Map&) = delete;
103 
104  protected:
105 
106  Map(Map&&) = default;
107  Map& operator=(Map&&) = default;
108 
109  public:
110 
112  using key_type = TId;
113 
115  using value_type = TValue;
116 
117  Map() = default;
118 
119  virtual ~Map() noexcept = default;
120 
121  virtual void reserve(const size_t) {
122  // default implementation is empty
123  }
124 
126  virtual void set(const TId id, const TValue value) = 0;
127 
135  virtual TValue get(const TId id) const = 0;
136 
146  virtual TValue get_noexcept(const TId id) const noexcept = 0;
147 
154  virtual size_t size() const = 0;
155 
163  virtual size_t used_memory() const = 0;
164 
169  virtual void clear() = 0;
170 
175  virtual void sort() {
176  // default implementation is empty
177  }
178 
179  // This function can usually be const in derived classes,
180  // but not always. It could, for instance, sort internal data.
181  // This is why it is not declared const here.
182  virtual void dump_as_list(const int /*fd*/) {
183  throw std::runtime_error("can't dump as list");
184  }
185 
186  // This function can usually be const in derived classes,
187  // but not always. It could, for instance, sort internal data.
188  // This is why it is not declared const here.
189  virtual void dump_as_array(const int /*fd*/) {
190  throw std::runtime_error("can't dump as array");
191  }
192 
193  }; // class Map
194 
195  } // namespace map
196 
197  template <typename TId, typename TValue>
198  class MapFactory {
199 
200  public:
201 
202  using id_type = TId;
203  using value_type = TValue;
205  using create_map_func = std::function<map_type*(const std::vector<std::string>&)>;
206 
207  private:
208 
209  std::map<const std::string, create_map_func> m_callbacks;
210 
211  MapFactory() = default;
212 
213  MapFactory(const MapFactory&) = delete;
214  MapFactory& operator=(const MapFactory&) = delete;
215 
216  MapFactory(MapFactory&&) = delete;
217  MapFactory& operator=(MapFactory&&) = delete;
218 
219  public:
220 
222  static MapFactory<id_type, value_type> factory;
223  return factory;
224  }
225 
226  bool register_map(const std::string& map_type_name, create_map_func func) {
227  return m_callbacks.emplace(map_type_name, func).second;
228  }
229 
230  bool has_map_type(const std::string& map_type_name) const {
231  return m_callbacks.count(map_type_name) != 0;
232  }
233 
234  std::vector<std::string> map_types() const {
235  std::vector<std::string> result;
236 
237  for (const auto& cb : m_callbacks) {
238  result.push_back(cb.first);
239  }
240 
241  std::sort(result.begin(), result.end());
242 
243  return result;
244  }
245 
246  std::unique_ptr<map_type> create_map(const std::string& config_string) const {
247  std::vector<std::string> config = osmium::split_string(config_string, ',');
248 
249  if (config.empty()) {
250  throw map_factory_error{"Need non-empty map type name"};
251  }
252 
253  auto it = m_callbacks.find(config[0]);
254  if (it != m_callbacks.end()) {
255  return std::unique_ptr<map_type>((it->second)(config));
256  }
257 
258  throw map_factory_error{std::string{"Support for map type '"} + config[0] + "' not compiled into this binary"};
259  }
260 
261  }; // class MapFactory
262 
263  namespace map {
264 
265  template <typename TId, typename TValue, template<typename, typename> class TMap>
266  struct create_map {
267  TMap<TId, TValue>* operator()(const std::vector<std::string>&) {
268  return new TMap<TId, TValue>();
269  }
270  };
271 
272  } // namespace map
273 
274  template <typename TId, typename TValue, template<typename, typename> class TMap>
275  inline bool register_map(const std::string& name) {
276  return osmium::index::MapFactory<TId, TValue>::instance().register_map(name, [](const std::vector<std::string>& config) {
277  return map::create_map<TId, TValue, TMap>()(config);
278  });
279  }
280 
281 #define OSMIUM_CONCATENATE_DETAIL_(x, y) x##y
282 #define OSMIUM_CONCATENATE_(x, y) OSMIUM_CONCATENATE_DETAIL_(x, y)
283 
284 #define REGISTER_MAP(id, value, klass, name) \
285 namespace osmium { namespace index { namespace detail { \
286  const bool OSMIUM_CONCATENATE_(registered_, name) = osmium::index::register_map<id, value, klass>(#name); \
287  inline bool OSMIUM_CONCATENATE_(get_registered_, name)() noexcept { \
288  return OSMIUM_CONCATENATE_(registered_, name); \
289  } \
290 } } }
291 
292  } // namespace index
293 
294 } // namespace osmium
295 
296 #endif // OSMIUM_INDEX_MAP_HPP
std::map< const std::string, create_map_func > m_callbacks
Definition: map.hpp:209
virtual size_t size() const =0
virtual size_t used_memory() const =0
TId id_type
Definition: map.hpp:202
TValue value_type
Definition: map.hpp:203
Definition: reader_iterator.hpp:39
Definition: map.hpp:266
TId key_type
The "key" type, usually osmium::unsigned_object_id_type.
Definition: map.hpp:112
Definition: map.hpp:51
bool register_map(const std::string &map_type_name, create_map_func func)
Definition: map.hpp:226
TMap< TId, TValue > * operator()(const std::vector< std::string > &)
Definition: map.hpp:267
bool register_map(const std::string &name)
Definition: map.hpp:275
Definition: map.hpp:198
TValue value_type
The "value" type, usually a Location or size_t.
Definition: map.hpp:115
virtual void reserve(const size_t)
Definition: map.hpp:121
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
virtual void dump_as_array(const int)
Definition: map.hpp:189
std::unique_ptr< map_type > create_map(const std::string &config_string) const
Definition: map.hpp:246
virtual void dump_as_list(const int)
Definition: map.hpp:182
virtual void sort()
Definition: map.hpp:175
virtual ~Map() noexcept=default
bool has_map_type(const std::string &map_type_name) const
Definition: map.hpp:230
static MapFactory< id_type, value_type > & instance()
Definition: map.hpp:221
std::function< map_type *(const std::vector< std::string > &)> create_map_func
Definition: map.hpp:205
Map & operator=(const Map &)=delete
virtual TValue get_noexcept(const TId id) const noexcept=0
map_factory_error(const std::string &message)
Definition: map.hpp:57
std::vector< std::string > map_types() const
Definition: map.hpp:234
map_factory_error(const char *message)
Definition: map.hpp:53
virtual void clear()=0
MapFactory & operator=(const MapFactory &)=delete
std::vector< std::string > split_string(const std::string &str, const char sep, bool compact=false)
Definition: string.hpp:50
virtual void set(const TId id, const TValue value)=0
Set the field with id to value.
Definition: map.hpp:97