Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::Channel< T, 0 > Class Template Reference

#include <channel.h>

Public Types

using iterator = ChannelIterator<T, 0>
 迭代器类型。

Public Member Functions

 Channel ()=default
 默认构造函数。
bool operator<< (const T &value)
 向通道写入数据。
bool operator<< (T &&value)
 向通道写入数据。
bool operator>> (T &value)
 从通道读取数据。
T operator* ()
 从通道读取数据。
std::unique_ptr< T > operator-> ()
 从通道读取数据。
 operator bool () const
 显式转换为布尔值。
iterator begin ()
 迭代器的 begin 函数。
iterator end ()
 迭代器的 end 函数。
 Channel (const Channel &)=delete
 删除复制构造函数。
Channel & operator= (const Channel &)=delete
 删除复制赋值函数。
 Channel (Channel &&)=delete
 删除移动构造函数。
Channel & operator= (Channel &&)=delete
 删除移动赋值函数。
bool operator<<= (const T &value)
 尝试向通道写入数据。
bool operator>>= (T &value)
 从通道尝试读取数据(非阻塞)。

Private Attributes

T buffer_
 缓冲区
std::mutex mutex_
 互斥锁
std::condition_variable r_cv_
 读 条件变量
std::condition_variable w_cv_
 写 条件变量
bool empty_ = true
 缓冲区是否为空
bool closed_ = false
 关闭标志
size_t read_index_
 读索引
size_t write_index_
 写索引

Friends

void close (Channel &channel)
 关闭通道。

Member Typedef Documentation

◆ iterator

template<class T>
using console::Channel< T, 0 >::iterator = ChannelIterator<T, 0>

迭代器类型。

Constructor & Destructor Documentation

◆ Channel() [1/3]

template<class T>
console::Channel< T, 0 >::Channel ( )
default

默认构造函数。

◆ Channel() [2/3]

template<class T>
console::Channel< T, 0 >::Channel ( const Channel< T, 0 > & )
delete

删除复制构造函数。

◆ Channel() [3/3]

template<class T>
console::Channel< T, 0 >::Channel ( Channel< T, 0 > && )
delete

删除移动构造函数。

Member Function Documentation

◆ begin()

template<class T>
iterator console::Channel< T, 0 >::begin ( )
inline

迭代器的 begin 函数。

◆ end()

template<class T>
iterator console::Channel< T, 0 >::end ( )
inline

迭代器的 end 函数。

◆ operator bool()

template<class T>
console::Channel< T, 0 >::operator bool ( ) const
inlineexplicit

显式转换为布尔值。

Returns
若通道已关闭且没有剩余数据,返回 false;否则返回 true。
Note
注意,验明通道为 true 后仍然可能出现问题,因为验明和读值不是原子的。 得到 true 只能表明验明前管道有效,不代表读取时有效。

◆ operator*()

template<class T>
T console::Channel< T, 0 >::operator* ( )
inline

从通道读取数据。

若通道为空,则阻塞等待直到有数据可读。

Returns
读取的值。
Exceptions
AsyncError若通道已关闭。
Note
若通道已关闭且没有剩余数据,则抛出 AsyncError。

◆ operator->()

template<class T>
std::unique_ptr< T > console::Channel< T, 0 >::operator-> ( )
inline

从通道读取数据。

若通道为空,则阻塞等待直到有数据可读。

Returns
读取的值。
Exceptions
AsyncError若通道已关闭。
Note
若通道已关闭且没有剩余数据,则抛出 AsyncError。

◆ operator<<() [1/2]

template<class T>
bool console::Channel< T, 0 >::operator<< ( const T & value)
inline

向通道写入数据。

若通道为空,则阻塞等待直到有空间可写。

Parameters
value要写入的值。
Returns
若写入成功。
Note
若通道已关闭,则 No-OP。

◆ operator<<() [2/2]

template<class T>
bool console::Channel< T, 0 >::operator<< ( T && value)
inline

向通道写入数据。

若通道为空,则阻塞等待直到有空间可写。

Parameters
value要写入的值。
Returns
若写入成功。
Note
若通道已关闭,则 No-OP。

◆ operator<<=()

bool console::Channel< T, N >::operator<<= ( const T & value)
inline

尝试向通道写入数据。

非阻塞;若通道已满或已关闭,则立即返回失败。

Parameters
value要写入的值。
Returns
若写入成功。

◆ operator=() [1/2]

template<class T>
Channel & console::Channel< T, 0 >::operator= ( Channel< T, 0 > && )
delete

删除移动赋值函数。

◆ operator=() [2/2]

template<class T>
Channel & console::Channel< T, 0 >::operator= ( const Channel< T, 0 > & )
delete

删除复制赋值函数。

◆ operator>>()

template<class T>
bool console::Channel< T, 0 >::operator>> ( T & value)
inline

从通道读取数据。

若通道为空,则阻塞等待直到有数据可读。

Parameters
value读取的值。
Returns
若读取成功。
Note
若通道已关闭且没有剩余数据,则 No-OP。

◆ operator>>=()

bool console::Channel< T, N >::operator>>= ( T & value)
inline

从通道尝试读取数据(非阻塞)。

若通道为空,则立即返回 false;否则读取数据并返回 true。

Parameters
value读取的值。
Returns
若读取成功。
Note
若通道已关闭且没有剩余数据,返回 false。

◆ close

template<class T>
void close ( Channel< T, 0 > & channel)
friend

关闭通道。

Note
设置关闭标志,唤醒所有线程。

Member Data Documentation

◆ buffer_

template<class T>
T console::Channel< T, 0 >::buffer_
private

缓冲区

◆ closed_

template<class T>
bool console::Channel< T, 0 >::closed_ = false
private

关闭标志

◆ empty_

template<class T>
bool console::Channel< T, 0 >::empty_ = true
private

缓冲区是否为空

◆ mutex_

template<class T>
std::mutex console::Channel< T, 0 >::mutex_
mutableprivate

互斥锁

◆ r_cv_

template<class T>
std::condition_variable console::Channel< T, 0 >::r_cv_
private

读 条件变量

◆ read_index_

size_t console::Channel< T, N >::read_index_
private

读索引

◆ w_cv_

template<class T>
std::condition_variable console::Channel< T, 0 >::w_cv_
private

写 条件变量

◆ write_index_

size_t console::Channel< T, N >::write_index_
private

写索引


The documentation for this class was generated from the following file: