-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathparams.cpp
More file actions
125 lines (108 loc) · 3.45 KB
/
params.cpp
File metadata and controls
125 lines (108 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "params.hpp"
#include "format.hpp"
#include "logging.hpp"
#include "overloaded.hpp"
#include "pgsql.hpp"
std::string to_string(param_value_t const &value)
{
return std::visit(
overloaded{[](null_param_t) { return std::string{}; },
[](std::string val) { return val; },
[](auto const &val) { return fmt::to_string(val); }},
value);
}
param_value_t params_t::get(std::string const &key) const
{
return m_map.at(key);
}
bool params_t::has(std::string const &key) const noexcept
{
return m_map.count(key) > 0;
}
bool params_t::get_bool(std::string const &key, bool default_value) const
{
return get_by_type<bool>(key, default_value);
}
int64_t params_t::get_int64(std::string const &key, int64_t default_value) const
{
return get_by_type<int64_t>(key, default_value);
}
double params_t::get_double(std::string const &key, double default_value) const
{
auto const it = m_map.find(key);
if (it == m_map.end()) {
return default_value;
}
if (std::holds_alternative<double>(it->second)) {
return std::get<double>(it->second);
}
if (std::holds_alternative<int64_t>(it->second)) {
return static_cast<double>(std::get<int64_t>(it->second));
}
throw fmt_error("Invalid value '{}' for {}.", to_string(it->second), key);
}
std::string params_t::get_string(std::string const &key) const
{
auto const it = m_map.find(key);
if (it == m_map.end()) {
throw fmt_error("Missing parameter '{}' on generalizer.", key);
}
return to_string(it->second);
}
std::string params_t::get_string(std::string const &key,
std::string const &default_value) const
{
return get_by_type<std::string>(key, default_value);
}
std::string params_t::get_identifier(std::string const &key) const
{
auto const it = m_map.find(key);
if (it == m_map.end()) {
return {};
}
std::string result = to_string(it->second);
check_identifier(result, key.c_str());
return result;
}
void params_t::check_identifier_with_default(std::string const &key,
std::string default_value)
{
auto const it = m_map.find(key);
if (it == m_map.end()) {
m_map.emplace(key, std::move(default_value));
} else {
check_identifier(to_string(it->second), key.c_str());
}
}
unsigned int uint_in_range(params_t const ¶ms, std::string const &key,
unsigned int min, unsigned int max,
unsigned int default_value)
{
int64_t const value = params.get_int64(key, default_value);
if (value < 0 || value > std::numeric_limits<unsigned int>::max()) {
throw fmt_error("Invalid value '{}' for parameter '{}'.", value, key);
}
auto uvalue = static_cast<unsigned int>(value);
if (uvalue < min || uvalue > max) {
throw fmt_error("Invalid value '{}' for parameter '{}'.", value, key);
}
return uvalue;
}
void write_to_debug_log(params_t const ¶ms, char const *message)
{
if (!get_logger().debug_enabled()) {
return;
}
log_debug("{}", message);
for (auto const &[key, value] : params) {
log_debug(" {}={}", key, to_string(value));
}
}