0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-29 12:18:54 +02:00

ircd::util: Add scope_restore; remove scope_light because generalized.

This commit is contained in:
Jason Volk 2019-01-02 15:18:15 -08:00
parent 815252e71f
commit 1cb3db6726
3 changed files with 66 additions and 49 deletions

View file

@ -1,48 +0,0 @@
// 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;
}

View file

@ -0,0 +1,65 @@
// 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_RESTORE_H
namespace ircd::util
{
template<class T> struct scope_restore;
};
template<class T>
struct ircd::util::scope_restore
{
T *restore {nullptr};
T theirs;
scope_restore(T &restore);
scope_restore(T &restore, T&& ours);
template<class... args> scope_restore(T &restore, args&&... ours);
scope_restore(const scope_restore &) = delete;
scope_restore(scope_restore &&) noexcept = delete;
~scope_restore() noexcept;
};
template<class T>
ircd::util::scope_restore<T>::scope_restore(T &restore)
:restore{&restore}
,theirs{std::move(restore)}
{
}
template<class T>
ircd::util::scope_restore<T>::scope_restore(T &restore,
T&& ours)
:restore{&restore}
,theirs{std::move(restore)}
{
restore = std::forward<T>(ours);
}
template<class T>
template<class... args>
ircd::util::scope_restore<T>::scope_restore(T &restore,
args&&... ours)
:restore{&restore}
,theirs{std::move(restore)}
{
new (&restore) T(std::forward<args>(ours)...);
}
template<class T>
ircd::util::scope_restore<T>::~scope_restore()
noexcept
{
assert(restore);
*restore = std::move(theirs);
}

View file

@ -45,8 +45,8 @@ namespace ircd
#include "bswap.h"
#include "tuple.h"
#include "timer.h"
#include "scope_light.h"
#include "scope_count.h"
#include "scope_restore.h"
#include "life_guard.h"
#include "pubsetbuf.h"
#include "string.h"