FuzzingOperations.cpp
74 lines
| 2.4 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r1202 | #include "FuzzingOperations.h" | ||
Alexandre Leroux
|
r1208 | #include "FuzzingUtils.h" | ||
#include <Data/IDataProvider.h> | ||||
Alexandre Leroux
|
r1202 | |||
#include <Variable/Variable.h> | ||||
#include <Variable/VariableController.h> | ||||
Alexandre Leroux
|
r1208 | #include <QUuid> | ||
Alexandre Leroux
|
r1202 | Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations") | ||
namespace { | ||||
struct CreateOperation : public IFuzzingOperation { | ||||
Alexandre Leroux
|
r1211 | bool canExecute(std::shared_ptr<Variable> variable) const override | ||
{ | ||||
Alexandre Leroux
|
r1208 | // A variable can be created only if it doesn't exist yet | ||
return variable == nullptr; | ||||
Alexandre Leroux
|
r1202 | } | ||
Alexandre Leroux
|
r1211 | void execute(std::shared_ptr<Variable> &variable, VariableController &variableController, | ||
const Properties &properties) const override | ||||
{ | ||||
// Retrieves metadata pool from properties, and choose one of the metadata entries to | ||||
// associate it with the variable | ||||
Alexandre Leroux
|
r1208 | auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value<MetadataPool>(); | ||
auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool); | ||||
// Retrieves provider | ||||
Alexandre Leroux
|
r1211 | auto variableProvider | ||
= properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >(); | ||||
Alexandre Leroux
|
r1208 | |||
auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString()); | ||||
r1254 | qCInfo(LOG_FuzzingOperations()) << "Creating variable" << variableName | |||
<< "(metadata:" << variableMetadata << ")"; | ||||
Alexandre Leroux
|
r1208 | |||
Alexandre Leroux
|
r1211 | auto newVariable | ||
= variableController.createVariable(variableName, variableMetadata, variableProvider); | ||||
Alexandre Leroux
|
r1208 | std::swap(variable, newVariable); | ||
Alexandre Leroux
|
r1202 | } | ||
}; | ||||
struct UnknownOperation : public IFuzzingOperation { | ||||
Alexandre Leroux
|
r1211 | bool canExecute(std::shared_ptr<Variable> variable) const override | ||
{ | ||||
Alexandre Leroux
|
r1202 | Q_UNUSED(variable); | ||
return false; | ||||
} | ||||
Alexandre Leroux
|
r1211 | void execute(std::shared_ptr<Variable> &variable, VariableController &variableController, | ||
const Properties &properties) const override | ||||
{ | ||||
Alexandre Leroux
|
r1202 | Q_UNUSED(variable); | ||
Q_UNUSED(variableController); | ||||
Q_UNUSED(properties); | ||||
// Does nothing | ||||
} | ||||
}; | ||||
} // namespace | ||||
std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperationType type) | ||||
{ | ||||
switch (type) { | ||||
Alexandre Leroux
|
r1211 | case FuzzingOperationType::CREATE: | ||
return std::make_unique<CreateOperation>(); | ||||
default: | ||||
// Default case returns unknown operation | ||||
break; | ||||
Alexandre Leroux
|
r1202 | } | ||
return std::make_unique<UnknownOperation>(); | ||||
} | ||||