From: Otto Moerbeek Date: Wed, 27 Mar 2024 13:21:42 +0000 (+0100) Subject: Tidy stat_t X-Git-Tag: rec-5.1.0-alpha1~55^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eca5db9f9aae5c0ff2b667a61c96c5ec52ffd46a;p=thirdparty%2Fpdns.git Tidy stat_t --- diff --git a/pdns/stat_t.hh b/pdns/stat_t.hh index d339cec37d..b084e761ec 100644 --- a/pdns/stat_t.hh +++ b/pdns/stat_t.hh @@ -30,53 +30,67 @@ namespace pdns { template class stat_t_trait { public: - typedef T base_t; - typedef std::atomic atomic_t; + using base_t = T; + using atomic_t = std::atomic; stat_t_trait() : stat_t_trait(base_t(0)) { } - stat_t_trait(const base_t x) { - new(&counter) atomic_t(x); + stat_t_trait(const base_t value) + { + new(&counter) atomic_t(value); + } + stat_t_trait& operator=(const base_t& value) { + ref().store(value); + return *this; } ~stat_t_trait() { - reinterpret_cast(&counter)->~atomic_t(); + ref().~atomic_t(); } + stat_t_trait(stat_t_trait&&) = delete; + stat_t_trait& operator=(const stat_t_trait&) = delete; + stat_t_trait& operator=(stat_t_trait&&) = delete; stat_t_trait(const stat_t_trait&) = delete; base_t operator++(int) { - return (*reinterpret_cast(&counter))++; + return ref()++; } base_t operator++() { - return ++(*reinterpret_cast(&counter)); + return ++(ref()); } base_t operator--(int) { - return (*reinterpret_cast(&counter))--; + return ref()--; } base_t operator--() { - return --(*reinterpret_cast(&counter)); + return --(ref()); } - base_t operator+=(const stat_t_trait& v) { - return *reinterpret_cast(&counter) += *reinterpret_cast(&v.counter); + base_t operator+=(const stat_t_trait& arg) { + return ref() += arg.ref(); } - base_t operator-=(const stat_t_trait& v) { - return *reinterpret_cast(&counter) -= *reinterpret_cast(&v.counter); + base_t operator-=(const stat_t_trait& arg) { + return ref() -= arg.ref(); } base_t load() const { - return reinterpret_cast(&counter)->load(); + return ref().load(); } - void store(base_t v) { - reinterpret_cast(&counter)->store(v); + void store(base_t value) { + ref().store(value); } operator base_t() const { - return reinterpret_cast(&counter)->load(); + return ref().load(); } private: + atomic_t& ref() { + return *reinterpret_cast(&counter); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + } + const atomic_t& ref() const { + return *reinterpret_cast(&counter); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + } typename std::aligned_storage_t counter; }; - typedef stat_t_trait stat_t; - typedef stat_t_trait stat32_t; - typedef stat_t_trait stat16_t; + using stat_t = stat_t_trait; + using stat32_t = stat_t_trait; + using stat16_t = stat_t_trait; } #else namespace pdns {