diff --git a/webserv/config/AConfig.cpp b/webserv/config/AConfig.cpp index 6336aca..daf7622 100644 --- a/webserv/config/AConfig.cpp +++ b/webserv/config/AConfig.cpp @@ -76,10 +76,10 @@ std::string AConfig::getErrorPage(int statusCode) const { if (directive->getName() == "error_page") { - auto value = directive->getValueAs>(); - if (value.first == statusCode) + auto value = directive->getValue().try_get>(); + if (value && value->first == statusCode) { - return value.second; + return value->second; } } } diff --git a/webserv/config/AConfig.hpp b/webserv/config/AConfig.hpp index d4ac8d2..4895a91 100644 --- a/webserv/config/AConfig.hpp +++ b/webserv/config/AConfig.hpp @@ -33,13 +33,7 @@ class AConfig { return std::nullopt; } - - auto value = directive->getValue(); - if (value.holds()) - { - return value.get(); - } - return std::nullopt; + return directive->getValue().try_get(); } protected: diff --git a/webserv/config/directive/ADirective.hpp b/webserv/config/directive/ADirective.hpp index abc7d55..85129d0 100644 --- a/webserv/config/directive/ADirective.hpp +++ b/webserv/config/directive/ADirective.hpp @@ -28,14 +28,10 @@ class ADirective [[nodiscard]] std::string getName() const; // [[nodiscard]] std::string toString() const; - template [[nodiscard]] T getValueAs() const - { - if (getValue().holds()) - { - return getValue().get(); - } - return T(); // TODO: does this work for all types? - } + // template [[nodiscard]] T getValueAs() const + // { + // return getValue().try_get(); + // } protected: std::string name_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) diff --git a/webserv/config/directive/DirectiveValue.cpp b/webserv/config/directive/DirectiveValue.cpp index aced0c8..9abeadc 100644 --- a/webserv/config/directive/DirectiveValue.cpp +++ b/webserv/config/directive/DirectiveValue.cpp @@ -6,52 +6,6 @@ #include // for 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(overloaded{[](int val) { return std::to_string(val); }, diff --git a/webserv/config/directive/DirectiveValue.hpp b/webserv/config/directive/DirectiveValue.hpp index bedd5b6..e7c7b1a 100644 --- a/webserv/config/directive/DirectiveValue.hpp +++ b/webserv/config/directive/DirectiveValue.hpp @@ -2,6 +2,7 @@ #include // for size_t #include // for ostream, operator<< +#include // for optional, nullopt #include // for string, basic_string, char_traits, to_string #include // for pair, move #include // for variant, visit, get, holds_alternative @@ -31,6 +32,15 @@ class DirectiveValue template T get() const { return std::get(value_); } + template std::optional try_get() const + { + if (std::holds_alternative(value_)) + { + return std::get(value_); + } + return std::nullopt; + } + template [[nodiscard]] bool holds() const { return std::holds_alternative(value_); } [[nodiscard]] std::string toString() const;