Consider the following code snippet( Python):
class Task:
def __init__(self,prev):
self.threads = []
assert isinstance(prev,Task)or isinstance(prev,Game)or prev is None
if isinstance(prev,Task):
self.prev = prev
self.game = prev.game
else: self.prev,self.game = None,prev
self.frame = 0
def idle_func(self): return self.idle_func
def __call__(self):
assert self.game is not None
for thread in self.threads[:]: thread()
self.frame+=1
return self if len(self.threads)> 0 else None
And then this also( Python):
class SDL_TEST(Task):
def __init__(self,game):
FrameTask.__init__(self,game)
self.threads = [self.sample_paint,Text(self,["This message was displayed","in two frames."])]
def sample_paint(self):
self.game.surf.fill((255,0,0))
This works well, and all. However, consider the same in C++:
class Task
{
protected:
typedef void (Task::*Mode)();
std::vector<Mode> threads;
public:
bool operator () ()
{
std::vector<Mode> cp_threads(threads); for( int i=0; i<cp_threads.size(); i++)
(this->*cp_threads[i])();
return threads.size() > 0;
}
};
I can't remember exactly what it was I typed up, but it was similar to this. I'm working on a game engine in my spare time at school, but my teacher demands that it be done in C++( my preference is Python, personally, but meh). Anyways, I've been trying to work this out, but I was unable to get it to work. Here's the idea:
A Task has multiple threads. If a thread terminates, it removes itself from the threads-list, which it has access to because a 'thread' is really just a member-function of a Task( which is how things should be, I believe. ) Would anybody be wiling to clue me in on what they think I should do?
--Clarification:
No, these threads are not literal threads. -- Merely functions that are called once per loop.
Also, in the second Python snippet, Text is another class that inherits from Task. Since __call__ is overloaded, and still takes 0 arguments, this is perfectly fine. Perhaps I should do something like:
class Callable
{
public:
virtual void operator () () =0;
};
class Task: public Callable
{
std::vector< Callable> threads;
};
The only problem I would have with this is that I would need to have threads have a reference or pointer to their respective tasks in order to have access to things, which I'm not too crazy about.
Perhaps even something like this:
class Callable{};typedef void (Callable::*Thread) ();
class Task: public Callable
{
private:
std::vector< Thread> threads;
public:
operator Thread () const{ return &Task::operator(); }
}
Basically, I'm asking what class model I should take to do the following:
Tasks have threads. Threads are member functions of a Task. A task can also be appended to a Tasks' threads list.
Further:
I have my own SmartArray class which is essentially a ( performance-wise) better version of std::vector<>. To use it, a class needs to inherit from the class SmartObject ( for a reference-count -- I also implemented a Reference class that is nested within SmartArray, which is template-based. If you're interested,
link here[
^], however it's a lengthy file and ultimately not the point of this post so I haven't pasted it into this post. ) How would I go about making a type-def'd type inherit the SmartObject type? Or is something like that even possible? Would it be better to do the following?
class CallerHolder: public SmartObject {
private:
const Thread function;
public:
CallerHolder( Thread func): function( func){}
CallerHolder( const CallerHolder& cp_ch): function( cp_ch.function){}
void operator()( Callable* caller){ (caller->*function)(); }
};