34#include "pw_async2/dispatcher.h"
35#include "pw_async2/poll.h"
36#include "pw_bytes/span.h"
37#include "pw_channel/properties.h"
38#include "pw_multibuf/multibuf.h"
39#include "pw_result/result.h"
40#include "pw_status/status.h"
42namespace pw::channel {
58template <DataType kDataType,
Property... kProperties>
62 [[nodiscard]]
static constexpr DataType
data_type() {
return kDataType; }
65 [[nodiscard]]
static constexpr bool reliable() {
66 return ((kProperties == Property::kReliable) || ...);
69 [[nodiscard]]
static constexpr bool seekable() {
70 return ((kProperties == Property::kSeekable) || ...);
73 [[nodiscard]]
static constexpr bool readable() {
74 return ((kProperties == Property::kReadable) || ...);
77 [[nodiscard]]
static constexpr bool writable() {
78 return ((kProperties == Property::kWritable) || ...);
111 template <
typename Sibling,
112 typename = internal::EnableIfConversionIsValid<Channel, Sibling>>
113 constexpr operator Sibling&() {
114 return as<Sibling>();
116 template <
typename Sibling,
117 typename = internal::EnableIfConversionIsValid<Channel, Sibling>>
118 constexpr operator const Sibling&()
const {
119 return as<Sibling>();
123 template <
typename Sibling>
124 [[nodiscard]] Sibling&
as() {
125 internal::CheckThatConversionIsValid<Channel, Sibling>();
126 return static_cast<Sibling&
>(
static_cast<AnyChannel&
>(*this));
128 template <
typename Sibling>
129 [[nodiscard]]
const Sibling&
as()
const {
130 internal::CheckThatConversionIsValid<Channel, Sibling>();
131 return static_cast<const Sibling&
>(
static_cast<const AnyChannel&
>(*this));
136 template <Property... kOtherChannelProperties>
137 [[nodiscard]]
auto&
as() {
140 template <Property... kOtherChannelProperties>
141 [[nodiscard]]
const auto&
as()
const {
146 [[nodiscard]] Channel<DataType::kByte, kProperties...>&
148 static_assert(kDataType == DataType::kDatagram,
149 "IgnoreDatagramBoundaries() may only be called to use a "
150 "datagram channel to a byte channel");
151 return static_cast<Channel<DataType::kByte, kProperties...
>&>(
155 [[nodiscard]]
const Channel<DataType::kByte, kProperties...>&
157 static_assert(kDataType == DataType::kDatagram,
158 "IgnoreDatagramBoundaries() may only be called to use a "
159 "datagram channel to a byte channel");
160 return static_cast<const Channel<DataType::kByte, kProperties...
>&>(
165 static_assert(internal::PropertiesAreValid<kProperties...>());
167 friend class AnyChannel;
169 explicit constexpr Channel() =
default;
187 :
private Channel<DataType::kByte, kReadable>,
188 private Channel<DataType::kByte, kWritable>,
189 private Channel<DataType::kByte, kReadable, kWritable>,
190 private Channel<DataType::kByte, kReliable, kReadable>,
191 private Channel<DataType::kByte, kReliable, kWritable>,
192 private Channel<DataType::kByte, kReliable, kReadable, kWritable>,
193 private Channel<DataType::kDatagram, kReadable>,
194 private Channel<DataType::kDatagram, kWritable>,
195 private Channel<DataType::kDatagram, kReadable, kWritable>,
196 private Channel<DataType::kDatagram, kReliable, kReadable>,
197 private Channel<DataType::kDatagram, kReliable, kWritable>,
198 private Channel<DataType::kDatagram, kReliable, kReadable, kWritable> {
210 [[nodiscard]]
constexpr DataType
data_type()
const {
return data_type_; }
214 return (properties_ & Property::kReliable) != 0;
219 return (properties_ & Property::kSeekable) != 0;
224 return (properties_ & Property::kReadable) != 0;
229 return (properties_ & Property::kWritable) != 0;
234 [[nodiscard]]
constexpr bool is_read_open()
const {
return read_open_; }
242 return read_open_ || write_open_;
269 if (result.
IsReady() && result->status().IsFailedPrecondition()) {
298 if (result.
IsReady() && result->IsFailedPrecondition()) {
326 return DoPendAllocateWriteBuffer(cx, min_bytes);
355 Status status = DoStageWrite(std::move(data));
375 if (status.
IsReady() && status->IsFailedPrecondition()) {
418 if (result.IsReady()) {
430 void set_read_closed() { read_open_ =
false; }
436 void set_write_closed() { write_open_ =
false; }
439 template <DataType, Property...>
440 friend class Channel;
442 template <DataType, Property...>
443 friend class internal::BaseChannelImpl;
446 explicit constexpr AnyChannel(DataType type, uint8_t properties)
448 properties_(properties),
456 virtual async2::PollResult<multibuf::MultiBuf> DoPendRead(
457 async2::Context& cx) = 0;
461 virtual async2::PollOptional<multibuf::MultiBuf> DoPendAllocateWriteBuffer(
462 async2::Context& cx,
size_t min_bytes) = 0;
466 virtual Status DoStageWrite(multibuf::MultiBuf&& buffer) = 0;
487template <Property... kProperties>
491template <Property... kProperties>
534template <DataType kDataType, Property... kProperties>
536 static_assert(internal::PropertiesAreValid<kProperties...>());
540template <Property... kProperties>
544template <Property... kProperties>
553template <
typename ChannelType>
558template <DataType kDataType, Property... kProperties>
562 explicit constexpr Implement() =
default;
568#include "pw_channel/internal/channel_specializations.h"
static constexpr Status FailedPrecondition()
Definition: status.h:243
constexpr bool IsFailedPrecondition() const
Definition: status.h:381
Definition: channel.h:198
constexpr bool readable() const
Returns whether the channel implementation is readable.
Definition: channel.h:223
async2::Poll< pw::Status > PendClose(async2::Context &cx)
Definition: channel.h:413
async2::Poll< Status > PendWrite(async2::Context &cx)
Definition: channel.h:370
async2::Poll< Status > PendReadyToWrite(pw::async2::Context &cx)
Definition: channel.h:293
constexpr bool is_read_or_write_open() const
True if the channel is open for either reading or writing.
Definition: channel.h:241
async2::PollResult< multibuf::MultiBuf > PendRead(async2::Context &cx)
Definition: channel.h:264
Status Seek(async2::Context &cx, ptrdiff_t position, Whence whence)
constexpr bool reliable() const
Returns whether the channel implementation is reliable.
Definition: channel.h:213
constexpr bool writable() const
Returns whether the channel implementation is writable.
Definition: channel.h:228
constexpr bool is_read_open() const
Definition: channel.h:234
Status StageWrite(multibuf::MultiBuf &&data)
Definition: channel.h:351
async2::PollOptional< multibuf::MultiBuf > PendAllocateWriteBuffer(async2::Context &cx, size_t min_bytes)
Definition: channel.h:324
constexpr bool is_write_open() const
Definition: channel.h:238
constexpr bool seekable() const
Returns whether the channel implementation is seekable.
Definition: channel.h:218
virtual async2::Poll< Status > DoPendClose(async2::Context &cx)=0
TODO: b/323622630 - Seek and Position are not yet implemented.
constexpr DataType data_type() const
Returns the data type of the channel implementation.
Definition: channel.h:210
async2::Poll< Status > PendWrite(async2::Context &cx)
constexpr bool is_read_open() const
async2::PollResult< multibuf::MultiBuf > PendRead(async2::Context &cx)
static constexpr bool seekable()
Returns whether the channel type is seekable.
Definition: channel.h:69
static constexpr bool readable()
Returns whether the channel type is readable.
Definition: channel.h:73
constexpr bool is_write_open() const
Sibling & as()
Returns a reference to this channel as another compatible channel type.
Definition: channel.h:124
static constexpr bool reliable()
Returns whether the channel type is reliable.
Definition: channel.h:65
auto & as()
Definition: channel.h:137
constexpr bool is_read_or_write_open() const
True if the channel is open for reading or writing.
Definition: channel.h:90
async2::Poll< Status > PendReadyToWrite(pw::async2::Context &cx)
async2::PollOptional< multibuf::MultiBuf > PendAllocateWriteBuffer(async2::Context &cx, size_t min_bytes)
Status StageWrite(multibuf::MultiBuf &&data)
async2::Poll< pw::Status > PendClose(async2::Context &cx)
Channel< DataType::kByte, kProperties... > & IgnoreDatagramBoundaries()
Definition: channel.h:147
static constexpr DataType data_type()
Returns the data type of this channel.
Definition: channel.h:62
static constexpr bool writable()
Returns whether the channel type is writable.
Definition: channel.h:77
Definition: channel.h:535
Definition: channel.h:554
Definition: multibuf_v1.h:248
constexpr bool IsReady() const noexcept
Returns whether or not this value is Ready.
Definition: poll.h:133
Property
Basic properties of a MultiBuf.
Definition: properties.h:25