TaskDispatcher.h 1.74 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#ifndef COMMANDDISPATCHER_H
#define COMMANDDISPATCHER_H

#include <QFuture>
#include <QVariant>
#include <deque>

#include "Task.h"

namespace nemo_interface {

class TaskDispatcher : QObject {
public:
  TaskDispatcher();
  ~TaskDispatcher();
  //!
  //! \brief dispatch Dispatches a task.
  //! \param c The task to dispatch.
  //! \return Returns a future containing the QVariant which the task c
  //! returned.
  //!
  std::future<QVariant> dispatch(std::unique_ptr<Task> c);
  //!
  //! \brief dispatchNext Dispatches the task c as the next task.
  //! \param c The task to dispatch.
  //! \return Returns a future containing the QVariant which the task c
  //! returned.
  //!
  std::future<QVariant> dispatchNext(std::unique_ptr<Task> c);
  //!
  //! \brief clear Clears the task list.
  //!
  void clear();
  //!
  //! \brief start Starts task dispatching. This function can be called after
  //! dispatching has been stopped, e.g. by stop() or reset().
  //!
  void start();
  //!
  //! \brief stop Stops task dispatching.
  //!
  //! Stops taks dispatching. The dispatcher will wait for the current task to
  //! complete and than terminate the dispatcher thread. After stop() has been
  //! called isInterruptionRequested will return true. This can be used to
  //! permaturely terminate a task.
  //!
  void stop();
  //!
  //! \brief reset Clears the task list and stops dispatching.
  //!
  void reset();
  bool isInterruptionRequested();
  bool isRunning();

  std::size_t pendingTasks();
56
  bool idle();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

protected:
  void run();

private:
  QFuture<void> _future;

  std::deque<std::unique_ptr<Task>> _taskQueue;
  std::deque<std::promise<QVariant>> _promiseQueue;
  bool _running;
  std::mutex _mutex;
};
} // namespace nemo_interface

#endif // COMMANDDISPATCHER_H