]> git.tdb.fi Git - builder.git/blobdiff - source/internaltask.h
Refactor InternalTask to take a functor
[builder.git] / source / internaltask.h
index 9e52247a258f3429095d73db17a04dd1535c560d..608ea9662609bb0e5912f65a247f193f3a292504 100644 (file)
@@ -1,34 +1,45 @@
 #ifndef INTERNALTASK_H_
 #define INTERNALTASK_H_
 
+#include <functional>
 #include <msp/core/thread.h>
 #include "task.h"
 
+/**
+Runs a worker thread.  Tools should derive a thread class from
+InternalTask::Worker.  The worker thread must set its status to either SUCCESS
+or ERROR before terminating.
+*/
 class InternalTask: public Task
 {
-public:
+private:
        class Worker: public Msp::Thread
        {
                friend class InternalTask;
 
-       protected:
-               Status status;
-
-               Worker();
+       private:
+               std::function<bool()> func;
+               volatile Status status = Task::RUNNING;
 
        public:
+               Worker(std::function<bool()> f): func(f) { }
+
                Status get_status() const { return status; }
+
+       private:
+               void main() override;
        };
 
-private:
-       Worker *worker;
+       Worker worker;
 
 public:
-       InternalTask(Worker *);
+       InternalTask(std::function<bool()> f): worker(f) { }
        ~InternalTask();
 
-       virtual void start();
-       virtual Status check();
+       std::string get_command() const override { return "<internal>"; }
+       void start() override;
+       Status check() override;
+       Status wait() override;
 };
 
 #endif