Passing parameters in event mapping with macro substitution

 

Is there any way to pass a parameter when using macro substitution for event mapping?

#define ON_EVENT(event,control,handler)  if(id==(event+CHARTEVENT_CUSTOM) && lparam==control.Id()) { handler(); return(true); }

 So would it be possible to pass the equivalent of OnClickButton(myParameter):

EVENT_MAP_BEGIN(CPanelDialog)
ON_EVENT(ON_CLICK,m_button,OnClickButton)
EVENT_MAP_END(CAppDialog)
I'm currently using intermediary methods, but it seems a bit long winded.
 
honest_knave:

Is there any way to pass a parameter when using macro substitution for event mapping?

#define ON_EVENT(event,control,handler)  if(id==(event+CHARTEVENT_CUSTOM) && lparam==control.Id()) { handler(); return(true); }

 So would it be possible to pass the equivalent of OnClickButton(myParameter):

EVENT_MAP_BEGIN(CPanelDialog)
ON_EVENT(ON_CLICK,m_button,OnClickButton)
EVENT_MAP_END(CAppDialog)
I'm currently using intermediary methods, but it seems a bit long winded.
What parameter is that ? Where it comes from ?
 
Alain Verleyen:
What parameter is that ? Where it comes from ?

It is just an example of a parameter. From what I can figure out, you cannot use a method with a parameter when using macro substitution event mapping.

This works fine: 

EVENT_MAP_BEGIN(CPanelDialog)
   ON_EVENT(ON_CLICK,  m_button1, OnClickButton1)
   ON_EVENT(ON_CLICK,  m_button2, OnClickButton2)
   ON_EVENT(ON_CLICK,  m_button3, OnClickButton3)
   ON_EVENT(ON_CLICK,  m_button4, OnClickButton4)
EVENT_MAP_END(CAppDialog)

But imagine the only difference between OnClickButton1(), OnClickButton2(), OnClickButton3() and OnClickButton4() is something that can be easily passed as a parameter. Currently, this is what I do:

void OnClickButton1() { OnClickButton(m_button1); }

void OnClickButton2() { OnClickButton(m_button2); }

void OnClickButton3() { OnClickButton(m_button3); }

void OnClickButton4() { OnClickButton(m_button4); }

void OnClickButton(CButton &button)
  {
   // all the code in here
  }


I am wondering if I can do away with 4 of these 5 methods and pass the parameter directly from the event mapping.

The following:

EVENT_MAP_BEGIN(CPanelDialog)
   ON_EVENT(ON_CLICK,  m_button1, OnClickButton(m_button1))
   ON_EVENT(ON_CLICK,  m_button2, OnClickButton(m_button2))
   ON_EVENT(ON_CLICK,  m_button3, OnClickButton(m_button3))
   ON_EVENT(ON_CLICK,  m_button4, OnClickButton(m_button4))
EVENT_MAP_END(CAppDialog)

Ends up with 4x: 

')' - expression expected
 

honest_knave,

a #define clause is replaced by preprocessor!

So:

int _;
#define
forall(All,_) _=All; while(_-->0)
#define
isZero(value,alternative) (value!=0.0?value:alternative)
#define
fmax3(a,b,c) fmax(a,fmax(b,c))
...

int o;

forall(OrdersTotal(),o) {
   double k,l,m,x,y,z;
   ...
   x = y/isZero(z,1);
   m = fmax3(k,l,m);

}

will be replaced as if you have coded:

o = OrdersTotal(); while(o-->0) {
   double k,l,m,x,y,z;
   ...
   x = y/(z!=0?z:1);
   m = fmax(k,fmax(l,m));
}
The variable int _; has to be declared all the other don't (I stopped guessing why!) - check it out yourself.
 
Thanks Carl, I'll have a play around.
 
honest_knave:

It is just an example of a parameter. From what I can figure out, you cannot use a method with a parameter when using macro substitution event mapping.

This works fine: 

EVENT_MAP_BEGIN(CPanelDialog)
   ON_EVENT(ON_CLICK,  m_button1, OnClickButton1)
   ON_EVENT(ON_CLICK,  m_button2, OnClickButton2)
   ON_EVENT(ON_CLICK,  m_button3, OnClickButton3)
   ON_EVENT(ON_CLICK,  m_button4, OnClickButton4)
EVENT_MAP_END(CAppDialog)

But imagine the only difference between OnClickButton1(), OnClickButton2(), OnClickButton3() and OnClickButton4() is something that can be easily passed as a parameter. Currently, this is what I do:

void OnClickButton1() { OnClickButton(m_button1); }

void OnClickButton2() { OnClickButton(m_button2); }

void OnClickButton3() { OnClickButton(m_button3); }

void OnClickButton4() { OnClickButton(m_button4); }

void OnClickButton(CButton &button)
  {
   // all the code in here
  }


I am wondering if I can do away with 4 of these 5 methods and pass the parameter directly from the event mapping.

The following:

EVENT_MAP_BEGIN(CPanelDialog)
   ON_EVENT(ON_CLICK,  m_button1, OnClickButton(m_button1))
   ON_EVENT(ON_CLICK,  m_button2, OnClickButton(m_button2))
   ON_EVENT(ON_CLICK,  m_button3, OnClickButton(m_button3))
   ON_EVENT(ON_CLICK,  m_button4, OnClickButton(m_button4))
EVENT_MAP_END(CAppDialog)

Ends up with 4x: 

')' - expression expected

You need to declare a "custom" ON_EVENT :

#define ON_EVENT_CUSTOM(event,control,handler,handler_param)  if(id==(event+CHARTEVENT_CUSTOM) && lparam==control.Id()) { handler(handler_param); return(true); }

Then

EVENT_MAP_BEGIN(CPanelDialog)
   ON_EVENT_CUSTOM(ON_CLICK,  m_button1, OnClickButton, m_button1)
   ON_EVENT_CUSTOM(ON_CLICK,  m_button2, OnClickButton, m_button2)
   ON_EVENT_CUSTOM(ON_CLICK,  m_button3, OnClickButton, m_button3)
   ON_EVENT_CUSTOM(ON_CLICK,  m_button4, OnClickButton, m_button4)
EVENT_MAP_END(CAppDialog)
Not compiled, not tested.
 
Works great - thank you Alain. I had tried the define without _CUSTOM and failed.
 

I have defined macro ON_EVENT_ARG (slightly modified macro ON_EVENT):

#define ON_EVENT_ARG(event, control, handler, argument)    if(id==(event+CHARTEVENT_CUSTOM) && lparam==control.Id()) { handler(argument); return(true); }

And then I used ON_EVENT_ARG instead of ON_EVENT:

EVENT_MAP_BEGIN(CTradePanel)
  ON_EVENT_ARG(ON_CLICK, this.button1, this.OnButtonClick, 1)
  ON_EVENT_ARG(ON_CLICK, this.button2, this.OnButtonClick, 2)
EVENT_MAP_END(CAppDialog)

if OnButtonClick is declared as:

void OnButtonClick(int value);

or, if OnButtonClick is declared as:

void OnButtonClick(CButtonClickArgs* args);

you can use:

EVENT_MAP_BEGIN(CTradePanel)
  ON_EVENT_ARG(ON_CLICK, this.button1, this.OnButtonClick, new CButtonClickArgs(1))
  ON_EVENT_ARG(ON_CLICK, this.button2, this.OnButtonClick, new CButtonClickArgs(2))
EVENT_MAP_END(CAppDialog)

for example.

Reason: