refactor(directive value): simplify value retrieval with try_get method

This commit is contained in:
whaffman 2025-10-06 11:28:15 +02:00
parent a8b71747bb
commit abb832a380
5 changed files with 18 additions and 64 deletions

View File

@ -76,10 +76,10 @@ std::string AConfig::getErrorPage(int statusCode) const
{ {
if (directive->getName() == "error_page") if (directive->getName() == "error_page")
{ {
auto value = directive->getValueAs<std::pair<int, std::string>>(); auto value = directive->getValue().try_get<std::pair<int, std::string>>();
if (value.first == statusCode) if (value && value->first == statusCode)
{ {
return value.second; return value->second;
} }
} }
} }

View File

@ -33,13 +33,7 @@ class AConfig
{ {
return std::nullopt; return std::nullopt;
} }
return directive->getValue().try_get<T>();
auto value = directive->getValue();
if (value.holds<T>())
{
return value.get<T>();
}
return std::nullopt;
} }
protected: protected:

View File

@ -28,14 +28,10 @@ class ADirective
[[nodiscard]] std::string getName() const; [[nodiscard]] std::string getName() const;
// [[nodiscard]] std::string toString() const; // [[nodiscard]] std::string toString() const;
template <typename T> [[nodiscard]] T getValueAs() const // template <typename T> [[nodiscard]] T getValueAs() const
{ // {
if (getValue().holds<T>()) // return getValue().try_get<T>();
{ // }
return getValue().get<T>();
}
return T(); // TODO: does this work for all types?
}
protected: protected:
std::string name_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) std::string name_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)

View File

@ -6,52 +6,6 @@
#include <variant> // for visit #include <variant> // for 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(overloaded{[](int val) { return std::to_string(val); }, return std::visit(overloaded{[](int val) { return std::to_string(val); },

View File

@ -2,6 +2,7 @@
#include <cstddef> // for size_t #include <cstddef> // for size_t
#include <iostream> // for ostream, operator<< #include <iostream> // for ostream, operator<<
#include <optional> // for optional, nullopt
#include <string> // for string, basic_string, char_traits, to_string #include <string> // for string, basic_string, char_traits, to_string
#include <utility> // for pair, move #include <utility> // for pair, move
#include <variant> // for variant, visit, get, holds_alternative #include <variant> // for variant, visit, get, holds_alternative
@ -31,6 +32,15 @@ class DirectiveValue
template <typename T> T get() const { return std::get<T>(value_); } template <typename T> T get() const { return std::get<T>(value_); }
template <typename T> std::optional<T> try_get() const
{
if (std::holds_alternative<T>(value_))
{
return std::get<T>(value_);
}
return std::nullopt;
}
template <typename T> [[nodiscard]] bool holds() const { return std::holds_alternative<T>(value_); } template <typename T> [[nodiscard]] bool holds() const { return std::holds_alternative<T>(value_); }
[[nodiscard]] std::string toString() const; [[nodiscard]] std::string toString() const;