Clean up pointer use:

* Use unique_ptr for Actions instead of manual memory management
 * Use unique_ptr in KeywordDatabase
 * Use unique_ptr instead of shared_ptr for file formats
 * Don't pass shared_ptr to Reader/Writer, use references instead
Also
 * Switch to C++17 so we can use map::try_emplace
This commit is contained in:
Twan van Laarhoven
2020-04-25 21:30:05 +02:00
parent 708b4389a0
commit 64ea1d7322
57 changed files with 363 additions and 385 deletions
+1 -1
View File
@@ -100,7 +100,7 @@ void SymbolBasicShapeEditor::onLeftDown (const Vector2D& pos, wxMouseEvent& ev
void SymbolBasicShapeEditor::onLeftUp (const Vector2D& pos, wxMouseEvent& ev) {
if (drawing && shape) {
// Finalize the shape
addAction(new AddSymbolPartAction(*getSymbol(), shape));
addAction(make_unique<AddSymbolPartAction>(*getSymbol(), shape));
// Select the part
control.selectPart(shape);
// no need to clean up, this editor is replaced
+2 -2
View File
@@ -16,6 +16,6 @@ void SymbolEditorBase::SetStatusText(const String& text) {
control.parent->SetStatusText(text);
}
void SymbolEditorBase::addAction(Action* action, bool allow_merge) {
getSymbol()->actions.addAction(action, allow_merge);
void SymbolEditorBase::addAction(unique_ptr<Action> action, bool allow_merge) {
getSymbol()->actions.addAction(move(action), allow_merge);
}
+1 -1
View File
@@ -30,7 +30,7 @@ class SymbolEditorBase : public IntrusivePtrVirtualBase {
inline SymbolP getSymbol() { return control.getSymbol(); }
/// Perform an action
void addAction(Action* action, bool allow_merge = true);
void addAction(unique_ptr<Action> action, bool allow_merge = true);
void SetStatusText(const String& text);
+5 -5
View File
@@ -166,9 +166,9 @@ void SymbolPartList::onLeftUp(wxMouseEvent& ev) {
if (par != drop_parent && par->parts.size() == 1 && !par->isSymbolSymmetry()) {
// this leaves a group without elements, remove it
findParent(*par, par, drag_position); // parent of the group
symbol->actions.addAction(new UngroupReorderSymbolPartsAction(*par, drag_position, *drop_parent, drop_position));
symbol->actions.addAction(make_unique<UngroupReorderSymbolPartsAction>(*par, drag_position, *drop_parent, drop_position));
} else {
symbol->actions.addAction(new ReorderSymbolPartsAction(*par, drag_position, *drop_parent, drop_position));
symbol->actions.addAction(make_unique<ReorderSymbolPartsAction>(*par, drag_position, *drop_parent, drop_position));
}
} else {
Refresh(false);
@@ -257,14 +257,14 @@ void SymbolPartList::onChar(wxKeyEvent& ev) {
if (cursor > 0 && cursor <= typing_in->name.size()) {
String new_name = typing_in->name;
new_name.erase(cursor - 1, 1);
symbol->actions.addAction(new SymbolPartNameAction(typing_in, new_name, cursor, cursor - 1));
symbol->actions.addAction(make_unique<SymbolPartNameAction>(typing_in, new_name, cursor, cursor - 1));
}
break;
case WXK_DELETE:
if (cursor < typing_in->name.size()) {
String new_name = typing_in->name;
new_name.erase(cursor, 1);
symbol->actions.addAction(new SymbolPartNameAction(typing_in, new_name, cursor, cursor));
symbol->actions.addAction(make_unique<SymbolPartNameAction>(typing_in, new_name, cursor, cursor));
}
break;
default:
@@ -281,7 +281,7 @@ void SymbolPartList::onChar(wxKeyEvent& ev) {
#endif
String new_name = typing_in->name;
new_name.insert(cursor, 1, key);
symbol->actions.addAction(new SymbolPartNameAction(typing_in, new_name, cursor, cursor + 1));
symbol->actions.addAction(make_unique<SymbolPartNameAction>(typing_in, new_name, cursor, cursor + 1));
}
}
}
+22 -18
View File
@@ -253,10 +253,11 @@ void SymbolPointEditor::onLeftDClick(const Vector2D& pos, wxMouseEvent& ev) {
findHoveredItem(pos, false);
if (hovering == SELECTED_NEW_POINT) {
// Add point
ControlPointAddAction* act = new ControlPointAddAction(part, hover_line_1_idx, hover_line_t);
addAction(act);
auto act = make_unique<ControlPointAddAction>(part, hover_line_1_idx, hover_line_t);
ControlPointP new_point = act->getNewPoint();
addAction(move(act));
// select the new point
selectPoint(act->getNewPoint(), false);
selectPoint(new_point, false);
selection = SELECTED_POINTS;
} else if (hovering == SELECTED_HANDLE && hover_handle.handle == HANDLE_MAIN) {
// Delete point
@@ -287,8 +288,9 @@ void SymbolPointEditor::onMouseDrag(const Vector2D& from, const Vector2D& to, wx
// Drag the curve
if (controlPointMoveAction) controlPointMoveAction = 0;
if (!curveDragAction) {
curveDragAction = new CurveDragAction(selected_line1, selected_line2);
addAction(curveDragAction);
auto action = make_unique<CurveDragAction>(selected_line1, selected_line2);
curveDragAction = action.get();
addAction(std::move(action));
}
curveDragAction->move(delta, selected_line_t);
control.Refresh(false);
@@ -297,8 +299,9 @@ void SymbolPointEditor::onMouseDrag(const Vector2D& from, const Vector2D& to, wx
if (curveDragAction) curveDragAction = 0;
if (!controlPointMoveAction) {
// create action we can add this movement to
controlPointMoveAction = new ControlPointMoveAction(selected_points);
addAction(controlPointMoveAction);
auto action = make_unique<ControlPointMoveAction>(selected_points);
controlPointMoveAction = action.get();
addAction(std::move(action));
}
controlPointMoveAction->constrain = ev.ControlDown(); // ctrl constrains
controlPointMoveAction->snap = snap(ev);
@@ -308,8 +311,9 @@ void SymbolPointEditor::onMouseDrag(const Vector2D& from, const Vector2D& to, wx
} else if (selection == SELECTED_HANDLE) {
// Move the selected handle
if (!handleMoveAction) {
handleMoveAction = new HandleMoveAction(selected_handle);
addAction(handleMoveAction);
auto action = make_unique<HandleMoveAction>(selected_handle);
handleMoveAction = action.get();
addAction(std::move(action));
}
handleMoveAction->constrain = ev.ControlDown(); // ctrl constrains
handleMoveAction->snap = snap(ev);
@@ -353,6 +357,7 @@ void SymbolPointEditor::onChar(wxKeyEvent& ev) {
if (ev.GetKeyCode() == WXK_DELETE) {
deleteSelection();
} else {
resetActions();
// move selection using arrow keys
double step = 1.0 / settings.symbol_grid_size;
Vector2D delta;
@@ -367,19 +372,18 @@ void SymbolPointEditor::onChar(wxKeyEvent& ev) {
// what to move
if (selection == SELECTED_POINTS || selection == SELECTED_LINE) {
// Move all selected points
controlPointMoveAction = new ControlPointMoveAction(selected_points);
addAction(controlPointMoveAction);
controlPointMoveAction->move(delta);
auto action = make_unique<ControlPointMoveAction>(selected_points);
action->move(delta);
addAction(std::move(action));
new_point += delta;
control.Refresh(false);
} else if (selection == SELECTED_HANDLE) {
// Move the selected handle
handleMoveAction = new HandleMoveAction(selected_handle);
addAction(handleMoveAction);
handleMoveAction->move(delta);
auto action = make_unique<HandleMoveAction>(selected_handle);
action->move(delta);
addAction(std::move(action));
control.Refresh(false);
}
resetActions();
}
}
@@ -477,12 +481,12 @@ void SymbolPointEditor::onChangeSegment(SegmentMode mode) {
assert(selected_line1);
assert(selected_line2);
if (selected_line1->segment_after == mode) return;
addAction(new SegmentModeAction(selected_line1, selected_line2, mode));
addAction(make_unique<SegmentModeAction>(selected_line1, selected_line2, mode));
control.Refresh(false);
}
void SymbolPointEditor::onChangeLock(LockMode mode) {
addAction(new LockModeAction(*selected_points.begin(), mode));
addAction(make_unique<LockModeAction>(*selected_points.begin(), mode));
control.Refresh(false);
}
+18 -14
View File
@@ -173,22 +173,22 @@ void SymbolSelectEditor::onUpdateUI(wxUpdateUIEvent& ev) {
void SymbolSelectEditor::onCommand(int id) {
if (id >= ID_SYMBOL_COMBINE && id < ID_SYMBOL_COMBINE_MAX) {
// change combine mode
addAction(new CombiningModeAction(
addAction(make_unique<CombiningModeAction>(
control.selected_parts.get(),
static_cast<SymbolShapeCombine>(id - ID_SYMBOL_COMBINE)
));
control.Refresh(false);
} else if (id == ID_EDIT_DUPLICATE && !isEditing()) {
// duplicate selection, not when dragging
addAction(new DuplicateSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(make_unique<DuplicateSymbolPartsAction>(*getSymbol(), control.selected_parts.get()));
control.Refresh(false);
} else if (id == ID_EDIT_GROUP && !isEditing()) {
// group selection, not when dragging
addAction(new GroupSymbolPartsAction(*getSymbol(), control.selected_parts.get(), make_intrusive<SymbolGroup>()));
addAction(make_unique<GroupSymbolPartsAction>(*getSymbol(), control.selected_parts.get(), make_intrusive<SymbolGroup>()));
control.Refresh(false);
} else if (id == ID_EDIT_UNGROUP && !isEditing()) {
// ungroup selection, not when dragging
addAction(new UngroupSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(make_unique<UngroupSymbolPartsAction>(*getSymbol(), control.selected_parts.get()));
control.Refresh(false);
}
}
@@ -312,24 +312,28 @@ void SymbolSelectEditor::onMouseDrag (const Vector2D& from, const Vector2D& to,
if (rotate) {
if (scaleX == 0 || scaleY == 0) {
// shear, center/fixed point on the opposite side
shearAction = new SymbolPartShearAction(control.selected_parts.get(), bounds.corner(-scaleX, -scaleY));
addAction(shearAction);
auto action = make_unique<SymbolPartShearAction>(control.selected_parts.get(), bounds.corner(-scaleX, -scaleY));
shearAction = action.get();
addAction(std::move(action));
} else {
// rotate
rotateAction = new SymbolPartRotateAction(control.selected_parts.get(), center);
addAction(rotateAction);
auto action = make_unique<SymbolPartRotateAction>(control.selected_parts.get(), center);
rotateAction = action.get();
addAction(std::move(action));
startAngle = angleTo(to);
}
} else {
// we are on a handle; start scaling
scaleAction = new SymbolPartScaleAction(control.selected_parts.get(), scaleX, scaleY);
addAction(scaleAction);
auto action = make_unique<SymbolPartScaleAction>(control.selected_parts.get(), scaleX, scaleY);
scaleAction = action.get();
addAction(std::move(action));
}
} else {
// move
click_mode = CLICK_MOVE;
moveAction = new SymbolPartMoveAction(control.selected_parts.get());
addAction(moveAction);
auto action = make_unique<SymbolPartMoveAction>(control.selected_parts.get());
moveAction = action.get();
addAction(std::move(action));
}
}
@@ -399,7 +403,7 @@ void SymbolSelectEditor::onKeyChange (wxKeyEvent& ev) {
void SymbolSelectEditor::onChar(wxKeyEvent& ev) {
if (ev.GetKeyCode() == WXK_DELETE) {
// delete selected parts
addAction(new RemoveSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(make_unique<RemoveSymbolPartsAction>(*getSymbol(), control.selected_parts.get()));
if (control.selected_parts.selected(highlightPart)) highlightPart = SymbolPartP(); // deleted it
control.selected_parts.clear();
resetActions();
@@ -416,7 +420,7 @@ void SymbolSelectEditor::onChar(wxKeyEvent& ev) {
ev.Skip();
return;
}
addAction(new SymbolPartMoveAction(control.selected_parts.get(), delta));
addAction(make_unique<SymbolPartMoveAction>(control.selected_parts.get(), delta));
}
}
+7 -6
View File
@@ -107,13 +107,13 @@ void SymbolSymmetryEditor::onCommand(int id) {
if (id >= ID_SYMMETRY && id < ID_SYMMETRY_MAX) {
SymbolSymmetryType kind = id == ID_SYMMETRY_ROTATION ? SYMMETRY_ROTATION : SYMMETRY_REFLECTION;
if (symmetry && symmetry->kind != kind) {
addAction(new SymmetryTypeAction(*symmetry, kind));
addAction(make_unique<SymmetryTypeAction>(*symmetry, kind));
control.Refresh(false);
}
resetActions();
} else if (id == ID_COPIES) {
if (symmetry && symmetry->copies != copies->GetValue()) {
addAction(new SymmetryCopiesAction(*symmetry, copies->GetValue()));
addAction(make_unique<SymmetryCopiesAction>(*symmetry, copies->GetValue()));
control.Refresh(false);
}
resetActions();
@@ -124,11 +124,11 @@ void SymbolSymmetryEditor::onCommand(int id) {
symmetry->center = Vector2D(0.5,0.5);
symmetry->handle = Vector2D(0.2,0);
symmetry->name = symmetry->expectedName();
addAction(new GroupSymbolPartsAction(*getSymbol(), control.selected_parts.get(), symmetry));
addAction(make_unique<GroupSymbolPartsAction>(*getSymbol(), control.selected_parts.get(), symmetry));
control.selected_parts.select(symmetry);
control.Refresh(false);
} else if (id == ID_REMOVE_SYMMETRY) {
addAction(new UngroupSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(make_unique<UngroupSymbolPartsAction>(*getSymbol(), control.selected_parts.get()));
symmetry = SymbolSymmetryP();
control.Refresh(false);
}
@@ -155,10 +155,11 @@ void SymbolSymmetryEditor::onMouseDrag (const Vector2D& from, const Vector2D& t
// Resize the object
if (selection == SELECTION_NONE) return;
if (!symmetryMoveAction) {
symmetryMoveAction = new SymmetryMoveAction(*symmetry, selection == SELECTION_HANDLE);
auto action = make_unique<SymmetryMoveAction>(*symmetry, selection == SELECTION_HANDLE);
symmetryMoveAction = action.get();
symmetryMoveAction->constrain = ev.ControlDown();
symmetryMoveAction->snap = ev.ShiftDown() != settings.symbol_grid_snap ? settings.symbol_grid_size : 0;
addAction(symmetryMoveAction);
addAction(std::move(action));
}
symmetryMoveAction->move(to - from);
control.Refresh(false);
+8 -4
View File
@@ -36,7 +36,8 @@ SymbolWindow::SymbolWindow(Window* parent, const String& filename)
: performer(nullptr)
{
// open file
Reader reader(make_shared<wxFileInputStream>(filename), nullptr, filename);
wxFileInputStream stream(filename);
Reader reader(stream, nullptr, filename);
SymbolP symbol;
reader.handle_greedy(symbol);
init(parent, symbol);
@@ -217,7 +218,8 @@ void SymbolWindow::onFileOpen(wxCommandEvent& ev) {
String ext = n.GetExt();
SymbolP symbol;
if (ext.Lower() == _("mse-symbol")) {
Reader reader(make_shared<wxFileInputStream>(name), nullptr, name);
wxFileInputStream stream(name);
Reader reader(stream, nullptr, name);
reader.handle_greedy(symbol);
} else {
wxBusyCursor busy;
@@ -240,7 +242,8 @@ void SymbolWindow::onFileSaveAs(wxCommandEvent& ev) {
String name = wxFileSelector(_("Save symbol"),settings.default_set_dir,_(""),_(""),_("Symbol files (*.mse-symbol)|*.mse-symbol"),wxFD_SAVE, this);
if (!name.empty()) {
settings.default_set_dir = wxPathOnly(name);
Writer writer(make_shared<wxFileOutputStream>(name), file_version_symbol);
wxFileOutputStream stream(name);
Writer writer(stream, file_version_symbol);
writer.handle(control->getSymbol());
}
}
@@ -250,7 +253,8 @@ void SymbolWindow::onFileStore(wxCommandEvent& ev) {
SymbolValueP value = static_pointer_cast<SymbolValue>(performer->value);
Package& package = performer->getLocalPackage();
FileName new_filename = package.newFileName(value->field().name,_(".mse-symbol")); // a new unique name in the package
Writer writer(package.openOut(new_filename), file_version_symbol);
auto stream = package.openOut(new_filename);
Writer writer(*stream, file_version_symbol);
writer.handle(control->getSymbol());
performer->addAction(value_action(value, new_filename));
}