Lambda's as C++ Event Handler

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Lambda's as C++ Event Handler

Postby jayden » Wed Sep 09, 2015 10:07 am

Rather than creating a separate method to handle an event such as when a TFloatAni finishes and the event is only going to dispose of the float ani I want to do this using a lambda.

Code: Select all
   
        TFloatAnimation *ani = new TFloatAnimation(NULL);
   ani->StartFromCurrent = true;
   ani->Duration = 1;
   ani->PropertyName = "Position.Y";
   ani->Parent = Panel1;
   //Sbow the panel
   if(Panel1->Position->Y > TabView->Height)
      ani->StopValue = TabView->Height - Panel1->Height;
   else
      ani->StopValue = TabView->Height + Panel1->Height;

   ani->Start();
   ani->OnFinish = [this](TObject *Sender) -> void {
      Sender->DisposeOf();
   }();

However at the moment I am getting an expression syntax compiler error - was wondering if anyone could help?
jayden
Active Poster
Active Poster
 
Posts: 24
Joined: Fri Aug 21, 2015 3:41 pm

Re: Lambda's as C++ Event Handler

Postby rlebeau » Wed Sep 09, 2015 3:25 pm

jayden wrote:I want to do this using a lambda


Sorry, but you cannot. You have to use a real method of a class object instance as the event handler:

Code: Select all
void __fastcall TMyForm::AniFinished(TObject *Sender)
{
    Sender->DisposeOf();
}

...

ani->OnFinish = &AniFinished;


jayden wrote:However at the moment I am getting an expression syntax compiler error - was wondering if anyone could help?


Lambdas are not compatible with Delphi-style closures. There is no way to assign a lambda directly as an event handler. The same is true with Delph-style anonymous methods, for that matter. Though it is possible to wrap an anonymous method inside of a delegating class object, so you might be able to do the same with a lambda. A lambda can be assigned to a std::function, so you could write a class that wraps a std::function and exposes a public method to act as the event handler and internally delegate to the std::function.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1532
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Lambda's as C++ Event Handler

Postby jayden » Thu Sep 10, 2015 7:03 am

Thanks, shame that you can't leverage lambdas but oh well!

Where you have attached the event handler you have set OnFinish to the address of AniFinished which is the logical way to do it, the reason I didn't was because all the Embarc examples ive seen dont prefix with the amp which I found odd at first.

Which is the best way to do it performance wise, address-of?
jayden
Active Poster
Active Poster
 
Posts: 24
Joined: Fri Aug 21, 2015 3:41 pm

Re: Lambda's as C++ Event Handler

Postby rlebeau » Thu Sep 10, 2015 10:41 pm

jayden wrote:Where you have attached the event handler you have set OnFinish to the address of AniFinished which is the logical way to do it, the reason I didn't was because all the Embarc examples ive seen dont prefix with the amp which I found odd at first.


For direct assignment via the '=' operator, the '&' is optional. Specifying just the method name is the same is getting the address of the method. Similar to how specifying just the name of a static array is the same as getting the address of the array.

There are cases where using the '&' operator is needed. Usually when passing a method as a parameter to another method, eg:

Code: Select all
void DoSomething(TNotifyEvent event)
{
    ...
    if (event) event(object);
    ...
}

void __fastcall TSomeClass::handler(TObject *Sender)
{
    ...
}

void __fastcall TSomeClass::DoIt()
{
    //DoSomething(handler);
    DoSomething(&handler);
}


jayden wrote:Which is the best way to do it performance wise, address-of?


There is no performance difference one way or the other.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1532
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: No registered users and 15 guests

cron