Skip to content

Commit

Permalink
fix: replace invoke tag with elide for latest msstl
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Nov 30, 2024
1 parent 56734ea commit 189423d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
7 changes: 0 additions & 7 deletions src/ll/api/base/CompilerPredefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,6 @@ template <MemFuncPtrT T, T f>
consteval bool virtualDetector() noexcept {
return reflection::getRawName<f>().find("::`vcall'{") != std::string_view::npos;
}
#if LL_HAS_CXX23
using stdOptionalConstructFromInvokeTag = std::_Construct_from_invoke_result_tag;
#endif

using FileHandleT = void*;

Expand Down Expand Up @@ -326,10 +323,6 @@ namespace ll::internal {

[[nodiscard]] void* getCurrentModuleHandle() noexcept; // Implemented in SystemUtils_linux.cpp

#if LL_HAS_CXX23
using stdOptionalConstructFromInvokeTag = std::__optional_construct_from_invoke_tag;
#endif

using FileHandleT = int;

template <class T, T v1, T v2, class = std::integral_constant<bool, true>>
Expand Down
51 changes: 51 additions & 0 deletions src/ll/api/base/Meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,55 @@ template <class Group, class T, auto Id = int64{}>
return Id;
}
}

template <typename FR, typename... PRs>
requires std::is_reference_v<FR> && (std::is_reference_v<PRs> && ...)
class elide {
using R = std::invoke_result_t<FR, PRs...>;
static_assert(!std::is_reference_v<R>, "F must return by value");

inline static constexpr bool excepts = noexcept(std::invoke(std::declval<FR>(), std::declval<PRs>()...));

static constexpr decltype(auto) to_invokable(FR f, PRs... args) noexcept {
using F = std::remove_reference_t<FR>;
using DF = std::remove_pointer_t<F>;
if constexpr (sizeof...(PRs)) {
return [&f, &args...]() noexcept(excepts) -> decltype(auto) {
return std::invoke(static_cast<FR>(f), static_cast<PRs>(args)...);
};
} else if constexpr (std::is_function_v<DF>) {
return static_cast<DF*>(f);
} else if constexpr (std::is_trivial_v<F> && sizeof(F) <= sizeof(void*)) {
return static_cast<F>(f);
} else {
return static_cast<FR>(f);
}
}
using invokable_t = decltype(to_invokable(std::declval<FR>(), std::declval<PRs>()...));
invokable_t invokable;

public:
template <typename F, typename... Params>
constexpr explicit elide(F&& arg, Params&&... args) noexcept
: invokable(to_invokable(std::forward<F>(arg), std::forward<Params>(args)...)) {}

constexpr operator R() noexcept(excepts) {
return std::invoke(
static_cast<std::conditional_t<std::is_lvalue_reference_v<FR>, invokable_t&, invokable_t&&>>(invokable)
);
}
constexpr R operator()() noexcept(excepts) { return this->operator R(); }

constexpr elide() = delete;
constexpr elide(elide const&) = delete;
constexpr elide(elide&&) = delete;
elide& operator=(elide const&) = delete;
elide& operator=(elide&&) = delete;
elide const volatile* operator&() const volatile = delete;
template <typename U>
void operator,(U&&) = delete;
};
template <typename F, typename... Params>
elide(F&&, Params&&...) -> elide<F&&, Params&&...>;

} // namespace ll::meta
29 changes: 8 additions & 21 deletions src/ll/api/command/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdexcept>
#include <type_traits>

#include "ll/api/base/Macro.h"
#include "ll/api/base/Meta.h"

namespace ll::command {

Expand Down Expand Up @@ -109,7 +109,7 @@ class Optional {
}
return static_cast<T&&>(std::forward<U>(right));
}
#if LL_HAS_CXX23

