# HG changeset patch # User alberth # Date 2016-09-04 12:50:22 # Node ID b6a9d97303fc95999cc898c68a6098a623d826dc # Parent 3130481e7a321f14fea3408dd1de2e26a781adc8 (svn r27641) -Codechange: Fold the _fios_items file list vector into its own class. diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -378,7 +378,7 @@ DEF_CONSOLE_CMD(ConLoad) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -402,7 +402,7 @@ DEF_CONSOLE_CMD(ConRemove) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -421,7 +421,7 @@ DEF_CONSOLE_CMD(ConListFiles) IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -448,7 +448,7 @@ DEF_CONSOLE_CMD(ConChangeDirectory) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -463,7 +463,7 @@ DEF_CONSOLE_CMD(ConPrintWorkingDirectory /* XXX - Workaround for broken file handling */ FiosGetSavegameList(SLD_LOAD_GAME); - FiosFreeSavegameList(); + _fios_items.Clear(); FiosGetDescText(&path, NULL); IConsolePrint(CC_DEFAULT, path); diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp --- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -256,6 +256,8 @@ public: /** * Get the number of items in the list. + * + * @return The number of items in the list. */ inline uint Length() const { diff --git a/src/fios.cpp b/src/fios.cpp --- a/src/fios.cpp +++ b/src/fios.cpp @@ -29,7 +29,7 @@ #include "safeguards.h" /* Variables to display file lists */ -SmallVector _fios_items; +FileList _fios_items; static char *_fios_path; static const char *_fios_path_last; SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; @@ -64,11 +64,9 @@ int CDECL CompareFiosItems(const FiosIte return r; } -/** Free the list of savegames. */ -void FiosFreeSavegameList() +FileList::~FileList() { - _fios_items.Clear(); - _fios_items.Compact(); + this->Clear(); } /** @@ -336,7 +334,7 @@ static void FiosGetFileList(SaveLoadDial { SortingBits order = _savegame_sort_order; _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - QSortT(_fios_items.Begin(), _fios_items.Length(), CompareFiosItems); + QSortT(_fios_items.files.Begin(), _fios_items.files.Length(), CompareFiosItems); _savegame_sort_order = order; } diff --git a/src/fios.h b/src/fios.h --- a/src/fios.h +++ b/src/fios.h @@ -115,6 +115,94 @@ struct FiosItem { char name[MAX_PATH]; }; +/** List of file information. */ +class FileList { +public: + ~FileList(); + + /** + * Construct a new entry in the file list. + * @return Pointer to the new items to be initialized. + */ + inline FiosItem *Append() + { + return this->files.Append(); + } + + /** + * Get the number of files in the list. + * @return The number of files stored in the list. + */ + inline uint Length() const + { + return this->files.Length(); + } + + /** + * Get a pointer to the first file information. + * @return Address of the first file information. + */ + inline const FiosItem *Begin() const + { + return this->files.Begin(); + } + + /** + * Get a pointer behind the last file information. + * @return Address behind the last file information. + */ + inline const FiosItem *End() const + { + return this->files.End(); + } + + /** + * Get a pointer to the indicated file information. File information must exist. + * @return Address of the indicated existing file information. + */ + inline const FiosItem *Get(uint index) const + { + return this->files.Get(index); + } + + /** + * Get a pointer to the indicated file information. File information must exist. + * @return Address of the indicated existing file information. + */ + inline FiosItem *Get(uint index) + { + return this->files.Get(index); + } + + inline const FiosItem &operator[](uint index) const + { + return this->files[index]; + } + + /** + * Get a reference to the indicated file information. File information must exist. + * @return The requested file information. + */ + inline FiosItem &operator[](uint index) + { + return this->files[index]; + } + + /** Remove all items from the list. */ + inline void Clear() + { + this->files.Clear(); + } + + /** Compact the list down to the smallest block size boundary. */ + inline void Compact() + { + this->files.Compact(); + } + + SmallVector files; ///< The list of files. +}; + enum SortingBits { SORT_ASCENDING = 0, SORT_DESCENDING = 1, @@ -124,7 +212,7 @@ enum SortingBits { DECLARE_ENUM_AS_BIT_SET(SortingBits) /* Variables to display file lists */ -extern SmallVector _fios_items; +extern FileList _fios_items; extern SaveLoadDialogMode _saveload_mode; extern SortingBits _savegame_sort_order; @@ -134,7 +222,6 @@ void FiosGetSavegameList(SaveLoadDialogM void FiosGetScenarioList(SaveLoadDialogMode mode); void FiosGetHeightmapList(SaveLoadDialogMode mode); -void FiosFreeSavegameList(); const char *FiosBrowseTo(const FiosItem *item); StringID FiosGetDescText(const char **path, uint64 *total_free); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -196,7 +196,7 @@ const TextColour _fios_colours[] = { void BuildFileList(SaveLoadDialogMode mode) { _fios_path_changed = true; - FiosFreeSavegameList(); + _fios_items.Clear(); switch (mode) { case SLD_LOAD_SCENARIO: @@ -327,7 +327,7 @@ public: if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) { DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE); } - FiosFreeSavegameList(); + _fios_items.Clear(); } virtual void DrawWidget(const Rect &r, int widget) const