From dfa75cd42183c29dffedb9e11d281242c0000d3f Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 22 Dec 2020 23:25:38 -0800 Subject: [PATCH] ircd::stats: Abstract items into templates w/ special instantiations. --- include/ircd/stats.h | 227 ++++++++++++++++--------------------------- ircd/stats.cc | 107 -------------------- 2 files changed, 84 insertions(+), 250 deletions(-) diff --git a/include/ircd/stats.h b/include/ircd/stats.h index bcea3a824..fa8afd129 100644 --- a/include/ircd/stats.h +++ b/include/ircd/stats.h @@ -44,6 +44,10 @@ namespace ircd::stats template struct item; template<> struct item; + // Category item + template struct ptr_item; + template struct int_item; + // Pointer-to-value items template<> struct item; template<> struct item; @@ -93,184 +97,121 @@ struct ircd::stats::item virtual ~item() noexcept; }; -template<> -struct ircd::stats::item +/// Abstract pointer item +template +struct ircd::stats::ptr_item :item { - uint64_t *val {nullptr}; + T *val {nullptr}; public: bool operator!() const override { - return !val || !*val; + return !val || *val == T{0}; } - operator const uint64_t &() const + operator const T &() const { assert(val); return *val; } - operator uint64_t &() + operator T &() { assert(val); return *val; } - item(uint64_t *const &, const json::members &); - item() = default; - - item &operator=(const uint64_t &val) & + ptr_item &operator=(const T &val) & { - static_cast(*this) = val; + static_cast(*this) = val; return *this; } + + ptr_item(T *const &val, const json::members &feature) + :item{typeid(T *), feature} + ,val{val} + {} + + ptr_item() = default; +}; + +/// Abstract value item +template +struct ircd::stats::int_item +:ptr_item +{ + T val {0}; + + public: + operator const T &() const noexcept + { + return val; + } + + operator T &() noexcept + { + return val; + } + + int_item &operator=(const T &val) & + { + static_cast(*this) = val; + return *this; + } + + int_item(const json::members &feature) + :ptr_item{std::addressof(this->val), feature} + ,val{0} + {} + + int_item() = default; +}; + +template<> +struct ircd::stats::item +:ptr_item +{ + using ptr_item::ptr_item; + using ptr_item::operator=; +}; + +template<> +struct ircd::stats::item +:int_item +{ + using int_item::int_item; + using int_item::operator=; }; template<> struct ircd::stats::item -:item +:ptr_item { - uint32_t *val {nullptr}; - - public: - bool operator!() const override - { - return !val || !*val; - } - - operator const uint32_t &() const - { - assert(val); - return *val; - } - - operator uint32_t &() - { - assert(val); - return *val; - } - - item(uint32_t *const &, const json::members &); - item() = default; - - item &operator=(const uint32_t &val) & - { - static_cast(*this) = val; - return *this; - } -}; - -template<> -struct ircd::stats::item -:item -{ - uint16_t *val {nullptr}; - - public: - bool operator!() const override - { - return !val || !*val; - } - - operator const uint16_t &() const - { - assert(val); - return *val; - } - - operator uint16_t &() - { - assert(val); - return *val; - } - - item(uint16_t *const &, const json::members &); - item() = default; - - item &operator=(const uint16_t &val) & - { - static_cast(*this) = val; - return *this; - } -}; - -template<> struct ircd::stats::item -:item -{ - uint64_t val {0}; - - public: - operator const uint64_t &() const noexcept - { - return val; - } - - operator uint64_t &() noexcept - { - return val; - } - - item(const json::members &); - item() = default; - - item &operator=(const uint64_t &val) & - { - static_cast(*this) = val; - return *this; - } + using ptr_item::ptr_item; + using ptr_item::operator=; }; template<> struct ircd::stats::item -:item +:int_item { - uint32_t val {0}; + using int_item::int_item; + using int_item::operator=; +}; - public: - operator const uint32_t &() const noexcept - { - return val; - } - - operator uint32_t &() noexcept - { - return val; - } - - item(const json::members &); - item() = default; - - item &operator=(const uint32_t &val) & - { - static_cast(*this) = val; - return *this; - } +template<> +struct ircd::stats::item +:ptr_item +{ + using ptr_item::ptr_item; + using ptr_item::operator=; }; template<> struct ircd::stats::item -:item +:int_item { - uint16_t val {0}; - - public: - operator const uint16_t &() const noexcept - { - return val; - } - - operator uint16_t &() noexcept - { - return val; - } - - item(const json::members &); - item() = default; - - item &operator=(const uint16_t &val) & - { - static_cast(*this) = val; - return *this; - } + using int_item::int_item; + using int_item::operator=; }; diff --git a/ircd/stats.cc b/ircd/stats.cc index 12022a978..8920a44f9 100644 --- a/ircd/stats.cc +++ b/ircd/stats.cc @@ -166,110 +166,3 @@ const noexcept return feature[key]; } - -// -// pointer-to-value items -// - -// -// item -// - -ircd::stats::item::item(uint64_t *const &val, - const json::members &feature) -:item -{ - typeid(uint64_t *), feature -} -,val -{ - val -} -{ -} - -// -// item -// - -ircd::stats::item::item(uint32_t *const &val, - const json::members &feature) -:item -{ - typeid(uint32_t *), feature -} -,val -{ - val -} -{ -} - -// -// item -// - -ircd::stats::item::item(uint16_t *const &val, - const json::members &feature) -:item -{ - typeid(uint16_t *), feature -} -,val -{ - val -} -{ -} - -// -// value-carrying items -// - -// -// item -// - -ircd::stats::item::item(const json::members &feature) -:item -{ - std::addressof(this->val), feature -} -,val -{ - 0UL -} -{ -} - -// -// item -// - -ircd::stats::item::item(const json::members &feature) -:item -{ - std::addressof(this->val), feature -} -,val -{ - 0U -} -{ -} - -// -// item -// - -ircd::stats::item::item(const json::members &feature) -:item -{ - std::addressof(this->val), feature -} -,val -{ - 0U -} -{ -}