Libosmium  2.16.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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 <osmium/util/string.hpp>
37 
38 #include <algorithm>
39 #include <cstddef>
40 #include <functional>
41 #include <map>
42 #include <memory>
43 #include <stdexcept>
44 #include <string>
45 #include <type_traits>
46 #include <vector>
47 
48 namespace osmium {
49 
50  struct map_factory_error : public std::runtime_error {
51 
52  explicit map_factory_error(const char* message) :
53  std::runtime_error(message) {
54  }
55 
56  explicit map_factory_error(const std::string& message) :
57  std::runtime_error(message) {
58  }
59 
60  }; // struct map_factory_error
61 
62  namespace index {
63 
67  namespace map {
68 
95  template <typename TId, typename TValue>
96  class Map {
97 
98  "TId template parameter for class Map must be unsigned integral type");
99 
100  protected:
101 
102  Map(Map&&) noexcept = default;
103  Map& operator=(Map&&) noexcept = default;
104 
105  public:
106 
108  using key_type = TId;
109 
111  using value_type = TValue;
112 
113  Map() noexcept = default;
114 
115  Map(const Map&) = delete;
116  Map& operator=(const Map&) = delete;
117 
118  virtual ~Map() noexcept = default;
119 
120  virtual void reserve(const std::size_t /*size*/) {
121  // default implementation is empty
122  }
123 
125  virtual void set(const TId id, const TValue value) = 0;
126 
134  virtual TValue get(const TId id) const = 0;
135 
145  virtual TValue get_noexcept(const TId id) const noexcept = 0;
146 
153  virtual std::size_t size() const = 0;
154 
162  virtual std::size_t used_memory() const = 0;
163 
168  virtual void clear() = 0;
169 
174  virtual void sort() {
175  // default implementation is empty
176  }
177 
178  // This function can usually be const in derived classes,
179  // but not always. It could, for instance, sort internal data.
180  // This is why it is not declared const here.
181  virtual void dump_as_list(const int /*fd*/) {
182  throw std::runtime_error{"can't dump as list"};
183  }
184 
185  // This function can usually be const in derived classes,
186  // but not always. It could, for instance, sort internal data.
187  // This is why it is not declared const here.
188  virtual void dump_as_array(const int /*fd*/) {
189  throw std::runtime_error{"can't dump as array"};
190  }
191 
192  }; // class Map
193 
194  } // namespace map
195 
196  template <typename TId, typename TValue>
197  class MapFactory {
198 
199  public:
200 
201  using id_type = TId;
202  using value_type = TValue;
204  using create_map_func = std::function<map_type*(const std::vector<std::string>&)>;
205 
206  private:
207 
208  std::map<const std::string, create_map_func> m_callbacks;
209 
210  MapFactory() = default;
211 
212  public:
213 
214  MapFactory(const MapFactory&) = delete;
215  MapFactory& operator=(const MapFactory&) = delete;
216 
217  MapFactory(MapFactory&&) = delete;
218  MapFactory& operator=(MapFactory&&) = delete;
219 
220  ~MapFactory() = default;
221 
223  static MapFactory<id_type, value_type> factory;
224  return factory;
225  }
226 
227  bool register_map(const std::string& map_type_name, create_map_func func) {
228  return m_callbacks.emplace(map_type_name, func).second;
229  }
230 
231  bool has_map_type(const std::string& map_type_name) const {
232  return m_callbacks.count(map_type_name) != 0;
233  }
234 
235  std::vector<std::string> map_types() const {
236  std::vector<std::string> result;
237 
238  for (const auto& cb : m_callbacks) {
239  result.push_back(cb.first);
240  }
241 
242  std::sort(result.begin(), result.end());
243 
244  return result;
245  }
246 
247  std::unique_ptr<map_type> create_map(const std::string& config_string) const {
248  std::vector<std::string> config{osmium::split_string(config_string, ',')};
249 
250  if (config.empty()) {
251  throw map_factory_error{"Need non-empty map type name"};
252  }
253 
254  const auto it = m_callbacks.find(config[0]);
255  if (it != m_callbacks.end()) {
256  return std::unique_ptr<map_type>((it->second)(config));
257  }
258 
259  throw map_factory_error{std::string{"Support for map type '"} + config[0] + "' not compiled into this binary"};
260  }
261 
262  }; // class MapFactory
263 
264  namespace map {
265 
266  template <typename TId, typename TValue, template <typename, typename> class TMap>
267  struct create_map {
268  TMap<TId, TValue>* operator()(const std::vector<std::string>& /*config_string*/) {
269  return new TMap<TId, TValue>();
270  }
271  };
272 
273  } // namespace map
274 
275  template <typename TId, typename TValue, template <typename, typename> class TMap>
276  inline bool register_map(const std::string& name) {
277  return osmium::index::MapFactory<TId, TValue>::instance().register_map(name, [](const std::vector<std::string>& config) {
278  return map::create_map<TId, TValue, TMap>()(config);
279  });
280  }
281 
282 #define OSMIUM_CONCATENATE_DETAIL_(x, y) x##y
283 #define OSMIUM_CONCATENATE_(x, y) OSMIUM_CONCATENATE_DETAIL_(x, y)
284 
285 #define REGISTER_MAP(id, value, klass, name) \
286 namespace osmium { namespace index { namespace detail { \
287  namespace OSMIUM_CONCATENATE_(register_map_, __COUNTER__) { \
288  const bool registered = osmium::index::register_map<id, value, klass>(#name); \
289  inline bool get_registered() noexcept { \
290  return registered; \
291  } } \
292 } } }
293 
294  } // namespace index
295 
296 } // namespace osmium
297 
298 #endif // OSMIUM_INDEX_MAP_HPP
osmium::index::MapFactory::map_types
std::vector< std::string > map_types() const
Definition: map.hpp:235
osmium::map_factory_error::map_factory_error
map_factory_error(const std::string &message)
Definition: map.hpp:56
osmium::index::map::Map::set
virtual void set(const TId id, const TValue value)=0
Set the field with id to value.
osmium::index::MapFactory::register_map
bool register_map(const std::string &map_type_name, create_map_func func)
Definition: map.hpp:227
osmium::index::MapFactory::value_type
TValue value_type
Definition: map.hpp:202
osmium::index::map::Map::key_type
TId key_type
The "key" type, usually osmium::unsigned_object_id_type.
Definition: map.hpp:108
osmium::index::map::create_map::operator()
TMap< TId, TValue > * operator()(const std::vector< std::string > &)
Definition: map.hpp:268
osmium::index::map::create_map
Definition: map.hpp:267
osmium::index::map::Map::value_type
TValue value_type
The "value" type, usually a Location or size_t.
Definition: map.hpp:111
osmium::index::MapFactory::has_map_type
bool has_map_type(const std::string &map_type_name) const
Definition: map.hpp:231
string.hpp
osmium::index::map::Map::dump_as_array
virtual void dump_as_array(const int)
Definition: map.hpp:188
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::index::MapFactory::MapFactory
MapFactory()=default
osmium::index::MapFactory::m_callbacks
std::map< const std::string, create_map_func > m_callbacks
Definition: map.hpp:208
osmium::index::MapFactory
Definition: map.hpp:197
osmium::index::map::Map::sort
virtual void sort()
Definition: map.hpp:174
osmium::map_factory_error::map_factory_error
map_factory_error(const char *message)
Definition: map.hpp:52
osmium::index::map::Map::size
virtual std::size_t size() const =0
osmium::index::map::Map::dump_as_list
virtual void dump_as_list(const int)
Definition: map.hpp:181
osmium::index::MapFactory::~MapFactory
~MapFactory()=default
osmium::index::MapFactory::operator=
MapFactory & operator=(const MapFactory &)=delete
osmium::index::map::Map::Map
Map() noexcept=default
osmium::index::MapFactory::create_map
std::unique_ptr< map_type > create_map(const std::string &config_string) const
Definition: map.hpp:247
osmium::index::MapFactory::id_type
TId id_type
Definition: map.hpp:201
osmium::index::map::Map::used_memory
virtual std::size_t used_memory() const =0
osmium::index::register_map
bool register_map(const std::string &name)
Definition: map.hpp:276
osmium::index::map::Map::get_noexcept
virtual TValue get_noexcept(const TId id) const noexcept=0
std
Definition: location.hpp:551
osmium::index::map::Map::reserve
virtual void reserve(const std::size_t)
Definition: map.hpp:120
osmium::index::map::Map::clear
virtual void clear()=0
osmium::index::MapFactory::instance
static MapFactory< id_type, value_type > & instance()
Definition: map.hpp:222
osmium::map_factory_error
Definition: map.hpp:50
osmium::index::map::Map::get
virtual TValue get(const TId id) const =0
osmium::index::map::Map
Definition: map.hpp:96
osmium::index::MapFactory::create_map_func
std::function< map_type *(const std::vector< std::string > &)> create_map_func
Definition: map.hpp:204
osmium::split_string
std::vector< std::string > split_string(const std::string &str, const char sep, bool compact=false)
Definition: string.hpp:50