(svn r13314) -Codechange: Switch EngineList from std::vector to GUIList
authorpeter1138
Wed, 28 May 2008 17:29:27 +0000
changeset 10764 e46596b87826
parent 10763 4849b875130e
child 10765 34cd0fa4baed
(svn r13314) -Codechange: Switch EngineList from std::vector to GUIList
src/autoreplace_gui.cpp
src/build_vehicle_gui.cpp
src/engine_gui.cpp
src/engine_gui.h
--- a/src/autoreplace_gui.cpp	Wed May 28 17:08:45 2008 +0000
+++ b/src/autoreplace_gui.cpp	Wed May 28 17:29:27 2008 +0000
@@ -27,7 +27,7 @@
 #include "table/sprites.h"
 #include "table/strings.h"
 
-void DrawEngineList(VehicleType type, int x, int y, const EngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group);
+void DrawEngineList(VehicleType type, int x, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group);
 
 static const StringID _rail_types_list[] = {
 	STR_RAIL_VEHICLES,
@@ -143,7 +143,7 @@
 	EngineID sel_engine[2];
 	uint16 count[2];
 	bool wagon_btnstate; ///< true means engine is selected
-	EngineList list[2];
+	GUIEngineList list[2];
 	bool update_left;
 	bool update_right;
 	bool init_lists;
@@ -185,8 +185,8 @@
 		VehicleType type = (VehicleType)this->window_number;
 		byte i = draw_left ? 0 : 1;
 
-		EngineList *list = &this->list[i];
-		list->clear();
+		GUIEngineList *list = &this->list[i];
+		list->Clear();
 
 		const Engine *e;
 		FOR_ALL_ENGINES_OF_TYPE(e, type) {
@@ -209,7 +209,7 @@
 				if (eid == this->sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
 			}
 
-			list->push_back(eid);
+			*list->Append() = eid;
 			if (eid == this->sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
 		}
 		this->sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
@@ -224,8 +224,8 @@
 		if (this->update_left == true) {
 			/* We need to rebuild the left list */
 			GenerateReplaceVehList(this, true);
-			SetVScrollCount(this, this->list[0].size());
-			if (this->init_lists && this->sel_engine[0] == INVALID_ENGINE && this->list[0].size() != 0) {
+			SetVScrollCount(this, this->list[0].Length());
+			if (this->init_lists && this->sel_engine[0] == INVALID_ENGINE && this->list[0].Length() != 0) {
 				this->sel_engine[0] = this->list[0][0];
 			}
 		}
@@ -234,12 +234,12 @@
 			/* Either we got a request to rebuild the right list or the left list selected a different engine */
 			if (this->sel_engine[0] == INVALID_ENGINE) {
 				/* Always empty the right list when nothing is selected in the left list */
-				this->list[1].clear();
+				this->list[1].Clear();
 				this->sel_engine[1] = INVALID_ENGINE;
 			} else {
 				GenerateReplaceVehList(this, false);
-				SetVScroll2Count(this, this->list[1].size());
-				if (this->init_lists && this->sel_engine[1] == INVALID_ENGINE && this->list[1].size() != 0) {
+				SetVScroll2Count(this, this->list[1].Length());
+				if (this->init_lists && this->sel_engine[1] == INVALID_ENGINE && this->list[1].Length() != 0) {
 					this->sel_engine[1] = this->list[1][0];
 				}
 			}
@@ -379,9 +379,9 @@
 		/* Draw the lists */
 		for (byte i = 0; i < 2; i++) {
 			uint widget     = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
-			EngineList *list = &this->list[i]; // which list to draw
+			GUIEngineList *list = &this->list[i]; // which list to draw
 			EngineID start  = i == 0 ? this->vscroll.pos : this->vscroll2.pos; // what is the offset for the start (scrolling)
-			EngineID end    = min((i == 0 ? this->vscroll.cap : this->vscroll2.cap) + start, list->size());
+			EngineID end    = min((i == 0 ? this->vscroll.cap : this->vscroll2.cap) + start, list->Length());
 
 			/* Do the actual drawing */
 			DrawEngineList((VehicleType)this->window_number, this->widget[widget].left + 2, this->widget[widget].top + 1, list, start, end, this->sel_engine[i], i == 0 ? this->widget[RVW_WIDGET_LEFT_MATRIX].right - 2 : 0, selected_group);
@@ -435,7 +435,7 @@
 				uint16 click_scroll_pos = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.pos : this->vscroll2.pos;
 				uint16 click_scroll_cap = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.cap : this->vscroll2.cap;
 				byte click_side         = widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
-				size_t engine_count     = this->list[click_side].size();
+				size_t engine_count     = this->list[click_side].Length();
 
 				if (i < click_scroll_cap) {
 					i += click_scroll_pos;
--- a/src/build_vehicle_gui.cpp	Wed May 28 17:08:45 2008 +0000
+++ b/src/build_vehicle_gui.cpp	Wed May 28 17:29:27 2008 +0000
@@ -745,13 +745,13 @@
  * @param selected_id what engine to highlight as selected, if any
  * @param count_location Offset to print the engine count (used by autoreplace). 0 means it's off
  */
-void DrawEngineList(VehicleType type, int x, int y, const EngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group)
+void DrawEngineList(VehicleType type, int x, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group)
 {
 	byte step_size = GetVehicleListHeight(type);
 	byte x_offset = 0;
 	byte y_offset = 0;
 
-	assert(max <= eng_list->size());
+	assert(max <= eng_list->Length());
 
 	switch (type) {
 		case VEH_TRAIN:
@@ -806,7 +806,7 @@
 	bool regenerate_list;
 	EngineID sel_engine;
 	EngineID rename_engine;
-	EngineList eng_list;
+	GUIEngineList eng_list;
 
 	BuildVehicleWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window(desc, tile == 0 ? (int)type : tile)
 	{
@@ -848,7 +848,7 @@
 
 		this->GenerateBuildList(); // generate the list, since we need it in the next line
 		/* Select the first engine in the list as default when opening the window */
-		if (this->eng_list.size() > 0) this->sel_engine = this->eng_list[0];
+		if (this->eng_list.Length() > 0) this->sel_engine = this->eng_list[0];
 
 		this->FindWindowPlacementAndResize(desc);
 	}
@@ -906,7 +906,7 @@
 
 		this->filter.railtype = (this->window_number <= VEH_END) ? RAILTYPE_END : GetRailType(this->window_number);
 
-		this->eng_list.clear();
+		this->eng_list.Clear();
 
 		/* Make list of all available train engines and wagons.
 		* Also check to see if the previously selected engine is still available,
@@ -920,7 +920,8 @@
 			if (this->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, this->filter.railtype)) continue;
 			if (!IsEngineBuildable(eid, VEH_TRAIN, _local_player)) continue;
 
-			this->eng_list.push_back(eid);
+			*this->eng_list.Append() = eid;
+
 			if (rvi->railveh_type != RAILVEH_WAGON) {
 				num_engines++;
 			} else {
@@ -949,14 +950,14 @@
 	{
 		EngineID sel_id = INVALID_ENGINE;
 
-		this->eng_list.clear();
+		this->eng_list.Clear();
 
 		const Engine *e;
 		FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
 			EngineID eid = e->index;
 			if (!IsEngineBuildable(eid, VEH_ROAD, _local_player)) continue;
 			if (!HasBit(this->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
-			this->eng_list.push_back(eid);
+			*this->eng_list.Append() = eid;
 
 			if (eid == this->sel_engine) sel_id = eid;
 		}
@@ -967,13 +968,13 @@
 	void GenerateBuildShipList()
 	{
 		EngineID sel_id = INVALID_ENGINE;
-		this->eng_list.clear();
+		this->eng_list.Clear();
 
 		const Engine *e;
 		FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) {
 			EngineID eid = e->index;
 			if (!IsEngineBuildable(eid, VEH_SHIP, _local_player)) continue;
-			this->eng_list.push_back(eid);
+			*this->eng_list.Append() = eid;
 
 			if (eid == this->sel_engine) sel_id = eid;
 		}
@@ -985,7 +986,7 @@
 	{
 		EngineID sel_id = INVALID_ENGINE;
 
-		this->eng_list.clear();
+		this->eng_list.Clear();
 
 		/* Make list of all available planes.
 		* Also check to see if the previously selected plane is still available,
@@ -998,7 +999,7 @@
 			/* First VEH_END window_numbers are fake to allow a window open for all different types at once */
 			if (this->window_number > VEH_END && !CanAircraftUseStation(eid, this->window_number)) continue;
 
-			this->eng_list.push_back(eid);
+			*this->eng_list.Append() = eid;
 			if (eid == this->sel_engine) sel_id = eid;
 		}
 
@@ -1039,7 +1040,7 @@
 
 			case BUILD_VEHICLE_WIDGET_LIST: {
 				uint i = (pt.y - this->widget[BUILD_VEHICLE_WIDGET_LIST].top) / GetVehicleListHeight(this->vehicle_type) + this->vscroll.pos;
-				size_t num_items = this->eng_list.size();
+				size_t num_items = this->eng_list.Length();
 				this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
 				this->SetDirty();
 				break;
@@ -1105,11 +1106,11 @@
 			this->GenerateBuildList();
 		}
 
-		uint max = min(this->vscroll.pos + this->vscroll.cap, this->eng_list.size());
+		uint max = min(this->vscroll.pos + this->vscroll.cap, this->eng_list.Length());
 
 		this->SetWidgetDisabledState(BUILD_VEHICLE_WIDGET_BUILD, this->window_number <= VEH_END);
 
-		SetVScrollCount(this, this->eng_list.size());
+		SetVScrollCount(this, this->eng_list.Length());
 		SetDParam(0, this->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); // This should only affect rail vehicles
 
 		/* Set text of sort by dropdown */
--- a/src/engine_gui.cpp	Wed May 28 17:08:45 2008 +0000
+++ b/src/engine_gui.cpp	Wed May 28 17:29:27 2008 +0000
@@ -197,13 +197,13 @@
  * @param el list to be sorted
  * @param compare function for evaluation of the quicksort
  */
-void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
+void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
 {
-	size_t size = el->size();
+	uint size = el->Length();
 	/* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
 	 * generally, do not sort if there are less than 2 items */
 	if (size < 2) return;
-	qsort(&((*el)[0]), size, sizeof(EngineID), compare); // MorphOS doesn't know vector::at(int) ...
+	qsort(el->Begin(), size, sizeof(*el->Begin()), compare); // MorphOS doesn't know vector::at(int) ...
 }
 
 /** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
@@ -212,11 +212,11 @@
  * @param begin start of sorting
  * @param num_items count of items to be sorted
  */
-void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
+void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
 {
-	assert(begin <= (uint)el->size());
-	assert(begin + num_items <= (uint)el->size());
+	assert(begin < el->Length());
+	assert(begin + num_items <= el->Length());
 	if (num_items < 2) return;
-	qsort(&((*el)[begin]), num_items, sizeof(EngineID), compare);
+	qsort(el->Get(begin), num_items, sizeof(*el->Begin()), compare);
 }
 
--- a/src/engine_gui.h	Wed May 28 17:08:45 2008 +0000
+++ b/src/engine_gui.h	Wed May 28 17:29:27 2008 +0000
@@ -5,12 +5,12 @@
 #ifndef ENGINE_GUI_H
 #define ENGINE_GUI_H
 
-#include <vector>
+#include "sortlist_type.h"
 
-typedef std::vector<EngineID> EngineList;
+typedef GUIList<EngineID> GUIEngineList;
 
 typedef int CDECL EngList_SortTypeFunction(const void*, const void*); ///< argument type for EngList_Sort()
-void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare);  ///< qsort of the engine list
-void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items); ///< qsort of specified portion of the engine list
+void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare);  ///< qsort of the engine list
+void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items); ///< qsort of specified portion of the engine list
 
 #endif /* ENGINE_GUI_H */