Function pointer and functors problem
Hi
Sorry for the stupid heading of the thread, i could not think any better. I am having a doubt about function pointers and functors. I know that the signatures of a function pointer and a pointer to a class member function are different. However signature of a global function and signature of a static class function are compatible. Here is what i want to achieve: There are 2 unrelated classes say A and B ( they are unrelated and hence i don't want to make them part of any inheritance structure) I have written a timer class in which i can register multiple timers, it takes a timeout value and a function pointer that would be invoked when the timer times out i.e basically i want a callback mechanism on timeout. So internally the timer is using a queue to store all these function pointer and all these function pointers have signature void (*ptr)( ); So the queue would be like: Code:
typedef void (*ptr)( ); Let me make it clear( i really suck in communication, sorry for that) Code:
class A Code:
Timer timer; timer.registerTimer(A::timerExpired,6)// expires after 5 seconds timer.registerTimer(B::timerExpired,2) // expires after 2 seconds Problem is that these two functions are not static and if i make them static how would i change the state of the objects ( static functions don't have this pointer) Is it even possible i.e to store pointers to member functions (of different classes) in a queue and invoke them. It can be done if i make A and B derived from class C containing a virtual function timerExpired and then storing a functor instead of a function pointer but i don't want to do that as in my case A and B are not at all related. Can it be done somehow using wrapper static functions of classes A and B ?? Please provide some hint or guidance. Thanks a lot for your patience Regards lali |
Quote:
Code:
typedef void (*ptr)( void *arg ); |
Personally I would use an existing functor library like Loki, Boost or an implementation of C++0x. Using one of these it is possible to register any of the following function, this example uses Loki.
Code:
typedef Functor<void,NullType> ptr; |
Quote:
Code:
#include <iostream> ta0kira |
Whilst the code posted by ta0kira would work for this instance I would have to say it is not a nice implementation due to it not being generic enough. It is the fundamentals of how functors are done yet any change to the function signature requires a change of the base class or worst still if you have many signatures in a project then many different base "wrappers" hand coded, for example if you wanted a return type or parameters. Added to this that all functions have to be member functions and have the same name and it is reinventing a well defined wheel which good libraries with good support all ready implement.
|
Yeah, I didn't read the thread very well I guess. I actually wrote something several years ago that did what ptr in your post does and I sort of misremembered how I did it. Regardless, it's often better to use something already built and maintained.
ta0kira |
Thanks for your replies, i had installed boost recently on my system. For the time being i am implementing the idea provided by ntubski as i need this thing done by the end of day. However, soon i would go through boost::function and would use that in future.
@ta0kira But don't you think that such libraries would also be using void* pointer and casting in their implementation ? Quote:
One more thing i would like to mention. I have found the boost libraries to be a source of extremely good learning material, making the life of an application programmer quite easy.Boost libraries come with documentation on how to *use* the library but no information about how it is implemented(boost after all is not meant for tutoring or mentoring) However i would also like to learn how things are implemented( and designed ) in such expertly written libraries. I don't want to be a mere user of such libraries and so want to improve my skill by reading code written by experienced people and then compare with what i would have thought about implementing the same thing and thereby learn in the process. Although boost libraries come with source code, if i begin reading a library say asio, it is dependent on other boost libraries like boost::system ,boost::function(i am not sure about that) and so most of the time i get lost in details. There are no pointers as how to read code, i mean where to start first. I can comprehend most of the code that i read except these library dependencies. Any hints on how i can tackle that problem ? I know that learning programming is not like following a list of sequentional steps from beginning to end and is not a straight path. It takes time and patience. However if any of you has ever tried learning from code written by others, please shed some light on your experience on how to do it. |
Here's a timer class (Timer.hpp) that I tinkered with a couple of years ago. I never used it for anything; maybe it can provide you with some ideas. (Btw, sorry for the lack of documentation; hopefully the TestTimer.cpp test-program will serve as a guide).
Timer.hpp (provides for blocking or non-blocking timer, with option to repeat): PHP Code:
PHP Code:
PHP Code:
Code:
g++ TimerTest.cpp -o timer dw |
Quote:
ta0kira |
All times are GMT -5. The time now is 05:33 PM. |