//
For simplicity assume "Callback" is a c-style function
// Worker class uses the callback to do some work, when the callback
// returns false work is done and the workerclass reports itself as
// available at the managerclass
template <typename ManagerClass, typename Callback>
class WorkerClass
{
private:
SomeClass* manager;
Callback callback
public:
WorkerClass (ManagerClass* _mgr)
{
manager = _mgr;
}
void doWork (void)
{
// doWork is called by a thread
if (!callback(...))
manager->returnToPool (this);
}
}
// We can push work into the manager class. The manager will
// distribute it over the workers (or queue it if all busy)
template <typename Callback>
class ManagerClass
{
private:
// Is this def. correct?! Shouldn't it involve something like
// WorkerClass<This::type, callback>
std::vector<WorkerClass*> workers;
ManagerClass()
{
workers.resize (4); // hardcoded for the moment
for (i=0; i<4; i++)
workers = new WorkerClass<???, Callback> (this); // ???
}
void queueJob (Callback callback)
{
// push Job into queue
// while WorkerClasses available & work to be done,
// assign jobs to the workerclasses
}
void returnToPool (WorkerClass* _worker)
{
// if Jobs in queue, accept Job and continue work
// otherwise, register as 'available' worker
}
}
Template syntax (template as component of other template)
I'm trying to convert my existing (non-template) threaded scheduling
code into a template and I know what I'd like to do, however I'm
missing some vocabulary to explain it to the compiler :)
See the code below:
- Inside the template class, how do I get the "type of 'this' " ?
- How do I correctly define the "WorkerClass" as a member of Managerclass?
- In the Manager-class constructor, how do I initialize the workerclass?
Thanks!
It's ManagerClass<Callback>.
You may wish to do a typedef WorkerClass<ManagerClass<Callback>,Callback> WorkerType; at class scope and then use WorkerType from then on.
You may wish to do a typedef WorkerClass<ManagerClass<Callback>,Callback> WorkerType; at class scope and then use WorkerType from then on.
When dealing with templates, use typedefs wherever possible.
And then, when using, you have your own set of types already defined.
As a matter of fact, if you want semi-readable code, you should never define templated types without typedefs. Even vector, list, ... Typedefs are free, so make sure to use them whenever possible.
template <typename ManagerClass, typename Callback>class WorkerClass{public: typedef WorkerClass< ManagerClass, Callback > Type; typedef ManagerClass * ManagerPtr; typedef ManagerClass & ManagerRef; typedef ManagerClass ManagerType; typedef Callback CallbackType; private: ManagerPtr manager; CallbackType callback public: WorkerClass (ManagerPtr _mgr)
template <typename Callback>class CallbackManager{public: typedef ManagerClass< Callback > Type; typedef Worker< Type, Callback > WorkerType; typedef WorkerType * WorkerPtr; typedef std::vector< WorkerPtr > Workers; typedef Callback * CallbackPtr; typedef Callback & CallbackRef; typedef Callback CallbackType; private: Workers workers; CallbackManager() { workers.resize (4); // hardcoded for the moment for (i=0; i<4; i++) workers = new WorkerType(this); } void queueJob ( CallbackRef callback ); void returnToPool ( WorkerPtr worker);
And then, when using, you have your own set of types already defined.
typedef CallbackManager< MyCallback > MyCallbackManager;...MyCallbackManager manager;MyCallbackManager::WorkerPtr wp = ...manager.returnToPool( wp );
As a matter of fact, if you want semi-readable code, you should never define templated types without typedefs. Even vector, list, ... Typedefs are free, so make sure to use them whenever possible.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement