Skip to content

Commit

Permalink
Cleanup: code style
Browse files Browse the repository at this point in the history
  • Loading branch information
kduske committed Jan 31, 2023
1 parent 68c076a commit fde32d3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 64 deletions.
107 changes: 59 additions & 48 deletions lib/kdl/include/kdl/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class [[nodiscard]] result
variant_type m_value;

explicit result(variant_type&& v)
: m_value(std::move(v))
: m_value{std::move(v)}
{
}

Expand All @@ -128,8 +128,9 @@ class [[nodiscard]] result
typename std::enable_if<std::disjunction_v<
std::is_convertible<T, Value>,
std::is_convertible<T, Errors>...>>::type* = nullptr>
// NOLINTNEXTLINE
result(T&& v)
: m_value(std::forward<T>(v))
: m_value{std::forward<T>(v)}
{
}

Expand All @@ -143,14 +144,15 @@ class [[nodiscard]] result
* @param other the result to convert
*/
template <typename... ErrorSubset>
// NOLINTNEXTLINE
result(result<Value, ErrorSubset...> other)
{
static_assert(
meta_is_subset<meta_type_list<ErrorSubset...>, meta_type_list<Errors...>>::value,
"Error types of result type to convert must be a subset of target result type");
std::move(other).visit(overload(
[&](Value&& v) { m_value = std::move(v); },
[&](auto&& e) { m_value = std::move(e); }));
[&](auto&& e) { m_value = std::forward<decltype(e)>(e); }));
}

public:
Expand Down Expand Up @@ -291,20 +293,20 @@ class [[nodiscard]] result
return visit(kdl::overload(
[&](const value_type& v) {
return f(v).visit(kdl::overload(
[]() { return Cm_Result(); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[]() { return Cm_Result{}; },
[](auto&& fn_e) { return Cm_Result{std::forward<decltype(fn_e)>(fn_e)}; }));
},
[](const auto& e) { return Cm_Result(e); }));
[](const auto& e) { return Cm_Result{e}; }));
}
else
{
return visit(kdl::overload(
[&](const value_type& v) {
return f(v).visit(kdl::overload(
[](Fn_Value&& fn_v) { return Cm_Result(std::move(fn_v)); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[](Fn_Value&& fn_v) { return Cm_Result{std::move(fn_v)}; },
[](auto&& fn_e) { return Cm_Result{std::forward<decltype(fn_e)>(fn_e)}; }));
},
[](const auto& e) { return Cm_Result(e); }));
[](const auto& e) { return Cm_Result{e}; }));
}
}
else
Expand Down Expand Up @@ -350,21 +352,25 @@ class [[nodiscard]] result
[&](value_type&& v) {
return f(std::move(v))
.visit(kdl::overload(
[]() { return Cm_Result(); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[]() { return Cm_Result{}; },
[](auto&& fn_e) {
return Cm_Result{std::forward<decltype(fn_e)>(fn_e)};
}));
},
[](auto&& e) { return Cm_Result(e); }));
[](auto&& e) { return Cm_Result{e}; }));
}
else
{
return std::move(*this).visit(kdl::overload(
[&](value_type&& v) {
return f(std::move(v))
.visit(kdl::overload(
[](Fn_Value&& fn_v) { return Cm_Result(std::move(fn_v)); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[](Fn_Value&& fn_v) { return Cm_Result{std::move(fn_v)}; },
[](auto&& fn_e) {
return Cm_Result{std::forward<decltype(fn_e)>(fn_e)};
}));
},
[](auto&& e) { return Cm_Result(e); }));
[](auto&& e) { return Cm_Result{e}; }));
}
}
else
Expand All @@ -378,13 +384,13 @@ class [[nodiscard]] result
f(std::move(v));
return Cm_Result{};
},
[](auto&& e) { return Cm_Result{std::move(e)}; }));
[](auto&& e) { return Cm_Result{std::forward<decltype(e)>(e)}; }));
}
else
{
return std::move(*this).visit(kdl::overload(
[&](value_type&& v) { return Cm_Result{f(std::move(v))}; },
[](auto&& e) { return Cm_Result{std::move(e)}; }));
[](auto&& e) { return Cm_Result{std::forward<decltype(e)>(e)}; }));
}
}
}
Expand Down Expand Up @@ -429,7 +435,7 @@ class [[nodiscard]] result

return std::move(*this).visit(kdl::overload(
[&](value_type&& v) { return f_result_type{std::move(v)}; },
[&](auto&& e) { return f(std::move(e)); }));
[&](auto&& e) { return f(std::forward<decltype(e)>(e)); }));
}

/**
Expand Down Expand Up @@ -492,7 +498,7 @@ class [[nodiscard]] result
return std::move(*this).visit(kdl::overload(
[](const value_type&) { return true; },
[&](auto&& error) {
f(std::move(error));
f(std::forward<decltype(error)>(error));
return false;
}));
}
Expand Down Expand Up @@ -532,7 +538,7 @@ class [[nodiscard]] result
{
return visit(kdl::overload(
[](const value_type& v) -> value_type { return v; },
[](const auto&) -> value_type { throw bad_result_access(); }));
[](const auto&) -> value_type { throw bad_result_access{}; }));
}

/**
Expand All @@ -547,7 +553,7 @@ class [[nodiscard]] result
{
return std::move(*this).visit(kdl::overload(
[](value_type&& v) -> value_type { return std::move(v); },
[](const auto&) -> value_type { throw bad_result_access(); }));
[](const auto&) -> value_type { throw bad_result_access{}; }));
}

/**
Expand All @@ -561,7 +567,7 @@ class [[nodiscard]] result
auto error() const&
{
return visit(kdl::overload(
[](const value_type&) -> std::variant<Errors...> { throw bad_result_access(); },
[](const value_type&) -> std::variant<Errors...> { throw bad_result_access{}; },
[](const auto& e) -> std::variant<Errors...> { return e; }));
}

Expand All @@ -576,8 +582,8 @@ class [[nodiscard]] result
auto error() &&
{
return visit(kdl::overload(
[](const value_type&) -> std::variant<Errors...> { throw bad_result_access(); },
[](auto&& e) -> std::variant<Errors...> { return std::move(e); }));
[](const value_type&) -> std::variant<Errors...> { throw bad_result_access{}; },
[](auto&& e) -> std::variant<Errors...> { return std::forward<decltype(e)>(e); }));
}

/**
Expand All @@ -604,6 +610,7 @@ class [[nodiscard]] result
/**
* Indicates whether the given result contains a value.
*/
// NOLINTNEXTLINE
operator bool() const { return is_success(); }

friend bool operator==(const result& lhs, const result& rhs)
Expand Down Expand Up @@ -654,7 +661,7 @@ class [[nodiscard]] result<void, Errors...>
variant_type m_value;

explicit result(variant_type&& v)
: m_value(std::move(v))
: m_value{std::move(v)}
{
}

Expand Down Expand Up @@ -683,8 +690,9 @@ class [[nodiscard]] result<void, Errors...>
typename std::enable_if<std::disjunction_v<
std::is_convertible<T, value_type>,
std::is_convertible<T, Errors>...>>::type* = nullptr>
// NOLINTNEXTLINE
result(T&& v)
: m_value(std::forward<T>(v))
: m_value{std::forward<T>(v)}
{
}

Expand All @@ -698,14 +706,15 @@ class [[nodiscard]] result<void, Errors...>
* @param other the result to convert
*/
template <typename... ErrorSubset>
// NOLINTNEXTLINE
result(result<void, ErrorSubset...> other)
{
static_assert(
meta_is_subset<meta_type_list<ErrorSubset...>, meta_type_list<Errors...>>::value,
"Error types of result type to convert must be a subset of target result type");
std::move(other).visit(overload(
[&]() { m_value = detail::void_success_value_type{}; },
[&](auto&& e) { m_value = std::move(e); }));
[&](auto&& e) { m_value = std::forward<decltype(e)>(e); }));
}

public:
Expand Down Expand Up @@ -749,7 +758,7 @@ class [[nodiscard]] result<void, Errors...>
return std::visit(
kdl::overload(
[&](detail::void_success_value_type&&) { return visitor(); },
[&](auto&& e) { return visitor(std::move(e)); }),
[&](auto&& e) { return visitor(std::forward<decltype(e)>(e)); }),
std::move(m_value));
}

Expand All @@ -772,20 +781,20 @@ class [[nodiscard]] result<void, Errors...>
return visit(kdl::overload(
[&]() {
return f().visit(kdl::overload(
[]() { return Cm_Result(); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[]() { return Cm_Result{}; },
[](auto&& fn_e) { return Cm_Result{std::forward<decltype(fn_e)>(fn_e)}; }));
},
[](const auto& e) { return Cm_Result(e); }));
[](const auto& e) { return Cm_Result{e}; }));
}
else
{
return visit(kdl::overload(
[&]() {
return f().visit(kdl::overload(
[](Fn_Value&& fn_v) { return Cm_Result(std::move(fn_v)); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[](Fn_Value&& fn_v) { return Cm_Result{std::move(fn_v)}; },
[](auto&& fn_e) { return Cm_Result(std::forward<decltype(fn_e)>(fn_e)); }));
},
[](const auto& e) { return Cm_Result(e); }));
[](const auto& e) { return Cm_Result{e}; }));
}
}
else
Expand Down Expand Up @@ -828,20 +837,20 @@ class [[nodiscard]] result<void, Errors...>
return std::move(*this).visit(kdl::overload(
[&]() {
return f().visit(kdl::overload(
[]() { return Cm_Result(); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[]() { return Cm_Result{}; },
[](auto&& fn_e) { return Cm_Result{std::forward<decltype(fn_e)>(fn_e)}; }));
},
[](auto&& e) { return Cm_Result(e); }));
[](auto&& e) { return Cm_Result{e}; }));
}
else
{
return std::move(*this).visit(kdl::overload(
[&]() {
return f().visit(kdl::overload(
[](Fn_Value&& fn_v) { return Cm_Result(std::move(fn_v)); },
[](auto&& fn_e) { return Cm_Result(std::move(fn_e)); }));
[](Fn_Value&& fn_v) { return Cm_Result{std::move(fn_v)}; },
[](auto&& fn_e) { return Cm_Result{std::forward<decltype(fn_e)>(fn_e)}; }));
},
[](auto&& e) { return Cm_Result(e); }));
[](auto&& e) { return Cm_Result{e}; }));
}
}
else
Expand All @@ -855,13 +864,13 @@ class [[nodiscard]] result<void, Errors...>
f();
return Cm_Result{};
},
[](auto&& e) { return Cm_Result{std::move(e)}; }));
[](auto&& e) { return Cm_Result{std::forward<decltype(e)>(e)}; }));
}
else
{
return std::move(*this).visit(kdl::overload(
[&]() { return Cm_Result{f()}; },
[](auto&& e) { return Cm_Result{std::move(e)}; }));
[](auto&& e) { return Cm_Result{std::forward<decltype(e)>(e)}; }));
}
}
}
Expand All @@ -882,7 +891,8 @@ class [[nodiscard]] result<void, Errors...>
"Function must return a result type with matching value type");

return std::move(*this).visit(kdl::overload(
[&]() { return f_result_type{}; }, [&](auto&& e) { return f(std::move(e)); }));
[&]() { return f_result_type{}; },
[&](auto&& e) { return f(std::forward<decltype(e)>(e)); }));
}

/**
Expand Down Expand Up @@ -913,7 +923,7 @@ class [[nodiscard]] result<void, Errors...>
return std::move(*this).visit(kdl::overload(
[]() { return true; },
[&](auto&& error) {
f(std::move(error));
f(std::forward<decltype(error)>(error));
return false;
}));
}
Expand Down Expand Up @@ -943,7 +953,7 @@ class [[nodiscard]] result<void, Errors...>
auto error() const&
{
return visit(kdl::overload(
[]() -> std::variant<Errors...> { throw bad_result_access(); },
[]() -> std::variant<Errors...> { throw bad_result_access{}; },
[](const auto& e) -> std::variant<Errors...> { return e; }));
}

Expand All @@ -958,8 +968,8 @@ class [[nodiscard]] result<void, Errors...>
auto error() &&
{
return visit(kdl::overload(
[]() -> std::variant<Errors...> { throw bad_result_access(); },
[](auto&& e) -> std::variant<Errors...> { return std::move(e); }));
[]() -> std::variant<Errors...> { throw bad_result_access{}; },
[](auto&& e) -> std::variant<Errors...> { return std::forward<decltype(e)>(e); }));
}

/**
Expand Down Expand Up @@ -989,6 +999,7 @@ class [[nodiscard]] result<void, Errors...>
/**
* Indicates whether this result is empty.
*/
// NOLINTNEXTLINE
operator bool() const { return is_success(); }

friend bool operator==(const result& lhs, const result& rhs)
Expand All @@ -999,5 +1010,5 @@ class [[nodiscard]] result<void, Errors...>
friend bool operator!=(const result& lhs, const result& rhs) { return !(lhs == rhs); }
};

constexpr auto void_success = kdl::result<void>();
constexpr auto void_success = kdl::result<void>{};
} // namespace kdl
Loading

0 comments on commit fde32d3

Please sign in to comment.