Jump to content
  • Advertisement
Sign in to follow this  
Kitt3n

Template syntax (template as component of other template)

This topic is 4163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

//
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
    }
}


Thanks!

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
When dealing with templates, use typedefs wherever 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!