libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
flag.hpp
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5
6namespace LibXR
7{
8
19namespace Flag
20{
21
31class Atomic
32{
33 public:
37 Atomic() = default;
38
42 void Set() noexcept { value_.store(1u, std::memory_order_release); }
43
47 void Clear() noexcept { value_.store(0u, std::memory_order_release); }
48
55 [[nodiscard]] bool IsSet() const noexcept
56 {
57 return value_.load(std::memory_order_acquire) != 0u;
58 }
59
66 [[nodiscard]] bool TestAndSet() noexcept
67 {
68 return value_.exchange(1u, std::memory_order_acq_rel) != 0u;
69 }
70
77 [[nodiscard]] bool TestAndClear() noexcept
78 {
79 return value_.exchange(0u, std::memory_order_acq_rel) != 0u;
80 }
81
90 [[nodiscard]] bool Exchange(bool set_value) noexcept
91 {
92 return value_.exchange(set_value ? 1u : 0u, std::memory_order_acq_rel) != 0u;
93 }
94
98 Atomic(const Atomic&) = delete;
99 Atomic& operator=(const Atomic&) = delete;
100
101 private:
102 std::atomic<uint8_t> value_{
103 0u};
104};
105
114class Plain
115{
116 public:
120 Plain() = default;
121
125 void Set() noexcept { value_ = true; }
126
130 void Clear() noexcept { value_ = false; }
131
138 [[nodiscard]] bool IsSet() const noexcept { return value_; }
139
146 [[nodiscard]] bool TestAndSet() noexcept
147 {
148 bool old = value_;
149 value_ = true;
150 return old;
151 }
152
159 [[nodiscard]] bool TestAndClear() noexcept
160 {
161 bool old = value_;
162 value_ = false;
163 return old;
164 }
165
174 [[nodiscard]] bool Exchange(bool set_value) noexcept
175 {
176 bool old = value_;
177 value_ = set_value;
178 return old;
179 }
180
181 private:
182 bool value_{
183 false};
184};
185
197template <typename FlagT>
199{
200 public:
208 explicit ScopedRestore(FlagT& flag, bool set_value = true)
209 : flag_(flag), prev_(flag_.Exchange(set_value))
210 {
211 }
212
216 ~ScopedRestore() { (void)flag_.Exchange(prev_); }
217
221 ScopedRestore(const ScopedRestore&) = delete;
222 ScopedRestore& operator=(const ScopedRestore&) = delete;
223
224 private:
225 FlagT& flag_;
226 bool prev_;
227};
228
229} // namespace Flag
230} // namespace LibXR
原子标志位 / Atomic flag
Definition flag.hpp:32
void Set() noexcept
置位标志 / Set the flag
Definition flag.hpp:42
Atomic(const Atomic &)=delete
禁用拷贝构造与拷贝赋值 / Copy construction and copy assignment are disabled
bool TestAndClear() noexcept
测试并清除:清除并返回旧状态 / Test-and-clear: clear and return previous state
Definition flag.hpp:77
Atomic()=default
构造函数,默认未置位 / Constructor, default cleared
bool TestAndSet() noexcept
测试并置位:置位并返回旧状态 / Test-and-set: set and return previous state
Definition flag.hpp:66
bool IsSet() const noexcept
判断是否已置位 / Check whether the flag is set
Definition flag.hpp:55
bool Exchange(bool set_value) noexcept
交换:写入指定值并返回旧状态 / Exchange: set to desired value and return previous state
Definition flag.hpp:90
void Clear() noexcept
清除标志 / Clear the flag
Definition flag.hpp:47
std::atomic< uint8_t > value_
标志值(0=clear, 1=set)/ Flag value (0=clear, 1=set)
Definition flag.hpp:102
普通标志位(非原子)/ Non-atomic flag
Definition flag.hpp:115
Plain()=default
构造函数,默认未置位 / Constructor, default cleared
void Set() noexcept
置位标志 / Set the flag
Definition flag.hpp:125
bool TestAndSet() noexcept
测试并置位:置位并返回旧状态 / Test-and-set: set and return previous state
Definition flag.hpp:146
bool TestAndClear() noexcept
测试并清除:清除并返回旧状态 / Test-and-clear: clear and return previous state
Definition flag.hpp:159
bool IsSet() const noexcept
判断是否已置位 / Check whether the flag is set
Definition flag.hpp:138
void Clear() noexcept
清除标志 / Clear the flag
Definition flag.hpp:130
bool Exchange(bool set_value) noexcept
交换:写入指定值并返回旧状态 / Exchange: set to desired value and return previous state
Definition flag.hpp:174
bool value_
标志值(false=clear, true=set)/ Flag value (false=clear, true=set)
Definition flag.hpp:182
作用域标志管理器:构造时写入指定值,析构时恢复原值 / Scoped flag restorer: set on entry, restore on exit
Definition flag.hpp:199
bool prev_
进入作用域前的旧值 / Previous value before entering scope
Definition flag.hpp:226
~ScopedRestore()
析构函数:恢复旧值 / Destructor: restore previous value
Definition flag.hpp:216
ScopedRestore(const ScopedRestore &)=delete
禁用拷贝构造与拷贝赋值 / Copy construction and copy assignment are disabled
FlagT & flag_
被管理的标志对象 / Managed flag instance
Definition flag.hpp:225
ScopedRestore(FlagT &flag, bool set_value=true)
构造函数:写入 set_value 并保存旧值 / Constructor: set set_value and store previous value
Definition flag.hpp:208
LibXR 命名空间
Definition ch32_can.hpp:14