From cf77a05fe164e7d8dc9b41d8261f38d75094891e Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Sun, 28 Apr 2013 09:53:27 +1000 Subject: Use std::vector for list of watchers instead of manual management. --- src/Document.cxx | 79 ++++++++++++++++++++------------------------------------ src/Document.h | 14 +++++----- 2 files changed, 35 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/Document.cxx b/src/Document.cxx index 068d406cf..10df07663 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -13,6 +13,7 @@ #include #include +#include #include "Platform.h" @@ -102,8 +103,6 @@ Document::Document() { useTabs = true; tabIndents = true; backspaceUnindents = false; - watchers = 0; - lenWatchers = 0; matchesValid = false; regex = 0; @@ -122,16 +121,13 @@ Document::Document() { } Document::~Document() { - for (int i = 0; i < lenWatchers; i++) { - watchers[i].watcher->NotifyDeleted(this, watchers[i].userData); + for (std::vector::iterator it = watchers.begin(); it != watchers.end(); ++it) { + it->watcher->NotifyDeleted(this, it->userData); } - delete []watchers; for (int j=0; jNotifyErrorOccurred(this, watchers[i].userData, status); + // Tell the watchers an error has occurred. + for (std::vector::iterator it = watchers.begin(); it != watchers.end(); ++it) { + it->watcher->NotifyErrorOccurred(this, it->userData, status); } } @@ -1739,8 +1735,9 @@ void Document::EnsureStyledTo(int pos) { pli->Colourise(endStyledTo, pos); } else { // Ask the watchers to style, and stop as soon as one responds. - for (int i = 0; pos > GetEndStyled() && i < lenWatchers; i++) { - watchers[i].watcher->NotifyStyleNeeded(this, watchers[i].userData, pos); + for (std::vector::iterator it = watchers.begin(); + (pos > GetEndStyled()) && (it != watchers.end()); ++it) { + it->watcher->NotifyStyleNeeded(this, it->userData, pos); } } } @@ -1748,8 +1745,8 @@ void Document::EnsureStyledTo(int pos) { void Document::LexerChanged() { // Tell the watchers the lexer has changed. - for (int i = 0; i < lenWatchers; i++) { - watchers[i].watcher->NotifyLexerChanged(this, watchers[i].userData); + for (std::vector::iterator it = watchers.begin(); it != watchers.end(); ++it) { + it->watcher->NotifyLexerChanged(this, it->userData); } } @@ -1859,54 +1856,34 @@ void SCI_METHOD Document::DecorationFillRange(int position, int value, int fillL } bool Document::AddWatcher(DocWatcher *watcher, void *userData) { - for (int i = 0; i < lenWatchers; i++) { - if ((watchers[i].watcher == watcher) && - (watchers[i].userData == userData)) - return false; - } - WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers + 1]; - for (int j = 0; j < lenWatchers; j++) - pwNew[j] = watchers[j]; - pwNew[lenWatchers].watcher = watcher; - pwNew[lenWatchers].userData = userData; - delete []watchers; - watchers = pwNew; - lenWatchers++; + WatcherWithUserData wwud(watcher, userData); + std::vector::iterator it = + std::find(watchers.begin(), watchers.end(), wwud); + if (it != watchers.end()) + return false; + watchers.push_back(wwud); return true; } bool Document::RemoveWatcher(DocWatcher *watcher, void *userData) { - for (int i = 0; i < lenWatchers; i++) { - if ((watchers[i].watcher == watcher) && - (watchers[i].userData == userData)) { - if (lenWatchers == 1) { - delete []watchers; - watchers = 0; - lenWatchers = 0; - } else { - WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers]; - for (int j = 0; j < lenWatchers - 1; j++) { - pwNew[j] = (j < i) ? watchers[j] : watchers[j + 1]; - } - delete []watchers; - watchers = pwNew; - lenWatchers--; - } - return true; - } + std::vector::iterator it = + std::find(watchers.begin(), watchers.end(), WatcherWithUserData(watcher, userData)); + if (it != watchers.end()) { + watchers.erase(it); + return true; } return false; } void Document::NotifyModifyAttempt() { - for (int i = 0; i < lenWatchers; i++) { - watchers[i].watcher->NotifyModifyAttempt(this, watchers[i].userData); + for (std::vector::iterator it = watchers.begin(); it != watchers.end(); ++it) { + it->watcher->NotifyModifyAttempt(this, it->userData); } } void Document::NotifySavePoint(bool atSavePoint) { - for (int i = 0; i < lenWatchers; i++) { - watchers[i].watcher->NotifySavePoint(this, watchers[i].userData, atSavePoint); + for (std::vector::iterator it = watchers.begin(); it != watchers.end(); ++it) { + it->watcher->NotifySavePoint(this, it->userData, atSavePoint); } } @@ -1916,8 +1893,8 @@ void Document::NotifyModified(DocModification mh) { } else if (mh.modificationType & SC_MOD_DELETETEXT) { decorations.DeleteRange(mh.position, mh.length); } - for (int i = 0; i < lenWatchers; i++) { - watchers[i].watcher->NotifyModified(this, mh, watchers[i].userData); + for (std::vector::iterator it = watchers.begin(); it != watchers.end(); ++it) { + it->watcher->NotifyModified(this, mh, it->userData); } } diff --git a/src/Document.h b/src/Document.h index 285134503..cb2f00d7a 100644 --- a/src/Document.h +++ b/src/Document.h @@ -198,13 +198,14 @@ class Document : PerLine, public IDocumentWithLineEnd, public ILoader { public: /** Used to pair watcher pointer with user data. */ - class WatcherWithUserData { - public: + struct WatcherWithUserData { DocWatcher *watcher; void *userData; - WatcherWithUserData() { - watcher = 0; - userData = 0; + WatcherWithUserData(DocWatcher *watcher_=0, void *userData_=0) : + watcher(watcher_), userData(userData_) { + } + bool operator==(const WatcherWithUserData &other) { + return (watcher == other.watcher) && (userData == other.userData); } }; @@ -220,8 +221,7 @@ private: int enteredStyling; int enteredReadOnlyCount; - WatcherWithUserData *watchers; - int lenWatchers; + std::vector watchers; // ldSize is not real data - it is for dimensions and loops enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize }; -- cgit v1.2.3