Dynamic_Cast EventArgs to WindowEventArgs [Solved]

For help with anything that CEGUI doesn't offer straight out-of-the-box, e.g.:
- Implementation of new features, such as new Core classes, widgets, WindowRenderers, etc. ...
- Modification of any existing features for specific purposes
- Integration of CEGUI in new engines or frameworks and writing of new plugins (Renderer, Parser, ...) or modules

Moderators: CEGUI MVP, CEGUI Team

Scorch
Not too shy to talk
Not too shy to talk
Posts: 21
Joined: Mon Feb 13, 2006 07:05
Location: Las Vegas, NV

Dynamic_Cast EventArgs to WindowEventArgs [Solved]

Postby Scorch » Tue Feb 14, 2006 16:10

Thanks to Crazy Eddie for helping me with registering a handler for all windows.

Now that my handler is being called I have a new problem.
I am trying to find out which window is calling my handler by printing out its name.

To do this, I need to cast the EventArgs in the parameter list to a windowEventArgs so I have access to the window parameter (then I can call getName or what ever I need)


Code: Select all

bool OnClick(const CEGUI::EventArgs &args) 



Here is the problem: I want to do it safly, so I would like to use a dynamic cast. However the following code throws a exception:

Code: Select all

try{
  pArgs = (CEGUI::EventArgs*)(&args);
  windowArgs = dynamic_cast<CEGUI::WindowEventArgs*> (pArgs);
  }
  catch(...)
  {
    pArgs = NULL;
    windowArgs = NULL;
  }


If I forgo the safty of the dynamic cast, I can c style cast:

Code: Select all

CEGUI::WindowEventArgs * windowArgs = (CEGUI::WindowEventArgs*) &args;


without a problem.

Am I going to be stuck with a c style cast? or is there a better way? Any Idea what exceptions dynamic_cast would throw? I know its not throwing because of incorrect type, because the c-style cast gives me a valid object and I can get the name.


Scorch
Last edited by Scorch on Wed Feb 15, 2006 20:49, edited 1 time in total.

User avatar
lindquist
CEGUI Team (Retired)
Posts: 770
Joined: Mon Jan 24, 2005 21:20
Location: Copenhagen, Denmark

Postby lindquist » Tue Feb 14, 2006 16:52

I'm not sure what exception it would throw, but CEGUI does'nt use RTTI, so the correct way to do it is with a C style cast or static_cast.

Code: Select all

const WindowEventArgs& we = static_cast<const WindowEventArgs&>(e);


And btw, you're casting away the constness of the EventArgs object. Which does'nt exactly add to the safety.
Now I think of it, that could be the reason for the exception!

Scorch
Not too shy to talk
Not too shy to talk
Posts: 21
Joined: Mon Feb 13, 2006 07:05
Location: Las Vegas, NV

Postby Scorch » Tue Feb 14, 2006 17:39

lindquist wrote:I'm not sure what exception it would throw, but CEGUI does'nt use RTTI, so the correct way to do it is with a C style cast or static_cast.

Code: Select all

const WindowEventArgs& we = static_cast<const WindowEventArgs&>(e);


And btw, you're casting away the constness of the EventArgs object. Which does'nt exactly add to the safety.
Now I think of it, that could be the reason for the exception!


When I get home tonight, i'll try static cast.
What is RTTI?
As for the const, I've tried eliminating that two ways. Once by c style casting it away from const before passing it to dynamic cast, and another i've tried is const_cast<>() but they both still cause throws.

I've also attempted to keep all my pointers constant but I couldn't get dynamic_cast<>() to compile with a const pointer template type. I may have a sentax error left somewhere.

User avatar
jacmoe
Just can't stay away
Just can't stay away
Posts: 136
Joined: Sun Apr 03, 2005 14:18
Location: Holbaek, Denmark
Contact:

Postby jacmoe » Tue Feb 14, 2006 19:08

RTTI is "Run Time Type Information".

dynamic_cast will only work if the code is built with RTTI turned on.

dynamic_cast will return the casted type when succesful, and 0 when unsuccesful.

Scorch
Not too shy to talk
Not too shy to talk
Posts: 21
Joined: Mon Feb 13, 2006 07:05
Location: Las Vegas, NV

Postby Scorch » Tue Feb 14, 2006 22:01

jacmoe wrote:RTTI is "Run Time Type Information".

dynamic_cast will only work if the code is built with RTTI turned on.

dynamic_cast will return the casted type when succesful, and 0 when unsuccesful.


I had no IDEA you could turn that off! I wonder what the benifits of that is. (don't bother answering here, don't want to hijack my own post).

User avatar
Dalfy
CEGUI Team (Retired)
Posts: 130
Joined: Tue Oct 11, 2005 16:13
Location: Paris, FRANCE
Contact:

Postby Dalfy » Wed Feb 15, 2006 08:23

If you need to remove the const you must do the conversion in two step (the order is not important):
static_cast<WindowEventArgs&>(const_cast<CEGUI::EventArgs&>(arg)).

You can most of the time do a static_cast instead of a dynamic_cast.

Scorch
Not too shy to talk
Not too shy to talk
Posts: 21
Joined: Mon Feb 13, 2006 07:05
Location: Las Vegas, NV

Postby Scorch » Wed Feb 15, 2006 20:49

I tried static cast, it works.
Thanks.

Was thinking about the benifits of turning off RTTI, and I guess that would save you some foot print.

Marking this issue as solved..


Return to “Modifications / Integrations / Customisations”

Who is online

Users browsing this forum: No registered users and 7 guests