3#ifndef _aspose_system_function_h_
4#define _aspose_system_function_h_
13#include "system/details/delegate_holding_variables.h"
18 template<
class Unused>
54 template <
class ReturnType,
class... ArgumentTypes>
55 class Delegate <ReturnType(ArgumentTypes...)> :
public System::Details::DelegateHoldingVariables
69 Delegate(Delegate&& o) noexcept : m_invoker(std::move(o.m_invoker)) {}
74 Delegate&
operator=(Delegate&& o)
noexcept { m_invoker = std::move(o.m_invoker); }
80 Delegate(T function,
typename std::enable_if<!std::is_bind_expression<T>::value && std::is_pointer<T>::value && std::is_function<
typename std::remove_pointer<T>::type>::value>::type * = 0)
81 : m_invoker( new FunctionInvoker<T>(std::move(function)) )
89 Delegate(T function,
typename std::enable_if<std::is_bind_expression<T>::value>::type * = 0)
90 : m_invoker( new BindInvoker<T>(function) )
100 : m_invoker( new FunctorInvoker<T>(functor) )
110 : m_invoker( new BindInvoker<T>(std::move(functor)) )
120 template <
class MemberType,
class ClassType>
121 Delegate(MemberType ClassType::* member, ClassType* obj)
122 : m_invoker( new ClassMemberInvoker<MemberType, ClassType>(member, obj) )
131 template <
class MemberType,
class MemberClass,
class ClassType>
133 : m_invoker(new ClassMemberInvoker<MemberType, ClassType>(member, obj))
141 template <
class R,
class... Args>
143 : m_invoker( new StdFunctionInvoker<std::function<R(Args...)>>(f) )
152 return m_invoker->invoke(args...);
160 if( m_invoker == f.m_invoker )
163 if( !m_invoker || !f.m_invoker )
166 return m_invoker->compare( *f.m_invoker.get());
171 bool Empty()
const {
return m_invoker->is_empty(); }
173#if defined(ASPOSE_GET_SHARED_MEMBERS) || defined(__DBG_FOR_EACH_MEMBER)
175 const std::shared_ptr<Details::HeldVariables>& GetHeldVariables() const noexcept
177 return m_heldVariables;
192 virtual ~BaseInvoker() {}
194 BaseInvoker(
const BaseInvoker&) =
delete;
195 BaseInvoker& operator=(
const BaseInvoker&) =
delete;
200 virtual ReturnType invoke(ArgumentTypes... args) = 0;
205 bool compare(
const BaseInvoker& other)
207 if( fast_compare_object() != other.fast_compare_object() )
210 return deep_compare(other);
217 return static_cast<size_t>(fast_compare_object());
223 virtual bool deep_compare(
const BaseInvoker& other)
const = 0;
228 virtual const void * fast_compare_object()
const = 0;
232 virtual bool is_empty()
const = 0;
241 struct FunctionInvoker :
public BaseInvoker
245 FunctionInvoker(T function) : m_function(std::move(function))
247 static_assert(!std::is_bind_expression<T>::value,
"Bind");
248 static_assert(std::is_pointer<T>::value,
"Only functions and static member functions is supported, not all callable objects (functors).");
254 ReturnType invoke(ArgumentTypes... args)
override
256 return m_function(args...);
262 const void * fast_compare_object()
const override
264 return reinterpret_cast<const void *
>(m_function);
270 bool deep_compare(
const BaseInvoker& other)
const override
272 auto obj =
dynamic_cast<const FunctionInvoker<T>*
>(&other);
273 return obj && m_function == obj->m_function;
278 bool is_empty()
const override {
return m_function ==
nullptr; }
290 struct BindInvoker :
public BaseInvoker
294 BindInvoker(T function) : m_function(std::move(function))
301 ReturnType invoke(ArgumentTypes... args)
override
303 return m_function(args...);
308 const void * fast_compare_object()
const override
310 return reinterpret_cast<const void *
>(1);
318 bool deep_compare(
const BaseInvoker& other)
const override
325 bool is_empty()
const override {
return false; }
337 struct FunctorInvoker :
public BaseInvoker
341 FunctorInvoker(T& functor) : m_functor(functor)
348 ReturnType invoke(ArgumentTypes... args)
override
350 return m_functor(args...);
356 const void * fast_compare_object()
const override
358 return reinterpret_cast<const void *
>(1);
364 bool deep_compare(
const BaseInvoker& other)
const override
366 auto obj =
dynamic_cast<const FunctorInvoker<T>*
>(&other);
367 return obj && m_functor == obj->m_functor;
372 bool is_empty()
const override {
return false; }
384 struct StdFunctionInvoker :
public BaseInvoker
388 StdFunctionInvoker(T function) : m_function(function)
395 ReturnType invoke(ArgumentTypes... args)
override
397 return m_function(args...);
403 const void * fast_compare_object()
const override
405 auto ptr = m_function.template target<ReturnType(*)(ArgumentTypes...)>();
413 bool deep_compare(
const BaseInvoker& other)
const override
420 bool is_empty()
const override {
return m_function ==
nullptr; }
432 template<
class MemberType,
class ClassType>
433 struct ClassMemberInvoker :
public BaseInvoker
436 using ClassFunction = MemberType ClassType::*;
441 ClassMemberInvoker(ClassFunction member,
const SharedPtr<ClassType>& obj)
442 : m_member_function(member), m_obj(obj, obj.get_Mode())
448 ClassMemberInvoker(ClassFunction member, ClassType* obj)
455 ReturnType invoke(ArgumentTypes... args)
override
457 auto* ptr = m_obj.get();
463 throw NullReferenceException(u
"Delegate: Object reference not set to an instance of an object.");
468 bool is_empty()
const override {
return m_obj ==
nullptr; }
474 const void * fast_compare_object()
const override
482 bool deep_compare(
const BaseInvoker& other)
const override
484 typedef ClassMemberInvoker<MemberType, ClassType> ThisType;
485 auto obj =
dynamic_cast<const ThisType*
>(&other);
486 return obj && m_member_function == obj->m_member_function && m_obj == obj->m_obj;
491 ClassFunction m_member_function;
494 SmartPtr<ClassType> m_obj;
500 std::shared_ptr<BaseInvoker> m_invoker;
bool Empty() const
Determines if the current delegate object is empty, e.g. does not point to any entity.
Definition: delegate.h:171
Delegate(long functor_tag, T &&functor)
Moving constructor. Constructs a delegate from the specified function object.
Definition: delegate.h:109
Delegate()=default
Default constructor. Constructs the delegate object that does not point to anything.
Delegate(T function, typename std::enable_if< std::is_bind_expression< T >::value >::type *=0)
Constructor. Constructs a delegate from the specified pointer to the function object generated by std...
Definition: delegate.h:89
Delegate(int functor_tag, T &functor)
Constructor. Constructs a delegate from the specified function object.
Definition: delegate.h:99
Delegate(T function, typename std::enable_if<!std::is_bind_expression< T >::value &&std::is_pointer< T >::value &&std::is_function< typename std::remove_pointer< T >::type >::value >::type *=0)
Constructor. Constructs a delegate object from the specified pointer to free function or static metho...
Definition: delegate.h:80
Delegate(const Delegate &)=default
ReturnType operator()(ArgumentTypes... args) const
Invokes a function, method or a function object that is pointed to by current delegate object.
Definition: delegate.h:150
bool operator==(const Delegate &f) const
Compares two delegate objects to check if they point to the same entity.
Definition: delegate.h:158
Delegate(MemberType MemberClass::*member, const SharedPtr< ClassType > &obj)
Constructor. Constructs a delegate that points to the specified non-static method of the specified ob...
Definition: delegate.h:132
Delegate(Delegate &&o) noexcept
Moving copy constructor. Takes the ownership of an entity pointed to by the specified delegate.
Definition: delegate.h:69
Delegate & operator=(Delegate &&o) noexcept
Moving assignment operator. Takes the ownership of an entity pointed to by the specified delegate.
Definition: delegate.h:74
Delegate & operator=(const Delegate &)=default
Delegate(std::function< R(Args...)> f)
Constructs a delegate object that points to an std::function function object.
Definition: delegate.h:142
Delegate(MemberType ClassType::*member, ClassType *obj)
Constructor. Constructs a delegate that points to the specified non-static method of the specified ob...
Definition: delegate.h:121
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: db_command.h:9
SmartPtrMode
SmartPtr pointer type: weak or shared. Defines whether pointer is being counted when it is being deci...
Definition: smart_ptr.h:68
static To * cast(From *pointer)
Performs actual pointer casting using dynamic_cast.
Definition: detail.h:38