mirror of
https://github.com/matrix-construct/construct
synced 2024-09-30 20:58:51 +02:00
ircd::util: Add scope_count tool.
This commit is contained in:
parent
09424ab344
commit
7628b8b7f7
2 changed files with 46 additions and 0 deletions
45
include/ircd/util/scope_count.h
Normal file
45
include/ircd/util/scope_count.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_UTIL_SCOPE_COUNT_H
|
||||
|
||||
namespace ircd::util
|
||||
{
|
||||
struct scope_count;
|
||||
};
|
||||
|
||||
/// A simple boiler-plate for incrementing a counter when constructed and
|
||||
/// decrementing it to its previous value when destructed. This takes a runtime
|
||||
/// reference to that counter.
|
||||
struct ircd::util::scope_count
|
||||
{
|
||||
ulong *count {nullptr};
|
||||
|
||||
scope_count(ulong &count);
|
||||
scope_count(const scope_count &) = delete;
|
||||
scope_count(scope_count &&) noexcept = delete;
|
||||
~scope_count() noexcept;
|
||||
};
|
||||
|
||||
inline
|
||||
ircd::util::scope_count::scope_count(ulong &count)
|
||||
:count{&count}
|
||||
{
|
||||
++count;
|
||||
}
|
||||
|
||||
inline
|
||||
ircd::util::scope_count::~scope_count()
|
||||
noexcept
|
||||
{
|
||||
assert(count);
|
||||
--(*count);
|
||||
}
|
|
@ -46,6 +46,7 @@ namespace ircd
|
|||
#include "tuple.h"
|
||||
#include "timer.h"
|
||||
#include "scope_light.h"
|
||||
#include "scope_count.h"
|
||||
#include "life_guard.h"
|
||||
#include "pubsetbuf.h"
|
||||
#include "string.h"
|
||||
|
|
Loading…
Reference in a new issue