mirror of
https://github.com/matrix-construct/construct
synced 2024-11-17 07:20:55 +01:00
ircd::util: Add scope_light tool.
This commit is contained in:
parent
84265c3a13
commit
09424ab344
2 changed files with 49 additions and 0 deletions
48
include/ircd/util/scope_light.h
Normal file
48
include/ircd/util/scope_light.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
// 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_LIGHT_H
|
||||
|
||||
namespace ircd::util
|
||||
{
|
||||
struct scope_light;
|
||||
};
|
||||
|
||||
/// A simple boiler-plate for setting a bool to true when constructed and
|
||||
/// resetting it to its previous value when destructed. This takes a runtime
|
||||
/// reference to that bool. This is not intended to be a replacement for the
|
||||
/// reentrance_assertion utility or ctx::mutex et al.
|
||||
struct ircd::util::scope_light
|
||||
{
|
||||
bool *light {nullptr};
|
||||
bool theirs {false};
|
||||
|
||||
scope_light(bool &light);
|
||||
scope_light(const scope_light &) = delete;
|
||||
scope_light(scope_light &&) noexcept = delete;
|
||||
~scope_light() noexcept;
|
||||
};
|
||||
|
||||
inline
|
||||
ircd::util::scope_light::scope_light(bool &light)
|
||||
:light{&light}
|
||||
,theirs{light}
|
||||
{
|
||||
light = true;
|
||||
}
|
||||
|
||||
inline
|
||||
ircd::util::scope_light::~scope_light()
|
||||
noexcept
|
||||
{
|
||||
assert(light);
|
||||
*light = theirs;
|
||||
}
|
|
@ -45,6 +45,7 @@ namespace ircd
|
|||
#include "bswap.h"
|
||||
#include "tuple.h"
|
||||
#include "timer.h"
|
||||
#include "scope_light.h"
|
||||
#include "life_guard.h"
|
||||
#include "pubsetbuf.h"
|
||||
#include "string.h"
|
||||
|
|
Loading…
Reference in a new issue