Libosmium  2.10.3
Fast and flexible C++ library for working with OpenStreetMap data
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-2016 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 
129  virtual const TValue get(const TId id) const = 0;
130 
137  virtual size_t size() const = 0;
138 
146  virtual size_t used_memory() const = 0;
147 
152  virtual void clear() = 0;
153 
158  virtual void sort() {
159  // default implementation is empty
160  }
161 
162  // This function can usually be const in derived classes,
163  // but not always. It could, for instance, sort internal data.
164  // This is why it is not declared const here.
165  virtual void dump_as_list(const int /*fd*/) {
166  throw std::runtime_error("can't dump as list");
167  }
168 
169  // This function can usually be const in derived classes,
170  // but not always. It could, for instance, sort internal data.
171  // This is why it is not declared const here.
172  virtual void dump_as_array(const int /*fd*/) {
173  throw std::runtime_error("can't dump as array");
174  }
175 
176  }; // class Map
177 
178  } // namespace map
179 
180  template <typename TId, typename TValue>
181  class MapFactory {
182 
183  public:
184 
185  using id_type = TId;
186  using value_type = TValue;
188  using create_map_func = std::function<map_type*(const std::vector<std::string>&)>;
189 
190  private:
191 
192  std::map<const std::string, create_map_func> m_callbacks;
193 
194  MapFactory() = default;
195 
196  MapFactory(const MapFactory&) = delete;
197  MapFactory& operator=(const MapFactory&) = delete;
198 
199  MapFactory(MapFactory&&) = delete;
200  MapFactory& operator=(MapFactory&&) = delete;
201 
202  public:
203 
205  static MapFactory<id_type, value_type> factory;
206  return factory;
207  }
208 
209  bool register_map(const std::string& map_type_name, create_map_func func) {
210  return m_callbacks.emplace(map_type_name, func).second;
211  }
212 
213  bool has_map_type(const std::string& map_type_name) const {
214  return m_callbacks.count(map_type_name) != 0;
215  }
216 
217  std::vector<std::string> map_types() const {
218  std::vector<std::string> result;
219 
220  for (const auto& cb : m_callbacks) {
221  result.push_back(cb.first);
222  }
223 
224  std::sort(result.begin(), result.end());
225 
226  return result;
227  }
228 
229  std::unique_ptr<map_type> create_map(const std::string& config_string) const {
230  std::vector<std::string> config = osmium::split_string(config_string, ',');
231 
232  if (config.empty()) {
233  throw map_factory_error{"Need non-empty map type name"};
234  }
235 
236  auto it = m_callbacks.find(config[0]);
237  if (it != m_callbacks.end()) {
238  return std::unique_ptr<map_type>((it->second)(config));
239  }
240 
241  throw map_factory_error{std::string{"Support for map type '"} + config[0] + "' not compiled into this binary"};
242  }
243 
244  }; // class MapFactory
245 
246  namespace map {
247 
248  template <typename TId, typename TValue, template<typename, typename> class TMap>
249  struct create_map {
250  TMap<TId, TValue>* operator()(const std::vector<std::string>&) {
251  return new TMap<TId, TValue>();
252  }
253  };
254 
255  } // namespace map
256 
257  template <typename TId, typename TValue, template<typename, typename> class TMap>
258  inline bool register_map(const std::string& name) {
259  return osmium::index::MapFactory<TId, TValue>::instance().register_map(name, [](const std::vector<std::string>& config) {
260  return map::create_map<TId, TValue, TMap>()(config);
261  });
262  }
263 
264 #define OSMIUM_CONCATENATE_DETAIL_(x, y) x##y
265 #define OSMIUM_CONCATENATE_(x, y) OSMIUM_CONCATENATE_DETAIL_(x, y)
266 
267 #define REGISTER_MAP(id, value, klass, name) \
268 namespace osmium { namespace index { namespace detail { \
269  const bool OSMIUM_CONCATENATE_(registered_, name) = osmium::index::register_map<id, value, klass>(#name); \
270  inline bool OSMIUM_CONCATENATE_(get_registered_, name)() noexcept { \
271  return OSMIUM_CONCATENATE_(registered_, name); \
272  } \
273 } } }
274 
275  } // namespace index
276 
277 } // namespace osmium
278 
279 #endif // OSMIUM_INDEX_MAP_HPP
std::map< const std::string, create_map_func > m_callbacks
Definition: map.hpp:192
std::unique_ptr< map_type > create_map(const std::string &config_string) const
Definition: map.hpp:229
TId id_type
Definition: map.hpp:185
TValue value_type
Definition: map.hpp:186
Definition: reader_iterator.hpp:39
Definition: map.hpp:249
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:209
TMap< TId, TValue > * operator()(const std::vector< std::string > &)
Definition: map.hpp:250
bool register_map(const std::string &name)
Definition: map.hpp:258
Definition: map.hpp:181
TValue value_type
The "value" type, usually a Location or size_t.
Definition: map.hpp:115
bool has_map_type(const std::string &map_type_name) const
Definition: map.hpp:213
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
virtual void dump_as_array(const int)
Definition: map.hpp:172
std::vector< std::string > map_types() const
Definition: map.hpp:217
virtual void dump_as_list(const int)
Definition: map.hpp:165
virtual void sort()
Definition: map.hpp:158
static MapFactory< id_type, value_type > & instance()
Definition: map.hpp:204
std::function< map_type *(const std::vector< std::string > &)> create_map_func
Definition: map.hpp:188
map_factory_error(const std::string &message)
Definition: map.hpp:57
map_factory_error(const char *message)
Definition: map.hpp:53
std::vector< std::string > split_string(const std::string &str, const char sep, bool compact=false)
Definition: string.hpp:50
Definition: map.hpp:97