forked from qt-creator/qt-creator
ProjectExplorer: Prepare RunWorkers for more flexible setups
A RunWorker can in theory (and will in practice) support several run modes. Also, it has turned out to be beneficial to specify restrictions on a fine grained level, so re-use the idea from the RunConfigurationFactory etc. constraints setup here. Creation of RunWorkerFactories can be made protected at some time. Change-Id: I9e2a84abfd7377c5bf0bbe84e0c7940b1317dc10 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -685,8 +685,7 @@ FixedRunConfigurationFactory::availableCreators(Target *parent) const
|
|||||||
|
|
||||||
static QList<RunWorkerFactory *> g_runWorkerFactories;
|
static QList<RunWorkerFactory *> g_runWorkerFactories;
|
||||||
|
|
||||||
RunWorkerFactory::RunWorkerFactory(Core::Id mode, Constraint constr, const WorkerCreator &prod, int prio)
|
RunWorkerFactory::RunWorkerFactory()
|
||||||
: m_runMode(mode), m_constraint(constr), m_producer(prod), m_priority(prio)
|
|
||||||
{
|
{
|
||||||
g_runWorkerFactories.append(this);
|
g_runWorkerFactories.append(this);
|
||||||
}
|
}
|
||||||
@@ -698,11 +697,40 @@ RunWorkerFactory::~RunWorkerFactory()
|
|||||||
|
|
||||||
bool RunWorkerFactory::canRun(RunConfiguration *runConfiguration, Core::Id runMode) const
|
bool RunWorkerFactory::canRun(RunConfiguration *runConfiguration, Core::Id runMode) const
|
||||||
{
|
{
|
||||||
if (runMode != this->m_runMode)
|
if (!m_supportedRunModes.contains(runMode))
|
||||||
return false;
|
return false;
|
||||||
if (!m_constraint)
|
|
||||||
|
for (const Constraint &constraint : m_constraints) {
|
||||||
|
if (!constraint(runConfiguration))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
return m_constraint(runConfiguration);
|
}
|
||||||
|
|
||||||
|
void RunWorkerFactory::setPriority(int priority)
|
||||||
|
{
|
||||||
|
m_priority = priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunWorkerFactory::setProducer(const WorkerCreator &producer)
|
||||||
|
{
|
||||||
|
m_producer = producer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunWorkerFactory::addConstraint(const Constraint &constraint)
|
||||||
|
{
|
||||||
|
// Default constructed Constraints are not worth keeping.
|
||||||
|
// FIXME: Make it a QTC_ASSERT once there is no code path
|
||||||
|
// using this "feature" anymore.
|
||||||
|
if (!constraint)
|
||||||
|
return;
|
||||||
|
m_constraints.append(constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunWorkerFactory::addSupportedRunMode(Core::Id runMode)
|
||||||
|
{
|
||||||
|
m_supportedRunModes.append(runMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RunWorkerFactory::destroyRemainingRunWorkerFactories()
|
void RunWorkerFactory::destroyRemainingRunWorkerFactories()
|
||||||
|
|||||||
@@ -453,12 +453,29 @@ public:
|
|||||||
using WorkerCreator = std::function<RunWorker *(RunControl *)>;
|
using WorkerCreator = std::function<RunWorker *(RunControl *)>;
|
||||||
using Constraint = std::function<bool(RunConfiguration *)>;
|
using Constraint = std::function<bool(RunConfiguration *)>;
|
||||||
|
|
||||||
RunWorkerFactory(Core::Id mode, Constraint constr, const WorkerCreator &prod,
|
RunWorkerFactory();
|
||||||
int prio = 0);
|
|
||||||
|
|
||||||
virtual ~RunWorkerFactory();
|
virtual ~RunWorkerFactory();
|
||||||
|
|
||||||
bool canRun(RunConfiguration *runConfiguration, Core::Id m_runMode) const;
|
template <class Worker>
|
||||||
|
void registerRunWorker()
|
||||||
|
{
|
||||||
|
m_producer = [](RunControl *rc) { return new Worker(rc); };
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class RunConfig>
|
||||||
|
void setSupportedRunConfiguration()
|
||||||
|
{
|
||||||
|
m_constraints.append([](RunConfiguration *runConfig) {
|
||||||
|
return qobject_cast<RunConfig *>(runConfig) != nullptr;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool canRun(RunConfiguration *runConfiguration, Core::Id runMode) const;
|
||||||
|
|
||||||
|
void setPriority(int priority);
|
||||||
|
void setProducer(const WorkerCreator &producer);
|
||||||
|
void addConstraint(const Constraint &constraint);
|
||||||
|
void addSupportedRunMode(Core::Id runMode);
|
||||||
|
|
||||||
int priority() const { return m_priority; }
|
int priority() const { return m_priority; }
|
||||||
WorkerCreator producer() const { return m_producer; }
|
WorkerCreator producer() const { return m_producer; }
|
||||||
@@ -469,8 +486,8 @@ private:
|
|||||||
friend class ProjectExplorerPlugin;
|
friend class ProjectExplorerPlugin;
|
||||||
static void destroyRemainingRunWorkerFactories();
|
static void destroyRemainingRunWorkerFactories();
|
||||||
|
|
||||||
Core::Id m_runMode;
|
QList<Core::Id> m_supportedRunModes;
|
||||||
Constraint m_constraint;
|
QList<Constraint> m_constraints;
|
||||||
WorkerCreator m_producer;
|
WorkerCreator m_producer;
|
||||||
int m_priority = 0;
|
int m_priority = 0;
|
||||||
};
|
};
|
||||||
@@ -546,20 +563,28 @@ public:
|
|||||||
static void registerWorker(Core::Id runMode, const WorkerCreator &producer,
|
static void registerWorker(Core::Id runMode, const WorkerCreator &producer,
|
||||||
const Constraint &constraint = {})
|
const Constraint &constraint = {})
|
||||||
{
|
{
|
||||||
(void) new RunWorkerFactory(runMode, constraint, producer);
|
auto factory = new RunWorkerFactory;
|
||||||
|
factory->setProducer(producer);
|
||||||
|
factory->addSupportedRunMode(runMode);
|
||||||
|
factory->addConstraint(constraint);
|
||||||
}
|
}
|
||||||
template <class Worker>
|
template <class Worker>
|
||||||
static void registerWorker(Core::Id runMode, const Constraint &constraint, int priority = 0)
|
static void registerWorker(Core::Id runMode, const Constraint &constraint, int priority = 0)
|
||||||
{
|
{
|
||||||
auto producer = [](RunControl *rc) { return new Worker(rc); };
|
auto factory = new RunWorkerFactory;
|
||||||
(void) new RunWorkerFactory(runMode, constraint, producer, priority);
|
factory->registerRunWorker<Worker>();
|
||||||
|
factory->addSupportedRunMode(runMode);
|
||||||
|
factory->addConstraint(constraint);
|
||||||
|
factory->setPriority(priority);
|
||||||
}
|
}
|
||||||
template <class Config, class Worker>
|
template <class Config, class Worker>
|
||||||
static void registerWorker(Core::Id runMode, int priority = 0)
|
static void registerWorker(Core::Id runMode, int priority = 0)
|
||||||
{
|
{
|
||||||
auto constraint = [](RunConfiguration *runConfig) { return qobject_cast<Config *>(runConfig) != nullptr; };
|
auto factory = new RunWorkerFactory;
|
||||||
auto producer = [](RunControl *rc) { return new Worker(rc); };
|
factory->registerRunWorker<Worker>();
|
||||||
(void) new RunWorkerFactory(runMode, constraint, producer, priority);
|
factory->addSupportedRunMode(runMode);
|
||||||
|
factory->setSupportedRunConfiguration<Config>();
|
||||||
|
factory->setPriority(priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
static WorkerCreator producer(RunConfiguration *runConfiguration, Core::Id runMode);
|
static WorkerCreator producer(RunConfiguration *runConfiguration, Core::Id runMode);
|
||||||
|
|||||||
Reference in New Issue
Block a user