refactor(DirectiveValue): simplify toString implementation using visitor overload pattern

This commit is contained in:
whaffman 2025-09-26 08:12:16 +02:00
parent 17b69afd2c
commit d3d1445264
2 changed files with 77 additions and 40 deletions

View File

@ -7,49 +7,75 @@
#include <utility> // for pair, move #include <utility> // for pair, move
#include <variant> // for variant, visit #include <variant> // for variant, visit
#include <vector> // for vector #include <vector> // for vector
//
// std::string DirectiveValue::toString() const
// {
// return std::visit(
// [](const auto &val) -> std::string {
// using T = std::decay_t<decltype(val)>;
// if constexpr (std::is_same_v<T, int> || std::is_same_v<T, size_t>)
// {
// return std::to_string(val);
// }
// else if constexpr (std::is_same_v<T, std::string>)
// {
// return val;
// }
// else if constexpr (std::is_same_v<T, bool>)
// {
// return val ? "true" : "false";
// }
// else if constexpr (std::is_same_v<T, std::vector<std::string>>)
// {
// std::string result = "[";
// for (size_t i = 0; i < val.size(); ++i)
// {
// if (i > 0)
// {
// result += ", ";
// }
// result += "\"" + val[i] + "\"";
// }
// result += "]";
// return result;
// }
// else if constexpr (std::is_same_v<T, std::pair<int, std::string>>)
// {
// return std::to_string(val.first) + " \"" + val.second + "\"";
// }
// else
// {
// return "<unknown>";
// }
// },
// value_);
// }
std::string DirectiveValue::toString() const std::string DirectiveValue::toString() const
{ {
return std::visit( return std::visit(overloaded{[](int val) { return std::to_string(val); },
[](const auto &val) -> std::string { [](size_t val) { return std::to_string(val); },
using T = std::decay_t<decltype(val)>; [](bool val) { return val ? std::string("true") : std::string("false"); },
[](const std::string &val) { return val; },
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, size_t>) [](const std::vector<std::string> &val) {
{ std::string result = "[";
return std::to_string(val); for (size_t i = 0; i < val.size(); ++i)
} {
else if constexpr (std::is_same_v<T, std::string>) if (i > 0)
{ {
return val; result += ", ";
} }
else if constexpr (std::is_same_v<T, bool>) result += "\"" + val[i] + "\"";
{ }
return val ? "true" : "false"; result += "]";
} return result;
else if constexpr (std::is_same_v<T, std::vector<std::string>>) },
{ [](const std::pair<int, std::string> &val) {
std::string result = "["; return std::to_string(val.first) + " \"" + val.second + "\"";
for (size_t i = 0; i < val.size(); ++i) }},
{ value_);
if (i > 0)
{
result += ", ";
}
result += "\"" + val[i] + "\"";
}
result += "]";
return result;
}
else if constexpr (std::is_same_v<T, std::pair<int, std::string>>)
{
return std::to_string(val.first) + " \"" + val.second + "\"";
}
else
{
return "<unknown>";
}
},
value_);
} }
std::ostream &operator<<(std::ostream &os, const ADirective &directive) std::ostream &operator<<(std::ostream &os, const ADirective &directive)

View File

@ -7,6 +7,17 @@
#include <vector> // for vector #include <vector> // for vector
#include <utility> // for pair, move #include <utility> // for pair, move
// Visitor overload pattern for std::visit
template<class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template<class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
// Define all possible directive value types // Define all possible directive value types
using DirectiveValueType = std::variant<int, // listen, error_page status, cgi_timeout using DirectiveValueType = std::variant<int, // listen, error_page status, cgi_timeout
size_t, size_t,