Logo Search packages:      
Sourcecode: webissues version File versions  Download package

batchjob.h

/**************************************************************************
* This file is part of the WebIssues program
* Copyright (C) 2006-2007 Micha? Męci?ski
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#ifndef BATCHJOB_H
#define BATCHJOB_H

#include <QList>
#include <QVariant>

namespace WebIssues
{

class Command;

/**
* A single job for a batch of commands.
*
* A job creates a single command for execution. A job is a method which creates the command
* and an array of arguments which can be read from the method.
*
* @see BatchJobQueue
*/
template<class BATCH>
00031 class BatchJob
{
public:
    /**
    * Default constructor.
    */
00037     BatchJob() :
        m_method( NULL )
    {
    }

    /**
    * Constructor using a method pointer.
    *
    * @param method Pointer to a job method.
    */
00047     BatchJob( Command* ( BATCH::*method )( const BatchJob<BATCH>& job ) ) :
        m_method( method )
    {
    }

    /**
    * Destructor.
    */
00055     ~BatchJob()
    {
    }

public:
    /**
    * Add an integer argument to the job.
    */
00063     void addArg( int number )
    {
        m_args.append( QVariant( number ) );
    }

    /**
    * Add a string argument to the job.
    */
00071     void addArg( const QString& string )
    {
        m_args.append( QVariant( string ) );
    }

    /**
    * Return all arguments of the job.
    */
00079     const QList<QVariant>& args() const { return m_args; }

    /**
    * Read an integer argument of the job.
    */
00084     int argInt( int index ) const
    {
        return m_args[ index ].toInt();
    }

    /**
    * Read a string argument of the job.
    */
00092     QString argString( int index ) const
    {
        return m_args[ index ].toString();
    }

    /**
    * Call the job method using the given object.
    *
    * @param batch The object whose method will be executed.
    *
    * @return The command created by the job method.
    */
00104     Command* call( BATCH* batch )
    {
        return ( batch->*m_method )( *this );
    }

private:
    Command* ( BATCH::*m_method )( const BatchJob<BATCH>& job );

    QList<QVariant> m_args;
};

/**
* A queue of jobs for a batch of commands.
*
* This template siplifies implementing a batch of commands by providing a queue
* of jobs. Each job in the queue creates a single command to execute by calling
* an appropriate method. Each job may store additional arguments.
*
* Example implementation of a batch:
*
* @code
* class MyBatch : public AbstractBatch
* {
* public:
*     void addJob1( int arg )
*     {
*         Job job( &MyBatch::job1 );
*         job.addArg( arg );
*         m_queue.addJob( job );
*     }
*
*     Command* fetchNext()
*     {
*         if ( m_queue.moreJobs() )
*             return m_queue.callJob( this );
*         return NULL;
*     }
*
* private:
*     typedef BatchJob<MyBatch> Job;
*     typedef BatchJobQueue<MyBatch> JobQueue;
*
*     Command* job1( const Job& job )
*     {
*         // create the command
*     }
*
*     JobQueue m_queue;
* };
* @endcode
*/
template<class BATCH>
00156 class BatchJobQueue
{
public:
    /**
    * Constructor.
    *
    * Create an empty queue.
    */
00164     BatchJobQueue() :
        m_next( 0 )
    {
    }

    /**
    * Destructor.
    */
00172     ~BatchJobQueue()
    {
    }

public:
    /**
    * Type of a single job in the queue.
    */
00180     typedef BatchJob<BATCH> Job;

public:
    /**
    * Add a job to the queue.
    */
00186     void addJob( const Job& job )
    {
        m_jobs.append( job );
    }

    /**
    * Return @c true if there are more jobs to execute.
    */
00194     bool moreJobs() const
    {
        return m_next < m_jobs.count();
    }

    /**
    * Call the current job and advance to the next one.
    *
    * @param batch The object whose method will be executed.
    *
    * @return The command created by the job method.
    */
00206     Command* callJob( BATCH* batch )
    {
        return m_jobs[ m_next++ ].call( batch );
    }

private:
    QList<Job> m_jobs;
    uint m_next;
};

}

#endif

Generated by  Doxygen 1.6.0   Back to index