refactor(DirectiveValue): simplify toString implementation using visitor overload pattern
This commit is contained in:
parent
17b69afd2c
commit
d3d1445264
@ -7,49 +7,75 @@
|
||||
#include <utility> // for pair, move
|
||||
#include <variant> // for variant, visit
|
||||
#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
|
||||
{
|
||||
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_);
|
||||
return std::visit(overloaded{[](int val) { return std::to_string(val); },
|
||||
[](size_t val) { return std::to_string(val); },
|
||||
[](bool val) { return val ? std::string("true") : std::string("false"); },
|
||||
[](const std::string &val) { return val; },
|
||||
[](const std::vector<std::string> &val) {
|
||||
std::string result = "[";
|
||||
for (size_t i = 0; i < val.size(); ++i)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
result += ", ";
|
||||
}
|
||||
result += "\"" + val[i] + "\"";
|
||||
}
|
||||
result += "]";
|
||||
return result;
|
||||
},
|
||||
[](const std::pair<int, std::string> &val) {
|
||||
return std::to_string(val.first) + " \"" + val.second + "\"";
|
||||
}},
|
||||
value_);
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const ADirective &directive)
|
||||
|
||||
@ -7,6 +7,17 @@
|
||||
#include <vector> // for vector
|
||||
#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
|
||||
using DirectiveValueType = std::variant<int, // listen, error_page status, cgi_timeout
|
||||
size_t,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user