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" 20 template <
typename T>
class delegate;
46 template<
class R,
class ...A>
47 class delegate<R(A...)>
50 using InvokerPtr = R(*)(
void*, A && ...);
51 using FunctorPtr = std::shared_ptr<Impl::IDelegateFunctor>;
55 delegate(
void) =
default;
58 delegate(
const delegate& arg) =
default;
61 delegate(delegate&& arg) =
default;
65 delegate& operator=(
const delegate& arg) =
default;
69 delegate& operator=(delegate&& arg) =
default;
75 static delegate
create(R(*
const pFunction)(A...))
77 return{
new Impl::StaticFunctor<R(A...)>(pFunction), &Impl::StaticFunctor<R(A...)>::Invoke };
85 static delegate
create(C* pObject, R(C::*pMethod)(A...))noexcept
87 return{
new Impl::MethodFunctor<R(C::*)(A...)>(pObject, pMethod), &Impl::MethodFunctor<R(C::*)(A...)>::Invoke };
95 static delegate
create(C&
object, R(C::*pMethod)(A...))noexcept
97 return{
new Impl::MethodFunctor<R(C::*)(A...)>(object, pMethod), &Impl::MethodFunctor<R(C::*)(A...)>::Invoke };
105 static delegate
create(
const C* pObject, R(C::*pMethod)(A...)const)noexcept
107 return{
new Impl::ConstMethodFunctor<R(C::*)(A...)
const>(pObject, pMethod), &Impl::ConstMethodFunctor<R(C::*)(A...)
const>::Invoke };
115 static delegate
create(
const C&
object, R(C::*pMethod)(A...)const)noexcept
117 return{
new Impl::ConstMethodFunctor<R(C::*)(A...)
const>(object, pMethod), &Impl::ConstMethodFunctor<R(C::*)(A...)
const>::Invoke };
123 static delegate
create(std::function<R(A...)>&& f)
125 return{
new Impl::LambdaFunctor<R(A...)>(std::forward<std::function<R(A...)>>(f)), &Impl::LambdaFunctor<R(A...)>::Invoke };
131 static delegate
create(
const std::function<R(A...)>& f)
133 return{
new Impl::LambdaFunctor<R(A...)>(f), &Impl::LambdaFunctor<R(A...)>::Invoke };
142 return *this->functorPtr == *rhs.functorPtr;
161 return this->functorPtr ==
nullptr;
168 return this->functorPtr !=
nullptr;
173 explicit operator bool()const noexcept
175 return (
bool)this->functorPtr;
183 return (
bool)(*this) &&
dynamic_cast<Impl::LambdaFunctor<R(A...)
>*>(this->functorPtr.get()) !=
nullptr;
192 return this->pInvoker(this->functorPtr.get(), std::forward<A>(args)...);
200 this->pInvoker =
nullptr;
201 this->functorPtr =
nullptr;
206 delegate(Impl::IDelegateFunctor* pFunctor, InvokerPtr pInvokerArg)noexcept
207 : pInvoker(pInvokerArg)
208 , functorPtr(pFunctor)
214 FunctorPtr functorPtr;
222 template<
class R,
class ...A>
223 inline delegate<R(A...)>
make_delegate(R(*
const function_ptr)(A...))noexcept
225 return delegate<R(A...)>::create(function_ptr);
232 template<
class C,
class R,
class ...A>
233 inline delegate<R(A...)>
make_delegate(C*
const pObject, R(C::*
const pMethod)(A...))noexcept
235 return delegate<R(A...)>::create(pObject, pMethod);
242 template<
class C,
class R,
class ...A>
243 inline delegate<R(A...)>
make_delegate(C
const*
const pObject, R(C::*
const pMethod)(A...)const)noexcept
245 return delegate<R(A...)>::create(pObject, pMethod);
252 template<
class C,
class R,
class ...A>
253 inline delegate<R(A...)>
make_delegate(C&
object, R(C::*
const pMethod)(A...))noexcept
255 return delegate<R(A...)>::create(
object, pMethod);
262 template<
class C,
class R,
class ...A>
263 inline delegate<R(A...)>
make_delegate(
const C&
object, R(C::*
const pMethod)(A...)const)noexcept
265 return delegate<R(A...)>::create(
object, pMethod);
271 template<
class R,
class ...A>
274 return delegate<R(A...)>::create(std::forward<std::function<R(A...)>>(f));
280 template<
class R,
class ...A>
281 inline delegate<R(A...)>
make_delegate(
const std::function<R(A...)>& f)noexcept
283 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:140
void reset()
Resets this instance.
Definition: delegate.hxx:198
delegate< R(A...)> make_delegate(R(*const function_ptr)(A...)) noexcept
Creates a delegate from a static function.
Definition: delegate.hxx:223
bool operator!=(std::nullptr_t const) const noexcept
Compares this instance to a nullptr.
Definition: delegate.hxx:166
bool is_lambda(void) const
Determines if this instance wraps a lamda expression or std::function
Definition: delegate.hxx:181
bool operator==(std::nullptr_t const) const noexcept
Compares this instance to a nullptr.
Definition: delegate.hxx:159
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:105
static delegate create(R(*const pFunction)(A...))
Creates a delegate from a static function.
Definition: delegate.hxx:75
static delegate create(C *pObject, R(C::*pMethod)(A...)) noexcept
Creates a delegate from a member function and object pointer.
Definition: delegate.hxx:85
static delegate create(std::function< R(A...)> &&f)
Creates a delegate from a lambda expression or std::function.
Definition: delegate.hxx:123
delegate(std::nullptr_t) noexcept
Constructs an empty delegate representing a nullptr.
Definition: delegate.hxx:155
Root namespace for the PLCnext API
bool operator==(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on equality.
Definition: BasicString.hxx:1752
Definition: ConstMethodFunctor.hxx:10
bool operator!=(const delegate &rhs) const noexcept
Compares this instance to the as argument passed rhs .
Definition: delegate.hxx:148
static delegate create(C &object, R(C::*pMethod)(A...)) noexcept
Creates a delegate from a member function and object reference.
Definition: delegate.hxx:95
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:115
static delegate create(const std::function< R(A...)> &f)
Creates a delegate from a lambda expression or std::function.
Definition: delegate.hxx:131
R operator()(A... args) const
The functor operator invokes the adapted callable target.
Definition: delegate.hxx:190