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"
17 using namespace Arp::System::Core;
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 template<
class Lambda>
56 delegate(Lambda f) : delegate(new Impl::LambdaFunctor<R(A...)>(
std::function<R(A...)>(f)), &Impl::LambdaFunctor<R(A...)>::Invoke) {}
72 delegate&
operator=(delegate&& arg)noexcept =
default;
78 static delegate
create(R(*
const pFunction)(A...))
80 return{
new Impl::StaticFunctor<R(A...)>(pFunction), &Impl::StaticFunctor<R(A...)>::Invoke };
88 static delegate
create(C* pObject, R(C::*pMethod)(A...))noexcept
90 return{
new Impl::MethodFunctor<R(C::*)(A...)>(pObject, pMethod), &Impl::MethodFunctor<R(C::*)(A...)>::Invoke };
98 static delegate
create(C&
object, R(C::*pMethod)(A...))noexcept
100 return{
new Impl::MethodFunctor<R(C::*)(A...)>(object, pMethod), &Impl::MethodFunctor<R(C::*)(A...)>::Invoke };
108 static delegate
create(
const C* pObject, R(C::*pMethod)(A...)const)noexcept
110 return{
new Impl::ConstMethodFunctor<R(C::*)(A...)
const>(pObject, pMethod), &Impl::ConstMethodFunctor<R(C::*)(A...)
const>::Invoke };
118 static delegate
create(
const C&
object, R(C::*pMethod)(A...)const)noexcept
120 return{
new Impl::ConstMethodFunctor<R(C::*)(A...)
const>(object, pMethod), &Impl::ConstMethodFunctor<R(C::*)(A...)
const>::Invoke };
126 static delegate
create(std::function<R(A...)>&& f)
128 return{
new Impl::LambdaFunctor<R(A...)>(std::move(f)), &Impl::LambdaFunctor<R(A...)>::Invoke };
134 static delegate
create(
const std::function<R(A...)>& f)
136 return{
new Impl::LambdaFunctor<R(A...)>(f), &Impl::LambdaFunctor<R(A...)>::Invoke };
145 return *this->functorPtr == *rhs.functorPtr;
164 return this->functorPtr ==
nullptr;
171 return this->functorPtr !=
nullptr;
176 explicit operator bool()const noexcept
178 return (
bool)this->functorPtr;
186 return (
bool)(*this) &&
dynamic_cast<Impl::LambdaFunctor<R(A...)
>*>(this->functorPtr.get()) !=
nullptr;
195 return this->pInvoker(this->functorPtr.get(), std::forward<A>(args)...);
203 this->pInvoker =
nullptr;
204 this->functorPtr =
nullptr;
208 delegate(Impl::IDelegateFunctor* pFunctor, InvokerPtr pInvokerArg)noexcept
209 : pInvoker(pInvokerArg)
210 , functorPtr(pFunctor)
216 FunctorPtr functorPtr;
224 template<
class R,
class ...A>
225 inline delegate<R(A...)>
make_delegate(R(*
const function_ptr)(A...))noexcept
227 return delegate<R(A...)>::create(function_ptr);
234 template<
class C,
class R,
class ...A>
235 inline delegate<R(A...)>
make_delegate(C*
const pObject, R(C::*
const pMethod)(A...))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
const*
const pObject, R(C::*
const pMethod)(A...)const)noexcept
247 return delegate<R(A...)>::create(pObject, pMethod);
254 template<
class C,
class R,
class ...A>
255 inline delegate<R(A...)>
make_delegate(C&
object, R(C::*
const pMethod)(A...))noexcept
257 return delegate<R(A...)>::create(
object, pMethod);
264 template<
class C,
class R,
class ...A>
265 inline delegate<R(A...)>
make_delegate(
const C&
object, R(C::*
const pMethod)(A...)const)noexcept
267 return delegate<R(A...)>::create(
object, pMethod);
273 template<
class R,
class ...A>
276 return delegate<R(A...)>::create(std::move(f));
282 template<
class R,
class ...A>
283 inline delegate<R(A...)>
make_delegate(
const std::function<R(A...)>& f)noexcept
285 return delegate<R(A...)>::create(f);
bool is_lambda(void) const
Determines if this instance wraps a lamda expression or std::function
Definition: delegate.hxx:184
bool operator!=(const delegate &rhs) const noexcept
Compares this instance to the as argument passed rhs .
Definition: delegate.hxx:151
delegate(const delegate &arg)=default
The default copy constructor.
delegate(void)=default
The default constructor.
delegate(std::nullptr_t) noexcept
Constructs an empty delegate representing a nullptr.
Definition: delegate.hxx:158
bool operator!=(std::nullptr_t const) const noexcept
Compares this instance to a nullptr.
Definition: delegate.hxx:169
static delegate create(std::function< R(A...)> &&f)
Creates a delegate from a lambda expression or std::function.
Definition: delegate.hxx:126
void reset()
Resets this instance.
Definition: delegate.hxx:201
static delegate create(R(*const pFunction)(A...))
Creates a delegate from a static function.
Definition: delegate.hxx:78
static delegate create(C &object, R(C::*pMethod)(A...)) noexcept
Creates a delegate from a member function and object reference.
Definition: delegate.hxx:98
delegate(Lambda f)
Constructs a delegate through a lambda expression implicitly.
Definition: delegate.hxx:56
delegate(delegate &&arg) noexcept=default
The default move constructor.
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:108
static delegate create(C *pObject, R(C::*pMethod)(A...)) noexcept
Creates a delegate from a member function and object pointer.
Definition: delegate.hxx:88
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:118
delegate & operator=(delegate &&arg) noexcept=default
The default move assign operator.
static delegate create(const std::function< R(A...)> &f)
Creates a delegate from a lambda expression or std::function.
Definition: delegate.hxx:134
R operator()(A... args) const
The functor operator invokes the adapted callable target.
Definition: delegate.hxx:193
delegate & operator=(const delegate &arg)=default
The default assign operator.
bool operator==(const delegate &rhs) const noexcept
Compares this instance to the as argument passed rhs .
Definition: delegate.hxx:143
bool operator==(std::nullptr_t const) const noexcept
Compares this instance to a nullptr.
Definition: delegate.hxx:162
delegate< R(A...)> make_delegate(R(*const function_ptr)(A...)) noexcept
Creates a delegate from a static function.
Definition: delegate.hxx:225
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:1876
Root namespace for the PLCnext API
Namespace of the C++ standard library