Libosmium: include/osmium/index/map.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
map.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_INDEX_MAP_HPP
2#define OSMIUM_INDEX_MAP_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/util/compatibility.hpp>
37#include <osmium/util/string.hpp>
38
39#include <algorithm>
40#include <cstddef>
41#include <functional>
42#include <map>
43#include <memory>
44#include <stdexcept>
45#include <string>
46#include <type_traits>
47#include <vector>
48
49namespace osmium {
50
51 struct OSMIUM_EXPORT 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 protected:
102
103 Map(Map&&) noexcept = default;
104 Map& operator=(Map&&) noexcept = default;
105
106 public:
107
109 using key_type = TId;
110
112 using value_type = TValue;
113
114 Map() noexcept = default;
115
116 Map(const Map&) = delete;
117 Map& operator=(const Map&) = delete;
118
119 virtual ~Map() noexcept = default;
120
121 virtual void reserve(const std::size_t /*size*/) {
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 std::size_t size() const = 0;
155
163 virtual std::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;
204 using map_type = osmium::index::map::Map<id_type, value_type>;
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 public:
214
215 MapFactory(const MapFactory&) = delete;
216 MapFactory& operator=(const MapFactory&) = delete;
217
218 MapFactory(MapFactory&&) = delete;
219 MapFactory& operator=(MapFactory&&) = delete;
220
221 ~MapFactory() noexcept = default;
222
223 static MapFactory<id_type, value_type>& instance() {
224 static MapFactory<id_type, value_type> factory;
225 return factory;
226 }
227
228 bool register_map(const std::string& map_type_name, create_map_func func) {
229 return m_callbacks.emplace(map_type_name, func).second;
230 }
231
232 bool has_map_type(const std::string& map_type_name) const {
233 return m_callbacks.count(map_type_name) != 0;
234 }
235
236 std::vector<std::string> map_types() const {
237 std::vector<std::string> result;
238 result.reserve(m_callbacks.size());
239
240 for (const auto& cb : m_callbacks) {
241 result.push_back(cb.first);
242 }
243
244 std::sort(result.begin(), result.end());
245
246 return result;
247 }
248
249 std::unique_ptr<map_type> create_map(const std::string& config_string) const {
250 std::vector<std::string> config{osmium::split_string(config_string, ',')};
251
252 if (config.empty()) {
253 throw map_factory_error{"Need non-empty map type name"};
254 }
255
256 const auto it = m_callbacks.find(config[0]);
257 if (it != m_callbacks.end()) {
258 return std::unique_ptr<map_type>((it->second)(config));
259 }
260
261 throw map_factory_error{std::string{"Support for map type '"} + config[0] + "' not compiled into this binary"};
262 }
263
264 }; // class MapFactory
265
266 namespace map {
267
268 template <typename TId, typename TValue, template <typename, typename> class TMap>
269 struct create_map {
270 TMap<TId, TValue>* operator()(const std::vector<std::string>& /*config_string*/) {
271 return new TMap<TId, TValue>();
272 }
273 };
274
275 } // namespace map
276
277 template <typename TId, typename TValue, template <typename, typename> class TMap>
278 inline bool register_map(const std::string& name) {
279 return osmium::index::MapFactory<TId, TValue>::instance().register_map(name, [](const std::vector<std::string>& config) {
280 return map::create_map<TId, TValue, TMap>()(config);
281 });
282 }
283
284#define OSMIUM_CONCATENATE_DETAIL_(x, y) x##y
285#define OSMIUM_CONCATENATE_(x, y) OSMIUM_CONCATENATE_DETAIL_(x, y)
286
287#define REGISTER_MAP(id, value, klass, name) \
288namespace osmium { namespace index { namespace detail { \
289 namespace OSMIUM_CONCATENATE_(register_map_, __COUNTER__) { \
290 const bool registered = osmium::index::register_map<id, value, klass>(#name); \
291 inline bool get_registered() noexcept { \
292 return registered; \
293 } } \
294} } }
295
296 } // namespace index
297
298} // namespace osmium
299
300#endif // OSMIUM_INDEX_MAP_HPP
osmium::index::MapFactory::value_type
TValue value_type
Definition map.hpp:203
osmium::index::MapFactory::register_map
bool register_map(const std::string &map_type_name, create_map_func func)
Definition map.hpp:228
osmium::index::MapFactory::~MapFactory
~MapFactory() noexcept=default
osmium::index::MapFactory::map_type
osmium::index::map::Map< id_type, value_type > map_type
Definition map.hpp:204
osmium::index::MapFactory::MapFactory
MapFactory(MapFactory &&)=delete
osmium::index::MapFactory::operator=
MapFactory & operator=(MapFactory &&)=delete
osmium::index::MapFactory::map_types
std::vector< std::string > map_types() const
Definition map.hpp:236
osmium::index::MapFactory::MapFactory
MapFactory(const MapFactory &)=delete
osmium::index::MapFactory::create_map
std::unique_ptr< map_type > create_map(const std::string &config_string) const
Definition map.hpp:249
osmium::index::MapFactory::operator=
MapFactory & operator=(const MapFactory &)=delete
osmium::index::MapFactory::MapFactory
MapFactory()=default
osmium::index::MapFactory::instance
static MapFactory< id_type, value_type > & instance()
Definition map.hpp:223
osmium::index::MapFactory::id_type
TId id_type
Definition map.hpp:202
osmium::index::MapFactory::m_callbacks
std::map< const std::string, create_map_func > m_callbacks
Definition map.hpp:209
osmium::index::MapFactory::has_map_type
bool has_map_type(const std::string &map_type_name) const
Definition map.hpp:232
osmium::index::MapFactory::create_map_func
std::function< map_type *(const std::vector< std::string > &)> create_map_func
Definition map.hpp:205
osmium::index::map::Map
Definition map.hpp:97
osmium::index::map::Map::get_noexcept
virtual TValue get_noexcept(const TId id) const noexcept=0
osmium::index::map::Map< unsigned_object_id_type, std::size_t >::key_type
unsigned_object_id_type key_type
Definition map.hpp:109
osmium::index::map::Map::used_memory
virtual std::size_t used_memory() const =0
osmium::index::map::Map::dump_as_array
virtual void dump_as_array(const int)
Definition map.hpp:189
osmium::index::map::Map::sort
virtual void sort()
Definition map.hpp:175
osmium::index::map::Map< unsigned_object_id_type, std::size_t >::value_type
std::size_t value_type
Definition map.hpp:112
osmium::index::map::Map::get
virtual TValue get(const TId id) const =0
osmium::index::map::Map::clear
virtual void clear()=0
osmium::index::map::Map< unsigned_object_id_type, std::size_t >::reserve
virtual void reserve(const std::size_t)
Definition map.hpp:121
osmium::index::map::Map::Map
Map(Map &&) noexcept=default
osmium::index::map::Map::dump_as_list
virtual void dump_as_list(const int)
Definition map.hpp:182
osmium::index::map::Map::set
virtual void set(const TId id, const TValue value)=0
Set the field with id to value.
osmium::index::map::Map::size
virtual std::size_t size() const =0
compatibility.hpp
OSMIUM_EXPORT
#define OSMIUM_EXPORT
Definition compatibility.hpp:45
osmium::config
Definition config.hpp:60
osmium::index::map
Key-value containers with unique integer values for a key.
Definition map.hpp:68
osmium::index::register_map
bool register_map(const std::string &name)
Definition map.hpp:278
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::split_string
std::vector< std::string > split_string(const std::string &str, const char sep, bool compact=false)
Definition string.hpp:50
std
Definition location.hpp:654
string.hpp
osmium::index::map::create_map
Definition map.hpp:269
osmium::index::map::create_map::operator()
TMap< TId, TValue > * operator()(const std::vector< std::string > &)
Definition map.hpp:270
osmium::map_factory_error
Definition map.hpp:51
osmium::map_factory_error::map_factory_error
map_factory_error(const std::string &message)
Definition map.hpp:57
osmium::map_factory_error::map_factory_error
map_factory_error(const char *message)
Definition map.hpp:53