summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2008-05-07 18:31:29 +0000
committersmatz <smatz@openttd.org>2008-05-07 18:31:29 +0000
commit3445b8054c81e12dc0d0078135a73eafe10a309f (patch)
treeea776ead1303bca953cbbccb0a0e78e08c7eaa45 /src
parent220bc49731e8f32cf92f31defab1bd5e313f7dc7 (diff)
downloadopenttd-3445b8054c81e12dc0d0078135a73eafe10a309f.tar.xz
(svn r12995) -Codechange: use std::vector for EngineList instead of C/C++ wrapper for CBlobT
Diffstat (limited to 'src')
-rw-r--r--src/autoreplace_gui.cpp26
-rw-r--r--src/build_vehicle_gui.cpp30
-rw-r--r--src/engine.cpp22
-rw-r--r--src/engine_func.h9
-rw-r--r--src/engine_type.h4
-rw-r--r--src/helpers.cpp94
-rw-r--r--src/misc/blob.hpp5
7 files changed, 57 insertions, 133 deletions
diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp
index 1c7cfb000..0d1400c1c 100644
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -197,7 +197,7 @@ static void GenerateReplaceVehList(Window *w, bool draw_left)
byte i = draw_left ? 0 : 1;
EngineList *list = &WP(w, replaceveh_d).list[i];
- EngList_RemoveAll(list);
+ list->clear();
const Engine *e;
FOR_ALL_ENGINES_OF_TYPE(e, type) {
@@ -220,7 +220,7 @@ static void GenerateReplaceVehList(Window *w, bool draw_left)
if (eid == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
}
- EngList_Add(list, eid);
+ list->push_back(eid);
if (eid == WP(w, replaceveh_d).sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
}
WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
@@ -237,8 +237,8 @@ static void GenerateLists(Window *w)
if (WP(w, replaceveh_d).update_left == true) {
/* We need to rebuild the left list */
GenerateReplaceVehList(w, true);
- SetVScrollCount(w, EngList_Count(&WP(w, replaceveh_d).list[0]));
- if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[0]) != 0) {
+ SetVScrollCount(w, WP(w, replaceveh_d).list[0].size());
+ if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE && WP(w, replaceveh_d).list[0].size() != 0) {
WP(w, replaceveh_d).sel_engine[0] = WP(w, replaceveh_d).list[0][0];
}
}
@@ -247,12 +247,12 @@ static void GenerateLists(Window *w)
/* Either we got a request to rebuild the right list or the left list selected a different engine */
if (WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE) {
/* Always empty the right list when nothing is selected in the left list */
- EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
+ WP(w, replaceveh_d).list[1].clear();
WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
} else {
GenerateReplaceVehList(w, false);
- SetVScroll2Count(w, EngList_Count(&WP(w, replaceveh_d).list[1]));
- if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[1] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[1]) != 0) {
+ SetVScroll2Count(w, WP(w, replaceveh_d).list[1].size());
+ if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[1] == INVALID_ENGINE && WP(w, replaceveh_d).list[1].size() != 0) {
WP(w, replaceveh_d).sel_engine[1] = WP(w, replaceveh_d).list[1][0];
}
}
@@ -279,8 +279,8 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
switch (e->event) {
case WE_CREATE:
WP(w, replaceveh_d).wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool)
- EngList_Create(&WP(w, replaceveh_d).list[0]);
- EngList_Create(&WP(w, replaceveh_d).list[1]);
+ new (&WP(w, replaceveh_d).list[0]) EngineList();
+ new (&WP(w, replaceveh_d).list[1]) EngineList();
WP(w, replaceveh_d).update_left = true;
WP(w, replaceveh_d).update_right = true;
WP(w, replaceveh_d).init_lists = true;
@@ -357,7 +357,7 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
uint widget = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
EngineID start = i == 0 ? w->vscroll.pos : w->vscroll2.pos; // what is the offset for the start (scrolling)
- EngineID end = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list));
+ EngineID end = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, list.size());
/* Do the actual drawing */
DrawEngineList((VehicleType)w->window_number, w->widget[widget].left + 2, w->widget[widget].top + 1, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0 ? w->widget[RVW_WIDGET_LEFT_MATRIX].right - 2 : 0, selected_group);
@@ -411,7 +411,7 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
uint16 click_scroll_pos = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.pos : w->vscroll2.pos;
uint16 click_scroll_cap = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.cap : w->vscroll2.cap;
byte click_side = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
- uint16 engine_count = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
+ uint16 engine_count = WP(w, replaceveh_d).list[click_side].size();
if (i < click_scroll_cap) {
i += click_scroll_pos;
@@ -476,8 +476,8 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
break;
case WE_DESTROY:
- EngList_RemoveAll(&WP(w, replaceveh_d).list[0]);
- EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
+ WP(w, replaceveh_d).list[0].~EngineList(); // call destructor explicitly
+ WP(w, replaceveh_d).list[1].~EngineList();
break;
}
}
diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp
index 66aeffeac..d96415ac3 100644
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -794,7 +794,7 @@ static void GenerateBuildTrainList(Window *w)
bv->filter.railtype = (w->window_number <= VEH_END) ? RAILTYPE_END : GetRailType(w->window_number);
- EngList_RemoveAll(&bv->eng_list);
+ bv->eng_list.clear();
/* Make list of all available train engines and wagons.
* Also check to see if the previously selected engine is still available,
@@ -808,7 +808,7 @@ static void GenerateBuildTrainList(Window *w)
if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, bv->filter.railtype)) continue;
if (!IsEngineBuildable(eid, VEH_TRAIN, _local_player)) continue;
- EngList_Add(&bv->eng_list, eid);
+ bv->eng_list.push_back(eid);
if (rvi->railveh_type != RAILVEH_WAGON) {
num_engines++;
} else {
@@ -838,14 +838,14 @@ static void GenerateBuildRoadVehList(Window *w)
EngineID sel_id = INVALID_ENGINE;
buildvehicle_d *bv = &WP(w, buildvehicle_d);
- EngList_RemoveAll(&bv->eng_list);
+ bv->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(bv->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
- EngList_Add(&bv->eng_list, eid);
+ bv->eng_list.push_back(eid);
if (eid == bv->sel_engine) sel_id = eid;
}
@@ -858,13 +858,13 @@ static void GenerateBuildShipList(Window *w)
EngineID sel_id = INVALID_ENGINE;
buildvehicle_d *bv = &WP(w, buildvehicle_d);
- EngList_RemoveAll(&bv->eng_list);
+ bv->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;
- EngList_Add(&bv->eng_list, eid);
+ bv->eng_list.push_back(eid);
if (eid == bv->sel_engine) sel_id = eid;
}
@@ -877,7 +877,7 @@ static void GenerateBuildAircraftList(Window *w)
EngineID sel_id = INVALID_ENGINE;
buildvehicle_d *bv = &WP(w, buildvehicle_d);
- EngList_RemoveAll(&bv->eng_list);
+ bv->eng_list.clear();
/* Make list of all available planes.
* Also check to see if the previously selected plane is still available,
@@ -890,7 +890,7 @@ static void GenerateBuildAircraftList(Window *w)
/* First VEH_END window_numbers are fake to allow a window open for all different types at once */
if (w->window_number > VEH_END && !CanAircraftUseStation(eid, w->window_number)) continue;
- EngList_Add(&bv->eng_list, eid);
+ bv->eng_list.push_back(eid);
if (eid == bv->sel_engine) sel_id = eid;
}
@@ -947,7 +947,7 @@ void DrawEngineList(VehicleType type, int x, int y, const EngineList eng_list, u
byte x_offset = 0;
byte y_offset = 0;
- assert(max <= EngList_Count(&eng_list));
+ assert(max <= eng_list.size());
switch (type) {
case VEH_TRAIN:
@@ -992,11 +992,11 @@ void DrawEngineList(VehicleType type, int x, int y, const EngineList eng_list, u
static void DrawBuildVehicleWindow(Window *w)
{
const buildvehicle_d *bv = &WP(w, buildvehicle_d);
- uint max = min(w->vscroll.pos + w->vscroll.cap, EngList_Count(&bv->eng_list));
+ uint max = min(w->vscroll.pos + w->vscroll.cap, bv->eng_list.size());
w->SetWidgetDisabledState(BUILD_VEHICLE_WIDGET_BUILD, w->window_number <= VEH_END);
- SetVScrollCount(w, EngList_Count(&bv->eng_list));
+ SetVScrollCount(w, bv->eng_list.size());
SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); // This should only affect rail vehicles
/* Set text of sort by dropdown */
@@ -1034,7 +1034,7 @@ static void BuildVehicleClickEvent(Window *w, WindowEvent *e)
case BUILD_VEHICLE_WIDGET_LIST: {
uint i = (e->we.click.pt.y - w->widget[BUILD_VEHICLE_WIDGET_LIST].top) / GetVehicleListHeight(bv->vehicle_type) + w->vscroll.pos;
- uint num_items = EngList_Count(&bv->eng_list);
+ uint num_items = bv->eng_list.size();
bv->sel_engine = (i < num_items) ? bv->eng_list[i] : INVALID_ENGINE;
w->SetDirty();
break;
@@ -1112,7 +1112,7 @@ static void NewVehicleWndProc(Window *w, WindowEvent *e)
break;
case WE_DESTROY:
- EngList_Destroy(&bv->eng_list);
+ bv->eng_list.~EngineList(); // call destructor explicitly
break;
case WE_PAINT:
@@ -1199,7 +1199,7 @@ void ShowBuildVehicleWindow(TileIndex tile, VehicleType type)
w->caption_color = (tile != 0) ? GetTileOwner(tile) : _local_player;
bv = &WP(w, buildvehicle_d);
- EngList_Create(&bv->eng_list);
+ new (&bv->eng_list) EngineList();
bv->sel_engine = INVALID_ENGINE;
bv->regenerate_list = false;
@@ -1226,5 +1226,5 @@ void ShowBuildVehicleWindow(TileIndex tile, VehicleType type)
GenerateBuildList(w); // 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 (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0];
+ if (bv->eng_list.size() > 0) bv->sel_engine = bv->eng_list[0];
}
diff --git a/src/engine.cpp b/src/engine.cpp
index 3d9df73f3..c28b096cb 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -114,6 +114,28 @@ Engine::~Engine()
free(this->name);
}
+/** Sort all items using qsort() and given 'CompareItems' function
+ * @param el list to be sorted
+ * @param compare function for evaluation of the quicksort
+ */
+void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
+{
+ qsort(&((*el)[0]), el->size(), sizeof(EngineID), compare);
+}
+
+/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
+ * @param el list to be sorted
+ * @param compare function for evaluation of the quicksort
+ * @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)
+{
+ assert(begin <= (uint)el->size());
+ assert(begin + num_items <= (uint)el->size());
+ qsort(&((*el)[begin]), num_items, sizeof(EngineID), compare);
+}
+
void SetupEngines()
{
_Engine_pool.CleanPool();
diff --git a/src/engine_func.h b/src/engine_func.h
index 56108e1b5..37863a964 100644
--- a/src/engine_func.h
+++ b/src/engine_func.h
@@ -28,15 +28,8 @@ void DeleteCustomEngineNames();
bool IsEngineBuildable(EngineID engine, VehicleType type, PlayerID player);
CargoID GetEngineCargoType(EngineID engine);
-/* Engine list manipulators - current implementation is only C wrapper of CBlobT<EngineID> class (helpers.cpp) */
-void EngList_Create(EngineList *el); ///< Creates engine list
-void EngList_Destroy(EngineList *el); ///< Deallocate and destroy engine list
-uint EngList_Count(const EngineList *el); ///< Returns number of items in the engine list
-void EngList_Add(EngineList *el, EngineID eid); ///< Append one item at the end of engine list
-EngineID* EngList_Items(EngineList *el); ///< Returns engine list items as C array
-void EngList_RemoveAll(EngineList *el); ///< Removes all items from engine list
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_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
#endif /* ENGINE_H */
diff --git a/src/engine_type.h b/src/engine_type.h
index 03bee6fe4..affa722ce 100644
--- a/src/engine_type.h
+++ b/src/engine_type.h
@@ -14,9 +14,11 @@
#include "player_type.h"
#include "strings_type.h"
+#include <vector>
+
typedef uint16 EngineID;
typedef uint16 EngineRenewID;
-typedef EngineID *EngineList; ///< engine list type placeholder acceptable for C code (see helpers.cpp)
+typedef std::vector<EngineID> EngineList; ///< engine list type
struct Engine;
diff --git a/src/helpers.cpp b/src/helpers.cpp
deleted file mode 100644
index af8dd4fbe..000000000
--- a/src/helpers.cpp
+++ /dev/null
@@ -1,94 +0,0 @@
-/* $Id$ */
-
-/** @file helpers.cpp Some not-so-generic helper functions. */
-
-#include "stdafx.h"
-
-#include "openttd.h"
-#include "engine_func.h"
-
-#include <new>
-#include "misc/blob.hpp"
-
-/* Engine list manipulators - current implementation is only C wrapper around CBlobT<EngineID> (see yapf/blob.hpp) */
-
-/* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT<EngineID>* always when we are called from C */
-#define B (*(CBlobT<EngineID>*)el)
-
-/** Create Engine List (and initialize it to empty)
- * @param el list to be created
- */
-void EngList_Create(EngineList *el)
-{
- /* call CBlobT constructor explicitly */
- new (&B) CBlobT<EngineID>();
-}
-
-/** Destroy Engine List (and free its contents)
- * @param el list to be destroyed
- */
-void EngList_Destroy(EngineList *el)
-{
- /* call CBlobT destructor explicitly */
- B.~CBlobT<EngineID>();
-}
-
-/** Return number of items stored in the Engine List
- * @param el list for count inquiry
- * @return the desired count
- */
-uint EngList_Count(const EngineList *el)
-{
- return B.Size();
-}
-
-/** Add new item at the end of Engine List
- * @param el list o which to add an engine
- * @param eid engine to add to the list
- */
-void EngList_Add(EngineList *el, EngineID eid)
-{
- B.Append(eid);
-}
-
-/** Return pointer to the items array held by Engine List
- * @param el list from which the array pointer has to be returned
- * @return the pointer required
- */
-EngineID* EngList_Items(EngineList *el)
-{
- return B.Data();
-}
-
-/** Clear the Engine List (by invalidating all its items == reseting item count to zero)
- * @param el list to be cleared
- */
-void EngList_RemoveAll(EngineList *el)
-{
- B.Clear();
-}
-
-/** Sort all items using qsort() and given 'CompareItems' function
- * @param el list to be sorted
- * @param compare function for evaluation of the quicksort
- */
-void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
-{
- qsort(B.Data(), B.Size(), sizeof(**el), compare);
-}
-
-/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
- * @param el list to be sorted
- * @param compare function for evaluation of the quicksort
- * @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)
-{
- assert(begin <= (uint)B.Size());
- assert(begin + num_items <= (uint)B.Size());
- qsort(B.Data() + begin, num_items, sizeof(**el), compare);
-}
-
-#undef B
-
diff --git a/src/misc/blob.hpp b/src/misc/blob.hpp
index 441c8a027..c96902997 100644
--- a/src/misc/blob.hpp
+++ b/src/misc/blob.hpp
@@ -373,7 +373,7 @@ public:
}
/** Check the validity of item index (only in debug mode) */
- FORCEINLINE void CheckIdx(bsize_t idx)
+ FORCEINLINE void CheckIdx(bsize_t idx) const
{
assert(idx >= 0); assert(idx < Size());
}
@@ -400,7 +400,8 @@ public:
/** Return pointer to the idx-th data item - const version */
FORCEINLINE const Titem* Data(bsize_t idx) const
{
- CheckIdx(idx); return (Data() + idx);
+ CheckIdx(idx);
+ return (Data() + idx);
}
/** Return number of items in the Blob */