mirror of
https://github.com/matrix-construct/construct
synced 2024-12-29 00:44:17 +01:00
ircd::util: Add maybe() exception tool.
This commit is contained in:
parent
ebfaa508fd
commit
71edd9adee
2 changed files with 78 additions and 0 deletions
77
include/ircd/util/maybe.h
Normal file
77
include/ircd/util/maybe.h
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
// 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_MAYBE_H
|
||||||
|
|
||||||
|
namespace ircd { inline namespace util
|
||||||
|
{
|
||||||
|
template<class function>
|
||||||
|
using maybe_retval = typename std::invoke_result<function>::type;
|
||||||
|
|
||||||
|
template<class function>
|
||||||
|
using maybe_type = std::tuple<maybe_retval<function>, std::exception_ptr>;
|
||||||
|
|
||||||
|
template<class function>
|
||||||
|
constexpr bool maybe_void();
|
||||||
|
using maybe_void_type = std::tuple<bool, std::exception_ptr>;
|
||||||
|
|
||||||
|
template<class function>
|
||||||
|
typename std::enable_if<!maybe_void<function>(), maybe_type<function>>::type
|
||||||
|
maybe(function &&) noexcept;
|
||||||
|
|
||||||
|
template<class function>
|
||||||
|
typename std::enable_if<maybe_void<function>(), maybe_void_type>::type
|
||||||
|
maybe(function &&) noexcept;
|
||||||
|
}}
|
||||||
|
|
||||||
|
template<class function>
|
||||||
|
typename std::enable_if<!ircd::util::maybe_void<function>(), ircd::util::maybe_type<function>>::type
|
||||||
|
ircd::util::maybe(function&& f)
|
||||||
|
noexcept try
|
||||||
|
{
|
||||||
|
return
|
||||||
|
{
|
||||||
|
f(), std::exception_ptr{}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch(...)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
{
|
||||||
|
{}, std::current_exception()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class function>
|
||||||
|
typename std::enable_if<ircd::util::maybe_void<function>(), ircd::util::maybe_void_type>::type
|
||||||
|
ircd::util::maybe(function&& f)
|
||||||
|
noexcept try
|
||||||
|
{
|
||||||
|
f();
|
||||||
|
return
|
||||||
|
{
|
||||||
|
true, std::exception_ptr{}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch(...)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
{
|
||||||
|
false, std::current_exception()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class function>
|
||||||
|
constexpr bool
|
||||||
|
ircd::util::maybe_void()
|
||||||
|
{
|
||||||
|
return std::is_same<maybe_retval<function>, void>();
|
||||||
|
}
|
|
@ -63,6 +63,7 @@ namespace ircd
|
||||||
#include "closure.h"
|
#include "closure.h"
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
#include "boolean.h"
|
#include "boolean.h"
|
||||||
|
#include "maybe.h"
|
||||||
|
|
||||||
// Unsorted section
|
// Unsorted section
|
||||||
namespace ircd {
|
namespace ircd {
|
||||||
|
|
Loading…
Reference in a new issue