librf

channel_v2.h
1 #pragma once
2
3 namespace resumef
4 {
5 #ifndef DOXYGEN_SKIP_PROPERTY
6 namespace detail
7 {
8 template<class _Ty, bool _Optional>
9 struct channel_impl_v2;
10 } //namespace detail
11
12 inline namespace channel_v2
13 {
14 #endif //DOXYGEN_SKIP_PROPERTY
15
24 template<class _Ty = bool, bool _Optional = !std::is_trivial_v<_Ty>, bool _OptimizationThread = false>
25 struct channel_t
26 {
27 static_assert((std::is_copy_constructible_v<_Ty>&& std::is_copy_assignable_v<_Ty>) ||
28 (std::is_move_constructible_v<_Ty> && std::is_move_assignable_v<_Ty>));
29
30 struct [[nodiscard]] read_awaiter;
31 struct [[nodiscard]] write_awaiter;
32
37 channel_t(size_t cache_size = 1);
38
42 size_t capacity() const noexcept;
43
49 read_awaiter operator co_await() const noexcept;
50
59 read_awaiter read() const noexcept;
60
65 template<class U
66 #ifndef DOXYGEN_SKIP_PROPERTY
67 COMMA_RESUMEF_ENABLE_IF(std::is_constructible_v<_Ty, U&&>)
68 #endif //DOXYGEN_SKIP_PROPERTY
69 >
70 #ifndef DOXYGEN_SKIP_PROPERTY
71 RESUMEF_REQUIRES(std::is_constructible_v<_Ty, U&&>)
72 #endif //DOXYGEN_SKIP_PROPERTY
73 write_awaiter operator << (U&& val) const noexcept(std::is_move_constructible_v<U>);
74
82 template<class U
83 #ifndef DOXYGEN_SKIP_PROPERTY
84 COMMA_RESUMEF_ENABLE_IF(std::is_constructible_v<_Ty, U&&>)
85 #endif //DOXYGEN_SKIP_PROPERTY
86 >
87 #ifndef DOXYGEN_SKIP_PROPERTY
88 RESUMEF_REQUIRES(std::is_constructible_v<_Ty, U&&>)
89 #endif //DOXYGEN_SKIP_PROPERTY
90 write_awaiter write(U&& val) const noexcept(std::is_move_constructible_v<U>);
91
92
93 #ifndef DOXYGEN_SKIP_PROPERTY
94 using value_type = _Ty;
95
96 static constexpr bool use_optional = _Optional;
97 static constexpr bool optimization_for_multithreading = _OptimizationThread;
98
99 using optional_type = std::conditional_t<use_optional, std::optional<value_type>, value_type>;
100 using channel_type = detail::channel_impl_v2<value_type, use_optional>;
101 using lock_type = typename channel_type::lock_type;
102
103 channel_t(const channel_t&) = default;
104 channel_t(channel_t&&) = default;
105 channel_t& operator = (const channel_t&) = default;
106 channel_t& operator = (channel_t&&) = default;
107 private:
108 std::shared_ptr<channel_type> _chan;
109 #endif //DOXYGEN_SKIP_PROPERTY
110 };
111
112
113 #ifndef DOXYGEN_SKIP_PROPERTY
114 //不支持channel_t<void>
115 template<bool _Option, bool _OptimizationThread>
116 struct channel_t<void, _Option, _OptimizationThread>
117 {
118 };
119 #endif //DOXYGEN_SKIP_PROPERTY
120
124 using semaphore_t = channel_t<bool, false, true>;
125
126 } //namespace channel_v2
127 } //namespace resumef
resumef::channel_t
可传递数据的模板信号量。
Definition: channel_v2.h:25