Skip to content

Struct __DelegateAny

AdamFull edited this page Aug 10, 2021 · 6 revisions

EasyDelegate::__DelegateAny

A container for creating delegates at the program execution stage. Allows you to add delegates with any signature, and make their further call. At the moment, it cannot be used together with TDelegateAnyCT and vice versa. More...

#include <EasyDelegateAnyImpl.hpp>

Public Functions

Name
template <_Enumerator eBase>
void
attach(__Delegate< typename __DelegateTypeStore< TakeStoreKey< _Enumerator, eBase >()>::signature > && _delegate)
Attaching existing delegate if signature is same.
template <_Enumerator eBase,class _LabbdaFunction >
void
attach(_LabbdaFunction && lfunc)
Attaches the passed method to the user defined enumeration key.
template <_Enumerator eBase,class ... Args>
void
attach(Args &&... args)
Attaches the passed method to the user defined enumeration key.
template <_Enumerator eBase>
void
detach()
Detaching delegate.
template <_Enumerator eBase,class ... Args>
void
execute(Args &&... args)
Executes the delegate for the specified enumerator.
template <_Enumerator eBase,class... Args>
auto
eval(Args &&... args)
Evaluates the delegate on the specified enumerator and returns the value.

Detailed Description

template <class _Enumerator ,
class _Comp >
struct EasyDelegate::__DelegateAny;

A container for creating delegates at the program execution stage. Allows you to add delegates with any signature, and make their further call. At the moment, it cannot be used together with TDelegateAnyCT and vice versa.

Template Parameters:

  • _Enumerator
  • _Signature

Public Functions Documentation

function attach

template <_Enumerator eBase>
inline void attach(
    __Delegate< typename __DelegateTypeStore< TakeStoreKey< _Enumerator, eBase >()>::signature > && _delegate
)

Attaching existing delegate if signature is same.

Parameters:

  • _delegate existing delegate as r-value

Template Parameters:

  • eBase User defined enumeration key

function attach

template <_Enumerator eBase,
class _LabbdaFunction >
inline void attach(
    _LabbdaFunction && lfunc
)

Attaches the passed method to the user defined enumeration key.

Parameters:

  • lfunc Universal ling to LambdaFunction signature

Template Parameters:

  • eBase User defined enumeration key
  • LabbdaFunction Lambda function signature

function attach

template <_Enumerator eBase,
class ... Args>
inline void attach(
    Args &&... args
)

Attaches the passed method to the user defined enumeration key.

Parameters:

  • args

Template Parameters:

  • eBase User defined enumeration key
  • Args

function detach

template <_Enumerator eBase>
inline void detach()

Detaching delegate.

Template Parameters:

  • eBase User defined enumeration key

function execute

template <_Enumerator eBase,
class ... Args>
inline void execute(
    Args &&... args
)

Executes the delegate for the specified enumerator.

Parameters:

  • args Delegate arguments

Template Parameters:

  • eEnum User defined enumeration key
  • Args Templated std::tuple arguments

function eval

template <_Enumerator eBase,
class... Args>
inline auto eval(
    Args &&... args
)

Evaluates the delegate on the specified enumerator and returns the value.

Parameters:

  • args Delegate arguments

Template Parameters:

  • eEnum User defined enumeration key
  • Args Templated std::tuple arguments

Return: SignatureDesc<_Signature>::return_type auto eval(Args&&... args)


AnyExample

#include <iostream>
#include "EasyDelegate.hpp"

using namespace EasyDelegate;

//    An important note. You cannot use TDelegateAny and TDelegateAnyCT at the 
//    same time due to some technical problems. The same is the case with the 
//    enumerator. When the problem is resolved, this will be indicated in the update.

// If you want use multiple enums, start new enum with higher index
enum class EEnumerator
{
    EIntDelegate,
    EBoolDelegate,
    EAnother
};

//This declarations should be in cpp file
DeclareDelegateFuncRuntime(EEnumerator, EEnumerator::EIntDelegate, int(int, int, bool))
DeclareDelegateFuncRuntime(EEnumerator, EEnumerator::EBoolDelegate, bool(bool, bool))
DeclareDelegateFuncRuntime(EEnumerator, EEnumerator::EAnother, void(int, int))

int foo(int x, int y, bool ts)
{
    return ts ? x : y;
}

void boob(int x, int y)
{
    std::cout << x + y << std::endl;
}

class Foo
{
public:
    bool foo(bool ts, bool fs)
    {
        return ts && fs;
    }
};

int main()
{
    //Declare any delegate object
    TDelegateAny<EEnumerator> _delegates;
    //Attach to static function 
    _delegates.attach<EEnumerator::EIntDelegate>(&foo);
    //Call function
    auto iresult = _delegates.eval<EEnumerator::EIntDelegate>(50, -50, false);

    //Now with class object (also you can use 'this', if your delegate inside a class)
    Foo boo;
    //Attach class method to delegate container
    _delegates.attach<EEnumerator::EBoolDelegate>(&boo, &Foo::foo); //Same with classes
    auto bresult = _delegates.eval<EEnumerator::EBoolDelegate>(true, true);
    _delegates.detach<EEnumerator::EBoolDelegate>();

    //Attaching existing delegate
    TDelegate<void(int, int)> AnotherDelegate;
    AnotherDelegate.attach(&boob);

    _delegates.attach<EEnumerator::EAnother>(std::move(AnotherDelegate));

    _delegates.execute<EEnumerator::EAnother>(1, 2);
    _delegates.execute<EEnumerator::EAnother>(2, 1);

    //And lambda functions
    _delegates.attach<EEnumerator::EBoolDelegate>([&](bool b, bool n)
    { 
        return boo.foo(b, n) || b;
    });

    auto lresult = _delegates.eval<EEnumerator::EBoolDelegate>(true, false);
    _delegates.detach<EEnumerator::EBoolDelegate>();

    std::cout << lresult << std::endl;

    return 0;
}

Clone this wiki locally