template <class Fn>
constexpr auto and_then(Fn&& fn) & {
using Ret = std::invoke_result_t<Fn, T&>;
Expand Down Expand Up @@ -155,9 +155,8 @@ class Optional {
using Ret = std::remove_cv_t<std::invoke_result_t<Fn, T&>>;
if (has_value()) {
return std::optional<Ret>{
internal::stdOptionalConstructFromInvokeTag{},
std::forward<Fn>(fn),
static_cast<T&>(mValue)

meta::elide(std::forward<Fn>(fn), static_cast<T&>(mValue))
};
} else {
return std::optional<Ret>{};
Expand All @@ -168,11 +167,7 @@ class Optional {
constexpr auto transform(Fn&& fn) const& {
using Ret = std::remove_cv_t<std::invoke_result_t<Fn, T const&>>;
if (has_value()) {
return std::optional<Ret>{
internal::stdOptionalConstructFromInvokeTag{},
std::forward<Fn>(fn),
static_cast<T const&>(mValue)
};
return std::optional<Ret>{meta::elide(std::forward<Fn>(fn), static_cast<T const&>(mValue))};
} else {
return std::optional<Ret>{};
}
Expand All @@ -182,11 +177,7 @@ class Optional {
constexpr auto transform(Fn&& fn) && {
using Ret = std::remove_cv_t<std::invoke_result_t<Fn, T>>;
if (has_value()) {
return std::optional<Ret>{
internal::stdOptionalConstructFromInvokeTag{},
std::forward<Fn>(fn),
static_cast<T&&>(mValue)
};
return std::optional<Ret>{meta::elide(std::forward<Fn>(fn), static_cast<T&&>(mValue))};
} else {
return std::optional<Ret>{};
}
Expand All @@ -196,15 +187,12 @@ class Optional {
constexpr auto transform(Fn&& fn) const&& {
using Ret = std::remove_cv_t<std::invoke_result_t<Fn, T const>>;
if (has_value()) {
return std::optional<Ret>{
internal::stdOptionalConstructFromInvokeTag{},
std::forward<Fn>(fn),
static_cast<T const&&>(mValue)
};
return std::optional<Ret>{meta::elide(std::forward<Fn>(fn), static_cast<T const&&>(mValue))};
} else {
return std::optional<Ret>{};
}
}

template <std::invocable<> Fn>
requires std::copy_constructible<T>
constexpr std::optional<T> or_else(Fn&& fn) const& {
Expand All @@ -224,7 +212,6 @@ class Optional {
return std::invoke(std::forward<Fn>(fn));
}
}
#endif // LL_HAS_CXX23

[[nodiscard]] constexpr T const&& value_or_default() const&& { return std::move(value()); }
[[nodiscard]] constexpr T&& value_or_default() && { return std::move(value()); }
Expand Down
10 changes: 2 additions & 8 deletions src/mc/deps/core/utility/optional_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdexcept>
#include <type_traits>

#include "ll/api/base/Macro.h"
#include "ll/api/base/Meta.h"

template <typename T>
requires(!std::is_reference_v<T>)
Expand Down Expand Up @@ -115,7 +115,6 @@ class optional_ref {
[[nodiscard]] constexpr decltype(auto) crend() const { return (value().crend()); }
[[nodiscard]] constexpr decltype(auto) crbegin() const { return (value().crbegin()); }

#if LL_HAS_CXX23
template <class Fn>
constexpr auto and_then(Fn&& fn) const {
using Ret = std::invoke_result_t<Fn, T&>;
Expand All @@ -138,11 +137,7 @@ class optional_ref {
} else {
using UnwrapT = std::remove_cv_t<Ret>;
if (has_value()) {
return std::optional<UnwrapT>{
ll::internal::stdOptionalConstructFromInvokeTag{},
std::forward<Fn>(fn),
static_cast<T&>(*mPtr)
};
return std::optional<UnwrapT>{ll::meta::elide(std::forward<Fn>(fn), static_cast<T&>(*mPtr))};
} else {
return std::optional<UnwrapT>{};
}
Expand All @@ -156,7 +151,6 @@ class optional_ref {
return std::invoke(std::forward<Fn>(fn));
}
}
#endif // LL_HAS_CXX23
};
// NOLINTEND
template <typename T>
Expand Down

0 comments on commit 189423d

Please sign in to comment.