Character MainPlayer;
// These are defined on Character
//MainPlayer.Jump();
//MainPlayer.Run();
//MainPlayer.Stop();
Schedular MainSchedular;
MainSchedular.Add(MainPlayer.Jump,2.0f);
MainSchedular.Add(MainPlayer.Run,6.0f);
///inside main loop check for expiring one..
MainSchedular.Execute();
🔒 Locked
Schedular Using Templates,Virtual Function OR what??... avoiding Function Pointers
Advertisement
Original Post
Hi All, I have a schedular in one game, what we have is i send functions pointers to my schedular and then in the specified time my shedular from its maintained priority queue picks the one with expiring time and executes that function. Something like this... but ofcourse in this code you can't use it exactly like this since i can't use Member Functions as function pointers.... But still it will give you and idea.. How its working,is, that every where we have defined static functions and then wrapers on those static functions and so on, since you can't use a member function as a function pointer so we need a static wrapper for that... I read once somewhere... which might not be true."if you have to use Function Pointers, then it means you have a problem in your design" I have been thinking interms of virtual functions and polymorphism, but its not quite helping me in the way i want,, Like i cant schedule an Objects Method1 and Method2 in a kind of queue,, which will be executed in some time, or may be later i need other types of Objects too. With function pointers i dont need to worry about any lists or labels or tables, or enums etc, just put the function pointer and it will work, Can anyone help me in designing a schedular which does not use FUNCTION POINTERS and does not use any lookup tables or any exhastive switch statments to schedule some kinds of functions in it. for example in an animation system, i schedule RUN in 2 seconds and then in 6 seconds i Schedule STOP or JUMP Etc, something like that, Any help would be greately appriciated.
Quote:
Original post by Telastyn
boost::function is your friend. boost::bind will help adapt things into a common signature. But in the end, this sort of thing will be more painful than it should be in C++.
Well.. thanks .. but if its only about making the thing work .. then its already working for me .but with a little more headach then usual..
What i want to know. is to find a way of doing it.. i mean if Boost::Bind is my friend. then i would like to know how they coded Boost::Bind ... if that makes any sense??
Just a note.
The issue with function pointers is state, in order to access state you must either make it global or pass it via some opaque pointer (in C one typically uses a void pointer) which must be casted to the correct type.
Both these solutions can work, but are ugly.
The solutions proposed by Telastyn can pass stateful or stateless functions in a nice fashion.
The issue with function pointers is state, in order to access state you must either make it global or pass it via some opaque pointer (in C one typically uses a void pointer) which must be casted to the correct type.
Both these solutions can work, but are ugly.
The solutions proposed by Telastyn can pass stateful or stateless functions in a nice fashion.
Quote:
Original post by waZim
What i want to know. is to find a way of doing it.. i mean if Boost::Bind is my friend. then i would like to know how they coded Boost::Bind ... if that makes any sense??
You can always look at
Here is some further explanation of what i want to accomplish..
Now if any one can help me with such structure... or change this code. or point me in a direction of what to add what to delete...
class CPlayer // Abstract Class{public: CPlayer(){}; ~CPlayer(){}; virtual void run() = 0;};//////////////////////////////////////////////////////////////////////////class CPlayer1 : public CPlayer{public: CPlayer1(){} ~CPlayer1(){} void run(){} // Run for Player Type 1};//////////////////////////////////////////////////////////////////////////class CPlayer2 : public CPlayer{public: CPlayer2(){} ~CPlayer2(){} void run(){} // Run for Player Type 2};//////////////////////////////////////////////////////////////////////////class CSchedular{public: CSchedular(){} ~CSchedular(){} ////////////////////////////////////////////////////////////////////////// void scheduleEvent(CPlayer* a_Player) { // Here i will assume i am Scheduling a_Player's run only then its ok.. // What can i do if i also want to schedule His Jump Function.. // What can i do if i also want to Schedule Another Type of Objects' Another Function for example NPC::Jump } ////////////////////////////////////////////////////////////////////////// void tickSchedule() { // Execute the Schedular }};//////////////////////////////////////////////////////////////////////////Now if any one can help me with such structure... or change this code. or point me in a direction of what to add what to delete...
Quote:
Original post by SiS-Shadowman Quote:
Original post by waZim
What i want to know. is to find a way of doing it.. i mean if Boost::Bind is my friend. then i would like to know how they coded Boost::Bind ... if that makes any sense??
You can always look at. There you can find out how it's done. Or you can look at the documentation at boost.org...
I found this on Boost.org
class button{public: boost::function<void()> onClick;};class player{public: void play(); void stop();};button playButton, stopButton;player thePlayer;void connect(){ playButton.onClick = boost::bind(&player::play, &thePlayer); stopButton.onClick = boost::bind(&player::stop, &thePlayer);}This could be a solution. and i think this Boost::function is a static templated function.. but now i don't see it definition anywhere..
EDIT : Boost::function is acutally a templated class not function...
http://www.boost.org/doc/libs/1_35_0/doc/html/boost/function.html
[Edited by - waZim on April 9, 2009 10:26:41 AM]
boost::bind and boost::function are thousands of lines of code. They're header-only, so just get boost and use them (no compilation needed).
Boost::function is a template class that relies on type erasure to store a function pointer to a specific function, regardless of type. It uses some template metahackery to infer and store argument and return-type data, and allows you to invoke it with operator(). Using boost function will involve 1(?) virtual function call.
Boost::bind is a helper function that takes in a functor of some type (Either a pointer to a function, or a class instance that overloads operator()), and uses some more template metahackery in order to store arguments, and returns something that is convertible to a boost::function.
As far as I know, neither boost::function nor boost::bind use static function wrappers.
Boost::bind is a helper function that takes in a functor of some type (Either a pointer to a function, or a class instance that overloads operator()), and uses some more template metahackery in order to store arguments, and returns something that is convertible to a boost::function.
As far as I know, neither boost::function nor boost::bind use static function wrappers.
Writing a boost::function-like utility is trivial. What gets in the way is the fact it needs to be generic, requiring the usage of a lot of preprocessing magic.
Here is what it is, basically, for unary functions. As you can see, it's fairly trivial: it's just type erasure.
The only real problems there are are how to do forwarding properly.
Now, how does bind work?
bind actually does many things. If you're just interested in creating a function object from a pointer to member and an object, that's another trivial thing.
Here is what it is, basically, for unary functions. As you can see, it's fairly trivial: it's just type erasure.
The only real problems there are are how to do forwarding properly.
namespace function_detail{ template<typename R, typename T> struct base { virtual base* clone() = 0; virtual R operator()(T) = 0; }; template<typename R, typename T, typename F> struct derived : base<R, T> { derived(F f_) : f(f_) { } derived* clone() { return new derived(f); } R operator()(T t) { return f(t); } private: F f; };}template<typename R, typename T>struct function{ template<typename F> function(F f) : p(new function_detail::derived<R, T, F>(f)) { } function(const function& other) : p(other.p->clone()) { } function& operator=(function other) { swap(other); } void swap(function& other) { std::swap(p, other.p); } R operator(T t) { return (*p)(t); } ~function() { delete p; }private: function_detail::base<R, T>* p;};Now, how does bind work?
bind actually does many things. If you're just interested in creating a function object from a pointer to member and an object, that's another trivial thing.
Would a functor work? something along the lines of
class f{
virtual operator()();
}
class playerRun : public f{
playerRun(player);
operator()(){do run stuff here}
}
then (*f)() to invoke the correct operator(). Not much cleaner than function pointers but more logically consistent.
class f{
virtual operator()();
}
class playerRun : public f{
playerRun(player);
operator()(){do run stuff here}
}
then (*f)() to invoke the correct operator(). Not much cleaner than function pointers but more logically consistent.
Quote:
Original post by stonemetal
Would a functor work? something along the lines of
class f{
virtual operator()();
}
class playerRun : public f{
playerRun(player);
operator()(){do run stuff here}
}
then (*f)() to invoke the correct operator(). Not much cleaner than function pointers but more logically consistent.
Isn't this too ugly to derive different classes for each type of function that i want to be able to schedule? if thats what you mean?
class playerJump : public f{playerJump(player);operator()(){do Jump stuff here}}Is that how am i suppose to use Functors?
Quote:
Original post by loufoque
Writing a boost::function-like utility is trivial. What gets in the way is the fact it needs to be generic, requiring the usage of a lot of preprocessing magic.
Here is what it is, basically, for unary functions. As you can see, it's fairly trivial: it's just type erasure.
The only real problems there are are how to do forwarding properly.
Now, how does bind work?
Thanks alot for your reply ... looks really hacky use of Templates.. while i am not much comfortable with them :)
First: I am least interrested in what Bind is and what it does.. i want to understand the process of creating something like what i have already been asking for.... Making a Schedular where i can schedule any kind of Function and then should be able to call it....
Second: I have been trying to compile your code. it works.. apart from one "()" missing in struct function... But i still can't get my mind work around how can i use all this now? can you please give an example usage of your code? In Terms of saving this Function Data and calling it (only).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement