mirror of
https://github.com/Naios/continuable.git
synced 2026-02-08 18:56:40 +08:00
correct perfect forwarding on various places
This commit is contained in:
parent
2955e21965
commit
b923fc3e2f
@ -22,6 +22,7 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "functional_unwrap.hpp"
|
#include "functional_unwrap.hpp"
|
||||||
|
|
||||||
@ -53,31 +54,31 @@ namespace detail
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename _CTy>
|
template<typename _CTy>
|
||||||
using unwrap_callback_t = do_unwrap_callback<::fu::function_type_of_t<_CTy>>;
|
using unwrap_callback_t = do_unwrap_callback<::fu::function_type_of_t<typename std::decay<_CTy>::type>>;
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename _CTy, typename... Args>
|
||||||
struct WeakProxyFactory;
|
struct WeakProxyFactory;
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename _CTy, typename... Args>
|
||||||
struct WeakProxyFactory<std::weak_ptr<std::function<void(Args...)>>>
|
struct WeakProxyFactory<_CTy, std::weak_ptr<std::function<void(Args...)>>>
|
||||||
{
|
{
|
||||||
static Callback<Args...> CreateProxy(WeakCallback<Args...> const& weak)
|
static Callback<Args...> CreateProxy(_CTy&& weak)
|
||||||
{
|
{
|
||||||
return [=](Args&&... args)
|
return [=](Args&&... args)
|
||||||
{
|
{
|
||||||
if (auto const callback = weak.lock())
|
if (auto const callback = weak.lock())
|
||||||
// FIXME: use std::forward
|
(*callback)(std::forward<Args>(args)...);
|
||||||
(*callback)(args...);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename _CTy, typename... Args>
|
||||||
struct WeakProxyFactory<std::shared_ptr<std::function<void(Args...)>>>
|
struct WeakProxyFactory<_CTy, std::shared_ptr<std::function<void(Args...)>>>
|
||||||
{
|
{
|
||||||
static Callback<Args...> CreateProxy(SharedCallback<Args...> const& shared)
|
static Callback<Args...> CreateProxy(_CTy&& shared)
|
||||||
{
|
{
|
||||||
return WeakProxyFactory<std::weak_ptr<std::function<void(Args...)>>>::CreateProxy(shared);
|
return WeakProxyFactory<std::weak_ptr<std::function<void(Args...)>>&&,
|
||||||
|
std::weak_ptr<std::function<void(Args...)>>>::CreateProxy(std::forward<_CTy>(shared));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -107,10 +108,12 @@ inline auto make_shared_callback(_CTy&& callback)
|
|||||||
/// Creates a weak callback which wraps the given shared or weak callback.
|
/// Creates a weak callback which wraps the given shared or weak callback.
|
||||||
/// If the given managed callback expires the callback is not invoked anymore.
|
/// If the given managed callback expires the callback is not invoked anymore.
|
||||||
template<typename _CTy>
|
template<typename _CTy>
|
||||||
inline auto make_weak_wrapped_callback(_CTy const& callback)
|
inline auto make_weak_wrapped_callback(_CTy&& callback)
|
||||||
-> decltype(detail::WeakProxyFactory<_CTy>::CreateProxy(std::declval<_CTy>()))
|
-> decltype(detail::WeakProxyFactory<_CTy, std::decay<_CTy>::type>::
|
||||||
|
CreateProxy(std::declval<_CTy>()))
|
||||||
{
|
{
|
||||||
return detail::WeakProxyFactory<_CTy>::CreateProxy(callback);
|
return detail::WeakProxyFactory<_CTy, std::decay<_CTy>::type>::
|
||||||
|
CreateProxy(std::forward<_CTy>(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // _CALLBACK_H_
|
#endif // _CALLBACK_H_
|
||||||
|
|||||||
@ -54,7 +54,7 @@ class WeakCallbackContainer
|
|||||||
if (auto const callback = weak_callback.lock())
|
if (auto const callback = weak_callback.lock())
|
||||||
{
|
{
|
||||||
// Invoke the original callback
|
// Invoke the original callback
|
||||||
(*callback)(args...); // FIXME: use std::forward
|
(*callback)(std::forward<Args>(args)...);
|
||||||
|
|
||||||
// Unregister the callback
|
// Unregister the callback
|
||||||
owner->InvalidateCallback(handle);
|
owner->InvalidateCallback(handle);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user