|
| 1 | +/* |
| 2 | +
|
| 3 | + Copyright (C) 2017 by Sergey A Kryukov: derived work |
| 4 | + http://www.SAKryukov.org |
| 5 | + http://www.codeproject.com/Members/SAKryukov |
| 6 | +
|
| 7 | + Based on original work by Sergey Ryazanov: |
| 8 | + "The Impossibly Fast C++ Delegates", 18 Jul 2005 |
| 9 | + https://www.codeproject.com/articles/11015/the-impossibly-fast-c-delegates |
| 10 | +
|
| 11 | + MIT license: |
| 12 | + http://en.wikipedia.org/wiki/MIT_License |
| 13 | +
|
| 14 | + Original publication: https://www.codeproject.com/Articles/1170503/The-Impossibly-Fast-Cplusplus-Delegates-Fixed |
| 15 | +
|
| 16 | +*/ |
| 17 | + |
| 18 | +#pragma once |
| 19 | +#include "DelegateBase.h" |
| 20 | + |
| 21 | +namespace SA { |
| 22 | + |
| 23 | + template <typename T> class delegate; |
| 24 | + template <typename T> class multicast_delegate; |
| 25 | + |
| 26 | + template<typename RET, typename ...PARAMS> |
| 27 | + class delegate<RET(PARAMS...)> final : private delegate_base<RET(PARAMS...)> { |
| 28 | + public: |
| 29 | + |
| 30 | + delegate() = default; |
| 31 | + |
| 32 | + bool isNull() const { return invocation.stub == nullptr; } |
| 33 | + bool operator ==(void* ptr) const { |
| 34 | + return (ptr == nullptr) && this->isNull(); |
| 35 | + } //operator == |
| 36 | + bool operator !=(void* ptr) const { |
| 37 | + return (ptr != nullptr) || (!this->isNull()); |
| 38 | + } //operator != |
| 39 | + |
| 40 | + delegate(const delegate& another) { another.invocation.Clone(invocation); } |
| 41 | + |
| 42 | + template <typename LAMBDA> |
| 43 | + delegate(const LAMBDA& lambda) { |
| 44 | + assign((void*)(&lambda), lambda_stub<LAMBDA>); |
| 45 | + } //delegate |
| 46 | + |
| 47 | + delegate& operator =(const delegate& another) { |
| 48 | + another.invocation.Clone(invocation); |
| 49 | + return *this; |
| 50 | + } //operator = |
| 51 | + |
| 52 | + template <typename LAMBDA> // template instantiation is not needed, will be deduced (inferred): |
| 53 | + delegate& operator =(const LAMBDA& instance) { |
| 54 | + assign((void*)(&instance), lambda_stub<LAMBDA>); |
| 55 | + return *this; |
| 56 | + } //operator = |
| 57 | + |
| 58 | + bool operator == (const delegate& another) const { return invocation == another.invocation; } |
| 59 | + bool operator != (const delegate& another) const { return invocation != another.invocation; } |
| 60 | + |
| 61 | + bool operator ==(const multicast_delegate<RET(PARAMS...)>& another) const { return another == (*this); } |
| 62 | + bool operator !=(const multicast_delegate<RET(PARAMS...)>& another) const { return another != (*this); } |
| 63 | + |
| 64 | + template <class T, RET(T::*TMethod)(PARAMS...)> |
| 65 | + static delegate create(T* instance) { |
| 66 | + return delegate(instance, method_stub<T, TMethod>); |
| 67 | + } //create |
| 68 | + |
| 69 | + template <class T, RET(T::*TMethod)(PARAMS...) const> |
| 70 | + static delegate create(T const* instance) { |
| 71 | + return delegate(const_cast<T*>(instance), const_method_stub<T, TMethod>); |
| 72 | + } //create |
| 73 | + |
| 74 | + template <RET(*TMethod)(PARAMS...)> |
| 75 | + static delegate create() { |
| 76 | + return delegate(nullptr, function_stub<TMethod>); |
| 77 | + } //create |
| 78 | + |
| 79 | + template <typename LAMBDA> |
| 80 | + static delegate create(const LAMBDA & instance) { |
| 81 | + return delegate((void*)(&instance), lambda_stub<LAMBDA>); |
| 82 | + } //create |
| 83 | + |
| 84 | + RET operator()(PARAMS... arg) const { |
| 85 | + return (*invocation.stub)(invocation.object, arg...); |
| 86 | + } //operator() |
| 87 | + |
| 88 | + private: |
| 89 | + |
| 90 | + delegate(void* anObject, typename delegate_base<RET(PARAMS...)>::stub_type aStub) { |
| 91 | + invocation.object = anObject; |
| 92 | + invocation.stub = aStub; |
| 93 | + } //delegate |
| 94 | + |
| 95 | + void assign(void* anObject, typename delegate_base<RET(PARAMS...)>::stub_type aStub) { |
| 96 | + this->invocation.object = anObject; |
| 97 | + this->invocation.stub = aStub; |
| 98 | + } //assign |
| 99 | + |
| 100 | + template <class T, RET(T::*TMethod)(PARAMS...)> |
| 101 | + static RET method_stub(void* this_ptr, PARAMS... params) { |
| 102 | + T* p = static_cast<T*>(this_ptr); |
| 103 | + return (p->*TMethod)(params...); |
| 104 | + } //method_stub |
| 105 | + |
| 106 | + template <class T, RET(T::*TMethod)(PARAMS...) const> |
| 107 | + static RET const_method_stub(void* this_ptr, PARAMS... params) { |
| 108 | + T* const p = static_cast<T*>(this_ptr); |
| 109 | + return (p->*TMethod)(params...); |
| 110 | + } //const_method_stub |
| 111 | + |
| 112 | + template <RET(*TMethod)(PARAMS...)> |
| 113 | + static RET function_stub(void* this_ptr, PARAMS... params) { |
| 114 | + return (TMethod)(params...); |
| 115 | + } //function_stub |
| 116 | + |
| 117 | + template <typename LAMBDA> |
| 118 | + static RET lambda_stub(void* this_ptr, PARAMS... arg) { |
| 119 | + LAMBDA* p = static_cast<LAMBDA*>(this_ptr); |
| 120 | + return (p->operator())(arg...); |
| 121 | + } //lambda_stub |
| 122 | + |
| 123 | + friend class multicast_delegate<RET(PARAMS...)>; |
| 124 | + typename delegate_base<RET(PARAMS...)>::InvocationElement invocation; |
| 125 | + |
| 126 | + }; //class delegate |
| 127 | + |
| 128 | +} /* namespace SA */ |
0 commit comments