From 668186ca5b065d0ef10bf929bee0c84007e09940 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 23 Mar 2024 21:55:50 +0000 Subject: [PATCH] Codechange: Remove macros involved with NewGRFClass. (#12363) Use direct class instantiation instead. --- src/newgrf_airport.cpp | 11 ++++---- src/newgrf_airport.h | 2 +- src/newgrf_class.h | 2 +- src/newgrf_class_func.h | 62 ++++++++++++++++------------------------- src/newgrf_object.cpp | 11 ++++---- src/newgrf_object.h | 4 +-- src/newgrf_roadstop.cpp | 11 ++++---- src/newgrf_roadstop.h | 2 +- src/newgrf_station.cpp | 11 ++++---- src/newgrf_station.h | 4 +-- 10 files changed, 55 insertions(+), 65 deletions(-) diff --git a/src/newgrf_airport.cpp b/src/newgrf_airport.cpp index 2c0012141d..bdac26abaf 100644 --- a/src/newgrf_airport.cpp +++ b/src/newgrf_airport.cpp @@ -23,8 +23,8 @@ * This includes initialising the defaults classes with an empty * entry, for standard airports. */ -template -/* static */ void NewGRFClass::InsertDefaults() +template <> +/* static */ void AirportClass::InsertDefaults() { AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL; AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE; @@ -32,13 +32,14 @@ template AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS; } -template -bool NewGRFClass::IsUIAvailable(uint) const +template <> +bool AirportClass::IsUIAvailable(uint) const { return true; } -INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX) +/* Instantiate AirportClass. */ +template class NewGRFClass; AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID); diff --git a/src/newgrf_airport.h b/src/newgrf_airport.h index 5d031f7c56..7099e42e47 100644 --- a/src/newgrf_airport.h +++ b/src/newgrf_airport.h @@ -141,7 +141,7 @@ private: }; /** Information related to airport classes. */ -typedef NewGRFClass AirportClass; +using AirportClass = NewGRFClass; void BindAirportSpecs(); diff --git a/src/newgrf_class.h b/src/newgrf_class.h index 6e375e5515..38e068a881 100644 --- a/src/newgrf_class.h +++ b/src/newgrf_class.h @@ -17,7 +17,7 @@ * Struct containing information relating to NewGRF classes for stations and airports. */ template -struct NewGRFClass { +class NewGRFClass { private: uint ui_count; ///< Number of specs in this class potentially available to the user. std::vector spec; ///< List of specifications. diff --git a/src/newgrf_class_func.h b/src/newgrf_class_func.h index 4e41915916..b3de6ba9c1 100644 --- a/src/newgrf_class_func.h +++ b/src/newgrf_class_func.h @@ -11,20 +11,13 @@ #include "table/strings.h" -/** - * Helper for defining the class method's signatures. - * @param type The type of the class. - */ -#define DEFINE_NEWGRF_CLASS_METHOD(type) \ - template \ - type NewGRFClass - /** Instantiate the array. */ template NewGRFClass NewGRFClass::classes[Tmax]; /** Reset the class, i.e. clear everything. */ -DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass() +template +void NewGRFClass::ResetClass() { this->global_id = 0; this->name = STR_EMPTY; @@ -34,7 +27,8 @@ DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass() } /** Reset the classes, i.e. clear everything. */ -DEFINE_NEWGRF_CLASS_METHOD(void)::Reset() +template +void NewGRFClass::Reset() { for (Tid i = (Tid)0; i < Tmax; i++) { classes[i].ResetClass(); @@ -50,7 +44,8 @@ DEFINE_NEWGRF_CLASS_METHOD(void)::Reset() * @note Upon allocating the same global class ID for a * second time, this first allocation will be given. */ -DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32_t global_id) +template +Tid NewGRFClass::Allocate(uint32_t global_id) { for (Tid i = (Tid)0; i < Tmax; i++) { if (classes[i].global_id == global_id) { @@ -71,7 +66,8 @@ DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32_t global_id) * Insert a spec into the class. * @param spec The spec to insert. */ -DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec) +template +void NewGRFClass::Insert(Tspec *spec) { this->spec.push_back(spec); @@ -83,7 +79,8 @@ DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec) * @param spec The spec to assign. * @note The spec must have a valid class id set. */ -DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec) +template +void NewGRFClass::Assign(Tspec *spec) { assert(spec->cls_id < Tmax); Get(spec->cls_id)->Insert(spec); @@ -105,7 +102,8 @@ NewGRFClass *NewGRFClass::Get(Tid cls_id) * Get the number of allocated classes. * @return The number of classes. */ -DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount() +template +uint NewGRFClass::GetClassCount() { uint i; for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {} @@ -116,7 +114,8 @@ DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount() * Get the number of classes available to the user. * @return The number of classes. */ -DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount() +template +uint NewGRFClass::GetUIClassCount() { uint cnt = 0; for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) { @@ -130,7 +129,8 @@ DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount() * @param index UI index of a class. * @return The class ID of the class. */ -DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index) +template +Tid NewGRFClass::GetUIClass(uint index) { for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) { if (classes[i].GetUISpecCount() == 0) continue; @@ -144,7 +144,8 @@ DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index) * @param index The index where to find the spec. * @return The spec at given location. */ -DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const +template +const Tspec *NewGRFClass::GetSpec(uint index) const { /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */ return index < this->GetSpecCount() ? this->spec[index] : nullptr; @@ -155,7 +156,8 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const * @param ui_index UI index of the spec. * @return index of the spec, or -1 if out of range. */ -DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const +template +int NewGRFClass::GetIndexFromUI(int ui_index) const { if (ui_index < 0) return -1; for (uint i = 0; i < this->GetSpecCount(); i++) { @@ -170,7 +172,8 @@ DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const * @param index index of the spec. * @return UI index of the spec, or -1 if out of range. */ -DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const +template +int NewGRFClass::GetUIFromIndex(int index) const { if ((uint)index >= this->GetSpecCount()) return -1; uint ui_index = 0; @@ -187,7 +190,8 @@ DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const * @param index Pointer to return the index of the spec in its class. If nullptr then not used. * @return The spec. */ -DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32_t grfid, uint16_t local_id, int *index) +template +const Tspec *NewGRFClass::GetByGrf(uint32_t grfid, uint16_t local_id, int *index) { uint j; @@ -205,21 +209,3 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32_t grfid, uint16_t loc return nullptr; } - -#undef DEFINE_NEWGRF_CLASS_METHOD - -/** Force instantiation of the methods so we don't get linker errors. */ -#define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \ - template void name::ResetClass(); \ - template void name::Reset(); \ - template Tid name::Allocate(uint32_t global_id); \ - template void name::Insert(Tspec *spec); \ - template void name::Assign(Tspec *spec); \ - template NewGRFClass *name::Get(Tid cls_id); \ - template uint name::GetClassCount(); \ - template uint name::GetUIClassCount(); \ - template Tid name::GetUIClass(uint index); \ - template const Tspec *name::GetSpec(uint index) const; \ - template int name::GetUIFromIndex(int index) const; \ - template int name::GetIndexFromUI(int ui_index) const; \ - template const Tspec *name::GetByGrf(uint32_t grfid, uint16_t local_id, int *index); diff --git a/src/newgrf_object.cpp b/src/newgrf_object.cpp index 8e47c650ef..d938bb4930 100644 --- a/src/newgrf_object.cpp +++ b/src/newgrf_object.cpp @@ -136,20 +136,21 @@ void ResetObjects() _object_specs[OBJECT_TRANSMITTER].cls_id = ObjectClass::Allocate('TRNS'); } -template -/* static */ void NewGRFClass::InsertDefaults() +template <> +/* static */ void ObjectClass::InsertDefaults() { ObjectClass::Get(ObjectClass::Allocate('LTHS'))->name = STR_OBJECT_CLASS_LTHS; ObjectClass::Get(ObjectClass::Allocate('TRNS'))->name = STR_OBJECT_CLASS_TRNS; } -template -bool NewGRFClass::IsUIAvailable(uint index) const +template <> +bool ObjectClass::IsUIAvailable(uint index) const { return this->GetSpec(index)->IsEverAvailable(); } -INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX) +/* Instantiate ObjectClass. */ +template class NewGRFClass; /* virtual */ uint32_t ObjectScopeResolver::GetRandomBits() const { diff --git a/src/newgrf_object.h b/src/newgrf_object.h index e55f9b8511..0d224410ab 100644 --- a/src/newgrf_object.h +++ b/src/newgrf_object.h @@ -163,8 +163,8 @@ private: TownScopeResolver *GetTown(); }; -/** Struct containing information relating to object classes. */ -typedef NewGRFClass ObjectClass; +/** Class containing information relating to object classes. */ +using ObjectClass = NewGRFClass; static const size_t OBJECT_SPRITE_GROUP_DEFAULT = 0; static const size_t OBJECT_SPRITE_GROUP_PURCHASE = 1; diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index 557b771ac8..482bce1e6a 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -27,8 +27,8 @@ #include "safeguards.h" -template -void NewGRFClass::InsertDefaults() +template <> +void RoadStopClass::InsertDefaults() { /* Set up initial data */ RoadStopClass::Get(RoadStopClass::Allocate('DFLT'))->name = STR_STATION_CLASS_DFLT; @@ -37,13 +37,14 @@ void NewGRFClass::InsertDefaults() RoadStopClass::Get(RoadStopClass::Allocate('WAYP'))->Insert(nullptr); } -template -bool NewGRFClass::IsUIAvailable(uint) const +template <> +bool RoadStopClass::IsUIAvailable(uint) const { return true; } -INSTANTIATE_NEWGRF_CLASS_METHODS(RoadStopClass, RoadStopSpec, RoadStopClassID, ROADSTOP_CLASS_MAX) +/* Instantiate RoadStopClass. */ +template class NewGRFClass; static const uint NUM_ROADSTOPSPECS_PER_STATION = 63; ///< Maximum number of parts per station. diff --git a/src/newgrf_roadstop.h b/src/newgrf_roadstop.h index 7c47f74bb8..8c231dc4ca 100644 --- a/src/newgrf_roadstop.h +++ b/src/newgrf_roadstop.h @@ -161,7 +161,7 @@ struct RoadStopSpec { static const RoadStopSpec *Get(uint16_t index); }; -typedef NewGRFClass RoadStopClass; +using RoadStopClass = NewGRFClass; void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view); diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 4a3bae2abb..f6c68eaa10 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -28,8 +28,8 @@ #include "safeguards.h" -template -/* static */ void NewGRFClass::InsertDefaults() +template <> +/* static */ void StationClass::InsertDefaults() { /* Set up initial data */ StationClass::Get(StationClass::Allocate('DFLT'))->name = STR_STATION_CLASS_DFLT; @@ -38,13 +38,14 @@ template StationClass::Get(StationClass::Allocate('WAYP'))->Insert(nullptr); } -template -bool NewGRFClass::IsUIAvailable(uint) const +template <> +bool StationClass::IsUIAvailable(uint) const { return true; } -INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX) +/* Instantiate StationClass. */ +template class NewGRFClass; static const uint NUM_STATIONSSPECS_PER_STATION = 255; ///< Maximum number of parts per station. diff --git a/src/newgrf_station.h b/src/newgrf_station.h index 08fcaf8efd..be9fa03dfe 100644 --- a/src/newgrf_station.h +++ b/src/newgrf_station.h @@ -175,8 +175,8 @@ struct StationSpec { std::vector>> layouts; }; -/** Struct containing information relating to station classes. */ -typedef NewGRFClass StationClass; +/** Class containing information relating to station classes. */ +using StationClass = NewGRFClass; const StationSpec *GetStationSpec(TileIndex t);