libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::WritePort Class Reference

WritePort class for handling write operations. More...

#include <libxr_rw.hpp>

Collaboration diagram for LibXR::WritePort:

Data Structures

struct  WriteInfo
 

Public Member Functions

 WritePort (size_t queue_size=3, size_t block_size=128)
 构造一个新的 WritePort 对象。 Constructs a new WritePort object.
 
size_t EmptySize ()
 获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
 
size_t Size ()
 获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
 
bool Writable ()
 判断端口是否可写。 Checks whether the port is writable.
 
WritePortoperator= (WriteFun fun)
 赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.
 
void UpdateStatus (bool in_isr, ErrorCode ans, WriteOperation &op, uint32_t size)
 更新写入操作的状态。 Updates the status of the write operation.
 
void UpdateStatus (WriteOperation &op)
 标记写入操作为运行中。 Marks the write operation as running.
 
ErrorCode operator() (ConstRawData data, WriteOperation &op)
 执行写入操作。 Performs a write operation.
 
void Reset ()
 Resets the WritePort.
 

Data Fields

WriteFun write_fun_ = nullptr
 
LockFreeQueue< WriteInfo > * queue_info_
 
LockFreeQueue< uint8_t > * queue_data_
 
Mutex mutex_
 
size_t write_size_ = 0
 

Detailed Description

WritePort class for handling write operations.

处理写入操作的WritePort类。

Definition at line 502 of file libxr_rw.hpp.

Constructor & Destructor Documentation

◆ WritePort()

LibXR::WritePort::WritePort ( size_t  queue_size = 3,
size_t  block_size = 128 
)
inline

构造一个新的 WritePort 对象。 Constructs a new WritePort object.

该构造函数初始化无锁操作队列 queue_info_ 和数据块队列 queue_data_。 This constructor initializes the lock-free operation queue queue_info_ and the data block queue queue_data_.

Parameters
queue_size队列的大小,默认为 3。 The size of the queue, default is 3.
block_size缓存的数据的最大大小,默认为 128。 The maximum size of cached data, default is 128.

Definition at line 529 of file libxr_rw.hpp.

530 : queue_info_(new LockFreeQueue<WriteInfo>(queue_size)),
531 queue_data_(new LockFreeQueue<uint8_t>(block_size))
532 {
533 }
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ EmptySize()

size_t LibXR::WritePort::EmptySize ( )
inline

获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.

Returns
返回数据队列的空闲大小。 Returns the empty size of the data queue.

Definition at line 542 of file libxr_rw.hpp.

542{ return queue_data_->EmptySize(); }
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue

◆ operator()()

ErrorCode LibXR::WritePort::operator() ( ConstRawData  data,
WriteOperation op 
)
inline

执行写入操作。 Performs a write operation.

该函数检查端口是否可写,并根据 data.size_op 的类型执行不同的操作。 This function checks if the port is writable and performs different actions based on data.size_ and the type of op.

Parameters
data需要写入的原始数据。 Raw data to be written.
op写入操作对象,包含操作类型和同步机制。 Write operation object containing the operation type and synchronization mechanism.
Returns
返回操作的 ErrorCode,指示操作结果。 Returns an ErrorCode indicating the result of the operation.

Definition at line 632 of file libxr_rw.hpp.

633 {
634 if (Writable())
635 {
636 if (data.size_ == 0)
637 {
638 op.UpdateStatus(false, ErrorCode::OK);
639 if (op.type == WriteOperation::OperationType::BLOCK)
640 {
641 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
642 }
643 return ErrorCode::OK;
644 }
645
646 Mutex::LockGuard lock_guard(mutex_);
647
648 if (queue_data_->EmptySize() < data.size_ || queue_info_->EmptySize() < 1)
649 {
650 return ErrorCode::FULL;
651 }
652
653 auto ans = queue_data_->PushBatch(reinterpret_cast<const uint8_t *>(data.addr_),
654 data.size_);
655
656 ans = queue_info_->Push(
657 WriteInfo{std::forward<WriteOperation>(op), static_cast<uint32_t>(data.size_)});
658
659 ans = write_fun_(*this);
660 if (op.type == WriteOperation::OperationType::BLOCK)
661 {
662 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
663 }
664 else
665 {
666 return ans;
667 }
668 }
669 else
670 {
671 return ErrorCode::NOT_SUPPORT;
672 }
673 }
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
Definition libxr_rw.hpp:560

