使用boost pfr 遍历结构体
This commit is contained in:
+51
-1
@@ -14,6 +14,54 @@
|
||||
|
||||
|
||||
namespace Psc {
|
||||
template<class T>
|
||||
class Copyable_Atomic {
|
||||
public:
|
||||
using value_type = T;
|
||||
Copyable_Atomic() = default;
|
||||
Copyable_Atomic(T value) : value_(value) {}
|
||||
Copyable_Atomic(const Copyable_Atomic& other) : value_(other.load()) {}
|
||||
Copyable_Atomic& operator=(const Copyable_Atomic& other) {
|
||||
store(other.load());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Copyable_Atomic& operator=(T value) {
|
||||
store(value);
|
||||
return *this;
|
||||
}
|
||||
T load(std::memory_order order = std::memory_order_seq_cst) const {
|
||||
return value_.load(order);
|
||||
}
|
||||
void store(T value, std::memory_order order = std::memory_order_seq_cst) {
|
||||
value_.store(value, order);
|
||||
}
|
||||
operator T() const {
|
||||
return load();
|
||||
}
|
||||
private:
|
||||
std::atomic<T> value_{};
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using clean_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
namespace PV {
|
||||
template<typename T> struct is_copyable_atomic_helper : std::false_type {};
|
||||
template<typename U> struct is_copyable_atomic_helper<Copyable_Atomic<U>> : std::true_type {};
|
||||
template<typename T> struct is_copyable_atomic : is_copyable_atomic_helper<clean_t<T>> {};
|
||||
template<typename T> struct copyable_atomic_value;
|
||||
template<typename U> struct copyable_atomic_value<Copyable_Atomic<U>> {
|
||||
using type = U;
|
||||
};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr bool is_copyable_atomic_v = PV::is_copyable_atomic<T>::value;
|
||||
|
||||
template<typename T>
|
||||
using copyable_atomic_value_t = typename PV::copyable_atomic_value<clean_t<T>>::type;
|
||||
|
||||
template<typename T> struct always_false : std::false_type {};
|
||||
template<typename T> using clean_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
namespace PV {
|
||||
@@ -165,10 +213,12 @@ namespace Psc {
|
||||
else if constexpr (std::is_arithmetic_v<C>) {
|
||||
return std::to_string(v); // 使用 std::to_string 转换数值类型
|
||||
}
|
||||
|
||||
else if constexpr (is_atomic_v<C>) {
|
||||
return to_string(v.load());
|
||||
}
|
||||
else if constexpr (is_copyable_atomic_v<C>) {
|
||||
return to_string(v.load());
|
||||
}
|
||||
else if constexpr (is_optional_v<C>) {
|
||||
return v.has_value() ? to_string(v.value()) : "nullopt";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user