From 25c5a64d39daa362be21a35de13f9cd440df99c4 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 19 Nov 2024 18:17:41 +0000 Subject: [PATCH] Codechange: Use std::visit instead of std::get_if for string parameters. (#13100) It's tidier and ensures all cases are handled, and doesn't use pointers. --- src/strings_internal.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/strings_internal.h b/src/strings_internal.h index d4f52c00e4..e56f2b8cea 100644 --- a/src/strings_internal.h +++ b/src/strings_internal.h @@ -91,10 +91,13 @@ public: template T GetNextParameter() { + struct visitor { + uint64_t operator()(const uint64_t &arg) { return arg; } + uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); } + }; + const auto ¶m = GetNextParameterReference(); - const uint64_t *data = std::get_if(¶m.data); - if (data != nullptr) return static_cast(*data); - throw std::out_of_range("Attempt to read string parameter as integer"); + return static_cast(std::visit(visitor{}, param.data)); } /** @@ -105,10 +108,13 @@ public: */ const char *GetNextParameterString() { + struct visitor { + const char *operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); } + const char *operator()(const std::string &arg) { return arg.c_str(); } + }; + const auto ¶m = GetNextParameterReference(); - const std::string *data = std::get_if(¶m.data); - if (data != nullptr) return data->c_str(); - throw std::out_of_range("Attempt to read integer parameter as string"); + return std::visit(visitor{}, param.data); } /**