◆ operator=()

WritePort & LibXR::WritePort::operator= ( WriteFun  fun)
inline

赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.

该函数允许使用 WriteFun 类型的函数对象赋值给 WritePort,从而设置 write_fun_。 This function allows assigning a WriteFun function object to WritePort, setting write_fun_.

Parameters
fun要分配的写入函数。 The write function to be assigned.
Returns
返回自身的引用,以支持链式调用。 Returns a reference to itself for chaining.

Definition at line 575 of file libxr_rw.hpp.

576 {
577 write_fun_ = fun;
578 return *this;
579 }

◆ Reset()

void LibXR::WritePort::Reset ( )
inline

Resets the WritePort.

重置WritePort。

Definition at line 677 of file libxr_rw.hpp.

678 {
679 queue_info_->Reset();
680 queue_data_->Reset();
681 write_size_ = 0;
682 }
void Reset()
重置队列 / Resets the queue

◆ Size()

size_t LibXR::WritePort::Size ( )
inline

获取当前数据队列的已使用大小。 Gets the used size of the current data queue.

Returns
返回数据队列的已使用大小。 Returns the size of the data queue.

Definition at line 551 of file libxr_rw.hpp.

551{ return queue_data_->Size(); }
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue

◆ UpdateStatus() [1/2]

void LibXR::WritePort::UpdateStatus ( bool  in_isr,
ErrorCode  ans,
WriteOperation op,
uint32_t  size 
)
inline

更新写入操作的状态。 Updates the status of the write operation.

该函数在写入操作过程中更新 write_size_ 并调用 UpdateStatus 方法更新 op 的状态。 This function updates write_size_ and calls UpdateStatus on op during a write operation.

Parameters
in_isr指示是否在中断上下文中执行。 Indicates whether the operation is executed in an interrupt context.
ans错误码,用于指示操作的结果。 Error code indicating the result of the operation.
op需要更新状态的 WriteOperation 引用。 Reference to the WriteOperation whose status needs to be updated.
size写入的数据大小。 The size of the written data.

Definition at line 598 of file libxr_rw.hpp.

599 {
600 write_size_ = size;
601 op.UpdateStatus(in_isr, std::forward<ErrorCode>(ans));
602 }

◆ UpdateStatus() [2/2]

void LibXR::WritePort::UpdateStatus ( WriteOperation op)
inline

标记写入操作为运行中。 Marks the write operation as running.

该函数用于将 op 标记为运行状态,以指示当前正在进行写入操作。 This function marks op as running to indicate an ongoing write operation.

Parameters
op需要更新状态的 WriteOperation 引用。 Reference to the WriteOperation whose status needs to be updated.

Definition at line 614 of file libxr_rw.hpp.

614{ op.MarkAsRunning(); }

◆ Writable()

bool LibXR::WritePort::Writable ( )
inline

判断端口是否可写。 Checks whether the port is writable.

Returns
如果 write_fun_ 不为空,则返回 true,否则返回 false。 Returns true if write_fun_ is not null, otherwise returns false.

Definition at line 560 of file libxr_rw.hpp.

560{ return write_fun_ != nullptr; }

Field Documentation

◆ mutex_

Mutex LibXR::WritePort::mutex_

Definition at line 513 of file libxr_rw.hpp.

◆ queue_data_

LockFreeQueue<uint8_t>* LibXR::WritePort::queue_data_

Definition at line 512 of file libxr_rw.hpp.

◆ queue_info_

LockFreeQueue<WriteInfo>* LibXR::WritePort::queue_info_

Definition at line 511 of file libxr_rw.hpp.

◆ write_fun_

WriteFun LibXR::WritePort::write_fun_ = nullptr

Definition at line 510 of file libxr_rw.hpp.

◆ write_size_

size_t LibXR::WritePort::write_size_ = 0

Definition at line 514 of file libxr_rw.hpp.


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