refactor(directive value): simplify value retrieval with try_get method
This commit is contained in:
parent
a8b71747bb
commit
abb832a380
@ -76,10 +76,10 @@ std::string AConfig::getErrorPage(int statusCode) const
|
||||
{
|
||||
if (directive->getName() == "error_page")
|
||||
{
|
||||
auto value = directive->getValueAs<std::pair<int, std::string>>();
|
||||
if (value.first == statusCode)
|
||||
auto value = directive->getValue().try_get<std::pair<int, std::string>>();
|
||||
if (value && value->first == statusCode)
|
||||
{
|
||||
return value.second;
|
||||
return value->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,13 +33,7 @@ class AConfig
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto value = directive->getValue();
|
||||
if (value.holds<T>())
|
||||
{
|
||||
return value.get<T>();
|
||||
}
|
||||
return std::nullopt;
|
||||
return directive->getValue().try_get<T>();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
@ -28,14 +28,10 @@ class ADirective
|
||||
[[nodiscard]] std::string getName() const;
|
||||
|
||||
// [[nodiscard]] std::string toString() const;
|
||||
template <typename T> [[nodiscard]] T getValueAs() const
|
||||
{
|
||||
if (getValue().holds<T>())
|
||||
{
|
||||
return getValue().get<T>();
|
||||
}
|
||||
return T(); // TODO: does this work for all types?
|
||||
}
|
||||
// template <typename T> [[nodiscard]] T getValueAs() const
|
||||
// {
|
||||
// return getValue().try_get<T>();
|
||||
// }
|
||||
|
||||
protected:
|
||||
std::string name_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
|
||||
|
||||
@ -6,52 +6,6 @@
|
||||
#include <variant> // for 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(overloaded{[](int val) { return std::to_string(val); },
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstddef> // for size_t
|
||||
#include <iostream> // for ostream, operator<<
|
||||
#include <optional> // for optional, nullopt
|
||||
#include <string> // for string, basic_string, char_traits, to_string
|
||||
#include <utility> // for pair, move
|
||||
#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> 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_); }
|
||||
|
||||
[[nodiscard]] std::string toString() const;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user