/* * Copyright (C) 2003-2004 * Slawomir Lisznianski * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Slawomir Lisznianski makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * */ #ifndef RHA_SIMPLESCHEDULER_HPP #define RHA_SIMPLESCHEDULER_HPP // For noncopyable semantics. #include namespace Rha { /** Scheduler implementation class. Use Scheduler class to create objects that will serve as task schedulers. Scheduler uses two other sub-components, Queue and Executor, to carry out queuing and execution of scheduled tasks. \attention Copying instances of Scheduler class is not supported. */ template class SimpleScheduler : private boost::noncopyable { public: typedef SimpleScheduler OfType; typedef typename E::Task Task; typedef typename E::Queue Queue; typedef E Executor; /// Constructs SimpleScheduler with default implementation of Queue and Executor. SimpleScheduler(); /** Constructs SimpleScheduler using passed Queue and default Executor. \param queue a queue object to use by the SimpleScheduler when queuing new tasks. */ SimpleScheduler(const Queue& queue); /** Constructs SimpleScheduler using passed Executor and default Queue. \param exec an executor object to use for managing threads and assignment of tasks to threads. */ SimpleScheduler(const Executor& exec); /** Constructs SimpleScheduler using passed Queue and Executor. \param queue a queue object to use by the SimpleScheduler when queuing new tasks. \param exec an executor object to use for managing threads and assignment of tasks to threads. */ SimpleScheduler(const Queue& queue, const Executor& exec); /** Destructs instance of SimpleScheduler object. Shuts down the Queue and Executor. When it's finished, all tasks are removed from the queue and no threads are running. \attention Declared as non-virtual to discourage inheritance. */ ~SimpleScheduler() throw (); // Do not inherit. /** Schedules a task for execution. \param task task to be scheduled and executed asynchronously */ inline SimpleScheduler& push(const Task& task); /** Schedules a task for execution (synonym for push). \param task task to be scheduled and executed asynchronously */ inline SimpleScheduler& operator<<(const Task& task); //inline SimpleScheduler& //operator>>(const Task& task); private: // Initializes queue and executor. // Guaranteed to be invoked prior to // begin scheduling. // void setUp__(); Queue M_queue; Executor M_executor; }; } using namespace Rha; template SimpleScheduler::SimpleScheduler() { setUp__(); } template SimpleScheduler::SimpleScheduler(const Queue& queue) : M_queue(queue) { setUp__(); } template SimpleScheduler::SimpleScheduler(const Executor& exec) : M_executor(exec) { setUp__(); } template SimpleScheduler::SimpleScheduler(const Queue& queue, const Executor& exec) : M_queue(queue), M_executor(exec) { setUp__(); } template SimpleScheduler::~SimpleScheduler() throw () {} template SimpleScheduler& SimpleScheduler::push(const Task& task) { M_executor(task); return (*this); } template SimpleScheduler& SimpleScheduler::operator<<(const Task& task) { M_executor(task); return (*this); } // I need this operator to receve task /* template SimpleScheduler& SimpleScheduler::operator>>(const Task& task) { M_executor(task); return (*this); } */ template void SimpleScheduler::setUp__() { M_queue.setUp(); M_executor.setUp(&M_queue); } #endif // RHA_SIMPLESCHEDULER_HPP