2016-06-17 17:00:05 +02:00
|
|
|
/*
|
2021-12-02 22:12:36 +01:00
|
|
|
* Copyright 2016-2021 Max Kellermann <max.kellermann@gmail.com>
|
2016-06-17 17:00:05 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-12-02 22:12:36 +01:00
|
|
|
#pragma once
|
2016-06-17 17:00:05 +02:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This object stores a function pointer wrapping a method, and a
|
|
|
|
* reference to an instance of the method's class. It can be used to
|
|
|
|
* wrap instance methods as callback functions.
|
|
|
|
*
|
|
|
|
* @param S the plain function signature type
|
|
|
|
*/
|
|
|
|
template<typename S=void()>
|
|
|
|
class BoundMethod;
|
|
|
|
|
2019-08-02 07:25:13 +02:00
|
|
|
template<typename R,
|
|
|
|
bool NoExcept,
|
|
|
|
typename... Args>
|
|
|
|
class BoundMethod<R(Args...) noexcept(NoExcept)> {
|
2021-04-20 19:57:02 +02:00
|
|
|
typedef R (*function_pointer)(void *instance, Args... args) noexcept(NoExcept);
|
2016-06-17 17:00:05 +02:00
|
|
|
|
|
|
|
void *instance_;
|
|
|
|
function_pointer function;
|
|
|
|
|
|
|
|
public:
|
2016-06-20 10:35:59 +02:00
|
|
|
/**
|
|
|
|
* Non-initializing trivial constructor
|
|
|
|
*/
|
2016-06-17 17:00:05 +02:00
|
|
|
BoundMethod() = default;
|
|
|
|
|
|
|
|
constexpr
|
2018-08-20 13:45:41 +02:00
|
|
|
BoundMethod(void *_instance, function_pointer _function) noexcept
|
2016-06-17 17:00:05 +02:00
|
|
|
:instance_(_instance), function(_function) {}
|
|
|
|
|
2016-06-20 10:35:26 +02:00
|
|
|
/**
|
|
|
|
* Construct an "undefined" object. It must not be called,
|
|
|
|
* and its "bool" operator returns false.
|
|
|
|
*/
|
2018-08-20 13:45:41 +02:00
|
|
|
BoundMethod(std::nullptr_t) noexcept:function(nullptr) {}
|
2016-06-20 10:35:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Was this object initialized with a valid function pointer?
|
|
|
|
*/
|
2018-08-20 13:45:41 +02:00
|
|
|
operator bool() const noexcept {
|
2016-06-20 10:35:26 +02:00
|
|
|
return function != nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-17 17:00:05 +02:00
|
|
|
R operator()(Args... args) const {
|
|
|
|
return function(instance_, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace BindMethodDetail {
|
|
|
|
|
|
|
|
/**
|
2021-12-03 08:05:19 +01:00
|
|
|
* Helper class which introspects a method/function pointer type.
|
2016-06-17 17:00:05 +02:00
|
|
|
*
|
2021-12-03 08:05:19 +01:00
|
|
|
* @param M the method/function pointer type
|
2016-06-17 17:00:05 +02:00
|
|
|
*/
|
|
|
|
template<typename M>
|
2021-12-03 08:05:19 +01:00
|
|
|
struct SignatureHelper;
|
2016-06-17 17:00:05 +02:00
|
|
|
|
2021-04-20 19:57:02 +02:00
|
|
|
template<typename R, bool NoExcept, typename T, typename... Args>
|
2021-12-03 08:05:19 +01:00
|
|
|
struct SignatureHelper<R (T::*)(Args...) noexcept(NoExcept)> {
|
2016-06-17 17:00:05 +02:00
|
|
|
/**
|
|
|
|
* The class which contains the given method (signature).
|
|
|
|
*/
|
|
|
|
typedef T class_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A function type which describes the "plain" function
|
|
|
|
* signature.
|
|
|
|
*/
|
2021-04-20 19:57:02 +02:00
|
|
|
typedef R plain_signature(Args...) noexcept(NoExcept);
|
2021-12-02 23:24:51 +01:00
|
|
|
|
|
|
|
typedef R (*function_pointer)(void *instance,
|
|
|
|
Args...) noexcept(NoExcept);
|
2016-06-17 17:00:05 +02:00
|
|
|
};
|
|
|
|
|
2021-04-20 19:57:02 +02:00
|
|
|
template<typename R, bool NoExcept, typename... Args>
|
2021-12-03 08:05:19 +01:00
|
|
|
struct SignatureHelper<R (*)(Args...) noexcept(NoExcept)> {
|
2021-12-02 23:25:40 +01:00
|
|
|
typedef R plain_signature(Args...) noexcept(NoExcept);
|
|
|
|
|
2021-04-20 19:57:02 +02:00
|
|
|
typedef R (*function_pointer)(void *instance,
|
|
|
|
Args...) noexcept(NoExcept);
|
2016-06-17 17:00:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a wrapper function.
|
|
|
|
*
|
2021-12-03 08:06:57 +01:00
|
|
|
* @param method the method/function pointer
|
2016-06-17 17:00:05 +02:00
|
|
|
*/
|
2021-12-02 23:20:49 +01:00
|
|
|
template<typename M, auto method>
|
2021-12-03 08:06:57 +01:00
|
|
|
struct WrapperGenerator;
|
2016-06-17 17:00:05 +02:00
|
|
|
|
2021-04-20 19:57:02 +02:00
|
|
|
template<typename T, bool NoExcept,
|
2021-12-02 22:36:11 +01:00
|
|
|
auto method, typename R, typename... Args>
|
2021-12-03 08:06:57 +01:00
|
|
|
struct WrapperGenerator<R (T::*)(Args...) noexcept(NoExcept), method> {
|
2021-12-02 22:45:04 +01:00
|
|
|
static R Invoke(void *_instance, Args... args) noexcept(NoExcept) {
|
|
|
|
auto &t = *(T *)_instance;
|
|
|
|
return (t.*method)(std::forward<Args>(args)...);
|
|
|
|
}
|
2016-06-17 17:00:05 +02:00
|
|
|
};
|
|
|
|
|
2021-12-02 22:47:20 +01:00
|
|
|
template<auto function, bool NoExcept, typename R, typename... Args>
|
2021-12-03 08:06:57 +01:00
|
|
|
struct WrapperGenerator<R (*)(Args...) noexcept(NoExcept), function> {
|
2021-12-02 22:45:04 +01:00
|
|
|
static R Invoke(void *, Args... args) noexcept(NoExcept) {
|
|
|
|
return function(std::forward<Args>(args)...);
|
|
|
|
}
|
2017-04-21 19:39:02 +02:00
|
|
|
};
|
|
|
|
|
2021-12-03 08:06:57 +01:00
|
|
|
template<auto method>
|
2021-12-03 08:05:19 +01:00
|
|
|
typename SignatureHelper<decltype(method)>::function_pointer
|
2021-12-03 08:06:57 +01:00
|
|
|
MakeBindMethodWrapper() noexcept
|
|
|
|
{
|
|
|
|
return WrapperGenerator<decltype(method), method>::Invoke;
|
|
|
|
}
|
|
|
|
|
2021-12-02 23:25:40 +01:00
|
|
|
template<auto function>
|
2021-12-03 08:05:19 +01:00
|
|
|
typename SignatureHelper<decltype(function)>::function_pointer
|
2018-08-20 13:45:41 +02:00
|
|
|
MakeBindFunctionWrapper() noexcept
|
2017-04-21 19:39:02 +02:00
|
|
|
{
|
2021-12-03 08:06:57 +01:00
|
|
|
return WrapperGenerator<decltype(function), function>::Invoke;
|
2017-04-21 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 17:00:05 +02:00
|
|
|
} /* namespace BindMethodDetail */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a #BoundMethod instance.
|
|
|
|
*
|
|
|
|
* @param method the method pointer
|
|
|
|
* @param instance the instance of #T to be bound
|
|
|
|
*/
|
2021-12-02 22:12:36 +01:00
|
|
|
template<auto method>
|
|
|
|
constexpr auto
|
2021-12-03 08:05:19 +01:00
|
|
|
BindMethod(typename BindMethodDetail::SignatureHelper<decltype(method)>::class_type &instance) noexcept
|
2016-06-17 17:00:05 +02:00
|
|
|
{
|
2021-12-03 08:05:19 +01:00
|
|
|
using H = BindMethodDetail::SignatureHelper<decltype(method)>;
|
2021-12-02 22:12:36 +01:00
|
|
|
using plain_signature = typename H::plain_signature;
|
|
|
|
return BoundMethod<plain_signature>{
|
|
|
|
&instance,
|
2021-12-02 23:20:49 +01:00
|
|
|
BindMethodDetail::MakeBindMethodWrapper<method>(),
|
2021-12-02 22:12:36 +01:00
|
|
|
};
|
2016-06-17 17:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut macro which takes an instance and a method pointer and
|
|
|
|
* constructs a #BoundMethod instance.
|
|
|
|
*/
|
|
|
|
#define BIND_METHOD(instance, method) \
|
2021-12-02 22:12:36 +01:00
|
|
|
BindMethod<method>(instance)
|
2016-06-17 17:00:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut wrapper for BIND_METHOD() which assumes "*this" is the
|
|
|
|
* instance to be bound.
|
|
|
|
*/
|
2021-12-02 22:24:38 +01:00
|
|
|
#define BIND_THIS_METHOD(method) BIND_METHOD(*this, &std::remove_reference_t<decltype(*this)>::method)
|
2016-06-17 17:00:05 +02:00
|
|
|
|
2017-04-21 19:39:02 +02:00
|
|
|
/**
|
|
|
|
* Construct a #BoundMethod instance for a plain function.
|
|
|
|
*
|
|
|
|
* @param function the function pointer
|
|
|
|
*/
|
2021-12-02 23:25:40 +01:00
|
|
|
template<auto function>
|
|
|
|
constexpr auto
|
2018-08-20 13:45:41 +02:00
|
|
|
BindFunction() noexcept
|
2017-04-21 19:39:02 +02:00
|
|
|
{
|
2021-12-03 08:05:19 +01:00
|
|
|
using H = BindMethodDetail::SignatureHelper<decltype(function)>;
|
2021-12-02 23:25:40 +01:00
|
|
|
using plain_signature = typename H::plain_signature;
|
|
|
|
return BoundMethod<plain_signature>{
|
|
|
|
nullptr,
|
|
|
|
BindMethodDetail::MakeBindFunctionWrapper<function>(),
|
|
|
|
};
|
2017-04-21 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut macro which takes a function pointer and constructs a
|
|
|
|
* #BoundMethod instance.
|
|
|
|
*/
|
|
|
|
#define BIND_FUNCTION(function) \
|
2021-12-02 23:25:40 +01:00
|
|
|
BindFunction<&function>()
|