libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Operation< Args > Class Template Reference

Defines an operation with different execution modes. More...

#include <libxr_rw.hpp>

Collaboration diagram for LibXR::Operation< Args >:
[legend]

Public Types

enum class  OperationType : uint8_t { CALLBACK , BLOCK , POLLING , NONE }
 
enum class  OperationPollingStatus : uint8_t { READY , RUNNING , DONE , ERROR }
 
using Callback = LibXR::Callback<Args>
 

Public Member Functions

 Operation ()
 Default constructor, initializes with NONE type.
 
 Operation (Semaphore &sem, uint32_t timeout=UINT32_MAX)
 Constructs a blocking operation with a semaphore and timeout.
 
 Operation (Callback &callback)
 Constructs a callback-based operation.
 
 Operation (OperationPollingStatus &status)
 Constructs a polling operation.
 
Operationoperator= (const Operation &op)
 Copy assignment operator.
 
Operationoperator= (Operation &&op) noexcept
 Move assignment operator.
 
template<typename InitOperation , typename = std::enable_if_t<std::is_same_v<std::decay_t<InitOperation>, Operation>>>
 Operation (InitOperation &&op)
 构造一个新的 Operation 对象(初始化操作)。 Constructs a new Operation object (initialization operation).
 
template<typename Status >
void UpdateStatus (bool in_isr, Status &&status)
 Updates operation status based on type.
 
void MarkAsRunning ()
 标记操作为运行状态。 Marks the operation as running.
 

Data Fields

union { 
 
   Callback *   callback 
 
   struct { 
 
      Semaphore *   sem 
 
      uint32_t   timeout 
 
   }   sem_info 
 
   OperationPollingStatus *   status 
 
data 
 
OperationType type
 

Detailed Description

template<typename Args>
class LibXR::Operation< Args >

Defines an operation with different execution modes.

定义了一种具有不同执行模式的操作。

Template Parameters
ArgsThe parameter types for callback operations.
Args用于回调操作的参数类型。

Definition at line 28 of file libxr_rw.hpp.

Member Typedef Documentation

◆ Callback

template<typename Args >
using LibXR::Operation< Args >::Callback = LibXR::Callback<Args>

Definition at line 31 of file libxr_rw.hpp.

Member Enumeration Documentation

◆ OperationPollingStatus

template<typename Args >
enum class LibXR::Operation::OperationPollingStatus : uint8_t
strong

Polling operation status. 轮询操作的状态。

Definition at line 45 of file libxr_rw.hpp.

46 {
47 READY,
48 RUNNING,
49 DONE,
50 ERROR
51 };

◆ OperationType

template<typename Args >
enum class LibXR::Operation::OperationType : uint8_t
strong

Operation types. 操作类型。

Definition at line 35 of file libxr_rw.hpp.

36 {
37 CALLBACK,
38 BLOCK,
39 POLLING,
40 NONE
41 };

Constructor & Destructor Documentation

◆ Operation() [1/5]

template<typename Args >
LibXR::Operation< Args >::Operation ( )
inline

Default constructor, initializes with NONE type.

默认构造函数,初始化为NONE类型。

Definition at line 55 of file libxr_rw.hpp.

55: type(OperationType::NONE) { Memory::FastSet(&data, 0, sizeof(data)); }
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
union LibXR::Operation::@5 data
OperationType type
Definition libxr_rw.hpp:231

◆ Operation() [2/5]

template<typename Args >
LibXR::Operation< Args >::Operation ( Semaphore & sem,
uint32_t timeout = UINT32_MAX )
inline

Constructs a blocking operation with a semaphore and timeout.

使用信号量和超时构造阻塞操作。

Parameters
semSemaphore reference.
timeoutTimeout duration (default is maximum).

Definition at line 63 of file libxr_rw.hpp.

63 : type(OperationType::BLOCK)
64 {
65 data.sem_info.sem = &sem;
66 data.sem_info.timeout = timeout;
67 }

◆ Operation() [3/5]

template<typename Args >
LibXR::Operation< Args >::Operation ( Callback & callback)
inline

Constructs a callback-based operation.

构造基于回调的操作。

Parameters
callbackCallback function reference.

Definition at line 74 of file libxr_rw.hpp.

74 : type(OperationType::CALLBACK)
75 {
76 data.callback = &callback;
77 }

◆ Operation() [4/5]

template<typename Args >
LibXR::Operation< Args >::Operation ( OperationPollingStatus & status)
inline

Constructs a polling operation.

构造轮询操作。

Parameters
statusReference to polling status.

Definition at line 84 of file libxr_rw.hpp.

84 : type(OperationType::POLLING)
85 {
86 data.status = &status;
87 }

◆ Operation() [5/5]

template<typename Args >
template<typename InitOperation , typename = std::enable_if_t<std::is_same_v<std::decay_t<InitOperation>, Operation>>>
LibXR::Operation< Args >::Operation ( InitOperation< Args > && op)
inline

构造一个新的 Operation 对象(初始化操作)。 Constructs a new Operation object (initialization operation).

该构造函数用于初始化一个 Operation 对象,接收一个初始化操作作为参数。 This constructor initializes an Operation object with an initialization operation as a parameter.

Definition at line 160 of file libxr_rw.hpp.

161 {
162 *this = std::forward<InitOperation>(op);
163 }

Member Function Documentation

◆ MarkAsRunning()

template<typename Args >
void LibXR::Operation< Args >::MarkAsRunning ( )
inline

标记操作为运行状态。 Marks the operation as running.

该函数用于在操作类型为 POLLING 时,将 data.status 设置为 RUNNING, 以指示该操作正在执行中。 This function sets data.status to RUNNING when the operation type is POLLING, indicating that the operation is currently in progress.

Note
该方法仅适用于 OperationType::POLLING 类型的操作,其他类型不会受到影响。 This method only applies to operations of type OperationType::POLLING, and other types remain unaffected.

Definition at line 207 of file libxr_rw.hpp.

208 {
209 if (type == OperationType::POLLING)
210 {
211 *data.status = OperationPollingStatus::RUNNING;
212 }
213 }

◆ operator=() [1/2]

template<typename Args >
Operation & LibXR::Operation< Args >::operator= ( const Operation< Args > & op)
inline

Copy assignment operator.

复制赋值运算符。

Parameters
opAnother Operation instance.
Returns
Reference to this operation.

Definition at line 95 of file libxr_rw.hpp.

96 {
97 if (this != &op)
98 {
99 type = op.type;
100 switch (type)
101 {
102 case OperationType::CALLBACK:
103 data.callback = op.data.callback;
104 break;
105 case OperationType::BLOCK:
106 data.sem_info.sem = op.data.sem_info.sem;
107 data.sem_info.timeout = op.data.sem_info.timeout;
108 break;
109 case OperationType::POLLING:
110 data.status = op.data.status;
111 break;
112 case OperationType::NONE:
113 break;
114 }
115 }
116 return *this;
117 }

◆ operator=() [2/2]

template<typename Args >
Operation & LibXR::Operation< Args >::operator= ( Operation< Args > && op)
inlinenoexcept

Move assignment operator.

移动赋值运算符。

Parameters
opAnother Operation instance.
Returns
Reference to this operation.

Definition at line 125 of file libxr_rw.hpp.

126 {
127 if (this != &op)
128 {
129 type = op.type;
130 switch (type)
131 {
132 case OperationType::CALLBACK:
133 data.callback = op.data.callback;
134 break;
135 case OperationType::BLOCK:
136 data.sem_info.sem = op.data.sem_info.sem;
137 data.sem_info.timeout = op.data.sem_info.timeout;
138 break;
139 case OperationType::POLLING:
140 data.status = op.data.status;
141 break;
142 case OperationType::NONE:
143 break;
144 }
145 }
146 return *this;
147 }

◆ UpdateStatus()

template<typename Args >
template<typename Status >
void LibXR::Operation< Args >::UpdateStatus ( bool in_isr,
Status && status )
inline

Updates operation status based on type.

根据类型更新操作状态。

Parameters
in_isrIndicates if executed within an interrupt.
argsParameters passed to the callback.

Definition at line 172 of file libxr_rw.hpp.

173 {
174 //TODO: 任何操作类型都应拿到执行结果。
175 switch (type)
176 {
177 case OperationType::CALLBACK:
178 data.callback->Run(in_isr, std::forward<Status>(status));
179 break;
180 case OperationType::BLOCK:
181 data.sem_info.sem->PostFromCallback(in_isr);
182 break;
183 case OperationType::POLLING:
184 /* 中断不允许阻塞语义 / ISR does not allow blocking semantics */
185 ASSERT(!in_isr);
186 *data.status = (status == ErrorCode::OK) ? OperationPollingStatus::DONE
187 : OperationPollingStatus::ERROR;
188 break;
189 case OperationType::NONE:
190 break;
191 }
192 }

Field Documentation

◆ callback

template<typename Args >
Callback* LibXR::Operation< Args >::callback

Definition at line 219 of file libxr_rw.hpp.

◆ [union]

union { ... } LibXR::Operation< Args >::data

Data storage for different operation types. 存储不同操作类型的数据。

◆ sem

template<typename Args >
Semaphore* LibXR::Operation< Args >::sem

Definition at line 222 of file libxr_rw.hpp.

◆ status

template<typename Args >
OperationPollingStatus* LibXR::Operation< Args >::status

Definition at line 225 of file libxr_rw.hpp.

◆ timeout

template<typename Args >
uint32_t LibXR::Operation< Args >::timeout

Definition at line 223 of file libxr_rw.hpp.

◆ type

template<typename Args >
OperationType LibXR::Operation< Args >::type

Operation type. 操作类型。

Definition at line 231 of file libxr_rw.hpp.


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