mirror of
https://github.com/matrix-construct/construct
synced 2024-06-11 14:38:57 +02:00
ircd::stats: Add value; add operator/access suite.
This commit is contained in:
parent
c427223340
commit
9907f7f477
|
@ -14,27 +14,97 @@
|
|||
namespace ircd::stats
|
||||
{
|
||||
struct item;
|
||||
using value_type = int128_t;
|
||||
|
||||
IRCD_EXCEPTION(ircd::error, error)
|
||||
IRCD_EXCEPTION(error, not_found)
|
||||
|
||||
extern std::map<string_view, item *> items;
|
||||
|
||||
const value_type &get(const item &);
|
||||
value_type &get(item &);
|
||||
|
||||
value_type &inc(item &, const value_type & = 1);
|
||||
value_type &dec(item &, const value_type & = 1);
|
||||
value_type &set(item &, const value_type & = 0);
|
||||
}
|
||||
|
||||
struct ircd::stats::item
|
||||
{
|
||||
using callback = std::function<string_view (const mutable_buffer &)>;
|
||||
|
||||
static const size_t NAME_MAX_LEN;
|
||||
|
||||
json::strung feature_;
|
||||
json::object feature;
|
||||
string_view name;
|
||||
callback cb;
|
||||
value_type val;
|
||||
|
||||
public:
|
||||
item(const json::members &, callback);
|
||||
item &operator+=(const value_type &) &;
|
||||
item &operator-=(const value_type &) &;
|
||||
item &operator=(const value_type &) &;
|
||||
|
||||
item(const json::members &);
|
||||
item(item &&) = delete;
|
||||
item(const item &) = delete;
|
||||
~item() noexcept;
|
||||
};
|
||||
|
||||
inline ircd::stats::item &
|
||||
ircd::stats::item::operator=(const value_type &v)
|
||||
&
|
||||
{
|
||||
set(*this, v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ircd::stats::item &
|
||||
ircd::stats::item::operator-=(const value_type &v)
|
||||
&
|
||||
{
|
||||
dec(*this, v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ircd::stats::item &
|
||||
ircd::stats::item::operator+=(const value_type &v)
|
||||
&
|
||||
{
|
||||
inc(*this, v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ircd::stats::value_type &
|
||||
ircd::stats::set(item &item,
|
||||
const value_type &v)
|
||||
{
|
||||
item.val = v;
|
||||
return get(item);
|
||||
}
|
||||
|
||||
inline ircd::stats::value_type &
|
||||
ircd::stats::dec(item &item,
|
||||
const value_type &n)
|
||||
{
|
||||
item.val -= n;
|
||||
return get(item);
|
||||
}
|
||||
|
||||
inline ircd::stats::value_type &
|
||||
ircd::stats::inc(item &item,
|
||||
const value_type &n)
|
||||
{
|
||||
item.val += n;
|
||||
return get(item);
|
||||
}
|
||||
|
||||
inline ircd::stats::value_type &
|
||||
ircd::stats::get(item &item)
|
||||
{
|
||||
return item.val;
|
||||
}
|
||||
|
||||
inline const ircd::stats::value_type &
|
||||
ircd::stats::get(const item &item)
|
||||
{
|
||||
return item.val;
|
||||
}
|
||||
|
|
|
@ -22,8 +22,7 @@ ircd::stats::item::NAME_MAX_LEN
|
|||
127
|
||||
};
|
||||
|
||||
ircd::stats::item::item(const json::members &opts,
|
||||
callback cb)
|
||||
ircd::stats::item::item(const json::members &opts)
|
||||
:feature_
|
||||
{
|
||||
opts
|
||||
|
@ -36,9 +35,9 @@ ircd::stats::item::item(const json::members &opts,
|
|||
{
|
||||
unquote(feature.at("name"))
|
||||
}
|
||||
,cb
|
||||
,val
|
||||
{
|
||||
std::move(cb)
|
||||
0UL
|
||||
}
|
||||
{
|
||||
if(name.size() > NAME_MAX_LEN)
|
||||
|
|
Loading…
Reference in a new issue