my new Event class: template variadics and how test a valid function\lambda

Pages: 12
Jun 9, 2016 at 12:29pm
see my class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<typename...a>
class Event
{
public:
    std::function<void()> EventName{[](){;}};
    std::function<void(a...parameters)> EventNameWithParameters{[](a...parameters){;}};

    Event(std::function<void()> Event=[](){;})
    {
        EventName=Event;
    }

    Event&operator ()()
    {
        EventName();
        return *this;
    }

    Event(std::function<void(a...parameters)> Event=[](a...parameters){;})
    {
        EventNameWithParameters=Event;
    }

    Event &operator ()(a...parameters)
    {
        EventNameWithParameters(parameters...);
        return *this;
    }
};


how i can use it(with and without parameters):

Event test{[](){MessageBox("hello world");}}
i get these error message: "invalid use of template-name 'Event' without an argument list"
i thot that i was doing rigth with:
template<typename...a>
what i'm doing wrong?
my objective is accept any or more parameters
Last edited on Jun 10, 2016 at 11:34am
Jun 9, 2016 at 1:42pm
template<typename...a> allows no paramter. So you do not need to do any extra work for this.

Change

Event test{[](){MessageBox("hello world");}}

to:

Event<> test{[](){MessageBox("hello world");}} // Note: <>


What is the use of the empty lambdas?
Jun 9, 2016 at 2:15pm
"What is the use of the empty lambdas?"
just an inicializate value
Jun 9, 2016 at 2:22pm
i have another error here:
Event(std::function<void(a...parameters)> Event=[](a...parameters){;})
what i'm doing wrong?
Jun 9, 2016 at 2:32pm
Remove the extra code for the function without parameter.
Jun 9, 2016 at 2:42pm
yes. now works fine. thanks for all
how can i test if the function is empty or something?
(someone said me before, but i forget it)
Jun 9, 2016 at 3:01pm
1
2
3
    std::function<void()> func;
    if(func) // Note: the operator bool tells whether the function is valid or not
      func();
Jun 9, 2016 at 3:07pm
doing these:
test=[](int, int){;};
means that have a function.
but doing these:
test=NULL
i will get empty.
heres how i test it:
1
2
3
4
5
6
7
bool IsEmpty()
    {
        if (EventNameWithParameters)
            return false;
        else
            return true;
    }

with that 'if', i fixed another confused error. thanks for all
Last edited on Jun 9, 2016 at 5:06pm
Jun 9, 2016 at 3:35pm
what you can tell me?
Jun 10, 2016 at 8:13am
what you can tell me?
About what? I don't get that question.
Jun 10, 2016 at 10:39am
maybe was you that said that i must do these(for be empty or something):
test=[](int, int){;};
but the 'if' calls it anyway:
1
2
3
std::function<void()> test=[](int, int){;};;
    if(test) // Note: the operator bool tells whether the function is valid or not
      test();

that's true don't do anything, but calls it.
so the 'if' only test if the 'test' is good for call nothing more, right?
seen again.. sorry. i read, again the comment.
thaks for all
Last edited on Jun 10, 2016 at 11:24am
Jun 10, 2016 at 11:28am
see the class updated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
template<typename...a>
class Event
{
public:
    std::function<void(a...parameters)> EventNameWithParameters{[](a...parameters){;}};


    Event(std::function<void(a...parameters)> Event=[](a...parameters){;})
    {
        EventNameWithParameters=Event;
    }

    Event &operator ()(a...parameters)
    {
        if(EventNameWithParameters)
            EventNameWithParameters(parameters...);
        return *this;
    }

    Event &operator =(std::function<void(a...parameters)> Event)
    {
        EventNameWithParameters=Event;
    }

    bool IsValid()
    {
        if (EventNameWithParameters)
            return true;
        else
            return false;
    }
};

//with parameters:
Event<int, int> test{[](int a, int b)
{
    MessageBox("hello world 1: " + to_string(a+b));
}};

//Without parameters
Event<> test2{[]()
{
    MessageBox("hello world!!!!");
}};

Readers: see the diference betwee with parameters and without.
like you see now the function is tested before been called.
Jun 13, 2016 at 8:58am
maybe was you that said that i must do these
No. It is your program and you must check whether something is usefull within that context or not.

So what do you think? Is it usefull or not?
Consider the effects within your program. Where I don't have the overview.
Jun 13, 2016 at 10:18am
yes it is, by several reasons:
1 - it's more easy create the lambda;
2 - always test before call it...

well the only problem is that i can't change it on General Area, only create it and inicializate it.
"Consider the effects within your program. Where I don't have the overview."
maybe you are warning me something, but i don't see the bad thing.
Jun 17, 2016 at 7:05pm
why i can't use it on parameter function?
1
2
3
4
Event<> timerprocedure;
    //std::function<void()> timerprocedure=nullptr;
    Timer(Event<> tmrprocedure=[](){;})
    {


error message: "could not convert '<lambda closure object>Timer::__lambda4{}' from 'Timer::__lambda4' to 'Event<>'"
i don't know what means these error... can you explain better?
Jun 20, 2016 at 8:01am
I would think that the implicit conversion is not possible since the lambda function is a pointer. Try:

Timer(Event<> tmrprocedure([](){;})) // Note the extra ()
Jun 20, 2016 at 5:22pm
Timer(Event<> tmrprocedure([](){;}))
error message:
"expected ',' or '...' before '(' token"
why if i don't use arguments?
Jun 21, 2016 at 7:57am
What are you trying to do? Defining a parameter with the same name or passing a paramter?

If you want to define a [default] paramer, it would look like this:

Timer(Event<> tmrprocedure = Event<>()) // You have already the empty lamda a default in Event
Jun 21, 2016 at 5:38pm
yes it a default constructor.

i did. so see these Timer sample\object:
Timer tmrCreate{[&](){Create();}};
error:
could not convert '{<lambda closure object>consolewindow::__lambda144{((consolewindow*)this)}}' from '<brace-enclosed initializer list>' to 'Timer'
why these error?
Jun 22, 2016 at 8:49am
Since it is not possible to cast a lamda implicitly you nee to do it explicitly:

Timer tmrCreate{Event<>([&](){Create();})};
Pages: 12