diff options
| -rw-r--r-- | src/Document.cxx | 79 | ||||
| -rw-r--r-- | src/Document.h | 14 | 
2 files changed, 35 insertions, 58 deletions
| 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 <string>  #include <vector> +#include <algorithm>  #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<WatcherWithUserData>::iterator it = watchers.begin(); it != watchers.end(); ++it) { +		it->watcher->NotifyDeleted(this, it->userData);  	} -	delete []watchers;  	for (int j=0; j<ldSize; j++) {  		delete perLineData[j];  		perLineData[j] = 0;  	} -	watchers = 0; -	lenWatchers = 0;  	delete regex;  	regex = 0;  	delete pli; @@ -309,9 +305,9 @@ int SCI_METHOD Document::LineEnd(int line) const {  }  void SCI_METHOD Document::SetErrorStatus(int status) { -	// Tell the watchers the lexer has changed. -	for (int i = 0; i < lenWatchers; i++) { -		watchers[i].watcher->NotifyErrorOccurred(this, watchers[i].userData, status); +	// Tell the watchers an error has occurred. +	for (std::vector<WatcherWithUserData>::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<WatcherWithUserData>::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<WatcherWithUserData>::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<WatcherWithUserData>::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<WatcherWithUserData>::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<WatcherWithUserData>::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<WatcherWithUserData>::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<WatcherWithUserData>::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<WatcherWithUserData> watchers;  	// ldSize is not real data - it is for dimensions and loops  	enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize }; | 
