]> git.tdb.fi Git - builder.git/commitdiff
Refactor InternalTask to take a functor
authorMikko Rasa <tdb@tdb.fi>
Thu, 22 Dec 2022 14:01:43 +0000 (16:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 22 Dec 2022 14:41:02 +0000 (16:41 +0200)
This way the tools don't all have to implement the same worker thread
boilerplate.

16 files changed:
source/androidmanifestgenerator.cpp
source/androidmanifestgenerator.h
source/compilecommandsgenerator.cpp
source/compilecommandsgenerator.h
source/copy.cpp
source/copy.h
source/internaltask.cpp
source/internaltask.h
source/pkgconfiggenerator.cpp
source/pkgconfiggenerator.h
source/tar.cpp
source/tar.h
source/vcxprojectgenerator.cpp
source/vcxprojectgenerator.h
source/vssolutiongenerator.cpp
source/vssolutiongenerator.h

index 8268bb80984d0ff075f89d2a6539b86803357315..70b1e20754d06d2b72db97a070894b8a3c571c41 100644 (file)
@@ -3,6 +3,7 @@
 #include "androidmanifestfile.h"
 #include "androidmanifestgenerator.h"
 #include "component.h"
+#include "internaltask.h"
 #include "sharedlibrary.h"
 #include "sourcepackage.h"
 
@@ -17,12 +18,10 @@ Target *AndroidManifestGenerator::create_target(const vector<Target *> &, const
 Task *AndroidManifestGenerator::run(const Target &target) const
 {
        const AndroidManifestFile &manifest = dynamic_cast<const AndroidManifestFile &>(target);
-       Worker *worker = new Worker(manifest);
-       return new InternalTask(worker);
+       return new InternalTask([&manifest]{ return _run(manifest); });
 }
 
-
-void AndroidManifestGenerator::Worker::main()
+bool AndroidManifestGenerator::_run(const AndroidManifestFile &manifest)
 {
        const Component &comp = *manifest.get_component();
        const SourcePackage &pkg = comp.get_package();
@@ -56,5 +55,5 @@ void AndroidManifestGenerator::Worker::main()
                IO::print(out, "\t<uses-permission android:name=\"%s\" />\n", p);
        out.write("</manifest>\n");
 
-       status = Task::SUCCESS;
+       return true;
 }
index 630eb7ce17624327a9a3c712f1873bfdc7bc5dfb..6013c5f51b8a1f2cebc2b8eaab6d046cc9cfbd58 100644 (file)
@@ -1,31 +1,20 @@
 #ifndef ANDROIDMANIFESTGENERATOR_H_
 #define ANDROIDMANIFESTGENERATOR_H_
 
-#include "internaltask.h"
 #include "tool.h"
 
 class AndroidManifestFile;
 
 class AndroidManifestGenerator: public Tool
 {
-private:
-       class Worker: public InternalTask::Worker
-       {
-       private:
-               const AndroidManifestFile &manifest;
-
-       public:
-               Worker(const AndroidManifestFile &m): manifest(m) { }
-
-       private:
-               void main() override;
-       };
-
 public:
        AndroidManifestGenerator(Builder &b): Tool(b, "AMG") { }
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Task *run(const Target &) const override;
+
+private:
+       static bool _run(const AndroidManifestFile &);
 };
 
 #endif
index 8e3947d7816a119916097b7bd7079b819759dd96..855a80a3307db63e3e4f24222e8c092c1ba33f00 100644 (file)
@@ -4,6 +4,7 @@
 #include "builder.h"
 #include "compilecommandsgenerator.h"
 #include "compilecommandsjson.h"
+#include "internaltask.h"
 #include "objectfile.h"
 #include "sourcefile.h"
 
@@ -18,18 +19,16 @@ Target *CompileCommandsGenerator::create_target(const vector<Target *> &, const
 Task *CompileCommandsGenerator::run(const Target &target) const
 {
        const CompileCommandsJson &cmds = dynamic_cast<const CompileCommandsJson &>(target);
-       Worker *worker = new Worker(cmds);
-       return new InternalTask(worker);
+       return new InternalTask([&cmds]{ return _run(cmds); });
 }
 
-
-void CompileCommandsGenerator::Worker::main()
+bool CompileCommandsGenerator::_run(const CompileCommandsJson &cmds)
 {
-       Builder &builder = target.get_package()->get_builder();
-       const SourcePackage &spkg = *target.get_package();
+       Builder &builder = cmds.get_package()->get_builder();
+       const SourcePackage &spkg = *cmds.get_package();
        string work_dir = c_escape(spkg.get_source_directory().str());
 
-       IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
+       IO::BufferedFile out(cmds.get_path().str(), IO::M_WRITE);
        IO::print(out, "[");
 
        bool first = true;
@@ -52,5 +51,5 @@ void CompileCommandsGenerator::Worker::main()
 
        IO::print(out, "\n]\n");
 
-       status = Task::SUCCESS;
+       return true;
 }
index a655d944161366a92eec881e015c7e28a94ea2fc..e3116f9c839312e5930c56d0c5342c9ea2805a03 100644 (file)
@@ -1,31 +1,20 @@
 #ifndef COMPILECOMMANDSGENERATOR_H_
 #define COMPILECOMMANDSGENERATOR_H_
 
-#include "internaltask.h"
 #include "tool.h"
 
 class CompileCommandsJson;
 
 class CompileCommandsGenerator: public Tool
 {
-private:
-       class Worker: public InternalTask::Worker
-       {
-       private:
-               const CompileCommandsJson &target;
-
-       public:
-               Worker(const CompileCommandsJson &t): target(t) { }
-
-       private:
-               void main() override;
-       };
-
 public:
        CompileCommandsGenerator(Builder &b): Tool(b, "CCJG") { }
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Task *run(const Target &) const override;
+
+private:
+       static bool _run(const CompileCommandsJson &);
 };
 
 #endif
index 45c5a81fa9ba60533a0471b105a2a26c4d25d2c0..31e62100d9751d991a0bf59529dd7699973e8ac5 100644 (file)
@@ -10,6 +10,7 @@
 #include "builder.h"
 #include "copy.h"
 #include "installedfile.h"
+#include "internaltask.h"
 
 using namespace std;
 using namespace Msp;
@@ -25,19 +26,17 @@ Target *Copy::create_target(const vector<Target *> &sources, const string &arg)
 Task *Copy::run(const Target &target) const
 {
        const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
-       Worker *worker = new Worker(install);
-       InternalTask *task = new InternalTask(worker);
+       InternalTask *task = new InternalTask([&install]{ return _run(install); });
        task->add_file(install.get_path());
        task->set_unlink();
        return task;
 }
 
-
-void Copy::Worker::main()
+bool Copy::_run(const InstalledFile &install)
 {
-       const FileTarget &source = target.get_source();
+       const FileTarget &source = install.get_source();
        const FS::Path &src_path = source.get_path();
-       const FS::Path &dst_path = target.get_path();
+       const FS::Path &dst_path = install.get_path();
 
        try
        {
@@ -55,8 +54,7 @@ void Copy::Worker::main()
        catch(const exception &e)
        {
                IO::print(IO::cerr, "%s\n", e.what());
-               status = Task::ERROR;
-               return;
+               return false;
        }
 
 #ifndef _WIN32
@@ -65,7 +63,7 @@ void Copy::Worker::main()
        if(stat(src_path.str().c_str(), &st)==0)
                chmod(dst_path.str().c_str(), st.st_mode&0777);
 
-       const FS::Path &link = target.get_symlink();
+       const FS::Path &link = install.get_symlink();
        if(!link.empty())
        {
                FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
@@ -75,5 +73,5 @@ void Copy::Worker::main()
        }
 #endif
 
-       status = Task::SUCCESS;
+       return true;
 }
index a5044d8ba50a58bfcf5c56866ab59c962ac556fd..c0a433996a9c0a14625ebf918c9c6092eac6d9e0 100644 (file)
@@ -1,9 +1,6 @@
 #ifndef COPY_H_
 #define COPY_H_
 
-#include <msp/core/thread.h>
-#include <msp/fs/path.h>
-#include "internaltask.h"
 #include "tool.h"
 
 class InstalledFile;
@@ -13,26 +10,14 @@ Copies a file to another place.  Used by the InstalledFile target.
 */
 class Copy: public Tool
 {
-private:
-       /**
-       A worker thread that actually does the data transfer.
-       */
-       class Worker: public InternalTask::Worker
-       {
-       private:
-               const InstalledFile &target;
-
-       public:
-               Worker(const InstalledFile &t): target(t) { }
-       private:
-               void main() override;
-       };
-
 public:
        Copy(Builder &b): Tool(b, "CP") { }
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Task *run(const Target &) const override;
+
+private:
+       static bool _run(const InstalledFile &);
 };
 
 #endif
index 5810be12670b7cecae652ae375666926fcd45e31..fc929ce02b2f843c2bd6de87331bbe081f11017c 100644 (file)
@@ -2,19 +2,18 @@
 
 InternalTask::~InternalTask()
 {
-       worker->join();
-       delete worker;
+       worker.join();
 }
 
 void InternalTask::start()
 {
        prepare();
-       worker->launch();
+       worker.launch();
 }
 
 Task::Status InternalTask::check()
 {
-       Status result = worker->get_status();
+       Status result = worker.get_status();
        if(result!=RUNNING)
                signal_finished.emit(result==SUCCESS);
        return result;
@@ -26,3 +25,12 @@ Task::Status InternalTask::wait()
        while((result = check())==RUNNING) ;
        return result;
 }
+
+
+void InternalTask::Worker::main()
+{
+       if(func())
+               status = Task::SUCCESS;
+       else
+               status = Task::ERROR;
+}
index 59545192a5bc82b130820d1e5b9bf05f0e94edc7..608ea9662609bb0e5912f65a247f193f3a292504 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef INTERNALTASK_H_
 #define INTERNALTASK_H_
 
+#include <functional>
 #include <msp/core/thread.h>
 #include "task.h"
 
@@ -11,25 +12,28 @@ or ERROR before terminating.
 */
 class InternalTask: public Task
 {
-public:
+private:
        class Worker: public Msp::Thread
        {
                friend class InternalTask;
 
-       protected:
+       private:
+               std::function<bool()> func;
                volatile Status status = Task::RUNNING;
 
-               Worker() = default;
-
        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 *w): worker(w) { }
+       InternalTask(std::function<bool()> f): worker(f) { }
        ~InternalTask();
 
        std::string get_command() const override { return "<internal>"; }
index 02f142331a124c8d4db38e8df0b630a28afd8ba7..a6178a7328c5e66fa5ad22df0bc0686013e0367b 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/io/file.h>
 #include <msp/io/print.h>
 #include "builder.h"
+#include "internaltask.h"
 #include "pkgconfigfile.h"
 #include "pkgconfiggenerator.h"
 
@@ -16,17 +17,15 @@ Target *PkgConfigGenerator::create_target(const vector<Target *> &, const string
 Task *PkgConfigGenerator::run(const Target &target) const
 {
        const PkgConfigFile &pkgc = dynamic_cast<const PkgConfigFile &>(target);
-       Worker *worker = new Worker(pkgc);
-       return new InternalTask(worker);
+       return new InternalTask([&pkgc]{ return _run(pkgc); });
 }
 
-
-void PkgConfigGenerator::Worker::main()
+bool PkgConfigGenerator::_run(const PkgConfigFile &pkgc)
 {
-       Builder &builder = target.get_package()->get_builder();
-       const SourcePackage &spkg = *target.get_package();
+       Builder &builder = pkgc.get_package()->get_builder();
+       const SourcePackage &spkg = *pkgc.get_package();
 
-       IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
+       IO::BufferedFile out(pkgc.get_path().str(), IO::M_WRITE);
        IO::print(out, "prefix=%s\n", builder.get_prefix().str());
        IO::print(out, "source=%s\n\n", spkg.get_source_directory());
 
@@ -60,10 +59,10 @@ void PkgConfigGenerator::Worker::main()
                        IO::print(out, " -D%s=%s", kvp.first, kvp.second);
        out.put('\n');
 
-       status = Task::SUCCESS;
+       return true;
 }
 
-string PkgConfigGenerator::Worker::prefixify(const FS::Path &path, const FS::Path &prefix)
+string PkgConfigGenerator::prefixify(const FS::Path &path, const FS::Path &prefix)
 {
        if(FS::descendant_depth(path, prefix)>=0)
        {
index 727b3e29536e0e24c14cb42c054d3d8179e05ca4..0f0886b851aa2583176d1499dcb0100d9a058168 100644 (file)
@@ -1,33 +1,21 @@
 #ifndef PKGCONFIGGENERATOR_H_
 #define PKGCONFIGGENERATOR_H_
 
-#include "internaltask.h"
 #include "tool.h"
 
 class PkgConfigFile;
 
 class PkgConfigGenerator: public Tool
 {
-private:
-       class Worker: public InternalTask::Worker
-       {
-       private:
-               const PkgConfigFile &target;
-
-       public:
-               Worker(const PkgConfigFile &t): target(t) { }
-
-       private:
-               void main() override;
-
-               std::string prefixify(const Msp::FS::Path &, const Msp::FS::Path &);
-       };
-
 public:
        PkgConfigGenerator(Builder &b): Tool(b, "PCG") { }
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Task *run(const Target &) const override;
+
+private:
+       static bool _run(const PkgConfigFile &);
+       static std::string prefixify(const Msp::FS::Path &, const Msp::FS::Path &);
 };
 
 #endif
index 8de76fe34feca85787d3d4f26f802ecea68332d9..446aa922a0c6baba68209219b800f57fc3dd0f61 100644 (file)
@@ -4,6 +4,7 @@
 #include <msp/io/file.h>
 #include <msp/io/print.h>
 #include "builder.h"
+#include "internaltask.h"
 #include "sourcepackage.h"
 #include "tar.h"
 #include "tarball.h"
@@ -34,16 +35,10 @@ Target *Tar::create_target(const vector<Target *> &sources, const string &arg)
 Task *Tar::run(const Target &target) const
 {
        const TarBall &tarball = dynamic_cast<const TarBall &>(target);
-       Worker *worker = new Worker(tarball);
-       return new InternalTask(worker);
+       return new InternalTask([&tarball]{ return _run(tarball); });
 }
 
-
-Tar::Worker::Worker(const TarBall &tb):
-       tarball(tb)
-{ }
-
-void Tar::Worker::main()
+bool Tar::_run(const TarBall &tarball)
 {
        const FS::Path &pkg_src = tarball.get_package()->get_source_directory();
        FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
@@ -62,8 +57,7 @@ void Tar::Worker::main()
                if(rel_path.size()>99)
                {
                        IO::print("Can't store %s in tar archive - too long name\n", rel_path);
-                       status = Task::ERROR;
-                       return;
+                       return false;
                }
 
                memcpy(buf, rel_path.data(), rel_path.size());
@@ -93,10 +87,10 @@ void Tar::Worker::main()
                }
        }
 
-       status = Task::SUCCESS;
+       return true;
 }
 
-void Tar::Worker::store_number(char *buf, unsigned value, unsigned length)
+void Tar::store_number(char *buf, unsigned value, unsigned length)
 {
        for(unsigned i=length; i--;)
        {
index d0240d93bf315c71b0b1536dd38e07ea16352d00..fa4a4d6070100098f3da70520caa7efa696cac4f 100644 (file)
@@ -1,33 +1,21 @@
 #ifndef TAR_H_
 #define TAR_H_
 
-#include <msp/core/thread.h>
-#include "internaltask.h"
 #include "tool.h"
 
 class TarBall;
 
 class Tar: public Tool
 {
-private:
-       class Worker: public InternalTask::Worker
-       {
-       private:
-               const TarBall &tarball;
-
-       public:
-               Worker(const TarBall &);
-
-       private:
-               void main() override;
-               void store_number(char *, unsigned, unsigned);
-       };
-
 public:
        Tar(Builder &);
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Task *run(const Target &) const override;
+
+private:
+       static bool _run(const TarBall &);
+       static void store_number(char *, unsigned, unsigned);
 };
 
 #endif
index 1528802eddc45e811cce4bc4fb8918644b24bc93..bafa8d94f42ee629d84454171be34514e4b603e5 100644 (file)
@@ -5,6 +5,7 @@
 #include "builder.h"
 #include "csourcefile.h"
 #include "executable.h"
+#include "internaltask.h"
 #include "sourcepackage.h"
 #include "vcxprojectfile.h"
 #include "vcxprojectgenerator.h"
@@ -20,17 +21,15 @@ Target *VcxProjectGenerator::create_target(const vector<Target *> &, const strin
 Task *VcxProjectGenerator::run(const Target &target) const
 {
        const VcxProjectFile &project = dynamic_cast<const VcxProjectFile &>(target);
-       Worker *worker = new Worker(project);
-       return new InternalTask(worker);
+       return new InternalTask([&project]{ return _run(project); });
 }
 
-
-void VcxProjectGenerator::Worker::main()
+bool VcxProjectGenerator::_run(const VcxProjectFile &project)
 {
-       const SourcePackage &spkg = *target.get_package();
+       const SourcePackage &spkg = *project.get_package();
        Builder &builder = spkg.get_builder();
 
-       IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
+       IO::BufferedFile out(project.get_path().str(), IO::M_WRITE);
        IO::print(out, "<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
 
        IO::print(out, "\t<ItemGroup Label=\"ProjectConfigurations\">\n");
@@ -49,7 +48,7 @@ void VcxProjectGenerator::Worker::main()
        IO::print(out, "\t<PropertyGroup Label=\"Globals\">\n");
        IO::print(out, "\t\t<VCProjectVersion>15.0</VCProjectVersion>\n");
        IO::print(out, "\t\t<Keyword>MakeFileProj</Keyword>\n");
-       IO::print(out, "\t\t<ProjectGuid>{%s}</ProjectGuid>\n", target.get_guid());
+       IO::print(out, "\t\t<ProjectGuid>{%s}</ProjectGuid>\n", project.get_guid());
        IO::print(out, "\t</PropertyGroup>\n");
 
        IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
@@ -134,5 +133,5 @@ void VcxProjectGenerator::Worker::main()
        IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
        IO::print(out, "</Project>\n");
 
-       status = Task::SUCCESS;
+       return true;
 }
index bf06f400b4fa4308de3cfd4bf74b9ff0efbcf48a..05f740183d26abf9bd719e7701f4576cfe810c9a 100644 (file)
@@ -1,31 +1,20 @@
 #ifndef VCXPROJECTGENERATOR_H_
 #define VCXPROJECTGENERATOR_H_
 
-#include "internaltask.h"
 #include "tool.h"
 
 class VcxProjectFile;
 
 class VcxProjectGenerator: public Tool
 {
-private:
-       class Worker: public InternalTask::Worker
-       {
-       private:
-               const VcxProjectFile &target;
-
-       public:
-               Worker(const VcxProjectFile &t): target(t) { }
-
-       private:
-               void main() override;
-       };
-
 public:
        VcxProjectGenerator(Builder &b): Tool(b, "VCXG") { }
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Task *run(const Target &) const override;
+
+private:
+       static bool _run(const VcxProjectFile &);
 };
 
 #endif
index 9f8a7ab9f936ed52aec9a5cb61166add543d0e4b..5fd8e4dff2a10d0371227f9f76c136ef0d04ba4b 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/io/file.h>
 #include <msp/io/print.h>
 #include "builder.h"
+#include "internaltask.h"
 #include "sourcepackage.h"
 #include "vcxprojectfile.h"
 #include "vssolutionfile.h"
@@ -18,22 +19,20 @@ Target *VsSolutionGenerator::create_target(const vector<Target *> &, const strin
 Task *VsSolutionGenerator::run(const Target &target) const
 {
        const VsSolutionFile &solution = dynamic_cast<const VsSolutionFile &>(target);
-       Worker *worker = new Worker(solution);
-       return new InternalTask(worker);
+       return new InternalTask([&solution]{ return _run(solution); });
 }
 
-
-void VsSolutionGenerator::Worker::main()
+bool VsSolutionGenerator::_run(const VsSolutionFile &solution)
 {
-       const SourcePackage &spkg = *target.get_package();
+       const SourcePackage &spkg = *solution.get_package();
        Builder &builder = spkg.get_builder();
 
-       IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
+       IO::BufferedFile out(solution.get_path().str(), IO::M_WRITE);
        IO::print(out, "Microsoft Visual Studio Solution File, Format Version 12.00\n");
        IO::print(out, "MinimumVisualStudioVersion = 10.0.40219.1\n");
 
        vector<const VcxProjectFile *> projects;
-       for(const Target *t: target.get_dependencies())
+       for(const Target *t: solution.get_dependencies())
                if(const VcxProjectFile *project = dynamic_cast<const VcxProjectFile *>(t))
                        projects.push_back(project);
 
@@ -66,5 +65,5 @@ void VsSolutionGenerator::Worker::main()
        IO::print(out, "\tEndGlobalSection\n");
        IO::print(out, "EndGlobal\n");
 
-       status = Task::SUCCESS;
+       return true;
 }
index 68d581f501d171a63cf9d9a2451d67df6dcd2c68..b7eaf31c056256ca13993d1daf3c58d0c3431400 100644 (file)
@@ -1,31 +1,20 @@
 #ifndef VSSOLUTIONGENERATOR_H_
 #define VSSOLUTIONGENERATOR_H_
 
-#include "internaltask.h"
 #include "tool.h"
 
 class VsSolutionFile;
 
 class VsSolutionGenerator: public Tool
 {
-private:
-       class Worker: public InternalTask::Worker
-       {
-       private:
-               const VsSolutionFile &target;
-
-       public:
-               Worker(const VsSolutionFile &t): target(t) { }
-
-       private:
-               void main() override;
-       };
-
 public:
        VsSolutionGenerator(Builder &b): Tool(b, "VSSG") { }
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Task *run(const Target &) const override;
+
+private:
+       static bool _run(const VsSolutionFile &);
 };
 
 #endif