8 #include "Arp/System/Core/Impl/StaticFunctor.hxx" 9 #include "Arp/System/Core/Impl/MethodFunctor.hxx" 10 #include "Arp/System/Core/Impl/ConstMethodFunctor.hxx" 11 #include "Arp/System/Core/Impl/LambdaFunctor.hxx" 14 #pragma warning(disable:4100) 22 template <
typename T>
class delegate;
48 template<
class R,
class ...A>
49 class delegate<R(A...)>
52 using InvokerPtr = R(*)(
void*, A && ...);
53 using FunctorPtr = std::shared_ptr<Impl::IDelegateFunctor>;
57 delegate(
void) =
default;
60 delegate(
const delegate& arg) =
default;
63 delegate(delegate&& arg) =
default;
67 delegate& operator=(
const delegate& arg) =
default;
71 delegate& operator=(delegate&& arg) =
default;
77 static delegate
create(R(*
const pFunction)(A...))
79 return{
new Impl::StaticFunctor<R(A...)>(pFunction), &Impl::StaticFunctor<R(A...)>::Invoke };
87 static delegate
create(C* pObject, R(C::*pMethod)(A...))noexcept
89 return{
new Impl::MethodFunctor<R(C::*)(A...)>(pObject, pMethod), &Impl::MethodFunctor<R(C::*)(A...)>::Invoke };
97 static delegate
create(C&
object, R(C::*pMethod)(A...))noexcept
99 return{
new Impl::MethodFunctor<R(C::*)(A...)>(object, pMethod), &Impl::MethodFunctor<R(C::*)(A...)>::Invoke };
107 static delegate
create(
const C* pObject, R(C::*pMethod)(A...)const)noexcept
109 return{
new Impl::ConstMethodFunctor<R(C::*)(A...)
const>(pObject, pMethod), &Impl::ConstMethodFunctor<R(C::*)(A...)
const>::Invoke };
117 static delegate
create(
const C&
object, R(C::*pMethod)(A...)const)noexcept
119 return{
new Impl::ConstMethodFunctor<R(C::*)(A...)
const>(object, pMethod), &Impl::ConstMethodFunctor<R(C::*)(A...)
const>::Invoke };
125 static delegate
create(std::function<R(A...)>&& f)
127 return{
new Impl::LambdaFunctor<R(A...)>(std::forward<std::function<R(A...)>>(f)), &Impl::LambdaFunctor<R(A...)>::Invoke };
133 static delegate
create(
const std::function<R(A...)>& f)
135 return{
new Impl::LambdaFunctor<R(A...)>(f), &Impl::LambdaFunctor<R(A...)>::Invoke };
144 return *this->functorPtr == *rhs.functorPtr;
152 return !(this->operator==(rhs));
163 return this->functorPtr ==
nullptr;
170 return this->functorPtr !=
nullptr;
175 explicit operator bool()const noexcept
177 return (
bool)this->functorPtr;
185 return (
bool)(*this) &&
dynamic_cast<Impl::LambdaFunctor<R(A...)
>*>(this->functorPtr.get()) !=
nullptr;
194 return this->pInvoker(this->functorPtr.get(), std::forward<A>(args)...);
198 delegate(Impl::IDelegateFunctor* pFunctor, InvokerPtr pInvokerArg)noexcept
199 : pInvoker(pInvokerArg)
200 , functorPtr(pFunctor)
206 FunctorPtr functorPtr;
214 template<
class R,
class ...A>
215 inline delegate<R(A...)>
make_delegate(R(*
const function_ptr)(A...))noexcept
217 return delegate<R(A...)>::create(function_ptr);
224 template<
class C,
class R,
class ...A>
225 inline delegate<R(A...)>
make_delegate(C*
const pObject, R(C::*
const pMethod)(A...))noexcept
227 return delegate<R(A...)>::create(pObject, pMethod);
234 template<
class C,
class R,
class ...A>
235 inline delegate<R(A...)>
make_delegate(C
const*
const pObject, R(C::*
const pMethod)(A...)const)noexcept
237 return delegate<R(A...)>::create(pObject, pMethod);
244 template<
class C,
class R,
class ...A>
245 inline delegate<R(A...)>
make_delegate(C&
object, R(C::*
const pMethod)(A...))noexcept
247 return delegate<R(A...)>::create(
object, pMethod);
254 template<
class C,
class R,
class ...A>
255 inline delegate<R(A...)>
make_delegate(
const C&
object, R(C::*
const pMethod)(A...)const)noexcept
257 return delegate<R(A...)>::create(
object, pMethod);
263 template<
class R,
class ...A>
266 return delegate<R(A...)>::create(std::forward<std::function<R(A...)>>(f));
272 template<
class R,
class ...A>
273 inline delegate<R(A...)>
make_delegate(
const std::function<R(A...)>& f)noexcept
275 return delegate<R(A...)>::create(f);
bool operator==(const delegate &rhs) const noexcept
Compares this instance to the as argument passed rhs .
Definition: delegate.hxx:142
delegate< R(A...)> make_delegate(R(*const function_ptr)(A...)) noexcept
Creates a delegate from a static function.
Definition: delegate.hxx:215
bool operator!=(std::nullptr_t const) const noexcept
Compares this instance to a nullptr.
Definition: delegate.hxx:168
bool is_lambda(void) const
Determines if this instance wraps a lamda expression or std::function
Definition: delegate.hxx:183
bool operator==(std::nullptr_t const) const noexcept
Compares this instance to a nullptr.
Definition: delegate.hxx:161
static delegate create(const C *pObject, R(C::*pMethod)(A...) const) noexcept
Creates a delegate from a const member function and const object pointer.
Definition: delegate.hxx:107
static delegate create(R(*const pFunction)(A...))
Creates a delegate from a static function.
Definition: delegate.hxx:77
static delegate create(C *pObject, R(C::*pMethod)(A...)) noexcept
Creates a delegate from a member function and object pointer.
Definition: delegate.hxx:87
static delegate create(std::function< R(A...)> &&f)
Creates a delegate from a lambda expression or std::function.
Definition: delegate.hxx:125
delegate(std::nullptr_t) noexcept
Constructs an empty delegate representing a nullptr.
Definition: delegate.hxx:157
Root namespace for the PLCnext API
Definition: ConstMethodFunctor.hxx:10
bool operator!=(const delegate &rhs) const noexcept
Compares this instance to the as argument passed rhs .
Definition: delegate.hxx:150
static delegate create(C &object, R(C::*pMethod)(A...)) noexcept
Creates a delegate from a member function and object reference.
Definition: delegate.hxx:97
static delegate create(const C &object, R(C::*pMethod)(A...) const) noexcept
Creates a delegate from a const member function and const object reference.
Definition: delegate.hxx:117
static delegate create(const std::function< R(A...)> &f)
Creates a delegate from a lambda expression or std::function.
Definition: delegate.hxx:133
R operator()(A... args) const
The functor operator invokes the adapted callable target.
Definition: delegate.hxx:192