Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: std::map of pointers to member functions

Reply
Thread Tools

Re: std::map of pointers to member functions

 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2003
"Jonathan Mcdougall" <> wrote...
> To make a long story short, I need to associate events with pointers
> to functions. The first idea which came to my mind was
>
> enum Event
> {
> clicked,
> moved
> };
>
> typedef void (*Handler)(int);
>
>
> void my_handler(int)
> {
> std::cout << "in my handler";
> }
>
>
> int main()
> {
> std::map<Event, Handler> handlers;
>
> handlers.insert(std::make_pair(clicked, &my_handler));
>
> if ( event_triggered(clicked) )
> (handlers[clicked])( /*whatever*/ );
> }
>
>
> I hope this makes sense.
>
> Now, the thing is, I need to include member functions too, so I came
> up with that :
>
> template<class Object, class Function> class Handler
> {
> private:
> Object obj_;
> Function fun_;
>
> public:
> template<class Object, class Function>
> Handler(Object obj, Function fun)
> : obj_(obj),
> fun_(fun)
> {
> }
>
> void operator()(int whatever)
> {
> ((*obj_).*fun_)(whatever);
> }
> };
>
> But now I am stuck with the std::map, since it cannot accept templates
> (as I understand).
>
> Could anybody show me a way of acheiving the equivalent results?


Nothing can accept templates. Everything has to eventually boil down
to become a concrete class or function. Since you cannot store different
instantiations of your template class 'Handler' in the same map, you need
to play the polymorphic card:

#include <map>
using std::map;

enum Event { Clicked, Moved };

class BaseHandler {
public:
~BaseHandler() {}
virtual void operator()(int whatever) = 0;
};

template<class Object> class Handler
: public BaseHandler {
public:
typedef void (Object::*Function)(int);
private:
Object* obj_;
Function fun_;
public:
Handler(Object* obj, Function fun)
: obj_(obj), fun_(fun)
{
}

void operator()(int whatever)
{
(obj_->*fun_)(whatever);
}
};

template<class Object> Handler<Object>*
makeHandler(Object* obj,
typename Handler<Object>::Function fun) {
return new Handler<Object>(obj, fun);
}

class ClickHandler {
public:
void doit(int);
};

int main() {
map<Event, BaseHandler*> myhandlers;
ClickHandler cH;
myhandlers[Clicked] = makeHandler(&cH, &ClickHandler::doit);

// use it and then _delete_ every entry in the map
}


I haven't got this to execute, besides, there is no code to use the
map, but you should get the idea...

Victor


 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      07-24-2003
>> But now I am stuck with the std::map, since it cannot accept templates
>> (as I understand).
>>
>> Could anybody show me a way of acheiving the equivalent results?

>
>Nothing can accept templates. Everything has to eventually boil down
>to become a concrete class or function.


It makes sense, I still have some difficulties understanding, or
visualizing, templates.

>Since you cannot store different
>instantiations of your template class 'Handler' in the same map, you need
>to play the polymorphic card:
>
> #include <map>
> using std::map;
>
> enum Event { Clicked, Moved };
>
> class BaseHandler {
> public:
> ~BaseHandler() {}
> virtual void operator()(int whatever) = 0;
> };
>
> template<class Object> class Handler
> : public BaseHandler {
> public:
> typedef void (Object::*Function)(int);
> private:
> Object* obj_;
> Function fun_;
> public:
> Handler(Object* obj, Function fun)
> : obj_(obj), fun_(fun)
> {
> }
>
> void operator()(int whatever)
> {
> (obj_->*fun_)(whatever);
> }
> };
>
> template<class Object> Handler<Object>*
> makeHandler(Object* obj,
> typename Handler<Object>::Function fun) {
> return new Handler<Object>(obj, fun);
> }
>
> class ClickHandler {
> public:
> void doit(int);
> };
>
> int main() {
> map<Event, BaseHandler*> myhandlers;
> ClickHandler cH;
> myhandlers[Clicked] = makeHandler(&cH, &ClickHandler::doit);
>
> // use it and then _delete_ every entry in the map
> }
>


Wow.

It took me a while to integrate it (since I am not very good with
templates), but it works like a charm, as your solutions always do.

Thank you,

Jonathan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
static member functions vs. member function pointers paul C++ 8 04-30-2009 11:03 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Member function pointers to member functions with default arguments Hamish C++ 3 01-25-2008 06:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57