Libosmium  2.2.0
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-2015 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  namespace index {
52 
56  namespace map {
57 
84  template <typename TId, typename TValue>
85  class Map {
86 
87 
88  Map(const Map&) = delete;
89  Map& operator=(const Map&) = delete;
90 
91  protected:
92 
93  Map(Map&&) = default;
94  Map& operator=(Map&&) = default;
95 
96  public:
97 
99  typedef TId key_type;
100 
102  typedef TValue value_type;
103 
104  Map() = default;
105 
106  virtual ~Map() = default;
107 
108  virtual void reserve(const size_t) {
109  // default implementation is empty
110  }
111 
113  virtual void set(const TId id, const TValue value) = 0;
114 
116  virtual const TValue get(const TId id) const = 0;
117 
124  virtual size_t size() const = 0;
125 
133  virtual size_t used_memory() const = 0;
134 
139  virtual void clear() = 0;
140 
145  virtual void sort() {
146  // default implementation is empty
147  }
148 
149  virtual void dump_as_list(const int /*fd*/) {
150  throw std::runtime_error("can't dump as list");
151  }
152 
153  virtual void dump_as_array(const int /*fd*/) {
154  throw std::runtime_error("can't dump as array");
155  }
156 
157  }; // class Map
158 
159  } // namespace map
160 
161  template <typename TId, typename TValue>
162  class MapFactory {
163 
164  public:
165 
166  typedef TId id_type;
167  typedef TValue value_type;
169  typedef std::function<map_type*(const std::vector<std::string>&)> create_map_func;
170 
171  private:
172 
173  std::map<const std::string, create_map_func> m_callbacks;
174 
175  MapFactory() = default;
176 
177  MapFactory(const MapFactory&) = delete;
178  MapFactory& operator=(const MapFactory&) = delete;
179 
180  MapFactory(MapFactory&&) = delete;
181  MapFactory& operator=(MapFactory&&) = delete;
182 
183  OSMIUM_NORETURN static void error(const std::string& map_type_name) {
184  std::string error_message {"Support for map type '"};
185  error_message += map_type_name;
186  error_message += "' not compiled into this binary.";
187  throw std::runtime_error(error_message);
188  }
189 
190  public:
191 
193  static MapFactory<id_type, value_type> factory;
194  return factory;
195  }
196 
197  bool register_map(const std::string& map_type_name, create_map_func func) {
198  return m_callbacks.emplace(map_type_name, func).second;
199  }
200 
201  bool has_map_type(const std::string& map_type_name) const {
202  return m_callbacks.count(map_type_name);
203  }
204 
205  std::vector<std::string> map_types() const {
206  std::vector<std::string> result;
207 
208  for (const auto& cb : m_callbacks) {
209  result.push_back(cb.first);
210  }
211 
212  std::sort(result.begin(), result.end());
213 
214  return result;
215  }
216 
217  std::unique_ptr<map_type> create_map(const std::string& config_string) const {
218  std::vector<std::string> config = osmium::split_string(config_string, ',');
219 
220  if (config.empty()) {
221  throw std::runtime_error("Need non-empty map type name.");
222  }
223 
224  auto it = m_callbacks.find(config[0]);
225  if (it != m_callbacks.end()) {
226  return std::unique_ptr<map_type>((it->second)(config));
227  }
228 
229  error(config[0]);
230  }
231 
232  }; // class MapFactory
233 
234  namespace map {
235 
236  template <typename TId, typename TValue, template<typename, typename> class TMap>
237  struct create_map {
238  TMap<TId, TValue>* operator()(const std::vector<std::string>&) {
239  return new TMap<TId, TValue>();
240  }
241  };
242 
243  } // namespace map
244 
245  template <typename TId, typename TValue, template<typename, typename> class TMap>
246  inline bool register_map(const std::string& name) {
247  return osmium::index::MapFactory<TId, TValue>::instance().register_map(name, [](const std::vector<std::string>& config) {
248  return map::create_map<TId, TValue, TMap>()(config);
249  });
250  }
251 
252 #define OSMIUM_CONCATENATE_DETAIL_(x, y) x##y
253 #define OSMIUM_CONCATENATE_(x, y) OSMIUM_CONCATENATE_DETAIL_(x, y)
254 #define OSMIUM_MAKE_UNIQUE_(x) OSMIUM_CONCATENATE_(x, __COUNTER__)
255 
256 #define REGISTER_MAP(id, value, klass, name) \
257 namespace { \
258  const bool OSMIUM_MAKE_UNIQUE_(registered_index_map_##name) = osmium::index::register_map<id, value, klass>(#name); \
259 }
260 
261  } // namespace index
262 
263 } // namespace osmium
264 
265 #endif // OSMIUM_INDEX_MAP_HPP
osmium::index::map::Map< id_type, value_type > map_type
Definition: map.hpp:168
std::map< const std::string, create_map_func > m_callbacks
Definition: map.hpp:173
#define OSMIUM_NORETURN
Definition: compatibility.hpp:44
virtual size_t size() const =0
virtual size_t used_memory() const =0
Definition: map.hpp:237
bool register_map(const std::string &map_type_name, create_map_func func)
Definition: map.hpp:197
TMap< TId, TValue > * operator()(const std::vector< std::string > &)
Definition: map.hpp:238
bool register_map(const std::string &name)
Definition: map.hpp:246
Definition: map.hpp:162
virtual void reserve(const size_t)
Definition: map.hpp:108
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
virtual void dump_as_array(const int)
Definition: map.hpp:153
std::unique_ptr< map_type > create_map(const std::string &config_string) const
Definition: map.hpp:217
TId key_type
The "key" type, usually osmium::unsigned_object_id_type.
Definition: map.hpp:99
virtual ~Map()=default
TId id_type
Definition: map.hpp:166
static OSMIUM_NORETURN void error(const std::string &map_type_name)
Definition: map.hpp:183
virtual void dump_as_list(const int)
Definition: map.hpp:149
virtual void sort()
Definition: map.hpp:145
bool has_map_type(const std::string &map_type_name) const
Definition: map.hpp:201
static MapFactory< id_type, value_type > & instance()
Definition: map.hpp:192
Map & operator=(const Map &)=delete
TValue value_type
The "value" type, usually a Location or size_t.
Definition: map.hpp:102
std::vector< std::string > map_types() const
Definition: map.hpp:205
virtual void clear()=0
TValue value_type
Definition: map.hpp:167
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:85
std::function< map_type *(const std::vector< std::string > &)> create_map_func
Definition: map.hpp:169