libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_cb.hpp
1#pragma once
2
3#include <cstring>
4#include <utility>
5
6namespace LibXR
7{
8
20template <typename ArgType, typename... Args>
22{
23 public:
29 using FunctionType = void (*)(bool, ArgType, Args...);
30
44 template <typename FunType, typename ArgT>
45 CallbackBlock(FunType &&fun, ArgT &&arg)
46 : fun_(std::forward<FunType>(fun)), arg_(std::forward<ArgT>(arg))
47 {
48 }
49
59 void Call(bool in_isr, Args &&...args)
60 {
61 in_isr_ = in_isr;
62 if (fun_)
63 {
64 fun_(in_isr, arg_, std::forward<Args>(args)...);
65 }
66 }
67
68 // 禁用拷贝构造与拷贝赋值
69 // Copy construction and copy assignment are disabled.
70 CallbackBlock(const CallbackBlock &other) = delete;
71 CallbackBlock &operator=(const CallbackBlock &other) = delete;
72
80 CallbackBlock(CallbackBlock &&other) noexcept
81 : fun_(std::exchange(other.fun_, nullptr)),
82 arg_(std::move(other.arg_)),
83 in_isr_(other.in_isr_)
84 {
85 }
86
97 {
98 if (this != &other)
99 {
100 fun_ = std::exchange(other.fun_, nullptr);
101 arg_ = std::move(other.arg_);
102 in_isr_ = other.in_isr_;
103 }
104 return *this;
105 }
106
107 private:
108 void (*fun_)(bool, ArgType,
109 Args...);
110 ArgType arg_;
111 bool in_isr_ = false;
113};
114
123template <typename... Args>
125{
126 public:
142 template <typename FunType, typename ArgType>
143 static Callback Create(FunType fun, ArgType arg)
144 {
145 void (*fun_ptr)(bool, ArgType, Args...) = fun;
146 auto cb_block = new CallbackBlock<ArgType, Args...>(fun_ptr, arg);
147
148 auto cb_fun = [](bool in_isr, void *cb_block, Args... args)
149 {
150 auto *cb = static_cast<CallbackBlock<ArgType, Args...> *>(cb_block);
151 cb->Call(in_isr, std::forward<Args>(args)...);
152 };
153
154 return Callback(cb_block, cb_fun);
155 }
156
161 Callback() : cb_block_(nullptr), cb_fun_(nullptr) {}
162
163 Callback(const Callback &) = default;
164 Callback &operator=(const Callback &) = default;
165
173 Callback(Callback &&other) noexcept
174 : cb_block_(std::exchange(other.cb_block_, nullptr)),
175 cb_fun_(std::exchange(other.cb_fun_, nullptr))
176 {
177 }
178
188 Callback &operator=(Callback &&other) noexcept
189 {
190 if (this != &other)
191 {
192 cb_block_ = std::exchange(other.cb_block_, nullptr);
193 cb_fun_ = std::exchange(other.cb_fun_, nullptr);
194 }
195 return *this;
196 }
197
207 template <typename... PassArgs>
208 void Run(bool in_isr, PassArgs &&...args) const
209 {
210 if (cb_fun_)
211 {
212 cb_fun_(in_isr, cb_block_, std::forward<PassArgs>(args)...);
213 }
214 }
215
223 bool Empty() const { return cb_block_ == nullptr || cb_fun_ == nullptr; }
224
225 private:
235 Callback(void *cb_block, void (*cb_fun)(bool, void *, Args...))
236 : cb_block_(cb_block), cb_fun_(cb_fun)
237 {
238 }
239
240 void *cb_block_;
241 void (*cb_fun_)(
242 bool, void *,
243 Args...);
244};
245
246} // namespace LibXR
提供一个回调函数的封装,实现参数绑定和回调执行。 Provides a wrapper for callback functions, enabling argument binding and inv...
Definition libxr_cb.hpp:22
void(*)(bool, ArgType, Args...) FunctionType
定义回调函数类型。 Defines the type of the callback function.
Definition libxr_cb.hpp:29
CallbackBlock(FunType &&fun, ArgT &&arg)
构造回调块,绑定回调函数和参数。 Constructs a callback block, binding a function and an argument.
Definition libxr_cb.hpp:45
CallbackBlock & operator=(CallbackBlock &&other) noexcept
移动赋值运算符,转移回调函数与参数。 Move assignment operator, transferring the callback function and argument.
Definition libxr_cb.hpp:96
void Call(bool in_isr, Args &&...args)
调用回调函数,并传递额外参数。 Calls the callback function, passing additional arguments.
Definition libxr_cb.hpp:59
CallbackBlock(CallbackBlock &&other) noexcept
移动构造函数,转移回调函数与参数。 Move constructor, transferring the callback function and argument.
Definition libxr_cb.hpp:80
ArgType arg_
绑定的参数。 The bound argument.
Definition libxr_cb.hpp:110
void(* fun_)(bool, ArgType, Args...)
绑定的回调函数。 The bound callback function.
Definition libxr_cb.hpp:108
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
Definition libxr_cb.hpp:125
void * cb_block_
存储回调块的指针。 Pointer to the callback block.
Definition libxr_cb.hpp:240
Callback(void *cb_block, void(*cb_fun)(bool, void *, Args...))
私有构造函数,仅用于内部创建回调实例。 Private constructor, used internally for creating callback instances.
Definition libxr_cb.hpp:235
Callback & operator=(Callback &&other) noexcept
移动赋值运算符,转移回调对象的所有权。 Move assignment operator, transferring ownership of the callback object.
Definition libxr_cb.hpp:188
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
Definition libxr_cb.hpp:208
static Callback Create(FunType fun, ArgType arg)
创建一个新的回调对象,并绑定回调函数和参数。 Creates a new callback instance, binding a function and an argument.
Definition libxr_cb.hpp:143
Callback(Callback &&other) noexcept
移动构造函数,转移回调对象的所有权。 Move constructor, transferring ownership of the callback object.
Definition libxr_cb.hpp:173
bool Empty() const
检查回调是否为空。 Checks if the callback is empty.
Definition libxr_cb.hpp:223
void(* cb_fun_)(bool, void *, Args...)
存储回调执行的函数指针。 Pointer to the callback execution function.
Definition libxr_cb.hpp:241
Callback()
默认构造函数,创建空回调对象。 Default constructor, creating an empty callback instance.
Definition libxr_cb.hpp:161
LibXR 命名空间
Definition ch32_gpio.hpp:9