Browse Source

使用concept全面替代enable_if

3.0.0
tearshark 2 years ago
parent
commit
c7e4819c8e

+ 5
- 3
include/librf/src/channel_v2.inl View File

@@ -44,7 +44,7 @@ namespace detail
this->_coro = nullptr;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void on_await_suspend(coroutine_handle<_PromiseT> handler) noexcept
{
_PromiseT& promise = handler.promise();
@@ -364,7 +364,8 @@ namespace detail
}
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
scoped_lock<lock_type> lock_(_channel->_lock);
@@ -429,7 +430,8 @@ namespace detail
}
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
scoped_lock<lock_type> lock_(_channel->_lock);

+ 9
- 3
include/librf/src/current_scheduler.h View File

@@ -11,7 +11,8 @@ namespace librf
{
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
_PromiseT& promise = handler.promise();
@@ -20,6 +21,7 @@ namespace librf

return false;
}

scheduler_t* await_resume() const noexcept
{
return _scheduler;
@@ -69,7 +71,8 @@ namespace librf
{
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
_PromiseT& promise = handler.promise();
@@ -78,6 +81,7 @@ namespace librf

return false;
}

state_base_t* await_resume() const noexcept
{
return _state;
@@ -126,7 +130,8 @@ namespace librf
{
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
_PromiseT& promise = handler.promise();
@@ -138,6 +143,7 @@ namespace librf

return false;
}

task_t* await_resume() const noexcept
{
return _task;

+ 9
- 9
include/librf/src/event_v2.inl View File

@@ -58,7 +58,7 @@ namespace librf
virtual bool on_notify(event_v2_impl* eptr) = 0;
virtual bool on_timeout() = 0;

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
scheduler_t* on_await_suspend(coroutine_handle<_PromiseT> handler) noexcept
{
_PromiseT& promise = handler.promise();
@@ -118,7 +118,7 @@ namespace librf
LIBRF_API bool on_notify(event_v2_impl* eptr, intptr_t idx);
LIBRF_API bool on_timeout();

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
scheduler_t* on_await_suspend(coroutine_handle<_PromiseT> handler) noexcept
{
_PromiseT& promise = handler.promise();
@@ -202,7 +202,7 @@ namespace librf
return _event->try_wait_one();
}

template<class _PromiseT, class _Timeout, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT, class _Timeout> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend2(coroutine_handle<_PromiseT> handler, const _Timeout& cb)
{
(void)cb;
@@ -227,7 +227,7 @@ namespace librf
return true;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
return await_suspend2(handler, nullptr);
@@ -261,7 +261,7 @@ namespace librf
: _Btype(std::forward<Args>(args)...)
, _tp(tp)
{}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
if (!_Btype::await_suspend2(handler, [this]{ this->_state->add_timeout_timer(_tp);}))
@@ -308,7 +308,7 @@ namespace librf
return _begin == _end;
}

template<class _PromiseT, class _Timeout, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT, class _Timeout> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend2(coroutine_handle<_PromiseT> handler, const _Timeout& cb)
{
(void)cb;
@@ -351,7 +351,7 @@ namespace librf
return true;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
return await_suspend2(handler, nullptr);
@@ -442,7 +442,7 @@ namespace librf
return _value;
}

template<class _PromiseT, class _Timeout, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT, class _Timeout> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend2(coroutine_handle<_PromiseT> handler, const _Timeout& cb)
{
(void)cb;
@@ -498,7 +498,7 @@ namespace librf
return true;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
return await_suspend2(handler, nullptr);

+ 6
- 30
include/librf/src/mutex_v2.h View File

@@ -138,11 +138,7 @@ namespace librf
* @param mtxs... 需要获得的锁列表。
* @return [co_await] batch_unlock_t
*/
template<class... _Mtxs
#ifndef DOXYGEN_SKIP_PROPERTY
, typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
#endif //DOXYGEN_SKIP_PROPERTY
>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
static future_t<batch_unlock_t<_Mtxs...>> lock(_Mtxs&... mtxs);

/**
@@ -151,11 +147,7 @@ namespace librf
* @param mtxs... 需要获得的锁列表。
* @return [co_await] void
*/
template<class... _Mtxs
#ifndef DOXYGEN_SKIP_PROPERTY
, typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
#endif //DOXYGEN_SKIP_PROPERTY
>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
static future_t<> lock(adopt_manual_unlock_t manual_unlock_tag, _Mtxs&... mtxs);

/**
@@ -163,11 +155,7 @@ namespace librf
* @param mtxs... 需要解锁的锁列表。
* @return [co_await] void
*/
template<class... _Mtxs
#ifndef DOXYGEN_SKIP_PROPERTY
, typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
#endif //DOXYGEN_SKIP_PROPERTY
>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
static future_t<> unlock(_Mtxs&... mtxs);


@@ -177,11 +165,7 @@ namespace librf
* @param mtxs... 需要获得的锁列表。
* @return batch_unlock_t
*/
template<class... _Mtxs
#ifndef DOXYGEN_SKIP_PROPERTY
, typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
#endif //DOXYGEN_SKIP_PROPERTY
>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
static batch_unlock_t<_Mtxs...> lock(void* unique_address, _Mtxs&... mtxs);

/**
@@ -190,11 +174,7 @@ namespace librf
* @param unique_address 代表获得锁的拥有者。
* @param mtxs... 需要获得的锁列表。
*/
template<class... _Mtxs
#ifndef DOXYGEN_SKIP_PROPERTY
, typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
#endif //DOXYGEN_SKIP_PROPERTY
>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
static void lock(adopt_manual_unlock_t manual_unlock_tag, void* unique_address, _Mtxs&... mtxs);

/**
@@ -202,11 +182,7 @@ namespace librf
* @param unique_address 代表获得锁的拥有者。
* @param mtxs... 需要解锁的锁列表。
*/
template<class... _Mtxs
#ifndef DOXYGEN_SKIP_PROPERTY
, typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
#endif //DOXYGEN_SKIP_PROPERTY
>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
static void unlock(void* unique_address, _Mtxs&... mtxs);

LIBRF_API mutex_t();

+ 25
- 25
include/librf/src/mutex_v2.inl View File

@@ -144,20 +144,20 @@ namespace librf
: batch_unlock_t(std::adopt_lock, sch, mtx._mutex)
{}

/*
//此函数,适合在非协程里使用
batch_unlock_t(void* sch, mutex_impl_ptr mtx)
: _mutex(std::move(mtx))
, _owner(sch)
{
if (_mutex != nullptr)
_mutex->lock_until_succeed(sch);
}
batch_unlock_t(void* sch, const mutex_t& mtx)
: batch_unlock_t(sch, mtx._mutex)
{}
*/
/*
//此函数,适合在非协程里使用
batch_unlock_t(void* sch, mutex_impl_ptr mtx)
: _mutex(std::move(mtx))
, _owner(sch)
{
if (_mutex != nullptr)
_mutex->lock_until_succeed(sch);
}
batch_unlock_t(void* sch, const mutex_t& mtx)
: batch_unlock_t(sch, mtx._mutex)
{}
*/

~batch_unlock_t()
{
@@ -205,7 +205,7 @@ namespace librf
return false;
}

template<class _PromiseT, class _Timeout, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT, class _Timeout> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend2(coroutine_handle<_PromiseT> handler, const _Timeout& cb)
{
(void)cb;
@@ -239,7 +239,7 @@ namespace librf
{
using lock_awaiter::lock_awaiter;

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
return await_suspend2(handler, nullptr);
@@ -266,7 +266,7 @@ namespace librf
struct mutex_t::manual_awaiter : public lock_awaiter
{
using lock_awaiter::lock_awaiter;
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
return await_suspend2(handler, nullptr);
@@ -308,7 +308,7 @@ namespace librf
return false;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
_PromiseT& promise = handler.promise();
@@ -355,7 +355,7 @@ namespace librf
return false;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
_PromiseT& promise = handler.promise();
@@ -528,7 +528,7 @@ namespace librf
template<class... _Mtxs>
batch_unlock_t()->batch_unlock_t<_Mtxs...>;

template<class... _Mtxs, typename>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
inline future_t<batch_unlock_t<_Mtxs...>> mutex_t::lock(_Mtxs&... mtxs)
{
batch_unlock_t<_Mtxs...> unlock_guard{ std::adopt_lock, librf_root_state(), mtxs... };
@@ -536,7 +536,7 @@ namespace librf
co_return std::move(unlock_guard);
}

template<class... _Mtxs, typename>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
inline future_t<> mutex_t::lock(adopt_manual_unlock_t _noused, _Mtxs&... mtxs)
{
(void)_noused; //GCC: 这个参数不起一个名字,会导致GCC编译器内部错误。
@@ -544,7 +544,7 @@ namespace librf
co_await detail::mutex_lock_await_lock_impl::_Lock_range(_MAA);
}

template<class... _Mtxs, typename>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
inline future_t<> mutex_t::unlock(_Mtxs&... mtxs)
{
void* unique_address = librf_root_state();
@@ -552,7 +552,7 @@ namespace librf
(mtxs.unlock(unique_address), ...);
}

template<class... _Mtxs, typename>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
inline batch_unlock_t<_Mtxs...> mutex_t::lock(void* unique_address, _Mtxs&... mtxs)
{
assert(unique_address != nullptr);
@@ -565,7 +565,7 @@ namespace librf
return su;
}

template<class... _Mtxs, typename>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
inline void mutex_t::lock(adopt_manual_unlock_t, void* unique_address, _Mtxs&... mtxs)
{
assert(unique_address != nullptr);
@@ -574,7 +574,7 @@ namespace librf
detail::scoped_lock_range_lock_impl::_Lock_range(_MAA);
}

template<class... _Mtxs, typename>
template<class... _Mtxs> requires(std::same_as<_Mtxs, mutex_t> && ...)
inline void mutex_t::unlock(void* unique_address, _Mtxs&... mtxs)
{
assert(unique_address != nullptr);

+ 2
- 2
include/librf/src/promise.h View File

@@ -18,7 +18,7 @@ namespace librf
{
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
inline void await_suspend(coroutine_handle<_PromiseT> handler) noexcept
{
_PromiseT& promise = handler.promise();
@@ -36,7 +36,7 @@ namespace librf
{
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
inline void await_suspend(coroutine_handle<_PromiseT> handler) noexcept
{
_PromiseT& promise = handler.promise();

+ 10
- 6
include/librf/src/state.h View File

@@ -174,14 +174,15 @@ namespace librf
//scoped_lock<lock_type> __guard(this->_mtx);
return _has_value.load(std::memory_order_acquire) != result_type::None;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void future_await_suspend(coroutine_handle<_PromiseT> handler);

LIBRF_API bool switch_scheduler_await_suspend(scheduler_t* sch);

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void promise_initial_suspend(coroutine_handle<_PromiseT> handler);
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void promise_final_suspend(coroutine_handle<_PromiseT> handler);

#if RESUMEF_INLINE_STATE
@@ -239,7 +240,8 @@ namespace librf
}

auto future_await_resume() -> value_type;
template<class _PromiseT, typename U, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT, typename U> requires(traits::is_promise_v<_PromiseT>)
void promise_yield_value(_PromiseT* promise, U&& val);

void set_exception(std::exception_ptr e);
@@ -282,7 +284,8 @@ namespace librf
}

auto future_await_resume()->reference_type;
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void promise_yield_value(_PromiseT* promise, reference_type val);

void set_exception(std::exception_ptr e);
@@ -313,7 +316,8 @@ namespace librf
explicit state_t(bool awaitor) noexcept :state_future_t(awaitor) {}
public:
LIBRF_API void future_await_resume();
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void promise_yield_value(_PromiseT* promise);

LIBRF_API void set_exception(std::exception_ptr e);

+ 7
- 7
include/librf/src/state.inl View File

@@ -1,7 +1,7 @@

namespace librf
{
template<class _PromiseT, typename _Enable>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void state_future_t::promise_initial_suspend(coroutine_handle<_PromiseT> handler)
{
assert(this->_scheduler == nullptr);
@@ -11,7 +11,7 @@ namespace librf
this->_is_initor = initor_type::Initial;
}

template<class _PromiseT, typename _Enable>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void state_future_t::promise_final_suspend(coroutine_handle<_PromiseT> handler)
{
scoped_lock<lock_type> __guard(this->_mtx);
@@ -27,7 +27,7 @@ namespace librf
sch->del_final(this);
}

template<class _PromiseT, typename _Enable>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void state_future_t::future_await_suspend(coroutine_handle<_PromiseT> handler)
{
_PromiseT& promise = handler.promise();
@@ -51,7 +51,7 @@ namespace librf

//------------------------------------------------------------------------------------------------

template<class _PromiseT, typename _Enable >
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void state_t<void>::promise_yield_value(_PromiseT* promise)
{
coroutine_handle<_PromiseT> handler = coroutine_handle<_PromiseT>::from_promise(*promise);
@@ -77,7 +77,7 @@ namespace librf
//------------------------------------------------------------------------------------------------

template<typename _Ty>
template<class _PromiseT, typename U, typename _Enable >
template<class _PromiseT, typename U> requires(traits::is_promise_v<_PromiseT>)
void state_t<_Ty>::promise_yield_value(_PromiseT* promise, U&& val)
{
coroutine_handle<_PromiseT> handler = coroutine_handle<_PromiseT>::from_promise(*promise);
@@ -191,8 +191,8 @@ namespace librf
//------------------------------------------------------------------------------------------------

template<typename _Ty>
template<class _PromiseT, typename _Enable >
void state_t<_Ty&>::promise_yield_value(_PromiseT* promise, reference_type val)
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void state_t<_Ty&>::promise_yield_value(_PromiseT* promise, typename state_t<_Ty&>::reference_type val)
{
coroutine_handle<_PromiseT> handler = coroutine_handle<_PromiseT>::from_promise(*promise);


+ 1
- 1
include/librf/src/switch_scheduler.h View File

@@ -25,7 +25,7 @@ namespace librf
return false;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
_PromiseT& promise = handler.promise();

+ 2
- 2
include/librf/src/when_v2.h View File

@@ -32,7 +32,7 @@ namespace librf
LIBRF_API bool on_timeout();

//将自己加入到通知链表里
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
scheduler_t* on_await_suspend(coroutine_handle<_PromiseT> handler) noexcept
{
_PromiseT& promise = handler.promise();
@@ -73,7 +73,7 @@ namespace librf
return _state->_counter.load(std::memory_order_relaxed) == 0;
}

template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
void await_suspend(coroutine_handle<_PromiseT> handler)
{
_state->on_await_suspend(handler);

+ 1
- 1
include/librf/src/yield.h View File

@@ -16,7 +16,7 @@ namespace librf
{
return false;
}
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
template<class _PromiseT> requires(traits::is_promise_v<_PromiseT>)
bool await_suspend(coroutine_handle<_PromiseT> handler)
{
counted_ptr<state_t<void>> _state = state_future_t::_Alloc_state<state_type>(true);

Loading…
Cancel
Save