diff options
author | Neil <nyamatongwe@gmail.com> | 2017-04-29 11:52:49 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2017-04-29 11:52:49 +1000 |
commit | 102a874a3a8ca376e08d1319b36833297bea39ae (patch) | |
tree | 1ca446e24494a212cce099f3f6b479109025c94f /src | |
parent | 1cc39b2c7bc22ae8aaddefd382c621474732b8c1 (diff) | |
download | scintilla-mirror-102a874a3a8ca376e08d1319b36833297bea39ae.tar.gz |
Use std::unique_ptr, std::vector, and move construction to simplify UndoHistory
and make it easier to modify.
Remove out of date warning suppression.
Diffstat (limited to 'src')
-rw-r--r-- | src/CellBuffer.cxx | 68 | ||||
-rw-r--r-- | src/CellBuffer.h | 16 | ||||
-rw-r--r-- | src/Document.cxx | 6 | ||||
-rw-r--r-- | src/Document.h | 2 |
4 files changed, 35 insertions, 57 deletions
diff --git a/src/CellBuffer.cxx b/src/CellBuffer.cxx index ba39e4a80..0cc3b4eda 100644 --- a/src/CellBuffer.cxx +++ b/src/CellBuffer.cxx @@ -12,6 +12,7 @@ #include <cstdarg> #include <stdexcept> +#include <vector> #include <algorithm> #include <memory> @@ -78,48 +79,36 @@ Sci::Line LineVector::LineFromPosition(Sci::Position pos) const { Action::Action() { at = startAction; position = 0; - data = 0; lenData = 0; mayCoalesce = false; } +Action::Action(Action &&other) { + at = other.at; + position = other.position; + data = std::move(other.data); + lenData = other.lenData; + mayCoalesce = other.mayCoalesce; +} + Action::~Action() { - Destroy(); } void Action::Create(actionType at_, Sci::Position position_, const char *data_, Sci::Position lenData_, bool mayCoalesce_) { - delete []data; - data = NULL; + data = nullptr; position = position_; at = at_; if (lenData_) { - data = new char[lenData_]; - memcpy(data, data_, lenData_); + data = std::unique_ptr<char []>(new char[lenData_]); + memcpy(&data[0], data_, lenData_); } lenData = lenData_; mayCoalesce = mayCoalesce_; } -void Action::Destroy() { - delete []data; - data = 0; -} - -void Action::Grab(Action *source) { - delete []data; - - position = source->position; - at = source->at; - data = source->data; - lenData = source->lenData; - mayCoalesce = source->mayCoalesce; - - // Ownership of source data transferred to this - source->position = 0; - source->at = startAction; - source->data = 0; - source->lenData = 0; - source->mayCoalesce = true; +void Action::Clear() { + data = nullptr; + lenData = 0; } // The undo history stores a sequence of user operations that represent the user's view of the @@ -142,8 +131,7 @@ void Action::Grab(Action *source) { UndoHistory::UndoHistory() { - lenActions = 100; - actions = new Action[lenActions]; + actions.resize(3); maxAction = 0; currentAction = 0; undoSequenceDepth = 0; @@ -154,22 +142,14 @@ UndoHistory::UndoHistory() { } UndoHistory::~UndoHistory() { - delete []actions; - actions = 0; } void UndoHistory::EnsureUndoRoom() { // Have to test that there is room for 2 more actions in the array // as two actions may be created by the calling function - if (currentAction >= (lenActions - 2)) { + if (static_cast<size_t>(currentAction) >= (actions.size() - 2)) { // Run out of undo nodes so extend the array - const int lenActionsNew = lenActions * 2; - Action *actionsNew = new Action[lenActionsNew]; - for (int act = 0; act <= currentAction; act++) - actionsNew[act].Grab(&actions[act]); - delete []actions; - lenActions = lenActionsNew; - actions = actionsNew; + actions.resize(actions.size() * 2); } } @@ -195,10 +175,6 @@ const char *UndoHistory::AppendAction(actionType at, Sci::Position position, con } // See if current action can be coalesced into previous action // Will work if both are inserts or deletes and position is same -#if defined(_MSC_VER) && defined(_PREFAST_) - // Visual Studio 2013 Code Analysis wrongly believes actions can be NULL at its next reference - __analysis_assume(actions); -#endif if ((currentAction == savePoint) || (currentAction == tentativePoint)) { currentAction++; } else if (!actions[currentAction].mayCoalesce) { @@ -246,7 +222,7 @@ const char *UndoHistory::AppendAction(actionType at, Sci::Position position, con currentAction++; actions[currentAction].Create(startAction); maxAction = currentAction; - return actions[actionWithData].data; + return actions[actionWithData].data.get(); } void UndoHistory::BeginUndoAction() { @@ -282,7 +258,7 @@ void UndoHistory::DropUndoSequence() { void UndoHistory::DeleteUndoHistory() { for (int i = 1; i < maxAction; i++) - actions[i].Destroy(); + actions[i].Clear(); maxAction = 0; currentAction = 0; actions[currentAction].Create(startAction); @@ -815,7 +791,7 @@ void CellBuffer::PerformUndoStep() { } BasicDeleteChars(actionStep.position, actionStep.lenData); } else if (actionStep.at == removeAction) { - BasicInsertString(actionStep.position, actionStep.data, actionStep.lenData); + BasicInsertString(actionStep.position, actionStep.data.get(), actionStep.lenData); } uh.CompletedUndoStep(); } @@ -835,7 +811,7 @@ const Action &CellBuffer::GetRedoStep() const { void CellBuffer::PerformRedoStep() { const Action &actionStep = uh.GetRedoStep(); if (actionStep.at == insertAction) { - BasicInsertString(actionStep.position, actionStep.data, actionStep.lenData); + BasicInsertString(actionStep.position, actionStep.data.get(), actionStep.lenData); } else if (actionStep.at == removeAction) { BasicDeleteChars(actionStep.position, actionStep.lenData); } diff --git a/src/CellBuffer.h b/src/CellBuffer.h index 8667b8bfa..8e670aca5 100644 --- a/src/CellBuffer.h +++ b/src/CellBuffer.h @@ -61,26 +61,28 @@ class Action { public: actionType at; Sci::Position position; - char *data; + std::unique_ptr<char[]> data; Sci::Position lenData; bool mayCoalesce; Action(); // Deleted so Action objects can not be copied. - Action(const Action &) = delete; - void operator=(const Action &) = delete; + Action(const Action &other) = delete; + Action &operator=(const Action &other) = delete; + Action &operator=(const Action &&other) = delete; + // Move constructor allows vector to be resized without reallocating. + // Could use =default but MSVC 2013 warns. + Action(Action &&other); ~Action(); void Create(actionType at_, Sci::Position position_=0, const char *data_=0, Sci::Position lenData_=0, bool mayCoalesce_=true); - void Destroy(); - void Grab(Action *source); + void Clear(); }; /** * */ class UndoHistory { - Action *actions; - int lenActions; + std::vector<Action> actions; int maxAction; int currentAction; int undoSequenceDepth; diff --git a/src/Document.cxx b/src/Document.cxx index 581358e52..4b0bd79e2 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -261,7 +261,7 @@ void Document::TentativeUndo() { modFlags |= SC_MULTILINEUNDOREDO; } NotifyModified(DocModification(modFlags, action.position, action.lenData, - linesAdded, action.data)); + linesAdded, action.data.get())); } bool endSavePoint = cb.IsSavePoint(); @@ -1208,7 +1208,7 @@ Sci::Position Document::Undo() { modFlags |= SC_MULTILINEUNDOREDO; } NotifyModified(DocModification(modFlags, action.position, action.lenData, - linesAdded, action.data)); + linesAdded, action.data.get())); } bool endSavePoint = cb.IsSavePoint(); @@ -1268,7 +1268,7 @@ Sci::Position Document::Redo() { } NotifyModified( DocModification(modFlags, action.position, action.lenData, - linesAdded, action.data)); + linesAdded, action.data.get())); } bool endSavePoint = cb.IsSavePoint(); diff --git a/src/Document.h b/src/Document.h index 62e26c6fc..77686f78c 100644 --- a/src/Document.h +++ b/src/Document.h @@ -513,7 +513,7 @@ public: position(act.position), length(act.lenData), linesAdded(linesAdded_), - text(act.data), + text(act.data.get()), line(0), foldLevelNow(0), foldLevelPrev(0), |