From 69f3505ba3066c23024dc6bb6878a339474581bf Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 1 Mar 2018 09:55:25 +1100 Subject: Use make_unique in preference to new. From Effective Modern C++ Item 21. --- src/CellBuffer.cxx | 2 +- src/ContractionState.cxx | 10 +++++----- src/Decoration.cxx | 2 +- src/Document.cxx | 10 +++++----- src/EditView.cxx | 2 +- src/ExternalLexer.cxx | 5 +++-- src/LineMarker.cxx | 6 +++--- src/Partitioning.h | 2 +- src/PerLine.cxx | 25 ++++++++++++------------- src/PositionCache.cxx | 18 +++++++++--------- src/RunStyles.cxx | 8 ++++---- src/ViewStyle.cxx | 2 +- 12 files changed, 46 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/CellBuffer.cxx b/src/CellBuffer.cxx index 513c9a9b8..b4a4029f6 100644 --- a/src/CellBuffer.cxx +++ b/src/CellBuffer.cxx @@ -97,7 +97,7 @@ void Action::Create(actionType at_, Sci::Position position_, const char *data_, position = position_; at = at_; if (lenData_) { - data = std::unique_ptr(new char[lenData_]); + data = std::make_unique(lenData_); memcpy(&data[0], data_, lenData_); } lenData = lenData_; diff --git a/src/ContractionState.cxx b/src/ContractionState.cxx index 8777e4af9..77b717a17 100644 --- a/src/ContractionState.cxx +++ b/src/ContractionState.cxx @@ -35,11 +35,11 @@ ContractionState::~ContractionState() { void ContractionState::EnsureData() { if (OneToOne()) { - visible.reset(new RunStyles()); - expanded.reset(new RunStyles()); - heights.reset(new RunStyles()); - foldDisplayTexts.reset(new SparseVector()); - displayLines.reset(new Partitioning(4)); + visible = std::make_unique>(); + expanded = std::make_unique>(); + heights = std::make_unique>(); + foldDisplayTexts = std::make_unique>(); + displayLines = std::make_unique>(4); InsertLines(0, linesInDocument); } } diff --git a/src/Decoration.cxx b/src/Decoration.cxx index bca2d6cb0..a9e22adda 100644 --- a/src/Decoration.cxx +++ b/src/Decoration.cxx @@ -55,7 +55,7 @@ Decoration *DecorationList::DecorationFromIndicator(int indicator) { Decoration *DecorationList::Create(int indicator, int length) { currentIndicator = indicator; - std::unique_ptr decoNew(new Decoration(indicator)); + std::unique_ptr decoNew = std::make_unique(indicator); decoNew->rs.InsertSpace(0, length); std::vector>::iterator it = std::lower_bound( diff --git a/src/Document.cxx b/src/Document.cxx index a6f408f3e..41d6b2375 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -118,11 +118,11 @@ Document::Document(int options) : UTF8BytesOfLeadInitialise(); - perLineData[ldMarkers].reset(new LineMarkers()); - perLineData[ldLevels].reset(new LineLevels()); - perLineData[ldState].reset(new LineState()); - perLineData[ldMargin].reset(new LineAnnotation()); - perLineData[ldAnnotation].reset(new LineAnnotation()); + perLineData[ldMarkers] = std::make_unique(); + perLineData[ldLevels] = std::make_unique(); + perLineData[ldState] = std::make_unique(); + perLineData[ldMargin] = std::make_unique(); + perLineData[ldAnnotation] = std::make_unique(); cb.SetPerLine(this); } diff --git a/src/EditView.cxx b/src/EditView.cxx index 8db21d5d9..d4cdf4dc8 100644 --- a/src/EditView.cxx +++ b/src/EditView.cxx @@ -227,7 +227,7 @@ bool EditView::ClearTabstops(Sci::Line line) { bool EditView::AddTabstop(Sci::Line line, int x) { if (!ldTabstops) { - ldTabstops.reset(new LineTabstops()); + ldTabstops = std::make_unique(); } LineTabstops *lt = static_cast(ldTabstops.get()); return lt && lt->AddTabstop(line, x); diff --git a/src/ExternalLexer.cxx b/src/ExternalLexer.cxx index fdef2ad57..1ab96913a 100644 --- a/src/ExternalLexer.cxx +++ b/src/ExternalLexer.cxx @@ -65,6 +65,8 @@ LexerLibrary::LexerLibrary(const char *moduleName_) { char lexname[100] = ""; GetLexerName(i, lexname, sizeof(lexname)); ExternalLexerModule *lex = new ExternalLexerModule(SCLEX_AUTOMATIC, NULL, lexname, NULL); + // This is storing a second reference to lex in the Catalogue as well as in modules. + // TODO: Should use std::shared_ptr or similar to ensure allocation safety. Catalogue::AddLexerModule(lex); // Remember ExternalLexerModule so we don't leak it @@ -112,8 +114,7 @@ void LexerManager::Load(const char *path) { if (ll->moduleName == path) return; } - LexerLibrary *lib = new LexerLibrary(path); - libraries.push_back(std::unique_ptr(lib)); + libraries.push_back(std::make_unique(path)); } void LexerManager::Clear() { diff --git a/src/LineMarker.cxx b/src/LineMarker.cxx index f556e77f8..e3c2c6713 100644 --- a/src/LineMarker.cxx +++ b/src/LineMarker.cxx @@ -25,17 +25,17 @@ using namespace Scintilla; void LineMarker::SetXPM(const char *textForm) { - pxpm.reset(new XPM(textForm)); + pxpm = std::make_unique(textForm); markType = SC_MARK_PIXMAP; } void LineMarker::SetXPM(const char *const *linesForm) { - pxpm.reset(new XPM(linesForm)); + pxpm = std::make_unique(linesForm); markType = SC_MARK_PIXMAP; } void LineMarker::SetRGBAImage(Point sizeRGBAImage, float scale, const unsigned char *pixelsRGBAImage) { - image.reset(new RGBAImage(static_cast(sizeRGBAImage.x), static_cast(sizeRGBAImage.y), scale, pixelsRGBAImage)); + image = std::make_unique(static_cast(sizeRGBAImage.x), static_cast(sizeRGBAImage.y), scale, pixelsRGBAImage); markType = SC_MARK_RGBAIMAGE; } diff --git a/src/Partitioning.h b/src/Partitioning.h index 112a543ca..57c8656c8 100644 --- a/src/Partitioning.h +++ b/src/Partitioning.h @@ -83,7 +83,7 @@ private: } void Allocate(ptrdiff_t growSize) { - body.reset(new SplitVectorWithRangeAdd(growSize)); + body = std::make_unique>(growSize); stepPartition = 0; stepLength = 0; body->Insert(0, 0); // This value stays 0 for ever diff --git a/src/PerLine.cxx b/src/PerLine.cxx index 2afbf3161..6785ef81b 100644 --- a/src/PerLine.cxx +++ b/src/PerLine.cxx @@ -119,7 +119,7 @@ Sci::Line LineMarkers::LineFromHandle(int markerHandle) { void LineMarkers::MergeMarkers(Sci::Line line) { if (markers[line + 1]) { if (!markers[line]) - markers[line].reset(new MarkerHandleSet); + markers[line] = std::make_unique(); markers[line]->CombineWith(markers[line + 1].get()); markers[line + 1].reset(); } @@ -155,7 +155,7 @@ int LineMarkers::AddMark(Sci::Line line, int markerNum, Sci::Line lines) { } if (!markers[line]) { // Need new structure to hold marker handle - markers[line].reset(new MarkerHandleSet()); + markers[line] = std::make_unique(); } markers[line]->InsertHandle(handleCurrent, markerNum); @@ -359,17 +359,16 @@ const unsigned char *LineAnnotation::Styles(Sci::Line line) const { return 0; } -static char *AllocateAnnotation(int length, int style) { +static std::unique_ptrAllocateAnnotation(int length, int style) { const size_t len = sizeof(AnnotationHeader) + length + ((style == IndividualStyles) ? length : 0); - char *ret = new char[len](); - return ret; + return std::make_unique(len); } void LineAnnotation::SetText(Sci::Line line, const char *text) { if (text && (line >= 0)) { annotations.EnsureLength(line+1); const int style = Style(line); - annotations[line].reset(AllocateAnnotation(static_cast(strlen(text)), style)); + annotations[line] = AllocateAnnotation(static_cast(strlen(text)), style); char *pa = annotations[line].get(); assert(pa); AnnotationHeader *pah = reinterpret_cast(pa); @@ -391,7 +390,7 @@ void LineAnnotation::ClearAll() { void LineAnnotation::SetStyle(Sci::Line line, int style) { annotations.EnsureLength(line+1); if (!annotations[line]) { - annotations[line].reset(AllocateAnnotation(0, style)); + annotations[line] = AllocateAnnotation(0, style); } reinterpret_cast(annotations[line].get())->style = static_cast(style); } @@ -400,16 +399,16 @@ void LineAnnotation::SetStyles(Sci::Line line, const unsigned char *styles) { if (line >= 0) { annotations.EnsureLength(line+1); if (!annotations[line]) { - annotations[line].reset(AllocateAnnotation(0, IndividualStyles)); + annotations[line] = AllocateAnnotation(0, IndividualStyles); } else { AnnotationHeader *pahSource = reinterpret_cast(annotations[line].get()); if (pahSource->style != IndividualStyles) { - char *allocation = AllocateAnnotation(pahSource->length, IndividualStyles); - AnnotationHeader *pahAlloc = reinterpret_cast(allocation); + std::unique_ptrallocation = AllocateAnnotation(pahSource->length, IndividualStyles); + AnnotationHeader *pahAlloc = reinterpret_cast(allocation.get()); pahAlloc->length = pahSource->length; pahAlloc->lines = pahSource->lines; - memcpy(allocation + sizeof(AnnotationHeader), annotations[line].get() + sizeof(AnnotationHeader), pahSource->length); - annotations[line].reset(allocation); + memcpy(allocation.get() + sizeof(AnnotationHeader), annotations[line].get() + sizeof(AnnotationHeader), pahSource->length); + annotations[line] = std::move(allocation); } } AnnotationHeader *pah = reinterpret_cast(annotations[line].get()); @@ -468,7 +467,7 @@ bool LineTabstops::ClearTabstops(Sci::Line line) { bool LineTabstops::AddTabstop(Sci::Line line, int x) { tabstops.EnsureLength(line + 1); if (!tabstops[line]) { - tabstops[line].reset(new TabstopList()); + tabstops[line] = std::make_unique(); } TabstopList *tl = tabstops[line].get(); diff --git a/src/PositionCache.cxx b/src/PositionCache.cxx index 99e3c4d56..273875e4e 100644 --- a/src/PositionCache.cxx +++ b/src/PositionCache.cxx @@ -74,11 +74,11 @@ LineLayout::~LineLayout() { void LineLayout::Resize(int maxLineLength_) { if (maxLineLength_ > maxLineLength) { Free(); - chars.reset(new char[maxLineLength_ + 1]); - styles.reset(new unsigned char[maxLineLength_ + 1]); + chars = std::make_unique(maxLineLength_ + 1); + styles = std::make_unique(maxLineLength_ + 1); // Extra position allocated as sometimes the Windows // GetTextExtentExPoint API writes an extra element. - positions.reset(new XYPOSITION[maxLineLength_ + 1 + 1]); + positions = std::make_unique(maxLineLength_ + 1 + 1); maxLineLength = maxLineLength_; } } @@ -126,15 +126,15 @@ bool LineLayout::InLine(int offset, int line) const { void LineLayout::SetLineStart(int line, int start) { if ((line >= lenLineStarts) && (line != 0)) { - int newMaxLines = line + 20; - int *newLineStarts = new int[newMaxLines]; + const int newMaxLines = line + 20; + std::unique_ptr newLineStarts = std::make_unique(newMaxLines); for (int i = 0; i < newMaxLines; i++) { if (i < lenLineStarts) newLineStarts[i] = lineStarts[i]; else newLineStarts[i] = 0; } - lineStarts.reset(newLineStarts); + lineStarts = std::move(newLineStarts); lenLineStarts = newMaxLines; } lineStarts[line] = start; @@ -339,7 +339,7 @@ LineLayout *LineLayoutCache::Retrieve(Sci::Line lineNumber, Sci::Line lineCaret, } } if (!cache[pos]) { - cache[pos].reset(new LineLayout(maxChars)); + cache[pos] = std::make_unique(maxChars); } cache[pos]->lineNumber = lineNumber; cache[pos]->inCache = true; @@ -557,7 +557,7 @@ PositionCacheEntry::PositionCacheEntry(const PositionCacheEntry &other) : styleNumber(other.styleNumber), len(other.styleNumber), clock(other.styleNumber), positions(nullptr) { if (other.positions) { const size_t lenData = len + (len / sizeof(XYPOSITION)) + 1; - positions.reset(new XYPOSITION[lenData]); + positions = std::make_unique(lenData); memcpy(positions.get(), other.positions.get(), lenData * sizeof(XYPOSITION)); } } @@ -569,7 +569,7 @@ void PositionCacheEntry::Set(unsigned int styleNumber_, const char *s_, len = len_; clock = clock_; if (s_ && positions_) { - positions.reset(new XYPOSITION[len + (len / sizeof(XYPOSITION)) + 1]); + positions = std::make_unique(len + (len / sizeof(XYPOSITION)) + 1); for (unsigned int i=0; i::RemoveRunIfSameAsPrevious(DISTANCE run) { template RunStyles::RunStyles() { - starts.reset(new Partitioning(8)); - styles.reset(new SplitVector