Skip to content

Class __DelegateMulti

AdamFull edited this page Aug 7, 2021 · 3 revisions

EasyDelegate::__DelegateMulti

Implementation of the ability to store multiple delegates with the same signature inside a single structure with a user-friendly interface.

#include <EasyDelegateMultiImpl.hpp>

Public Functions

Name
template <_Enumerator eBase>
void
attach(__Delegate< _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 from included std::map container.
template <_Enumerator eBase,class ... Args>
void
execute(Args &&... args)
Executes the delegate for the specified enumerator.
template <class ... Args>
void
execute(Args &&... args)
Executes all the delegates that were attached to the object.
template <_Enumerator eBase,class ... Args>
auto
eval(Args &&... args)
Evaluates the delegate on the specified enumerator and returns the value.
template <class ... Args>
auto
eval(Args &&... args)
Executes all the delegates attached to the object and returns the std::map object containing the calculation results. You can also refer to the result by the numerator.
template <_Enumerator eBase,class ... Args>
auto
operator()(Args &&... args)
Illegal in any c++ standart.

Detailed Description

template <class _Enumerator ,
class _Signature ,
class _Comp >
class EasyDelegate::__DelegateMulti;

Implementation of the ability to store multiple delegates with the same signature inside a single structure with a user-friendly interface.

Template Parameters:

  • _Enumerator The enumerator class used for binding to a functional object
  • _Signature Signature of the function accepted by the delegate
  • _Comp Comparator for the enumerator

Public Functions Documentation

function attach

template <_Enumerator eBase>
inline void attach(
    __Delegate< _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

Template Parameters:

  • eBase User defined enumeration key
  • LabbdaFunction Lambda object

function attach

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

Attaches the passed method to the User defined enumeration key.

Parameters:

  • args Delegate arguments

Template Parameters:

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

function detach

template <_Enumerator eBase>
inline void detach()

Detaching delegate from included std::map container.

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:

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

function execute

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

Executes all the delegates that were attached to the object.

Parameters:

  • args Delegate arguments

Template Parameters:

  • 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:

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

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

function eval

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

Executes all the delegates attached to the object and returns the std::map object containing the calculation results. You can also refer to the result by the numerator.

Parameters:

  • args Delegate arguments

Template Parameters:

  • Args Templated std::tuple arguments

Return: std::map<_Enumerator, return_type>

function operator()

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

Illegal in any c++ standart.

Parameters:

  • args

Template Parameters:

  • eBase
  • Args

Return: SignatureDesc<_Signature>::return_type


Example

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

using namespace EasyDelegate;

enum class EEnumerator
{
    EFirst,
    ESecond,
    EThird,
    EFourth,
    EAnother
};

void foo(int x, int y)
{
    std::cout << x - y << std::endl;
}

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

int ifoo(int x, int y)
{
    return x - y;
}

int iboo(int x, int y)
{
    return x + y;
}

class Foo
{
public:
    void foo(int x, int y)
    {
        std::cout << "Class foo: " << x / y << std::endl;
    }

    int boo(int x, int y)
    {
        return x * (x % y) * (x - y);
    }
};

int main()
{
    Foo cfoo;

    //Declare multi delegate container
    TDelegateMulti<EEnumerator, void(int, int)> MultDelegate;
    //Attaching delegates
    MultDelegate.attach<EEnumerator::EFirst>(&foo);
    MultDelegate.attach<EEnumerator::ESecond>(&boo);
    MultDelegate.attach<EEnumerator::EThird>(&cfoo, &Foo::foo);

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

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

    //Executing all delegates
    MultDelegate.execute(5, 6);

    //Declare multi delegate container with another signature
    TDelegateMulti<EEnumerator, int(int, int)> MultDelegateE;
    MultDelegateE.attach<EEnumerator::EFirst>(&ifoo);
    MultDelegateE.attach<EEnumerator::ESecond>(&iboo);
    MultDelegateE.attach<EEnumerator::EThird>([&](int x, int y)
    { 
        return x * x - cfoo.boo(x * y, x % y) + y * y; 
    });
    MultDelegateE.attach<EEnumerator::EFourth>(&cfoo, &Foo::boo);

    //Evaluating all delegates
    auto results = MultDelegateE.eval(5, 9);
    for (auto &[_key, _value] : results)
    {
        std::cout << _value << std::endl;
    }

    return 0;
}

Clone this wiki locally