diff --git a/src/Gui/SelectionFilter.cpp b/src/Gui/SelectionFilter.cpp index b6059ffbe9..dd4e2d89d9 100644 --- a/src/Gui/SelectionFilter.cpp +++ b/src/Gui/SelectionFilter.cpp @@ -137,13 +137,13 @@ bool SelectionFilterGatePython::allow(App::Document*, App::DocumentObject* obj, // ---------------------------------------------------------------------------- SelectionFilter::SelectionFilter(const char* filter) - : Ast(0) + : Ast(nullptr) { setFilter(filter); } SelectionFilter::SelectionFilter(const std::string& filter) - : Ast(0) + : Ast(nullptr) { setFilter(filter.c_str()); } @@ -151,8 +151,7 @@ SelectionFilter::SelectionFilter(const std::string& filter) void SelectionFilter::setFilter(const char* filter) { if (!filter || filter[0] == 0) { - delete Ast; - Ast = 0; + Ast.reset(); Filter.clear(); } else { @@ -172,43 +171,41 @@ bool SelectionFilter::match(void) return false; Result.clear(); - for (std::vector< Node_Object *>::iterator it= Ast->Objects.begin();it!=Ast->Objects.end();++it) { - int min; - int max; + for (const auto& it : Ast->Objects) { + std::size_t min = 1; + std::size_t max = 1; - if ((*it)->Slice) { - min = (*it)->Slice->Min; - max = (*it)->Slice->Max; - } - else { - min = 1; - max = 1; + if (it->Slice) { + min = it->Slice->Min; + max = it->Slice->Max; } - std::vector temp = Gui::Selection().getSelectionEx(0,(*it)->ObjectType); + std::vector temp = Gui::Selection().getSelectionEx(nullptr, it->ObjectType); // test if subnames present - if ((*it)->SubName.empty()) { + if (it->SubName.empty()) { // if no subnames the count of the object get tested - if ((int)temp.size()max) + if (temp.size() < min || temp.size() > max) return false; } else { // if subnames present count all subs over the selected object of type - int subCount=0; + std::size_t subCount = 0; for (std::vector::const_iterator it2=temp.begin();it2!=temp.end();++it2) { const std::vector& subNames = it2->getSubNames(); if (subNames.empty()) return false; for (std::vector::const_iterator it3=subNames.begin();it3!=subNames.end();++it3) { - if (it3->find((*it)->SubName) != 0) + if (it3->find(it->SubName) != 0) return false; } subCount += subNames.size(); } - if (subCountmax) + + if (subCount < min || subCount > max) return false; } + Result.push_back(temp); } return true; @@ -219,13 +216,13 @@ bool SelectionFilter::test(App::DocumentObject*pObj, const char*sSubName) if (!Ast) return false; - for (std::vector< Node_Object *>::iterator it= Ast->Objects.begin();it!=Ast->Objects.end();++it) { - if (pObj->getTypeId().isDerivedFrom((*it)->ObjectType)) { + for (const auto& it : Ast->Objects) { + if (pObj->getTypeId().isDerivedFrom(it->ObjectType)) { if (!sSubName) return true; - if ((*it)->SubName.empty()) + if (it->SubName.empty()) return true; - if (std::string(sSubName).find((*it)->SubName) == 0) + if (std::string(sSubName).find(it->SubName) == 0) return true; } } @@ -401,7 +398,7 @@ bool SelectionFilter::parse(void) ActFilter = this; /*int my_parse_result =*/ SelectionParser::yyparse(); ActFilter = 0; - Ast = TopBlock; + Ast.reset(TopBlock); TopBlock = 0; SelectionParser::SelectionFilter_delete_buffer (my_string_buffer); diff --git a/src/Gui/SelectionFilter.h b/src/Gui/SelectionFilter.h index c0fd212c54..7f625f854e 100644 --- a/src/Gui/SelectionFilter.h +++ b/src/Gui/SelectionFilter.h @@ -24,6 +24,7 @@ #ifndef GUI_SelectionFilter_H #define GUI_SelectionFilter_H +#include #include #include #include "Selection.h" @@ -87,8 +88,7 @@ protected: std::string Errors; bool parse(void); - Node_Block *Ast; - + std::shared_ptr Ast; }; /** Filter object for the SelectionSengleton @@ -220,11 +220,14 @@ struct Node_Object Node_Slice *Slice; std::string SubName; }; +typedef std::shared_ptr Node_ObjectPtr; struct Node_Block { - Node_Block(Node_Object* obj){Objects.push_back(obj);} - std::vector< Node_Object *> Objects; + Node_Block(Node_Object* obj){ + Objects.emplace_back(obj); + } + std::vector Objects; }; diff --git a/src/Gui/SelectionFilter.tab.c b/src/Gui/SelectionFilter.tab.c index a63e31e53d..5dabdbc950 100644 --- a/src/Gui/SelectionFilter.tab.c +++ b/src/Gui/SelectionFilter.tab.c @@ -1408,7 +1408,7 @@ yyreduce: /* Line 1464 of yacc.c */ #line 53 "SelectionFilter.y" - { (yyval.block) = (yyvsp[(1) - (2)].block) ; (yyval.block)->Objects.push_back((yyvsp[(2) - (2)].object)); ;} + { (yyval.block) = (yyvsp[(1) - (2)].block) ; (yyval.block)->Objects.emplace_back((yyvsp[(2) - (2)].object)); ;} break; case 13: diff --git a/src/Gui/SelectionFilter.y b/src/Gui/SelectionFilter.y index 6f135a93f5..051599e7ed 100644 --- a/src/Gui/SelectionFilter.y +++ b/src/Gui/SelectionFilter.y @@ -69,7 +69,7 @@ count : { $$ = 0 } matchline : type subname count { $$ = new Node_Object($1,$2,$3) } matchlines : matchline { $$ = new Node_Block($1); } - | matchlines matchline { $$ = $1 ; $$->Objects.push_back($2); } + | matchlines matchline { $$ = $1 ; $$->Objects.emplace_back($2); } block : matchlines { $$ = $1 }