From d3d1445264e084aaf08e24d2b661682b62c64237 Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 26 Sep 2025 08:12:16 +0200 Subject: [PATCH] refactor(DirectiveValue): simplify toString implementation using visitor overload pattern --- webserv/config/directive/DirectiveValue.cpp | 106 ++++++++++++-------- webserv/config/directive/DirectiveValue.hpp | 11 ++ 2 files changed, 77 insertions(+), 40 deletions(-) diff --git a/webserv/config/directive/DirectiveValue.cpp b/webserv/config/directive/DirectiveValue.cpp index 9566bde..eec67bf 100644 --- a/webserv/config/directive/DirectiveValue.cpp +++ b/webserv/config/directive/DirectiveValue.cpp @@ -7,49 +7,75 @@ #include // for pair, move #include // for variant, visit #include // for vector + // + +// std::string DirectiveValue::toString() const +// { +// return std::visit( +// [](const auto &val) -> std::string { +// using T = std::decay_t; + +// if constexpr (std::is_same_v || std::is_same_v) +// { +// return std::to_string(val); +// } +// else if constexpr (std::is_same_v) +// { +// return val; +// } +// else if constexpr (std::is_same_v) +// { +// return val ? "true" : "false"; +// } +// else if constexpr (std::is_same_v>) +// { +// 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>) +// { +// return std::to_string(val.first) + " \"" + val.second + "\""; +// } +// else +// { +// return ""; +// } +// }, +// value_); +// } std::string DirectiveValue::toString() const { - return std::visit( - [](const auto &val) -> std::string { - using T = std::decay_t; - - if constexpr (std::is_same_v || std::is_same_v) - { - return std::to_string(val); - } - else if constexpr (std::is_same_v) - { - return val; - } - else if constexpr (std::is_same_v) - { - return val ? "true" : "false"; - } - else if constexpr (std::is_same_v>) - { - 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>) - { - return std::to_string(val.first) + " \"" + val.second + "\""; - } - else - { - return ""; - } - }, - 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 &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 &val) { + return std::to_string(val.first) + " \"" + val.second + "\""; + }}, + value_); } std::ostream &operator<<(std::ostream &os, const ADirective &directive) diff --git a/webserv/config/directive/DirectiveValue.hpp b/webserv/config/directive/DirectiveValue.hpp index a2f97d5..383f402 100644 --- a/webserv/config/directive/DirectiveValue.hpp +++ b/webserv/config/directive/DirectiveValue.hpp @@ -7,6 +7,17 @@ #include // for vector #include // for pair, move +// Visitor overload pattern for std::visit +template +struct overloaded : Ts... { + using Ts::operator()...; +}; + +template +overloaded(Ts...) -> overloaded; + + + // Define all possible directive value types using DirectiveValueType = std::variant