-
Notifications
You must be signed in to change notification settings - Fork 1
Class __Delegate
Implementation of a simple delegate based on the std::function functionality from the c++ standard library. Allows you to implement a functional object of a class method or a static function for further invocation.
#include <EasyDelegateImpl.hpp>
| Name | |
|---|---|
| template <class _LabbdaFunction > void |
attach(_LabbdaFunction && lfunc) The method is intended for binding a lambda function to a delegate. |
| template <class ... Args> void |
attach(Args &&... args) The method is intended for binding a static function to a delegate. A reference to the required function is passed as arguments. |
| template <class _Class ,class _ReturnType ,class ... Args> void |
attach(_Class * c, _ReturnType(_Class::*)(Args...) m) For class or const class method. Receiving class pointer and class method reference. |
| void |
detach() Detaching function delegate. |
| template <class ... Args> auto |
operator()(Args &&... args) Redefining the parenthesis operator for convenient delegate invocation. |
| operator bool() |
template <class _Signature >
class EasyDelegate::__Delegate;Implementation of a simple delegate based on the std::function functionality from the c++ standard library. Allows you to implement a functional object of a class method or a static function for further invocation.
Template Parameters:
- _Signature signature of the delegate function
template <class _LabbdaFunction >
inline void attach(
_LabbdaFunction && lfunc
)The method is intended for binding a lambda function to a delegate.
Parameters:
- lfunc
Template Parameters:
- LabbdaFunction lambda function type transited with template parameter.
template <class ... Args>
inline void attach(
Args &&... args
)The method is intended for binding a static function to a delegate. A reference to the required function is passed as arguments.
Parameters:
- args static function reference...
Return: void
template <class _Class ,
class _ReturnType ,
class ... Args>
inline void attach(
_Class * c,
_ReturnType(_Class::*)(Args...) m
)For class or const class method. Receiving class pointer and class method reference.
Parameters:
- c Class pointer
- m Method reference
inline void detach()Detaching function delegate.
template <class ... Args>
inline auto operator()(
Args &&... args
)Redefining the parenthesis operator for convenient delegate invocation.
Parameters:
- args Delegate arguments
Template Parameters:
- Args Templated std::tuple arguments
Return: auto
inline operator bool()#include <iostream>
#include "EasyDelegate.hpp"
using namespace EasyDelegate;
int foo(int x, int y, bool ts)
{
return ts ? x : y;
}
class Foo
{
public:
bool foo(bool ts, bool fs)
{
return ts && fs;
}
};
int main()
{
//Creating delegate object with specified signature
TDelegate<int(int, int, bool)> fooDelegate;
//Attaching static function to delegate
fooDelegate.attach(&foo);
//Calling delegate function
auto iresult = fooDelegate(5, 10, true);
//Removing function from delegate. After detach, you can attach another function.
fooDelegate.detach();
//Using delegate with class member
Foo fuf;
//Declare delegate with another signature
TDelegate<bool(bool, bool)> fooClassDelegate;
//Attaching reference to class and class method. Class method should be public, or static
fooClassDelegate.attach(&fuf, &Foo::foo);
//Executing delegate and getting result
auto bresult = fooClassDelegate(true, false);
//We can attach another function after detach
fooClassDelegate.detach();
//Also you can attach lambda
fooClassDelegate.attach([&](bool f, bool s)
{
return fuf.foo(f, f || s);
});
//And another lambda
TDelegate<int(int, bool)> fooLambdaDelegate;
fooLambdaDelegate.attach([&](int i, bool b)
{
auto subres = fooClassDelegate(i, b);
return fuf.foo(b, i > 0) && subres ? i * i : i * 2;
});
auto lresult = fooLambdaDelegate(iresult, bresult);
std::cout << lresult << std::endl;
return 0;
}