Skip to content

Commit

Permalink
Working around nvcc problems to use CTAD
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Jan 15, 2024
1 parent cf70bf1 commit d34090e
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,68 @@

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 {
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))
{
}

explicit constexpr scope_exit(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
{
}
explicit constexpr scope_exit(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(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();
}
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();
}

scope_exit(scope_exit const&) = delete;
scope_exit& operator=(scope_exit const&) = delete;
scope_exit& operator=(scope_exit&& rhs) = delete;
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
{
if (active)
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;
};

template <typename F>
scope_exit(F) -> scope_exit<F>;
} // namespace detail

#if !defined(HPX_COMPUTE_CODE)
template <typename F>
using scope_exit = detail::scope_exit<F>;
#else
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));
}
#endif
} // namespace hpx::experimental

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

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 {
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())
{
}

explicit constexpr scope_fail(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
, active(std::uncaught_exceptions())
{
}
explicit constexpr scope_fail(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(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();
}
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();
}

scope_fail(scope_fail const&) = delete;
scope_fail& operator=(scope_fail const&) = delete;
scope_fail& operator=(scope_fail&& rhs) = delete;
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
{
if (active < std::uncaught_exceptions())
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;
};

template <typename F>
scope_fail(F) -> scope_fail<F>;
} // namespace detail

#if !defined(HPX_COMPUTE_CODE)
template <typename F>
using scope_fail = detail::scope_fail<F>;
#else
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));
}
#endif
} // namespace hpx::experimental

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

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 {
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())
{
}

explicit constexpr scope_success(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(f)
, active(std::uncaught_exceptions())
{
}
explicit constexpr scope_success(F const& f) noexcept(
std::is_nothrow_copy_constructible_v<F>)
: f(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();
}
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();
}

scope_success(scope_success const&) = delete;
scope_success& operator=(scope_success const&) = delete;
scope_success& operator=(scope_success&& rhs) = delete;
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()))
{
if (active >= std::uncaught_exceptions())
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;
};

template <typename F>
scope_success(F) -> scope_success<F>;
} // namespace detail

#if !defined(HPX_COMPUTE_CODE)
template <typename F>
using scope_success = detail::scope_success<F>;
#else
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));
}
#endif
} // namespace hpx::experimental

#endif

0 comments on commit d34090e

Please sign in to comment.