All pastes #1823625 Raw Edit

adf88

public diff v1 · immutable
#1823625 ·published 2010-03-05 10:26 UTC
rendered paste body
-static uint _total_news = 0;                ///< current number of news items-static NewsItem *_oldest_news = NULL;       ///< head of news items queue-static NewsItem *_latest_news = NULL;       ///< tail of news items queue+static NewsList _news;                  ///< news items queue in ascending order - oldest news are on the front and latest on the back===================================================================-	for (NewsItem *ni = _oldest_news; ni != NULL; ) {-		NewsItem *next = ni->next;-		delete ni;-		ni = next;-	}--	_total_news = 0;-	_oldest_news = NULL;-	_latest_news = NULL;+	DeleteAllLinkedListItems(&_news);===================================================================-	if (_total_news++ == 0) {-		assert(_oldest_news == NULL);-		_oldest_news = ni;-		ni->prev = NULL;-	} else {-		assert(_latest_news->next == NULL);-		_latest_news->next = ni;-		ni->prev = _latest_news;-	}+	_news.PushBack(ni);-	ni->next = NULL;-	_latest_news = ni;-===================================================================--	if (ni->prev != NULL) {-		ni->prev->next = ni->next;-	} else {-		assert(_oldest_news == ni);-		_oldest_news = ni->next;-	}--	if (ni->next != NULL) {-		ni->next->prev = ni->prev;-	} else {-		assert(_latest_news == ni);-		_latest_news = ni->prev;-	}--	if (_current_news == ni) _current_news = ni->prev;-	_total_news--;+	_news.Remove(ni);+	if (_current_news == ni) _current_news = ni->Prev();===================================================================-void DeleteVehicleNews(VehicleID vid, StringID news)-{-	NewsItem *ni = _oldest_news;--	while (ni != NULL) {-		NewsItem *next = ni->next;-		if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&-				(news == INVALID_STRING_ID || ni->string_id == news)) {- 			DeleteNewsItem(ni);- 		}-		ni = next;- 	}-}--void DeleteStationNews(StationID sid)-{-	NewsItem *ni = _oldest_news;--	while (ni != NULL) {-		NewsItem *next = ni->next;-		if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {-			DeleteNewsItem(ni);-		}-		ni = next;-	}-}--void DeleteIndustryNews(IndustryID iid)-{-	NewsItem *ni = _oldest_news;--	while (ni != NULL) {-		NewsItem *next = ni->next;-		if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {-			DeleteNewsItem(ni);-		}-		ni = next;-	}-}+template <NewsReferenceType Tnews_reference_type>+struct DeleteNewsItemIfRefMatch {+	uint32 ref;+	DeleteNewsItemIfRefMatch(uint32 ref) : ref(ref) { }++	void operator () (NewsItem *ni)+	{+		if ((ni->reftype1 == Tnews_reference_type && ni->ref1 == ref) ||+				(ni->reftype2 == Tnews_reference_type && ni->ref2 == ref)) {+			DeleteNewsItem(ni);+		}+	}+};++template <NewsReferenceType Tnews_reference_type>+struct DeleteNewsItemIfRefAndStringMatch : DeleteNewsItemIfRefMatch<Tnews_reference_type> {+	StringID string_id;++	DeleteNewsItemIfRefAndStringMatch(uint32 ref, StringID string_id) : DeleteNewsItemIfRefMatch<Tnews_reference_type>(ref), string_id(string_id) { }++	void operator () (NewsItem *ni)+	{+		if (ni->string_id == string_id) static_cast<DeleteNewsItemIfRefMatch<Tnews_reference_type>&>(*this)(ni);+	}+};++void DeleteVehicleNews(VehicleID vid, StringID news)+{+	if (news == INVALID_STRING_ID) {+		ForAllLinkedListItems(&_news, DeleteNewsItemIfRefMatch<NR_VEHICLE>(vid));+	} else {+		ForAllLinkedListItems(&_news, DeleteNewsItemIfRefAndStringMatch<NR_VEHICLE>(vid, news));+	}+}++void DeleteStationNews(StationID sid) { ForAllLinkedListItems(&_news, DeleteNewsItemIfRefMatch<NR_STATION>(sid)); }++void DeleteIndustryNews(IndustryID iid) { ForAllLinkedListItems(&_news, DeleteNewsItemIfRefMatch<NR_INDUSTRY>(iid)); }===================================================================-		AllocCache *ac = (AllocCache *)this->data[index];-		ac->next = this->alloc_cache;-		this->alloc_cache = ac;+		this->alloc_cache.PushFront((AllocCacheItem*)this->data[index]);===================================================================-	if (Tcache) {-		while (this->alloc_cache != NULL) {-			AllocCache *ac = this->alloc_cache;-			this->alloc_cache = ac->next;-			free(ac);-		}-	}+	if (Tcache) FreeAllLinkedListItems(&this->alloc_cache);===================================================================-/**- * Constructor container baseclass.- * @param tp Type of the container.- */-NWidgetContainer::NWidgetContainer(WidgetType tp) : NWidgetBase(tp)-{-	this->head = NULL;-	this->tail = NULL;-}-===================================================================-	while (this->head != NULL) {-		NWidgetBase *wid = this->head->next;-		delete this->head;-		this->head = wid;-	}-	this->tail = NULL;+	DeleteAllLinkedListItems(&this->children);===================================================================-	assert(wid->next == NULL && wid->prev == NULL);--	if (this->head == NULL) {-		this->head = wid;-		this->tail = wid;-	} else {-		assert(this->tail != NULL);-		assert(this->tail->next == NULL);--		this->tail->next = wid;-		wid->prev = this->tail;-		this->tail = wid;-	}+	this->children.PushBack(wid);===================================================================-	NWidgetBase *next;    ///< Pointer to next widget in container. Managed by parent container widget.-	NWidgetBase *prev;    ///< Pointer to previous widget in container. Managed by parent container widget.===================================================================-	NWidgetBase *head; ///< Pointer to first widget in container.-	NWidgetBase *tail; ///< Pointer to last widget in container.+	WidgetsList children; // List of widgets in container.===================================================================-/**- * Perhaps ugly macro, but this saves us the trouble of writing the same function- * twice, just with different variables. Yes, templates would be handy. It was- * either this define or an even more ugly void* magic function- */-#define IConsoleAddSorted(_base, item_new, IConsoleType, type)                 \-{                                                                              \-	IConsoleType *item, *item_before;                                            \-	/* first command */                                                          \-	if (_base == NULL) {                                                         \-		_base = item_new;                                                          \-		return;                                                                    \-	}                                                                            \-                                                                               \-	item_before = NULL;                                                          \-	item = _base;                                                                \-                                                                               \-	/* BEGIN - Alphabetically insert the commands into the linked list */        \-	while (item != NULL) {                                                       \-		int i = strcmp(item->name, item_new->name);                                \-		if (i == 0) {                                                              \-			IConsoleError(type " with this name already exists; insertion aborted"); \-			free(item_new);                                                          \-			return;                                                                  \-		}                                                                          \-                                                                               \-		if (i > 0) break; /* insert at this position */                            \-                                                                               \-		item_before = item;                                                        \-		item = item->next;                                                         \-	}                                                                            \-                                                                               \-	if (item_before == NULL) {                                                   \-		_base = item_new;                                                          \-	} else {                                                                     \-		item_before->next = item_new;                                              \-	}                                                                            \-                                                                               \-	item_new->next = item;                                                       \-	/* END - Alphabetical insert */                                              \+template <class Tconsole>+void IConsoleAddSorted(SmallLinkedStack<Tconsole> *list, Tconsole *item_new, const char *type)+{+	Tconsole *item, *item_before;++	item_before = NULL;+	item = list->Front();++	/* BEGIN - Alphabetically insert the commands into the linked list */+	while (item != NULL) {+		int i = strcmp(item->name, item_new->name);+		if (i == 0) {+			char message[80];+			snprintf(message, sizeof(message), "%s with this name already exists; insertion aborted", type);+			IConsoleError(message);+			free(item_new);+			return;+		}++		if (i > 0) break; /* insert at this position */++		item_before = item;+		item = item->next;+	}++	if (item_before == NULL) {+		list->PushFront(item_new);+	} else {+		list->InsertAfter(item_before, item_new);+	}+	/* END - Alphabetical insert */+}