Cleaned up delegate.
This commit is contained in:
1 parent
51b37abae7
commit
357d4625fa
4 files changed
+37
-36
No files matched your search
@@ -15,7 +15,7 @@
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/Uuid.h"
|
||||
#include "lib/utils/mpmc_bounded_queue.h"
|
||||
#include "system/delegate/Delegate.h"
|
||||
#include "system/Delegate.h"
|
||||
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "QuickTest.h"
|
||||
#include "feed_handler/FeedHandlerBinder.h"
|
||||
#include "system/delegate/Delegate.h"
|
||||
#include "system/Delegate.h"
|
||||
#include "system/reactor/Reactor.h"
|
||||
|
||||
namespace bts::cmds {
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
#include "Delegate.h"
|
||||
|
||||
+33
-34
@@ -1,49 +1,52 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace bts::spartan {
|
||||
|
||||
struct DelegateHelper {
|
||||
template <class T>
|
||||
struct handler_traits;
|
||||
template <class T, class R>
|
||||
struct handler_traits<R T::*> {
|
||||
using HandlerType = T;
|
||||
using HandlerReturnType = R;
|
||||
};
|
||||
|
||||
template <auto handler_member, typename R, typename... Arg>
|
||||
static R invoke_member_handler(void *handler_obj, Arg... arg) {
|
||||
using HandlerMethod = decltype(handler_member);
|
||||
static_assert(std::is_member_function_pointer<HandlerMethod>::value);
|
||||
using HandlerType = typename handler_traits<HandlerMethod>::HandlerType;
|
||||
return (((HandlerType *)handler_obj)->*handler_member)(
|
||||
std::forward<Arg>(arg)...);
|
||||
}
|
||||
template <auto handler, typename R, typename... Arg>
|
||||
static R invoke_static_handler(void *, Arg... arg) {
|
||||
return handler(std::forward<Arg>(arg)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Signature>
|
||||
class Delegate;
|
||||
|
||||
template <typename R, typename... Arg>
|
||||
class Delegate<R(Arg...)> {
|
||||
|
||||
public:
|
||||
private:
|
||||
void *target_obj_;
|
||||
R (*invoke_)(void *, Arg...);
|
||||
|
||||
template <class T>
|
||||
struct handler_traits;
|
||||
template <class T, class Re>
|
||||
struct handler_traits<Re T::*> {
|
||||
using HandlerType = T;
|
||||
using HandlerReturnType = Re;
|
||||
};
|
||||
template <auto handler_member, typename Re, typename... Args>
|
||||
static R invoke_member_handler(void *handler_obj, Args... arg) {
|
||||
using HandlerMethod = decltype(handler_member);
|
||||
static_assert(std::is_member_function_pointer<HandlerMethod>::value);
|
||||
using HandlerType = typename handler_traits<HandlerMethod>::HandlerType;
|
||||
return (((HandlerType *)handler_obj)->*handler_member)(
|
||||
std::forward<Arg>(arg)...);
|
||||
}
|
||||
template <auto handler, typename Re, typename... Args>
|
||||
static R invoke_static_handler(void *, Args... arg) {
|
||||
return handler(std::forward<Arg>(arg)...);
|
||||
}
|
||||
|
||||
public:
|
||||
Delegate() : target_obj_(), invoke_() {}
|
||||
|
||||
Delegate(Delegate const &d)
|
||||
: target_obj_(d.target_obj), invoke_(d.invoke_) {}
|
||||
|
||||
Delegate&operator=(Delegate const&d) {
|
||||
Delegate &operator=(Delegate const &d) {
|
||||
target_obj_ = d.target_obj_;
|
||||
invoke_ = d.invoke_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <class C>
|
||||
void bind(C &handler_obj) {
|
||||
target_obj_ = &handler_obj;
|
||||
@@ -51,27 +54,23 @@ class Delegate<R(Arg...)> {
|
||||
}
|
||||
|
||||
template <auto handler_member>
|
||||
void bind(typename DelegateHelper::handler_traits<
|
||||
void bind(typename handler_traits<
|
||||
decltype(handler_member)>::HandlerType &handler_obj) {
|
||||
target_obj_ = &handler_obj;
|
||||
invoke_ =
|
||||
DelegateHelper::invoke_member_handler<handler_member, R, Arg...>;
|
||||
invoke_member_handler<handler_member, R, Arg...>;
|
||||
}
|
||||
|
||||
template <auto static_handler>
|
||||
void bind() {
|
||||
target_obj_ = nullptr;
|
||||
invoke_ =
|
||||
DelegateHelper::invoke_static_handler<static_handler, R, Arg...>;
|
||||
invoke_static_handler<static_handler, R, Arg...>;
|
||||
}
|
||||
|
||||
R operator()(Arg &&... arg) {
|
||||
return invoke_(target_obj_, std::forward<Arg>(arg)...);
|
||||
}
|
||||
|
||||
private:
|
||||
void *target_obj_;
|
||||
R (*invoke_)(void *, Arg...);
|
||||
};
|
||||
|
||||
} // namespace bts::spartan
|
||||
Reference in new issue
Block a user