Skip to main content
gamedev.net
🔒 Locked

Generic Functors

Started by jollyjeffers Jan 30, 2004 at 7:53 PM 4 replies 450+ views
Original Post
jollyjeffers
jollyjeffers
hi all, I''m getting a real headache from this, and I''d sooooo appreciate a little friendly help! I have a general GUI/UI system in C++. It has the standard buttons/labels/text boxes/whatever.. I read a tutorial on Functors, and I made a previous post: here. That solved a few problems, but I''m still missing one key thing. Generics. Say, I want to allow a control to have (Java style) addHandler() type methods - yet still in some generic sense - that is, not be too specific as to what TYPE of event (mouse, click, option-change etc..) and not too specific WHO handle it (what type of window class). I used templates:
template < class _handler > class HANDLER_ClickEvent { 
BUT, that doesn''t work generically, I can''t just ask for a "HANDLER_ClickEvent" class as it requires the template parameter be defined, which i Don''t want to do (could be any number of different classes that handle events). any ideas? I really don''t get how I can do a generalised *event* from a given control to a given container... cheers, Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
jollyjeffers
jollyjeffers
forgot to mention...

quote:
I can''t just ask for a "HANDLER_ClickEvent" class


just to clarify.. to create a method:

void addClickHandler( HANDLER_ClickEvent *e ) { };

does not work, because the declaration for HANDLER_ClickEvent requires a template parameter... such as [code]HANDLER_ClickEvent< MyClass >


Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
SiCrane
SiCrane
In that last post you made, remember that base class most people use, that you decided not to? This is why they use that base class.
jollyjeffers
jollyjeffers
quote:
remember that base class most people use, that you decided not to? This is why they use that base class.


sorry, I don''t follow (sorry!)..

The fact is, there is no base class, cos if I define:

virtual void operator()() = 0;

in my base, then I can take a basic function with no parameters and no return... but thats no good if I want a function that takes an X/Y coordinate (for example)...

to solve this problem I could have 5-6 types of "base" class, but that seems to defeat the point of having a base class!

Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
GamerSg
GamerSg
Im not quite sure what your qsn is, but it''s also something im working on.

I think what you want to do is define a Handler which is either static or a pointer in a class which is arbitary/unknown. This means, you need not define the class type until it is used. Am i right?

Something like this

class MyGame
{
public:
..
..
..
Event* mouseOver;//you can do this without template arguments if you use a pointer to a base class
MouseEvent mouseClick;//this won''t work because you need to define template arguments, this is your problem i guess, because you want to be able to reuse your code in MyGame2, MyGame3.

};

You will definitely need a base class. You will need to use polymorphism as well. Here is how i do it.

class Game
{
public:
//all the stuff

//these functions will handle your buttons/etc..

//These functions assume that testing will be done elsewhere to determine that mouseOver/mouseClick/etc.. required to be called.So they take no parameters

void mouseOver();
void mouseClick();
void mouseOut();
void KeyPress();
//if you wanted 1 function to handle all

void handleMouse(int state);//where state defines click/mouseover/etc..


}

void Game::init()
{
//this line means that Button''s mouseOver code is defined in Game::mouseOver function.

Button->mouseOver = new MouseEvent<Game>(this,&Game::mouseOver);//not so sure of the syntax, i dont have my code now

}


void Game::testMouse(int x,int y)
{
if(x/y within Button)
{
Button->mouseOver->call();//or you could overload () operator for nicer looking syntax

}
}
}

hplus0603
hplus0603
Google for "signal/slot".
enum Bool { True, False, FileNotFound };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.