From 0ac9189543c0cabc11d91336406f311bb49ed5b4 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Thu, 12 Dec 2024 10:33:42 +0100 Subject: [PATCH] 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. --- src/format.hpp | 1 + src/gen/gen-base.cpp | 6 +++--- src/gen/params.cpp | 19 +++++-------------- src/gen/params.hpp | 6 +----- 4 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/format.hpp b/src/format.hpp index 3d4a25f27..de5c20739 100644 --- a/src/format.hpp +++ b/src/format.hpp @@ -12,6 +12,7 @@ #define FMT_HEADER_ONLY #include +#include #include diff --git a/src/gen/gen-base.cpp b/src/gen/gen-base.cpp index f71c13f9b..2dc7631d9 100644 --- a/src/gen/gen-base.cpp +++ b/src/gen/gen-base.cpp @@ -105,7 +105,7 @@ pg_result_t gen_base_t::dbexec(std::string const &templ) { fmt::dynamic_format_arg_store format_store; for (auto const &[key, value] : get_params()) { - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); + format_store.push_back(fmt::arg(key.c_str(), value)); } return dbexec_internal(connection(), templ, format_store); } @@ -115,10 +115,10 @@ pg_result_t gen_base_t::dbexec(params_t const &tmp_params, { fmt::dynamic_format_arg_store format_store; for (auto const &[key, value] : get_params()) { - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); + format_store.push_back(fmt::arg(key.c_str(), value)); } for (auto const &[key, value] : tmp_params) { - format_store.push_back(fmt::arg(key.c_str(), to_string(value))); + format_store.push_back(fmt::arg(key.c_str(), value)); } return dbexec_internal(connection(), templ, format_store); } diff --git a/src/gen/params.cpp b/src/gen/params.cpp index 1806981cc..f22f836ee 100644 --- a/src/gen/params.cpp +++ b/src/gen/params.cpp @@ -14,15 +14,6 @@ #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); @@ -58,7 +49,7 @@ double params_t::get_double(std::string const &key, double default_value) const return static_cast(std::get(it->second)); } - throw fmt_error("Invalid value '{}' for {}.", to_string(it->second), key); + throw fmt_error("Invalid value '{}' for {}.", it->second, key); } 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 if (it == m_map.end()) { throw fmt_error("Missing parameter '{}' on generalizer.", key); } - return to_string(it->second); + return fmt::format("{}", it->second); } 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 if (it == m_map.end()) { return {}; } - std::string result = to_string(it->second); + std::string result = fmt::format("{}", it->second); check_identifier(result, key.c_str()); return result; } @@ -94,7 +85,7 @@ void params_t::check_identifier_with_default(std::string const &key, if (it == m_map.end()) { m_map.emplace(key, std::move(default_value)); } else { - check_identifier(to_string(it->second), key.c_str()); + check_identifier(fmt::format("{}", it->second), key.c_str()); } } @@ -120,6 +111,6 @@ void write_to_debug_log(params_t const ¶ms, char const *message) } log_debug("{}", message); for (auto const &[key, value] : params) { - log_debug(" {}={}", key, to_string(value)); + log_debug(" {}={}", key, value); } } diff --git a/src/gen/params.hpp b/src/gen/params.hpp index 174465820..51da49d1a 100644 --- a/src/gen/params.hpp +++ b/src/gen/params.hpp @@ -24,9 +24,6 @@ using null_param_t = std::monostate; using param_value_t = std::variant; -/// Convert a parameter value into a string. -std::string to_string(param_value_t const &value); - /** * A collection of parameters. */ @@ -79,8 +76,7 @@ class params_t } if (!std::holds_alternative(it->second)) { - throw fmt_error("Invalid value '{}' for {}.", to_string(it->second), - key); + throw fmt_error("Invalid value '{}' for {}.", it->second, key); } return std::get(it->second); }