2021-03-05 20:23:23 +00:00
|
|
|
|
|
|
|
#ifndef GINVOKE_HPP
|
|
|
|
#define GINVOKE_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <utility>
|
|
|
|
#include <optional>
|
|
|
|
#include <variant>
|
|
|
|
#include <iterator>
|
|
|
|
#include <functional>
|
|
|
|
#include <exception>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "overload.hpp"
|
|
|
|
#include "make_array.hpp"
|
|
|
|
#include "hexify.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
namespace glib {
|
|
|
|
|
|
|
|
namespace impl
|
|
|
|
{
|
|
|
|
using static_c_str = const char *;
|
|
|
|
using varstring_t = std::variant<std::string, static_c_str>;
|
|
|
|
struct varstring : varstring_t
|
|
|
|
{
|
2021-03-19 23:52:24 +00:00
|
|
|
varstring(std::string &&s) noexcept : varstring_t{std::move(s)} {}
|
|
|
|
varstring(static_c_str &&s) noexcept : varstring_t{std::move(s)} {}
|
|
|
|
varstring(std::nullptr_t) = delete;
|
|
|
|
varstring(const varstring &) = delete;
|
|
|
|
varstring(varstring &&) = default;
|
|
|
|
|
2021-03-05 20:23:23 +00:00
|
|
|
const char* c_str() const && = delete;
|
|
|
|
const char* c_str() const &
|
|
|
|
{
|
|
|
|
return std::visit(overload{
|
|
|
|
[](const std::string &s){ return s.c_str(); },
|
|
|
|
[](const static_c_str s){ return s; }
|
|
|
|
}, static_cast<const varstring_t &>(*this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hresult
|
|
|
|
{
|
|
|
|
std::int32_t code;
|
|
|
|
varstring message;
|
|
|
|
};
|
|
|
|
std::optional<hresult> get_if_hresult_error(std::exception_ptr) noexcept;
|
|
|
|
}
|
|
|
|
|
2021-03-21 19:30:22 +00:00
|
|
|
template<typename OStream, typename T,
|
|
|
|
std::enable_if_t<!std::is_enum_v<T>,int> = 0>
|
|
|
|
inline auto &describe_argument(OStream &s, const T &a)
|
|
|
|
{ return s << a; }
|
|
|
|
template<typename OStream, typename T,
|
|
|
|
std::enable_if_t< std::is_enum_v<T>,int> = 0>
|
|
|
|
inline auto &describe_argument(OStream &s, const T &a)
|
|
|
|
{ return s << static_cast<std::underlying_type_t<T>>(a); }
|
2021-03-20 21:29:37 +00:00
|
|
|
|
2021-03-05 20:23:23 +00:00
|
|
|
template<typename OStream>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline auto &describe_argument(OStream &s, std::string_view const a)
|
|
|
|
{ return s << std::quoted(a); }
|
2021-03-05 20:23:23 +00:00
|
|
|
template<typename OStream>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline auto &describe_argument(OStream &s, const std::string & a)
|
|
|
|
{ return s << std::quoted(a); }
|
2021-03-05 20:23:23 +00:00
|
|
|
template<typename OStream>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline auto &describe_argument(OStream &s, const char * const a)
|
|
|
|
{ return s << std::quoted(a); }
|
2021-03-05 20:23:23 +00:00
|
|
|
// TODO: overload for const GString *
|
2021-03-21 19:30:22 +00:00
|
|
|
|
|
|
|
// not implemented (TODO maybe):
|
2021-03-05 20:23:23 +00:00
|
|
|
template<typename OStream>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline auto &describe_argument(OStream &s, std::wstring_view const a) = delete;
|
2021-03-05 20:23:23 +00:00
|
|
|
template<typename OStream>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline auto &describe_argument(OStream &s, const std::wstring & a) = delete;
|
2021-03-05 20:23:23 +00:00
|
|
|
template<typename OStream>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline auto &describe_argument(OStream &s, const wchar_t * const a) = delete;
|
2021-03-05 20:23:23 +00:00
|
|
|
|
2021-03-19 23:15:08 +00:00
|
|
|
inline impl::varstring describe_arguments() noexcept { return {""}; }
|
|
|
|
|
2021-03-05 20:23:23 +00:00
|
|
|
template<typename... Arg>
|
2021-03-19 21:43:00 +00:00
|
|
|
inline impl::varstring describe_arguments(const Arg &... a) noexcept try
|
2021-03-05 20:23:23 +00:00
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
2021-03-19 21:43:00 +00:00
|
|
|
((describe_argument(ss,a) << ','), ...);
|
2021-03-05 20:23:23 +00:00
|
|
|
auto s = std::move(ss).str();
|
|
|
|
s.pop_back();
|
2021-03-19 23:52:24 +00:00
|
|
|
return {std::move(s)};
|
2021-03-05 20:23:23 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2021-03-19 21:58:12 +00:00
|
|
|
return {"<failed to stringify arguments>"};
|
2021-03-05 20:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define FORMAT "%s(%s) failed: %s"
|
|
|
|
template<typename... Arg>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline void log_invocation_failure(const char *e,
|
|
|
|
const char *func_name, const Arg &... a) noexcept
|
2021-03-05 20:23:23 +00:00
|
|
|
{
|
|
|
|
const auto args = describe_arguments(a...);
|
|
|
|
g_warning(FORMAT, func_name, args.c_str(), e);
|
|
|
|
}
|
|
|
|
template<typename... Arg>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline void log_invocation_failure_desc(const char *e, const char *e_desc,
|
|
|
|
const char *func_name, const Arg &... a) noexcept
|
2021-03-05 20:23:23 +00:00
|
|
|
{
|
|
|
|
const auto args = describe_arguments(a...);
|
|
|
|
g_warning(FORMAT": %s", func_name, args.c_str(), e, e_desc);
|
|
|
|
}
|
|
|
|
#undef FORMAT
|
|
|
|
|
2021-03-20 02:48:05 +00:00
|
|
|
struct regular_void {};
|
2021-03-05 20:23:23 +00:00
|
|
|
|
|
|
|
template<typename Invokable, typename... Arg>
|
2021-03-20 02:48:05 +00:00
|
|
|
inline auto invoke(Invokable &&i, const Arg &... a)
|
2021-03-05 20:23:23 +00:00
|
|
|
{
|
2021-03-21 19:30:22 +00:00
|
|
|
using R = decltype(std::invoke(std::forward<Invokable>(i), a...));
|
|
|
|
if constexpr (std::is_void_v<R>)
|
2021-03-20 02:48:05 +00:00
|
|
|
{
|
|
|
|
std::invoke(std::forward<Invokable>(i), a...);
|
|
|
|
return regular_void{};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return std::invoke(std::forward<Invokable>(i), a...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Invokable, typename... Arg>
|
2021-03-21 19:30:22 +00:00
|
|
|
inline auto try_invoke(
|
|
|
|
const char *func_name, Invokable &&i, const Arg &... a) noexcept
|
2021-03-20 02:48:05 +00:00
|
|
|
-> std::optional<decltype(invoke(std::forward<Invokable>(i), a...))>
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return invoke(std::forward<Invokable>(i), a...);
|
2021-03-05 20:23:23 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
log_invocation_failure(e.what(), func_name, a...);
|
2021-03-20 02:48:05 +00:00
|
|
|
return {};
|
2021-03-05 20:23:23 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (const auto e = impl::get_if_hresult_error(std::current_exception()))
|
|
|
|
{
|
|
|
|
auto hr = make_array("hresult 0x01234567\0");
|
|
|
|
hexify32(static_cast<std::uint32_t>(e->code), std::end(hr)-1);
|
2021-03-21 19:30:22 +00:00
|
|
|
log_invocation_failure_desc(
|
|
|
|
std::begin(hr), e->message.c_str(), func_name, a...);
|
2021-03-05 20:23:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
log_invocation_failure("unknown error", func_name, a...);
|
|
|
|
|
2021-03-20 02:48:05 +00:00
|
|
|
return {};
|
2021-03-05 20:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace glib
|
|
|
|
|
|
|
|
|
2021-03-21 19:30:22 +00:00
|
|
|
#define g_try_invoke(invokable, ...) \
|
|
|
|
glib::try_invoke(#invokable, invokable, __VA_ARGS__)
|
|
|
|
|
|
|
|
#define g_try_invoke0(invokable) \
|
|
|
|
glib::try_invoke(#invokable, invokable)
|
2021-03-05 20:23:23 +00:00
|
|
|
|
|
|
|
#endif
|