avoid static cast in payload

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

avoid static cast in payload

Post by mael15 »

I have two parts of my app that I want to keep seperate. When something important happens in one part, I want to notify the other with a message that can have very different payloads. I would create specialized payloads that inherit from AdditionalInfoPayloadBase and static_cast them depending on TChangeType if needed in every specialized page derived from PageBase.
What I have successfully done in other circumstances is this, but it seems a bit outdated or even dangerous:

Code: Select all

void PageContainer::notifyOfChange(TChangeType whatChanged, AdditionalInfoPayloadBase *payload = nullptr){
	for(auto page : *getPages())
		page->checkForChangeConsequence(whatChanged, payload);
}

virtual void PageBase::checkForChangeConsequence(TChangeType whatChanged, AdditionalInfoPayloadBase *payload = nullptr) = 0;
Is there a better/safer way?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: avoid static cast in payload

Post by ONEEYEMAN »

Hi,
Could you give more details please?
Namely what type of change you are looking for?
What type of variable(s) will be changing?
Are you changing something inside the thread?

Thank you.
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: avoid static cast in payload

Post by mael15 »

What I have in mind is something like the observer pattern, only without registering observers somewhere.
I have different kinds of pages inheriting from PageBase in a vector, there will be additional kinds of pages in the future. I want to notify every page of a possibly relevant change in other parts of the app, adding optional addition info payload i.e. a pointer or a wxPoint. Right now I am thinking of inheriting these additional info entities from AdditionalInfoPayloadBase. When a page has to react to a certain TChangeType, it has to static_cast the AdditionalInfoPayloadBase into the actual payload, maybe AdditionalInfoPayloadBaseWithPointer and that is what bothers me. The cast has to be correct and is a possible problem in the future. Every page has to do the correct cast associated with a certain TChangeType.
I thought about using a template, but maybe there is a more elegant way without casting? or something more error safe?
This has nothing to do with threads, everything happens within the main thread.
thank you!
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: avoid static cast in payload

Post by alys666 »

use dynamic_cast<>. at least it will raise an exception if actual cast was wrong.
dynamic cast checks at runtime if pointer can be really cast to given type.
ubuntu 20.04, wxWidgets 3.2.1
Post Reply