Skip to content

Commit 0ac9189

Browse files
committed
Refactor: Use fmt builtin functionality to format variants
When including the fmt/std.h header the fmt library has support for formatting std::variant. So we don't need to do this ourselves.
1 parent cc0ea60 commit 0ac9189

File tree

4 files changed

+10
-22
lines changed

4 files changed

+10
-22
lines changed

src/format.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#define FMT_HEADER_ONLY
1414
#include <fmt/format.h>
15+
#include <fmt/std.h>
1516

1617
#include <stdexcept>
1718

src/gen/gen-base.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pg_result_t gen_base_t::dbexec(std::string const &templ)
105105
{
106106
fmt::dynamic_format_arg_store<fmt::format_context> format_store;
107107
for (auto const &[key, value] : get_params()) {
108-
format_store.push_back(fmt::arg(key.c_str(), to_string(value)));
108+
format_store.push_back(fmt::arg(key.c_str(), value));
109109
}
110110
return dbexec_internal(connection(), templ, format_store);
111111
}
@@ -115,10 +115,10 @@ pg_result_t gen_base_t::dbexec(params_t const &tmp_params,
115115
{
116116
fmt::dynamic_format_arg_store<fmt::format_context> format_store;
117117
for (auto const &[key, value] : get_params()) {
118-
format_store.push_back(fmt::arg(key.c_str(), to_string(value)));
118+
format_store.push_back(fmt::arg(key.c_str(), value));
119119
}
120120
for (auto const &[key, value] : tmp_params) {
121-
format_store.push_back(fmt::arg(key.c_str(), to_string(value)));
121+
format_store.push_back(fmt::arg(key.c_str(), value));
122122
}
123123
return dbexec_internal(connection(), templ, format_store);
124124
}

src/gen/params.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
#include "overloaded.hpp"
1515
#include "pgsql.hpp"
1616

17-
std::string to_string(param_value_t const &value)
18-
{
19-
return std::visit(
20-
overloaded{[](null_param_t) { return std::string{}; },
21-
[](std::string val) { return val; },
22-
[](auto const &val) { return fmt::to_string(val); }},
23-
value);
24-
}
25-
2617
param_value_t params_t::get(std::string const &key) const
2718
{
2819
return m_map.at(key);
@@ -58,7 +49,7 @@ double params_t::get_double(std::string const &key, double default_value) const
5849
return static_cast<double>(std::get<int64_t>(it->second));
5950
}
6051

61-
throw fmt_error("Invalid value '{}' for {}.", to_string(it->second), key);
52+
throw fmt_error("Invalid value '{}' for {}.", it->second, key);
6253
}
6354

6455
std::string params_t::get_string(std::string const &key) const
@@ -67,7 +58,7 @@ std::string params_t::get_string(std::string const &key) const
6758
if (it == m_map.end()) {
6859
throw fmt_error("Missing parameter '{}' on generalizer.", key);
6960
}
70-
return to_string(it->second);
61+
return fmt::format("{}", it->second);
7162
}
7263

7364
std::string params_t::get_string(std::string const &key,
@@ -82,7 +73,7 @@ std::string params_t::get_identifier(std::string const &key) const
8273
if (it == m_map.end()) {
8374
return {};
8475
}
85-
std::string result = to_string(it->second);
76+
std::string result = fmt::format("{}", it->second);
8677
check_identifier(result, key.c_str());
8778
return result;
8879
}
@@ -94,7 +85,7 @@ void params_t::check_identifier_with_default(std::string const &key,
9485
if (it == m_map.end()) {
9586
m_map.emplace(key, std::move(default_value));
9687
} else {
97-
check_identifier(to_string(it->second), key.c_str());
88+
check_identifier(fmt::format("{}", it->second), key.c_str());
9889
}
9990
}
10091

@@ -120,6 +111,6 @@ void write_to_debug_log(params_t const &params, char const *message)
120111
}
121112
log_debug("{}", message);
122113
for (auto const &[key, value] : params) {
123-
log_debug(" {}={}", key, to_string(value));
114+
log_debug(" {}={}", key, value);
124115
}
125116
}

src/gen/params.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ using null_param_t = std::monostate;
2424
using param_value_t =
2525
std::variant<null_param_t, std::string, int64_t, double, bool>;
2626

27-
/// Convert a parameter value into a string.
28-
std::string to_string(param_value_t const &value);
29-
3027
/**
3128
* A collection of parameters.
3229
*/
@@ -79,8 +76,7 @@ class params_t
7976
}
8077

8178
if (!std::holds_alternative<T>(it->second)) {
82-
throw fmt_error("Invalid value '{}' for {}.", to_string(it->second),
83-
key);
79+
throw fmt_error("Invalid value '{}' for {}.", it->second, key);
8480
}
8581
return std::get<T>(it->second);
8682
}

0 commit comments

Comments
 (0)