From 3323402aaa0d02a87f32a9469ee4e2fdb579bbaa Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 18 May 2023 11:20:35 +0200 Subject: [PATCH] Codechange: rename smallvec_type to container_func and use only when needed --- src/animated_tile.cpp | 2 +- src/core/CMakeLists.txt | 2 +- .../{smallvec_type.hpp => container_func.hpp} | 37 +++++++++---------- src/core/pool_type.hpp | 1 - src/core/smallstack_type.hpp | 1 - src/economy.cpp | 1 + src/error_gui.cpp | 1 + src/game/game_text.hpp | 2 - src/gfx.cpp | 1 + src/gfx_layout.cpp | 1 + src/group_gui.cpp | 1 + src/language.h | 1 - src/music/midifile.cpp | 1 + src/music/midifile.hpp | 1 - src/music/win32_m.cpp | 1 + src/music_gui.cpp | 1 + src/network/network_content.h | 1 + src/newgrf.cpp | 1 + src/newgrf.h | 2 +- src/newgrf_commons.h | 1 - src/newgrf_debug.h | 1 - src/newgrf_engine.cpp | 1 + src/newgrf_railtype.cpp | 1 + src/newgrf_roadtype.cpp | 1 + src/newgrf_text.h | 1 - src/rail_cmd.cpp | 1 + src/road_cmd.cpp | 1 + src/saveload/animated_tile_sl.cpp | 1 - src/saveload/oldloader_sl.cpp | 1 - src/settings_func.h | 1 - src/settingsgen/settingsgen.cpp | 2 +- src/sortlist_type.h | 2 +- src/station_cmd.cpp | 1 + src/strgen/strgen.cpp | 1 + src/strgen/strgen_base.cpp | 1 + src/stringfilter_type.h | 1 - src/subsidy.cpp | 1 + src/texteff.cpp | 1 - src/vehicle.cpp | 1 + src/vehicle_base.h | 1 + src/vehicle_gui.cpp | 1 + src/vehicle_gui_base.h | 1 - src/vehiclelist.h | 1 - src/viewport_sprite_sorter.h | 1 - src/widgets/dropdown_type.h | 1 - src/window_gui.h | 1 - 46 files changed, 45 insertions(+), 43 deletions(-) rename src/core/{smallvec_type.hpp => container_func.hpp} (51%) diff --git a/src/animated_tile.cpp b/src/animated_tile.cpp index e31d1b8e03..0b5401564c 100644 --- a/src/animated_tile.cpp +++ b/src/animated_tile.cpp @@ -8,7 +8,7 @@ /** @file animated_tile.cpp Everything related to animated tiles. */ #include "stdafx.h" -#include "core/smallvec_type.hpp" +#include "core/container_func.hpp" #include "tile_cmd.h" #include "viewport_func.h" #include "framerate_type.h" diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index b0576299a2..afe1112316 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -23,7 +23,7 @@ add_files( random_func.cpp random_func.hpp smallstack_type.hpp - smallvec_type.hpp + container_func.hpp span_type.hpp strong_typedef_type.hpp ) diff --git a/src/core/smallvec_type.hpp b/src/core/container_func.hpp similarity index 51% rename from src/core/smallvec_type.hpp rename to src/core/container_func.hpp index 086e2b531f..5a713649aa 100644 --- a/src/core/smallvec_type.hpp +++ b/src/core/container_func.hpp @@ -5,46 +5,45 @@ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . */ -/** @file smallvec_type.hpp Simple vector class that allows allocating an item without the need to copy this->data needlessly. */ +/** @file container_func.hpp Some simple functions to help with accessing containers. */ -#ifndef SMALLVEC_TYPE_HPP -#define SMALLVEC_TYPE_HPP - -#include "mem_func.hpp" +#ifndef CONTAINER_FUNC_HPP +#define CONTAINER_FUNC_HPP /** - * Helper function to append an item to a vector if it is not already contained - * Consider using std::set, std::unordered_set or std::flat_set in new code + * Helper function to append an item to a container if it is not already contained. + * The container must have a \c emplace_back function. + * Consider using std::set, std::unordered_set or std::flat_set in new code. * - * @param vec A reference to the vector to be extended + * @param container A reference to the container to be extended * @param item Reference to the item to be copy-constructed if not found * * @return Whether the item was already present */ -template -inline bool include(std::vector& vec, const T &item) +template +inline bool include(Container &container, typename Container::const_reference &item) { - const bool is_member = std::find(vec.begin(), vec.end(), item) != vec.end(); - if (!is_member) vec.emplace_back(item); + const bool is_member = std::find(container.begin(), container.end(), item) != container.end(); + if (!is_member) container.emplace_back(item); return is_member; } /** * Helper function to get the index of an item - * Consider using std::set, std::unordered_set or std::flat_set in new code + * Consider using std::set, std::unordered_set or std::flat_set in new code. * - * @param vec A reference to the vector to be extended + * @param container A reference to the container to be searched. * @param item Reference to the item to be search for * * @return Index of element if found, otherwise -1 */ -template -int find_index(std::vector const& vec, T const& item) +template +int find_index(Container const &container, typename Container::const_reference item) { - auto const it = std::find(vec.begin(), vec.end(), item); - if (it != vec.end()) return it - vec.begin(); + auto const it = std::find(container.begin(), container.end(), item); + if (it != container.end()) return std::distance(container.begin(), it); return -1; } -#endif /* SMALLVEC_TYPE_HPP */ +#endif /* CONTAINER_FUNC_HPP */ diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index 20e8f8b29f..d325327521 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -10,7 +10,6 @@ #ifndef POOL_TYPE_HPP #define POOL_TYPE_HPP -#include "smallvec_type.hpp" #include "enum_type.hpp" /** Various types of a pool. */ diff --git a/src/core/smallstack_type.hpp b/src/core/smallstack_type.hpp index c05454b8ae..503d027bf3 100644 --- a/src/core/smallstack_type.hpp +++ b/src/core/smallstack_type.hpp @@ -10,7 +10,6 @@ #ifndef SMALLSTACK_TYPE_HPP #define SMALLSTACK_TYPE_HPP -#include "smallvec_type.hpp" #include /** diff --git a/src/economy.cpp b/src/economy.cpp index 644d6e023c..b4bc993f79 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -41,6 +41,7 @@ #include "economy_base.h" #include "core/pool_func.hpp" #include "core/backup_type.hpp" +#include "core/container_func.hpp" #include "cargo_type.h" #include "water.h" #include "game/game.hpp" diff --git a/src/error_gui.cpp b/src/error_gui.cpp index 117a9db8ee..ad0758fc24 100644 --- a/src/error_gui.cpp +++ b/src/error_gui.cpp @@ -8,6 +8,7 @@ /** @file error_gui.cpp GUI related to errors. */ #include "stdafx.h" +#include "core/mem_func.hpp" #include "landscape.h" #include "newgrf_text.h" #include "error.h" diff --git a/src/game/game_text.hpp b/src/game/game_text.hpp index 0f4c49698d..619d094e6c 100644 --- a/src/game/game_text.hpp +++ b/src/game/game_text.hpp @@ -10,8 +10,6 @@ #ifndef GAME_TEXT_HPP #define GAME_TEXT_HPP -#include "../core/smallvec_type.hpp" - struct StringParam { enum ParamType { RAW_STRING, diff --git a/src/gfx.cpp b/src/gfx.cpp index f9d587e258..d2f826366e 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -22,6 +22,7 @@ #include "newgrf_debug.h" #include "thread.h" #include "core/backup_type.hpp" +#include "core/container_func.hpp" #include "viewport_func.h" #include "table/palettes.h" diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index 73420b0add..9de917a80e 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -8,6 +8,7 @@ /** @file gfx_layout.cpp Handling of laying out text. */ #include "stdafx.h" +#include "core/math_func.hpp" #include "gfx_layout.h" #include "string_func.h" #include "debug.h" diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 9f72b22235..d2ddf10b0e 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -22,6 +22,7 @@ #include "tilehighlight_func.h" #include "vehicle_gui_base.h" #include "core/geometry_func.hpp" +#include "core/container_func.hpp" #include "company_base.h" #include "company_gui.h" #include "gui.h" diff --git a/src/language.h b/src/language.h index 9d2499068b..0966ccb197 100644 --- a/src/language.h +++ b/src/language.h @@ -10,7 +10,6 @@ #ifndef LANGUAGE_H #define LANGUAGE_H -#include "core/smallvec_type.hpp" #ifdef WITH_ICU_I18N #include #endif /* WITH_ICU_I18N */ diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index 894de4f072..4598569a01 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -12,6 +12,7 @@ #include "../fileio_type.h" #include "../string_func.h" #include "../core/endian_func.hpp" +#include "../core/mem_func.hpp" #include "../base_media_base.h" #include "midi.h" diff --git a/src/music/midifile.hpp b/src/music/midifile.hpp index 254fda9af0..16e6678e27 100644 --- a/src/music/midifile.hpp +++ b/src/music/midifile.hpp @@ -11,7 +11,6 @@ #define MUSIC_MIDIFILE_HPP #include "../stdafx.h" -#include "../core/smallvec_type.hpp" #include "midi.h" struct MusicSongInfo; diff --git a/src/music/win32_m.cpp b/src/music/win32_m.cpp index 8ff8a72ac9..0f2ee1652a 100644 --- a/src/music/win32_m.cpp +++ b/src/music/win32_m.cpp @@ -17,6 +17,7 @@ #include "midifile.hpp" #include "midi.h" #include "../base_media_base.h" +#include "../core/mem_func.hpp" #include #include "../safeguards.h" diff --git a/src/music_gui.cpp b/src/music_gui.cpp index 1bf66211f1..63f8e50242 100644 --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -18,6 +18,7 @@ #include "gfx_func.h" #include "zoom_func.h" #include "core/random_func.hpp" +#include "core/mem_func.hpp" #include "error.h" #include "core/geometry_func.hpp" #include "string_func.h" diff --git a/src/network/network_content.h b/src/network/network_content.h index a11d29b991..3172564a7c 100644 --- a/src/network/network_content.h +++ b/src/network/network_content.h @@ -13,6 +13,7 @@ #include "core/tcp_content.h" #include "core/http.h" #include +#include "../core/container_func.hpp" /** Vector with content info */ typedef std::vector ContentVector; diff --git a/src/newgrf.cpp b/src/newgrf.cpp index ba49f32c92..ec1257223f 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -9,6 +9,7 @@ #include "stdafx.h" +#include "core/container_func.hpp" #include "debug.h" #include "fileio_func.h" #include "engine_func.h" diff --git a/src/newgrf.h b/src/newgrf.h index 50f2897d5b..af7bb4b85c 100644 --- a/src/newgrf.h +++ b/src/newgrf.h @@ -16,7 +16,7 @@ #include "fileio_type.h" #include "core/bitmath_func.hpp" #include "core/alloc_type.hpp" -#include "core/smallvec_type.hpp" +#include "core/mem_func.hpp" /** * List of different canal 'features'. diff --git a/src/newgrf_commons.h b/src/newgrf_commons.h index 4a4df50707..c83653ccf0 100644 --- a/src/newgrf_commons.h +++ b/src/newgrf_commons.h @@ -15,7 +15,6 @@ #include "sprite.h" #include "core/alloc_type.hpp" -#include "core/smallvec_type.hpp" #include "command_type.h" #include "direction_type.h" #include "company_type.h" diff --git a/src/newgrf_debug.h b/src/newgrf_debug.h index 34530dc759..56a9c60117 100644 --- a/src/newgrf_debug.h +++ b/src/newgrf_debug.h @@ -11,7 +11,6 @@ #define NEWGRF_DEBUG_H #include "newgrf.h" -#include "core/smallvec_type.hpp" #include "tile_type.h" #include "vehicle_type.h" diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index b276cb1385..1719057539 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -17,6 +17,7 @@ #include "timer/timer_game_calendar.h" #include "vehicle_func.h" #include "core/random_func.hpp" +#include "core/container_func.hpp" #include "aircraft.h" #include "station_base.h" #include "company_base.h" diff --git a/src/newgrf_railtype.cpp b/src/newgrf_railtype.cpp index 848d51f230..7309df603b 100644 --- a/src/newgrf_railtype.cpp +++ b/src/newgrf_railtype.cpp @@ -8,6 +8,7 @@ /** @file newgrf_railtype.cpp NewGRF handling of rail types. */ #include "stdafx.h" +#include "core/container_func.hpp" #include "debug.h" #include "newgrf_railtype.h" #include "timer/timer_game_calendar.h" diff --git a/src/newgrf_roadtype.cpp b/src/newgrf_roadtype.cpp index ecd1ce54e5..c51963d171 100644 --- a/src/newgrf_roadtype.cpp +++ b/src/newgrf_roadtype.cpp @@ -8,6 +8,7 @@ /** @file newgrf_roadtype.cpp NewGRF handling of road types. */ #include "stdafx.h" +#include "core/container_func.hpp" #include "debug.h" #include "newgrf_roadtype.h" #include "timer/timer_game_calendar.h" diff --git a/src/newgrf_text.h b/src/newgrf_text.h index 82b27a57eb..03d4822052 100644 --- a/src/newgrf_text.h +++ b/src/newgrf_text.h @@ -12,7 +12,6 @@ #include "string_type.h" #include "strings_type.h" -#include "core/smallvec_type.hpp" #include "table/control_codes.h" #include diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index a2c97af8b7..1f44022211 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -26,6 +26,7 @@ #include "pbs.h" #include "company_base.h" #include "core/backup_type.hpp" +#include "core/container_func.hpp" #include "timer/timer_game_calendar.h" #include "strings_func.h" #include "company_gui.h" diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 6766a299f4..0f112dc0a7 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -30,6 +30,7 @@ #include "town.h" #include "company_base.h" #include "core/random_func.hpp" +#include "core/container_func.hpp" #include "newgrf_debug.h" #include "newgrf_railtype.h" #include "newgrf_roadtype.h" diff --git a/src/saveload/animated_tile_sl.cpp b/src/saveload/animated_tile_sl.cpp index a36366125a..950cc9c20c 100644 --- a/src/saveload/animated_tile_sl.cpp +++ b/src/saveload/animated_tile_sl.cpp @@ -13,7 +13,6 @@ #include "compat/animated_tile_sl_compat.h" #include "../tile_type.h" -#include "../core/smallvec_type.hpp" #include "../safeguards.h" diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 55a03cd846..56705daf87 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -27,7 +27,6 @@ #include "../engine_func.h" #include "../company_base.h" #include "../disaster_vehicle.h" -#include "../core/smallvec_type.hpp" #include "../timer/timer.h" #include "../timer/timer_game_tick.h" #include "../timer/timer_game_calendar.h" diff --git a/src/settings_func.h b/src/settings_func.h index 9f56b6de14..198bab3e7d 100644 --- a/src/settings_func.h +++ b/src/settings_func.h @@ -10,7 +10,6 @@ #ifndef SETTINGS_FUNC_H #define SETTINGS_FUNC_H -#include "core/smallvec_type.hpp" #include "company_type.h" #include "string_type.h" diff --git a/src/settingsgen/settingsgen.cpp b/src/settingsgen/settingsgen.cpp index 8952f9ee77..603278859d 100644 --- a/src/settingsgen/settingsgen.cpp +++ b/src/settingsgen/settingsgen.cpp @@ -12,7 +12,7 @@ #include "../strings_type.h" #include "../misc/getoptdata.h" #include "../ini_type.h" -#include "../core/smallvec_type.hpp" +#include "../core/mem_func.hpp" #include "../error_func.h" #if !defined(_WIN32) || defined(__CYGWIN__) diff --git a/src/sortlist_type.h b/src/sortlist_type.h index 9c14d382bd..21cbca16fa 100644 --- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -12,7 +12,7 @@ #include "core/enum_type.hpp" #include "core/bitmath_func.hpp" -#include "core/smallvec_type.hpp" +#include "core/mem_func.hpp" #include "date_type.h" /** Flags of the sort list. */ diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 20b2143f93..d09710dcbc 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -45,6 +45,7 @@ #include "pbs.h" #include "debug.h" #include "core/random_func.hpp" +#include "core/container_func.hpp" #include "company_base.h" #include "table/airporttile_ids.h" #include "newgrf_airporttiles.h" diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index bd32b4f352..e4f4f55ed7 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -9,6 +9,7 @@ #include "../stdafx.h" #include "../core/endian_func.hpp" +#include "../core/mem_func.hpp" #include "../error_func.h" #include "../string_func.h" #include "../strings_type.h" diff --git a/src/strgen/strgen_base.cpp b/src/strgen/strgen_base.cpp index 4f293dc41c..4f190f47e2 100644 --- a/src/strgen/strgen_base.cpp +++ b/src/strgen/strgen_base.cpp @@ -10,6 +10,7 @@ #include "../stdafx.h" #include "../core/alloc_func.hpp" #include "../core/endian_func.hpp" +#include "../core/mem_func.hpp" #include "../error_func.h" #include "../string_func.h" #include "../table/control_codes.h" diff --git a/src/stringfilter_type.h b/src/stringfilter_type.h index d71a1115d7..5b24587cb5 100644 --- a/src/stringfilter_type.h +++ b/src/stringfilter_type.h @@ -10,7 +10,6 @@ #ifndef STRINGFILTER_TYPE_H #define STRINGFILTER_TYPE_H -#include "core/smallvec_type.hpp" #include "strings_type.h" /** diff --git a/src/subsidy.cpp b/src/subsidy.cpp index 21649c9dd5..88879592eb 100644 --- a/src/subsidy.cpp +++ b/src/subsidy.cpp @@ -20,6 +20,7 @@ #include "subsidy_func.h" #include "core/pool_func.hpp" #include "core/random_func.hpp" +#include "core/container_func.hpp" #include "game/game.hpp" #include "command_func.h" #include "string_func.h" diff --git a/src/texteff.cpp b/src/texteff.cpp index 3235b8720d..6695d29944 100644 --- a/src/texteff.cpp +++ b/src/texteff.cpp @@ -11,7 +11,6 @@ #include "texteff.hpp" #include "transparency.h" #include "strings_func.h" -#include "core/smallvec_type.hpp" #include "viewport_func.h" #include "settings_type.h" #include "command_type.h" diff --git a/src/vehicle.cpp b/src/vehicle.cpp index b61764e261..89880910ff 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -38,6 +38,7 @@ #include "roadstop_base.h" #include "core/random_func.hpp" #include "core/backup_type.hpp" +#include "core/container_func.hpp" #include "order_backup.h" #include "sound_func.h" #include "effectvehicle_func.h" diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 8aa5fc0873..f725784d0c 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -23,6 +23,7 @@ #include "network/network.h" #include "saveload/saveload.h" #include "timer/timer_game_calendar.h" +#include "core/mem_func.hpp" const uint TILE_AXIAL_DISTANCE = 192; // Logical length of the tile in any DiagDirection used in vehicle movement. const uint TILE_CORNER_DISTANCE = 128; // Logical length of the tile corner crossing in any non-diagonal direction used in vehicle movement. diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 77782f070d..345771a84d 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -31,6 +31,7 @@ #include "articulated_vehicles.h" #include "spritecache.h" #include "core/geometry_func.hpp" +#include "core/container_func.hpp" #include "company_base.h" #include "engine_func.h" #include "station_base.h" diff --git a/src/vehicle_gui_base.h b/src/vehicle_gui_base.h index 8e6c272448..86c722bfd6 100644 --- a/src/vehicle_gui_base.h +++ b/src/vehicle_gui_base.h @@ -10,7 +10,6 @@ #ifndef VEHICLE_GUI_BASE_H #define VEHICLE_GUI_BASE_H -#include "core/smallvec_type.hpp" #include "cargo_type.h" #include "timer/timer_game_calendar.h" #include "economy_type.h" diff --git a/src/vehiclelist.h b/src/vehiclelist.h index 6f6e5cb5fe..df9649d5e3 100644 --- a/src/vehiclelist.h +++ b/src/vehiclelist.h @@ -10,7 +10,6 @@ #ifndef VEHICLELIST_H #define VEHICLELIST_H -#include "core/smallvec_type.hpp" #include "vehicle_type.h" #include "company_type.h" #include "tile_type.h" diff --git a/src/viewport_sprite_sorter.h b/src/viewport_sprite_sorter.h index 2a91f48a4f..0528fc39a9 100644 --- a/src/viewport_sprite_sorter.h +++ b/src/viewport_sprite_sorter.h @@ -8,7 +8,6 @@ /** @file viewport_sprite_sorter.h Types related to sprite sorting. */ #include "stdafx.h" -#include "core/smallvec_type.hpp" #include "gfx_type.h" #ifndef VIEWPORT_SPRITE_SORTER_H diff --git a/src/widgets/dropdown_type.h b/src/widgets/dropdown_type.h index 0d8951ef7b..56ed0061b5 100644 --- a/src/widgets/dropdown_type.h +++ b/src/widgets/dropdown_type.h @@ -12,7 +12,6 @@ #include "../window_type.h" #include "../gfx_func.h" -#include "../core/smallvec_type.hpp" #include "table/strings.h" /** diff --git a/src/window_gui.h b/src/window_gui.h index 953282bf7b..3ef708ab4d 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -16,7 +16,6 @@ #include "company_type.h" #include "tile_type.h" #include "widget_type.h" -#include "core/smallvec_type.hpp" #include "string_type.h" /**