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