[wx-dev] RFC improved event handling

Brian Vanderburg II BrianVanderburg2 at aim.com
Wed Jul 30 04:18:09 PDT 2008


Peter Most wrote:
>>  It's nice that after many years we suddenly have not one but two proposals
>> about improving this but before I read this one attentively (I didn't have 
>> time to do it yet, sorry),
>>     
>
> No problem, you are probably more busy with the current release. Besides I don't
> need you to play compiler, but want to know whether this goes in the right
> direction. If it does, then I would start modifying event.h/event.cpp to see
> what other problems might arise.
>
>   
>> could you please explain what are the 
>> differences compared to the proposal in the "Template based
>> wxEvtHandler::Connect feature request/suggestion" thread?
>>
>>     
>
> The concept is basically the same, but IMHO I think my solution is a little cleaner,
> but I'm obviously biased (@Brian: If I write something wrong, please correct me!)
>
> The biggest differences are:
>
> - To associate the event with the event type my approach uses a template
> (wxEventTypeTemplate). Brian uses the preprocessor to create an explicit
> class.
>   
I agree this is nicer, and since m_value is not static, it is save to 
use the same type object more than one and still get a unique value for 
each event type:

DEFINE_NEW_EVENT_TYPE(wxEVT_MOTION, wxMouseEvent);
DEFINE_NEW_EVENT_TYPE(wxEVT_LEFT_DOWN, wxMouseEvent);
etc

> - My solution would completely encapsulate the wxEventType which would
> enhance readability and ensures that wxEventType will be used consistently
> throughout the codebase.
> Brian uses the implicit conversion to int.
>
> Minor differences are:
>
> - Brian needs the downcast template function which I couldn't figure out why,
> (but I could be missing something here) because this should always compile :
>
> void ( BaseClass::*method )( wxColorEvent & ) = &DerivedClass::handleColorEvent;
>
> But maybe it has something todo with the fact that his ConnectMethod is declared like this:
>
> template <typename E, typename C, typename D>
>     void ConnectMember(const E& type, void (C::*fn)(typename E::EventObjectType&), D* obj)
>
> where he allows different types for the method and the actual handler object.
>   
For MinGW GCC 3.45 I had the problem where if I did:

template <typename E, typename C>
    void ConnectMember(const E& type, void (C::*fn)(typename E::EventObjectType&), C* obj)


Derived* d;
Connect(EVT_MOTION, &Base::OnMotion, d);

It would complain that no match was found since it is expecting Base* 
and not Derived* as the obj

In my example it still needs to be:

template <typename E, typename C, typename D>
    void ConnectMember(const E& type, void (C::*fn)(typename E::EventObjectType&), D* obj)


But the downcast is not needed since in the functor creation D gets 
converted to C implicitly anyways.
> What I already wanted to get right was the names/interface. I think that the names:
> wxEvtFunctor, wxEvtMethod, wxEvtFunction and the overloaded Connect-method fit
> better in the naming scheme of wxWidgets.
>
>   

A few possible problems I see maybe.

If it is created in the header file, and two different source files 
include the header file, could that possibly result in two different 
wxEVT_MOTION items with different event codes?  I know that most linkers 
will optimize away all but one instance of the template which should be 
fine, but what if wxWidgets is built as a DLL?  Then the DLL would have 
an instance of wxEVT_MOTION and the application may have another 
instance of wxEVT_MOTION with a different event value.   If the 
instantiation is kept in a single source file this wouldn't be a 
problem, but I don't know if DLLs can export instantiated template classes:

#define DECLARE_NEW_EVENT_TYPE(name, event) \
    extern WX_DLLIMPEXP_BASE const wxEventTypeTemplate<event> name;

#define DEFINE_NEW_EVENT_TYPE(name, event) \
    const wxEventTypeTemplate<event> name;

Again that may not be a problem but I'm not sure.


Other thoughts, once the connection point of the event handlers is made 
typesafe, it would be nice if the sending points could be made typesafe 
as well.  Currently, what is to stop me from saying:

wxMouseEvent evt(wxEVT_PAINT);
handler->ProcessEvent(evt)?

But probably best to focus on one part at a time.


Brian Vanderburg II


More information about the wx-dev mailing list