Page 1 of 1

Help in "routing" events between classes

Posted: Tue Oct 16, 2018 9:53 am
by Parduz
I have a Serial class that manages the transmission of a RS232, and a listening thread to read that port.

It is declared like this:

Code: Select all

wxDECLARE_EVENT(wxEVT_MYTHREAD, wxCommandEvent);
wxDECLARE_EVENT(wxEVT_SERIAL, wxCommandEvent);
class CommManager : public wxEvtHandler, public wxThreadHelper
{
	public:
		/** Default constructor */
		CommManager() {
			Bind(wxEVT_MYTHREAD, &CommManager::OnSerialRxCompleted, this);
		};
		/** Default destructor */
		virtual ~CommManager();
		
		// [...]

	private:
		void OnSerialRxCompleted(wxCommandEvent& SerialEvent);
		// [...]
}
When the listening thread detects a '\r' in the serial port it fires the wxEVT_MYTHREAD event (using wxPostEvent), handled by the OnSerialRxCompleted function, which stores the rx buffer and fires the wxEVT_SERIAL using QueueEvent.

Right now this event is handled on the main form. I'd like instead to have a class (let's call it "cDataHandler") that handle that wxEVT_SERIAL event, do protocol checks on the received message, prepare some data and finally fires an event for the main form (with a enumerated parameter), which will show the updated data (and only those thanks to that parameter) on the screen.
I'm confused on how should i declare that "cDataHandler" and how to "bind" the events.

Thnx

Re: Help in "routing" events between classes

Posted: Tue Oct 16, 2018 12:41 pm
by doublemax
I'm not quite sure what your problem is, based on the code and your description, it seems you already know everything you need to know :)

Derive "cDataHandler" from wxEvtHandler and pass its pointer into "CommManager", so that it knows where to send the event.

wxWidgets does not have any "broadcast" event system.

Re: Help in "routing" events between classes

Posted: Tue Oct 16, 2018 1:09 pm
by Parduz
doublemax wrote:wxWidgets does not have any "broadcast" event system.
This annihilate pretty all of my doubts. :D
doublemax wrote:I'm not quite sure what your problem is, based on the code and your description, it seems you already know everything you need to know :)
Basically, i thought that wxTheApp->GetTopWindow()->GetEventHandler() was some sort of "magic place" to queue event in and let the "appropriate handlers" take care of them.
doublemax wrote:Derive "cDataHandler" from wxEvtHandler and pass its pointer into "CommManager", so that it knows where to send the event
This means that CommManager needs to know who's the event listener (handler, in wx terms), while I was hoping to be able to make event "firers" ignorant about the event handlers.
- EDIT -

The common way to use events in the old Borland CBuilder were to "expose as property" the function pointer the event will call. So you can use a generic "Object" (say, my CommManager) like:

Code: Select all

	CommManagerInstance->OnSerialEvent = mySerialEventHandlerFunction;
The CommManager then just checked if the internal variable was not NULL to fire the event,
The only thing that the handler and the firer had both to know was the "type" of the event.

I know that that way was very "personal" of the BCB environment, but there's any way I could obtain something similar in wxWidgets?

Re: Help in "routing" events between classes

Posted: Tue Oct 16, 2018 3:49 pm
by doublemax
You could use wxTheApp as global event handler, you send events to it and your receiver class Bind()s to wxTheApp and grabs the events from there.

Or try to find a simple signal/slot library. It doesn't have to be a wxWidgets solution.