mirror of
https://github.com/matrix-construct/construct
synced 2024-10-04 22:58:52 +02:00
ircd::util: Add scope::nominal and scope::exceptional constructs.
This commit is contained in:
parent
50b2942131
commit
95a52a4ab7
1 changed files with 38 additions and 21 deletions
|
@ -63,46 +63,63 @@ struct NAME \
|
||||||
IRCD_STRONG_TYPEDEF(TYPE, IRCD_UNIQUE(strong_t))
|
IRCD_STRONG_TYPEDEF(TYPE, IRCD_UNIQUE(strong_t))
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
using custom_ptr = std::unique_ptr<T, std::function<void (T *) noexcept>>;
|
|
||||||
|
|
||||||
|
|
||||||
struct scope
|
struct scope
|
||||||
{
|
{
|
||||||
struct uncaught;
|
struct nominal;
|
||||||
|
struct exceptional;
|
||||||
|
|
||||||
const std::function<void ()> func;
|
const std::function<void ()> func;
|
||||||
|
|
||||||
template<class F>
|
template<class F>
|
||||||
scope(F &&func)
|
scope(F &&func): func(std::forward<F>(func)) {}
|
||||||
:func(std::forward<F>(func))
|
scope() = default;
|
||||||
{}
|
|
||||||
|
|
||||||
scope(const scope &) = delete;
|
scope(const scope &) = delete;
|
||||||
|
scope &operator=(const scope &) = delete;
|
||||||
~scope() noexcept
|
~scope() noexcept
|
||||||
{
|
{
|
||||||
func();
|
func();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct scope::uncaught
|
struct scope::nominal
|
||||||
|
:scope
|
||||||
{
|
{
|
||||||
const std::function<void ()> func;
|
|
||||||
|
|
||||||
template<class F>
|
template<class F>
|
||||||
uncaught(F &&func)
|
nominal(F &&func)
|
||||||
:func(std::forward<F>(func))
|
:scope
|
||||||
{}
|
{
|
||||||
|
[func(std::forward<F>(func))]
|
||||||
|
{
|
||||||
|
if(likely(!std::uncaught_exception()))
|
||||||
|
func();
|
||||||
|
}
|
||||||
|
}{}
|
||||||
|
|
||||||
uncaught(const uncaught &) = delete;
|
nominal() = default;
|
||||||
~uncaught() noexcept
|
};
|
||||||
|
|
||||||
|
struct scope::exceptional
|
||||||
|
:scope
|
||||||
|
{
|
||||||
|
template<class F>
|
||||||
|
exceptional(F &&func)
|
||||||
|
:scope
|
||||||
|
{
|
||||||
|
[func(std::forward<F>(func))]
|
||||||
{
|
{
|
||||||
if(unlikely(std::uncaught_exception()))
|
if(unlikely(std::uncaught_exception()))
|
||||||
func();
|
func();
|
||||||
}
|
}
|
||||||
|
}{}
|
||||||
|
|
||||||
|
exceptional() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
using custom_ptr = std::unique_ptr<T, std::function<void (T *) noexcept>>;
|
||||||
|
|
||||||
|
|
||||||
// For conforming enums include a _NUM_ as the last element,
|
// For conforming enums include a _NUM_ as the last element,
|
||||||
// then num_of<my_enum>() works
|
// then num_of<my_enum>() works
|
||||||
template<class Enum>
|
template<class Enum>
|
||||||
|
|
Loading…
Reference in a new issue