Skip to content

Commit

Permalink
Merge pull request #6418 from STEllAR-GROUP/fixing_6417
Browse files Browse the repository at this point in the history
Working around nvcc problems to use CTAD
  • Loading branch information
hkaiser authored Jan 15, 2024
2 parents cf70bf1 + f58d28c commit c0d79c8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,61 @@

namespace hpx::experimental {

template <typename F>
struct scope_exit
{
explicit constexpr scope_exit(F&& f) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(f))
{
}
namespace detail {

explicit constexpr scope_exit(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
template <typename F>
struct scope_exit
{
}
explicit constexpr scope_exit(F&& f) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(f))
{
}

constexpr scope_exit(scope_exit&& rhs) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(rhs.f))
, active(rhs.active)
{
rhs.release();
}
explicit constexpr scope_exit(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
{
}

scope_exit(scope_exit const&) = delete;
scope_exit& operator=(scope_exit const&) = delete;
scope_exit& operator=(scope_exit&& rhs) = delete;
constexpr scope_exit(scope_exit&& rhs) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(rhs.f))
, active(rhs.active)
{
rhs.release();
}

HPX_CONSTEXPR_DESTRUCTOR ~scope_exit() noexcept
{
if (active)
scope_exit(scope_exit const&) = delete;
scope_exit& operator=(scope_exit const&) = delete;
scope_exit& operator=(scope_exit&& rhs) = delete;

HPX_CONSTEXPR_DESTRUCTOR ~scope_exit() noexcept
{
f();
if (active)
{
f();
}
}
}

constexpr void release() noexcept
{
active = false;
}
constexpr void release() noexcept
{
active = false;
}

private:
F f;
bool active = true;
};
private:
F f;
bool active = true;
};
} // namespace detail

template <typename F>
scope_exit(F) -> scope_exit<F>;
auto scope_exit(F&& f)
{
return detail::scope_exit<std::decay_t<F>>(HPX_FORWARD(F, f));
}
} // namespace hpx::experimental

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,63 @@

namespace hpx::experimental {

template <typename F>
struct scope_fail
{
explicit constexpr scope_fail(F&& f) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(f))
, active(std::uncaught_exceptions())
{
}
namespace detail {

explicit constexpr scope_fail(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
, active(std::uncaught_exceptions())
template <typename F>
struct scope_fail
{
}
explicit constexpr scope_fail(F&& f) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(f))
, active(std::uncaught_exceptions())
{
}

constexpr scope_fail(scope_fail&& rhs) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(rhs.f))
, active(rhs.active)
{
rhs.release();
}
explicit constexpr scope_fail(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
, active(std::uncaught_exceptions())
{
}

scope_fail(scope_fail const&) = delete;
scope_fail& operator=(scope_fail const&) = delete;
scope_fail& operator=(scope_fail&& rhs) = delete;
constexpr scope_fail(scope_fail&& rhs) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(rhs.f))
, active(rhs.active)
{
rhs.release();
}

HPX_CONSTEXPR_DESTRUCTOR ~scope_fail() noexcept
{
if (active < std::uncaught_exceptions())
scope_fail(scope_fail const&) = delete;
scope_fail& operator=(scope_fail const&) = delete;
scope_fail& operator=(scope_fail&& rhs) = delete;

HPX_CONSTEXPR_DESTRUCTOR ~scope_fail() noexcept
{
f();
if (active < std::uncaught_exceptions())
{
f();
}
}
}

constexpr void release() noexcept
{
active = (std::numeric_limits<int>::max)();
}
constexpr void release() noexcept
{
active = (std::numeric_limits<int>::max)();
}

private:
F f;
int active;
};
private:
F f;
int active;
};
} // namespace detail

template <typename F>
scope_fail(F) -> scope_fail<F>;
auto scope_fail(F&& f)
{
return detail::scope_fail<std::decay_t<F>>(HPX_FORWARD(F, f));
}
} // namespace hpx::experimental

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,64 @@

namespace hpx::experimental {

template <typename F>
struct scope_success
{
explicit constexpr scope_success(F&& f) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(f))
, active(std::uncaught_exceptions())
{
}
namespace detail {

explicit constexpr scope_success(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
, active(std::uncaught_exceptions())
template <typename F>
struct scope_success
{
}
explicit constexpr scope_success(F&& f) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(f))
, active(std::uncaught_exceptions())
{
}

constexpr scope_success(scope_success&& rhs) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(rhs.f))
, active(rhs.active)
{
rhs.release();
}
explicit constexpr scope_success(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
, active(std::uncaught_exceptions())
{
}

scope_success(scope_success const&) = delete;
scope_success& operator=(scope_success const&) = delete;
scope_success& operator=(scope_success&& rhs) = delete;
constexpr scope_success(scope_success&& rhs) noexcept(
std::is_nothrow_move_constructible_v<F> ||
std::is_nothrow_copy_constructible_v<F>)
: f(HPX_MOVE(rhs.f))
, active(rhs.active)
{
rhs.release();
}

HPX_CONSTEXPR_DESTRUCTOR ~scope_success() noexcept(noexcept(this->f()))
{
if (active >= std::uncaught_exceptions())
scope_success(scope_success const&) = delete;
scope_success& operator=(scope_success const&) = delete;
scope_success& operator=(scope_success&& rhs) = delete;

HPX_CONSTEXPR_DESTRUCTOR ~scope_success() noexcept(
noexcept(this->f()))
{
f();
if (active >= std::uncaught_exceptions())
{
f();
}
}
}

constexpr void release() noexcept
{
active = -1;
}
constexpr void release() noexcept
{
active = -1;
}

private:
F f;
int active;
};
private:
F f;
int active;
};
} // namespace detail

template <typename F>
scope_success(F) -> scope_success<F>;
auto scope_success(F&& f)
{
return detail::scope_success<std::decay_t<F>>(HPX_FORWARD(F, f));
}
} // namespace hpx::experimental

#endif

0 comments on commit c0d79c8

Please sign in to comment.