2019-01-03 00:18:15 +01:00
|
|
|
// 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
|
|
|
|
|
2019-06-23 10:01:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
inline namespace util
|
2019-01-03 00:18:15 +01:00
|
|
|
{
|
|
|
|
template<class T> struct scope_restore;
|
2019-06-23 10:01:26 +02:00
|
|
|
}}
|
2019-01-03 00:18:15 +01:00
|
|
|
|
2020-05-29 00:17:05 +02:00
|
|
|
/// Overwrite a value for the duration of the instance restoring the
|
|
|
|
/// original value at destruction. Device cannot be moved or copied.
|
|
|
|
///
|
2019-01-03 00:18:15 +01:00
|
|
|
template<class T>
|
|
|
|
struct ircd::util::scope_restore
|
|
|
|
{
|
|
|
|
T *restore {nullptr};
|
|
|
|
T theirs;
|
|
|
|
|
2019-09-20 04:58:46 +02:00
|
|
|
scope_restore(T &restore) noexcept;
|
2019-01-03 00:18:15 +01:00
|
|
|
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)
|
2019-09-20 04:58:46 +02:00
|
|
|
noexcept
|
2019-01-03 00:18:15 +01:00
|
|
|
: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);
|
|
|
|
}
|