-
-
Notifications
You must be signed in to change notification settings - Fork 62
added findVariable method to pyminsky class, updated findObject to guarante ... #533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,28 +30,73 @@ namespace pyminsky | |
|
|
||
| minsky::Item& findObject(const std::string& typeName) | ||
| { | ||
| using namespace minsky; | ||
| auto& canvas = minsky.canvas; | ||
| using namespace minsky; | ||
| auto& canvas = minsky.canvas; | ||
|
|
||
| canvas.item.reset(); | ||
| canvas.item.reset(); // Reset the item to ensure no leftover references | ||
|
|
||
| if (typeName == "Group" && !canvas.model->groups.empty()) | ||
| canvas.item = canvas.model->groups.front(); | ||
| else | ||
| minsky.model->recursiveDo(&GroupItems::items, [&](const Items&, Items::const_iterator i) | ||
| bool found = false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This found flag is redundant. It just shadows the validity status of canvas.item
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood. I was getting a segfault in the renameVariables.sh test, so I used it for added safety. |
||
|
|
||
| if (typeName == "Group" && !canvas.model->groups.empty()) | ||
| { | ||
| canvas.item = canvas.model->groups.front(); | ||
| found = true; | ||
| } | ||
| else | ||
| { | ||
| if ((*i)->classType() == typeName) | ||
| found = minsky.model->recursiveDo(&GroupItems::items, [&](const Items&, Items::const_iterator i) | ||
| { | ||
| canvas.item = *i; | ||
| return true; // Stop recursion | ||
| } | ||
| return false; | ||
| }); | ||
| if ((*i)->classType() == typeName) | ||
| { | ||
| canvas.item = *i; | ||
| return true; // Stop recursion | ||
| } | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| // Check if an object was found | ||
| if (!found || !canvas.item) | ||
| { | ||
| // std::cerr << "findObject: Object of type '" << typeName << "' not found or invalid!" << std::endl; | ||
| throw std::runtime_error("Object not found"); | ||
| } | ||
|
|
||
| return *canvas.item; | ||
| return *canvas.item; // Return the dereferenced item | ||
| } | ||
|
|
||
| // Find a variable by its name | ||
| minsky::Item& findVariable(const std::string& name) | ||
| { | ||
| using namespace minsky; | ||
| auto& canvas = minsky.canvas; | ||
|
|
||
| canvas.item.reset(); // Reset item to ensure clean start | ||
|
|
||
| bool found = minsky.model->recursiveDo( | ||
| &GroupItems::items, | ||
| [&](const Items&, Items::const_iterator i) { | ||
| if (auto v = dynamic_cast<VariableBase*>(i->get())) | ||
| { | ||
| if (v->name() == name) | ||
| { | ||
| canvas.item = *i; | ||
| return true; // Stop recursion | ||
| } | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| if (!found || !canvas.item) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. found flag redundant here too. |
||
| { | ||
| // std::cerr << "findVariable: Variable '" << name << "' not found or invalid!" << std::endl; | ||
| throw std::runtime_error("Variable not found"); | ||
| } | ||
|
|
||
| return *canvas.item; // Return the dereferenced item | ||
| } | ||
| CLASSDESC_ADD_FUNCTION(findObject); | ||
| CLASSDESC_ADD_FUNCTION(findVariable); | ||
| } | ||
|
|
||
| CLASSDESC_PYTHON_MODULE(pyminsky); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation has gone a bit weird
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. I must have accidentally pressed the tab key on that line. I'll check the indentation more carefully prior to a pull request next time.