Green refactor: Remove need for a mutable FileContext pointer in FileModule

This commit is contained in:
Marius Kintel 2016-07-21 15:28:15 -04:00
parent 4c89a65174
commit 6f71c7c889
4 changed files with 23 additions and 27 deletions

View file

@ -40,7 +40,6 @@ namespace fs = boost::filesystem;
FileModule::~FileModule()
{
delete context;
}
std::string FileModule::dump(const std::string &indent, const std::string &name) const
@ -160,14 +159,20 @@ AbstractNode *FileModule::instantiate(const Context *ctx, const ModuleInstantiat
{
assert(evalctx == NULL);
delete this->context;
this->context = new FileContext(ctx);
context->initializeModule(*this);
FileContext context(ctx);
return this->instantiateWithFileContext(&context, inst, evalctx);
}
AbstractNode *FileModule::instantiateWithFileContext(FileContext *ctx, const ModuleInstantiation *inst, EvalContext *evalctx) const
{
assert(evalctx == NULL);
ctx->initializeModule(*this);
AbstractNode *node = new RootNode(inst);
try {
// FIXME: Set document path to the path of the module
std::vector<AbstractNode *> instantiatednodes = this->scope.instantiateChildren(context);
std::vector<AbstractNode *> instantiatednodes = this->scope.instantiateChildren(ctx);
node->children.insert(node->children.end(), instantiatednodes.begin(), instantiatednodes.end());
}
catch (EvaluationException &e) {
@ -176,9 +181,3 @@ AbstractNode *FileModule::instantiate(const Context *ctx, const ModuleInstantiat
return node;
}
ValuePtr FileModule::lookup_variable(const std::string &name) const
{
if (!this->context) return ValuePtr::undefined;
return this->context->lookup_variable(name, true);
}

View file

@ -12,12 +12,14 @@
class FileModule : public AbstractModule
{
public:
FileModule() : context(nullptr), is_handling_dependencies(false) {}
FileModule() : is_handling_dependencies(false) {}
virtual ~FileModule();
virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, EvalContext *evalctx = NULL) const;
virtual std::string dump(const std::string &indent, const std::string &name) const;
void setModulePath(const std::string &path) { this->path = path; }
AbstractNode *instantiateWithFileContext(class FileContext *ctx, const ModuleInstantiation *inst, EvalContext *evalctx) const;
void setModulePath(const std::string &path) { this->path = path; }
const std::string &modulePath() const { return this->path; }
void registerUse(const std::string path);
void registerInclude(const std::string &localpath, const std::string &fullpath);
@ -26,14 +28,11 @@ public:
bool hasIncludes() const { return !this->includes.empty(); }
bool usesLibraries() const { return !this->usedlibs.empty(); }
bool isHandlingDependencies() const { return this->is_handling_dependencies; }
ValuePtr lookup_variable(const std::string &name) const;
LocalScope scope;
typedef std::unordered_set<std::string> ModuleContainer;
ModuleContainer usedlibs;
private:
// Reference to retain the context that was used in the last evaluation
mutable class FileContext *context;
struct IncludeFile {
std::string filename;
bool valid;

View file

@ -97,7 +97,7 @@ private:
void initActionIcon(QAction *action, const char *darkResource, const char *lightResource);
void handleFileDrop(const QString &filename);
void refreshDocument();
void updateCamera();
void updateCamera(const class FileContext &ctx);
void updateTemporalVariables();
bool fileChangedOnDisk();
void compileTopLevelDocument();

View file

@ -1016,7 +1016,6 @@ void MainWindow::compileDone(bool didchange)
if (didchange) {
updateTemporalVariables();
instantiateRoot();
updateCamera();
updateCompileResult();
callslot = afterCompileSlot;
}
@ -1070,8 +1069,10 @@ void MainWindow::instantiateRoot()
ModuleInstantiation mi = ModuleInstantiation( "group" );
this->root_inst = mi;
this->absolute_root_node = this->root_module->instantiate(&top_ctx, &this->root_inst, NULL);
FileContext filectx(&top_ctx);
this->absolute_root_node = this->root_module->instantiateWithFileContext(&filectx, &this->root_inst, NULL);
this->updateCamera(filectx);
if (this->absolute_root_node) {
// Do we have an explicit root node (! modifier)?
if (!(this->root_node = find_root_tag(this->absolute_root_node))) {
@ -1604,11 +1605,8 @@ void MainWindow::updateTemporalVariables()
* are assigned on top-level, the values are used to change the camera
* rotation, translation and distance.
*/
void MainWindow::updateCamera()
void MainWindow::updateCamera(const FileContext &ctx)
{
if (!root_module)
return;
bool camera_set = false;
Camera cam(qglview->cam);
@ -1622,7 +1620,7 @@ void MainWindow::updateCamera()
double d = cam.zoomValue();
double x, y, z;
const ValuePtr vpr = root_module->lookup_variable("$vpr");
const ValuePtr vpr = ctx.lookup_variable("$vpr");
if (vpr->getVec3(x, y, z)) {
rx = x;
ry = y;
@ -1630,7 +1628,7 @@ void MainWindow::updateCamera()
camera_set = true;
}
const ValuePtr vpt = root_module->lookup_variable("$vpt");
const ValuePtr vpt = ctx.lookup_variable("$vpt");
if (vpt->getVec3(x, y, z)) {
tx = x;
ty = y;
@ -1638,7 +1636,7 @@ void MainWindow::updateCamera()
camera_set = true;
}
const ValuePtr vpd = root_module->lookup_variable("$vpd");
const ValuePtr vpd = ctx.lookup_variable("$vpd");
if (vpd->type() == Value::NUMBER) {
d = vpd->toDouble();
camera_set = true;