Browse Source

完善event_v2的concept约束

tags/v2.9.7
tearshark 4 years ago
parent
commit
bac50e93cd
4 changed files with 79 additions and 31 deletions
  1. 2
    2
      librf/src/event_v2.cpp
  2. 23
    22
      librf/src/event_v2.h
  3. 16
    2
      librf/src/type_concept.inl
  4. 38
    5
      librf/src/type_traits.inl

+ 2
- 2
librf/src/event_v2.cpp View File



_counter.store(0, std::memory_order_release); _counter.store(0, std::memory_order_release);


counted_ptr<state_event_t> state;
state_event_ptr state;
for (; (state = try_pop_list(_wait_awakes)) != nullptr;) for (; (state = try_pop_list(_wait_awakes)) != nullptr;)
{ {
(void)state->on_notify(); (void)state->on_notify();
{ {
scoped_lock<lock_type> lock_(_lock); scoped_lock<lock_type> lock_(_lock);


counted_ptr<state_event_t> state;
state_event_ptr state;
for (; (state = try_pop_list(_wait_awakes)) != nullptr;) for (; (state = try_pop_list(_wait_awakes)) != nullptr;)
{ {
if (state->on_notify()) if (state->on_notify())

+ 23
- 22
librf/src/event_v2.h View File

//而when_any会导致所有的event_t都被触发 //而when_any会导致所有的event_t都被触发
//改日有空再补上 //改日有空再补上


struct [[nodiscard]] any_awaiter;


template<_IteratorT _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_v<_Iter> && std::is_same_v<event_t&, decltype(*std::declval<_Iter>())>)
>
template<class _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_of_v<_Iter, event_t>)
> RESUMEF_REQUIRES(_IteratorOfT<_Iter, event_t>)
static future_t<intptr_t> static future_t<intptr_t>
wait_any(_Iter begin_, _Iter end_) wait_any(_Iter begin_, _Iter end_)
{ {
} }


template<class _Cont template<class _Cont
COMMA_RESUMEF_ENABLE_IF(traits::is_container_v<_Cont>)
>
COMMA_RESUMEF_ENABLE_IF(traits::is_container_of_v<_Cont, event_t>)
> RESUMEF_REQUIRES(_ContainerOfT<_Cont, event_t>)
static future_t<intptr_t> static future_t<intptr_t>
wait_any(_Cont& cnt_) wait_any(_Cont& cnt_)
{ {
co_return idx.first; co_return idx.first;
} }


template<class _Rep, class _Period, _IteratorT _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_v<_Iter> && std::is_same_v<event_t&, decltype(*std::declval<_Iter>())>)
>
template<class _Rep, class _Period, class _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_of_v<_Iter, event_t>)
> RESUMEF_REQUIRES(_IteratorOfT<_Iter, event_t>)
static future_t<intptr_t> static future_t<intptr_t>
wait_any_for(const std::chrono::duration<_Rep, _Period>& dt, _Iter begin_, _Iter end_) wait_any_for(const std::chrono::duration<_Rep, _Period>& dt, _Iter begin_, _Iter end_)
{ {
co_return idx.first; co_return idx.first;
} }


template<class _Rep, class _Period, _ContainerT _Cont
COMMA_RESUMEF_ENABLE_IF(traits::is_container_v<_Cont>)
>
template<class _Rep, class _Period, class _Cont
COMMA_RESUMEF_ENABLE_IF(traits::is_container_of_v<_Cont, event_t>)
> RESUMEF_REQUIRES(_ContainerOfT<_Cont, event_t>)
static future_t<intptr_t> static future_t<intptr_t>
wait_any_for(const std::chrono::duration<_Rep, _Period>& dt, _Cont& cont) wait_any_for(const std::chrono::duration<_Rep, _Period>& dt, _Cont& cont)
{ {






template<_IteratorT _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_v<_Iter> && std::is_same_v<event_t&, decltype(*std::declval<_Iter>())>)
>
template<class _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_of_v<_Iter, event_t>)
> RESUMEF_REQUIRES(_IteratorOfT<_Iter, event_t>)
static future_t<bool> static future_t<bool>
wait_all(_Iter begin_, _Iter end_) wait_all(_Iter begin_, _Iter end_)
{ {
} }


template<class _Cont template<class _Cont
COMMA_RESUMEF_ENABLE_IF(traits::is_container_v<_Cont>)
>
COMMA_RESUMEF_ENABLE_IF(traits::is_container_of_v<_Cont, event_t>)
> RESUMEF_REQUIRES(_ContainerOfT<_Cont, event_t>)
static future_t<bool> static future_t<bool>
wait_all(_Cont& cnt_) wait_all(_Cont& cnt_)
{ {
co_return is_all_succeeded(vb); co_return is_all_succeeded(vb);
} }


template<class _Rep, class _Period, _IteratorT _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_v<_Iter> && std::is_same_v<event_t&, decltype(*std::declval<_Iter>())>)
>
template<class _Rep, class _Period, class _Iter
COMMA_RESUMEF_ENABLE_IF(traits::is_iterator_of_v<_Iter, event_t>)
> RESUMEF_REQUIRES(_IteratorOfT<_Iter, event_t>)
static future_t<bool> static future_t<bool>
wait_all_for(const std::chrono::duration<_Rep, _Period>& dt, _Iter begin_, _Iter end_) wait_all_for(const std::chrono::duration<_Rep, _Period>& dt, _Iter begin_, _Iter end_)
{ {
co_return is_all_succeeded(vb); co_return is_all_succeeded(vb);
} }


template<class _Rep, class _Period, _ContainerT _Cont
COMMA_RESUMEF_ENABLE_IF(traits::is_container_v<_Cont>)
>
template<class _Rep, class _Period, class _Cont
COMMA_RESUMEF_ENABLE_IF(traits::is_container_of_v<_Cont, event_t>)
> RESUMEF_REQUIRES(_ContainerOfT<_Cont, event_t>)
static future_t<bool> static future_t<bool>
wait_all_for(const std::chrono::duration<_Rep, _Period>& dt, _Cont& cont) wait_all_for(const std::chrono::duration<_Rep, _Period>& dt, _Cont& cont)
{ {

+ 16
- 2
librf/src/type_concept.inl View File

concept _HasStateT = requires(T&& v) concept _HasStateT = requires(T&& v)
{ {
{ v._state }; { v._state };
{ traits::is_state_pointer_v<decltype(v._state)> != false };
requires traits::is_state_pointer_v<decltype(v._state)>;
}; };
template<typename T> template<typename T>
concept _IteratorT = requires(T&& u, T&& v) concept _IteratorT = requires(T&& u, T&& v)
{ {
{ ++u }->T; { ++u }->T;
{ u != v } -> bool;
{ u != v } ->bool;
{ *u }; { *u };
}; };
template<typename T, typename E>
concept _IteratorOfT = _IteratorT<T> && requires(T && u)
{
{ *u } ->std::same_as<E&>;
};
template<typename T> template<typename T>
concept _WhenIterT = _IteratorT<T> && requires(T&& u) concept _WhenIterT = _IteratorT<T> && requires(T&& u)
{ {
requires std::same_as<decltype(std::begin(v)), decltype(std::end(v))>; requires std::same_as<decltype(std::begin(v)), decltype(std::end(v))>;
}; };
template<typename T, typename E>
concept _ContainerOfT = _ContainerT<T> && requires(T && u)
{
{ *std::begin(u) } ->std::same_as<E&>;
};
#define COMMA_RESUMEF_ENABLE_IF(...) #define COMMA_RESUMEF_ENABLE_IF(...)
#define RESUMEF_ENABLE_IF(...) #define RESUMEF_ENABLE_IF(...)
#define RESUMEF_REQUIRES(...) requires __VA_ARGS__ #define RESUMEF_REQUIRES(...) requires __VA_ARGS__
#define _AwaitableT typename #define _AwaitableT typename
#define _WhenTaskT typename #define _WhenTaskT typename
#define _IteratorT typename #define _IteratorT typename
#define _IteratorOfT typename
#define _WhenIterT typename #define _WhenIterT typename
#define _ContainerT typename #define _ContainerT typename
#define _ContainerOfT typename
#define COMMA_RESUMEF_ENABLE_IF(...) ,typename=std::enable_if_t<__VA_ARGS__> #define COMMA_RESUMEF_ENABLE_IF(...) ,typename=std::enable_if_t<__VA_ARGS__>
#define RESUMEF_ENABLE_IF(...) typename=std::enable_if_t<__VA_ARGS__> #define RESUMEF_ENABLE_IF(...) typename=std::enable_if_t<__VA_ARGS__>

+ 38
- 5
librf/src/type_traits.inl View File

//is_iterator_v<T> //is_iterator_v<T>
//判断是不是一个支持向后迭代的迭代器 //判断是不是一个支持向后迭代的迭代器
// //
//is_iterator_of_v<T, E>
//判断是不是一个支持向后迭代的迭代器,并且迭代器通过 operator *()返回的类型是 E&。
//
//is_container<T> //is_container<T>
//is_container_v<T> //is_container_v<T>
//判断是不是一个封闭区间的容器,或者数组。 //判断是不是一个封闭区间的容器,或者数组。
//
//is_container_of_v<T, E>
//判断是不是一个封闭区间的容器,或者数组。其元素类型是E。


template<class _Ty> template<class _Ty>
struct is_coroutine_handle : std::false_type {}; struct is_coroutine_handle : std::false_type {};
: std::true_type{}; : std::true_type{};
template<class _Ty> template<class _Ty>
constexpr bool is_iterator_v = is_iterator<_Ty>::value; constexpr bool is_iterator_v = is_iterator<_Ty>::value;
template<class _Ty, class _Ety>
constexpr bool is_iterator_of_v = std::conjunction<
is_iterator<_Ty>
, std::is_same<_Ety&, decltype(*std::declval<_Ty>())>
>::value;


template<class _Ty, class = std::void_t<>> template<class _Ty, class = std::void_t<>>
struct is_container : std::false_type {}; struct is_container : std::false_type {};
struct is_container struct is_container
<_Ty, <_Ty,
std::void_t< std::void_t<
decltype(std::declval<_Ty>().begin())
, decltype(std::declval<_Ty>().end())
decltype(std::begin(std::declval<_Ty>()))
, decltype(std::end(std::declval<_Ty>()))
> >
> >
: std::true_type {};
//: is_iterator<decltype(std::declval<_Ty>().begin())> {};

: is_iterator<decltype(std::begin(std::declval<_Ty>()))> {};
template<class _Ty, size_t _Size> template<class _Ty, size_t _Size>
struct is_container<_Ty[_Size]> : std::true_type {}; struct is_container<_Ty[_Size]> : std::true_type {};
template<class _Ty, size_t _Size> template<class _Ty, size_t _Size>


template<class _Ty> template<class _Ty>
constexpr bool is_container_v = is_container<remove_cvref_t<_Ty>>::value; constexpr bool is_container_v = is_container<remove_cvref_t<_Ty>>::value;

template<class _Ty, class _Ety, class = std::void_t<>>
struct is_container_of : std::false_type {};
template<class _Ty, class _Ety>
struct is_container_of
<_Ty, _Ety,
std::void_t<
decltype(std::begin(std::declval<_Ty>()))
, decltype(std::end(std::declval<_Ty>()))
>
>
: std::conjunction<
is_iterator<decltype(std::begin(std::declval<_Ty>()))>,
std::is_same<_Ety&, decltype(*std::begin(std::declval<_Ty>()))>
> {};
template<class _Ty, size_t _Size>
struct is_container_of<_Ty[_Size], _Ty> : std::true_type {};
template<class _Ty, size_t _Size>
struct is_container_of<_Ty(&)[_Size], _Ty> : std::true_type {};
template<class _Ty, size_t _Size>
struct is_container_of<_Ty(&&)[_Size], _Ty> : std::true_type {};

template<class _Ty, class _Ety>
constexpr bool is_container_of_v = is_container_of<remove_cvref_t<_Ty>, _Ety>::value;
} }
} }

Loading…
Cancel
Save