Codechange: Remove macros involved with NewGRFClass. (#12363)

Use direct class instantiation instead.
This commit is contained in:
Peter Nelson 2024-03-23 21:55:50 +00:00 committed by GitHub
parent ff35288ddf
commit 668186ca5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 55 additions and 65 deletions

View File

@ -23,8 +23,8 @@
* This includes initialising the defaults classes with an empty * This includes initialising the defaults classes with an empty
* entry, for standard airports. * entry, for standard airports.
*/ */
template <typename Tspec, typename Tid, Tid Tmax> template <>
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults() /* static */ void AirportClass::InsertDefaults()
{ {
AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL; AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL;
AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE; AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE;
@ -32,13 +32,14 @@ template <typename Tspec, typename Tid, Tid Tmax>
AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS; AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS;
} }
template <typename Tspec, typename Tid, Tid Tmax> template <>
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint) const bool AirportClass::IsUIAvailable(uint) const
{ {
return true; return true;
} }
INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX) /* Instantiate AirportClass. */
template class NewGRFClass<AirportSpec, AirportClassID, APC_MAX>;
AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID); AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);

View File

@ -141,7 +141,7 @@ private:
}; };
/** Information related to airport classes. */ /** Information related to airport classes. */
typedef NewGRFClass<AirportSpec, AirportClassID, APC_MAX> AirportClass; using AirportClass = NewGRFClass<AirportSpec, AirportClassID, APC_MAX>;
void BindAirportSpecs(); void BindAirportSpecs();

View File

@ -17,7 +17,7 @@
* Struct containing information relating to NewGRF classes for stations and airports. * Struct containing information relating to NewGRF classes for stations and airports.
*/ */
template <typename Tspec, typename Tid, Tid Tmax> template <typename Tspec, typename Tid, Tid Tmax>
struct NewGRFClass { class NewGRFClass {
private: private:
uint ui_count; ///< Number of specs in this class potentially available to the user. uint ui_count; ///< Number of specs in this class potentially available to the user.
std::vector<Tspec *> spec; ///< List of specifications. std::vector<Tspec *> spec; ///< List of specifications.

View File

@ -11,20 +11,13 @@
#include "table/strings.h" #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 <typename Tspec, typename Tid, Tid Tmax> \
type NewGRFClass<Tspec, Tid, Tmax>
/** Instantiate the array. */ /** Instantiate the array. */
template <typename Tspec, typename Tid, Tid Tmax> template <typename Tspec, typename Tid, Tid Tmax>
NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax]; NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
/** Reset the class, i.e. clear everything. */ /** Reset the class, i.e. clear everything. */
DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass() template <typename Tspec, typename Tid, Tid Tmax>
void NewGRFClass<Tspec, Tid, Tmax>::ResetClass()
{ {
this->global_id = 0; this->global_id = 0;
this->name = STR_EMPTY; this->name = STR_EMPTY;
@ -34,7 +27,8 @@ DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
} }
/** Reset the classes, i.e. clear everything. */ /** Reset the classes, i.e. clear everything. */
DEFINE_NEWGRF_CLASS_METHOD(void)::Reset() template <typename Tspec, typename Tid, Tid Tmax>
void NewGRFClass<Tspec, Tid, Tmax>::Reset()
{ {
for (Tid i = (Tid)0; i < Tmax; i++) { for (Tid i = (Tid)0; i < Tmax; i++) {
classes[i].ResetClass(); classes[i].ResetClass();
@ -50,7 +44,8 @@ DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
* @note Upon allocating the same global class ID for a * @note Upon allocating the same global class ID for a
* second time, this first allocation will be given. * second time, this first allocation will be given.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32_t global_id) template <typename Tspec, typename Tid, Tid Tmax>
Tid NewGRFClass<Tspec, Tid, Tmax>::Allocate(uint32_t global_id)
{ {
for (Tid i = (Tid)0; i < Tmax; i++) { for (Tid i = (Tid)0; i < Tmax; i++) {
if (classes[i].global_id == global_id) { 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. * Insert a spec into the class.
* @param spec The spec to insert. * @param spec The spec to insert.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec) template <typename Tspec, typename Tid, Tid Tmax>
void NewGRFClass<Tspec, Tid, Tmax>::Insert(Tspec *spec)
{ {
this->spec.push_back(spec); this->spec.push_back(spec);
@ -83,7 +79,8 @@ DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
* @param spec The spec to assign. * @param spec The spec to assign.
* @note The spec must have a valid class id set. * @note The spec must have a valid class id set.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec) template <typename Tspec, typename Tid, Tid Tmax>
void NewGRFClass<Tspec, Tid, Tmax>::Assign(Tspec *spec)
{ {
assert(spec->cls_id < Tmax); assert(spec->cls_id < Tmax);
Get(spec->cls_id)->Insert(spec); Get(spec->cls_id)->Insert(spec);
@ -105,7 +102,8 @@ NewGRFClass<Tspec, Tid, Tmax> *NewGRFClass<Tspec, Tid, Tmax>::Get(Tid cls_id)
* Get the number of allocated classes. * Get the number of allocated classes.
* @return The number of classes. * @return The number of classes.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount() template <typename Tspec, typename Tid, Tid Tmax>
uint NewGRFClass<Tspec, Tid, Tmax>::GetClassCount()
{ {
uint i; uint i;
for (i = 0; i < Tmax && classes[i].global_id != 0; 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. * Get the number of classes available to the user.
* @return The number of classes. * @return The number of classes.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount() template <typename Tspec, typename Tid, Tid Tmax>
uint NewGRFClass<Tspec, Tid, Tmax>::GetUIClassCount()
{ {
uint cnt = 0; uint cnt = 0;
for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) { 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. * @param index UI index of a class.
* @return The class ID of the class. * @return The class ID of the class.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index) template <typename Tspec, typename Tid, Tid Tmax>
Tid NewGRFClass<Tspec, Tid, Tmax>::GetUIClass(uint index)
{ {
for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) { for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
if (classes[i].GetUISpecCount() == 0) continue; 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. * @param index The index where to find the spec.
* @return The spec at given location. * @return The spec at given location.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const template <typename Tspec, typename Tid, Tid Tmax>
const Tspec *NewGRFClass<Tspec, Tid, Tmax>::GetSpec(uint index) const
{ {
/* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */ /* 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; 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. * @param ui_index UI index of the spec.
* @return index of the spec, or -1 if out of range. * @return index of the spec, or -1 if out of range.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const template <typename Tspec, typename Tid, Tid Tmax>
int NewGRFClass<Tspec, Tid, Tmax>::GetIndexFromUI(int ui_index) const
{ {
if (ui_index < 0) return -1; if (ui_index < 0) return -1;
for (uint i = 0; i < this->GetSpecCount(); i++) { 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. * @param index index of the spec.
* @return UI index of the spec, or -1 if out of range. * @return UI index of the spec, or -1 if out of range.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const template <typename Tspec, typename Tid, Tid Tmax>
int NewGRFClass<Tspec, Tid, Tmax>::GetUIFromIndex(int index) const
{ {
if ((uint)index >= this->GetSpecCount()) return -1; if ((uint)index >= this->GetSpecCount()) return -1;
uint ui_index = 0; 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. * @param index Pointer to return the index of the spec in its class. If nullptr then not used.
* @return The spec. * @return The spec.
*/ */
DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32_t grfid, uint16_t local_id, int *index) template <typename Tspec, typename Tid, Tid Tmax>
const Tspec *NewGRFClass<Tspec, Tid, Tmax>::GetByGrf(uint32_t grfid, uint16_t local_id, int *index)
{ {
uint j; uint j;
@ -205,21 +209,3 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32_t grfid, uint16_t loc
return nullptr; 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<Tspec, Tid, Tmax> *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);

View File

@ -136,20 +136,21 @@ void ResetObjects()
_object_specs[OBJECT_TRANSMITTER].cls_id = ObjectClass::Allocate('TRNS'); _object_specs[OBJECT_TRANSMITTER].cls_id = ObjectClass::Allocate('TRNS');
} }
template <typename Tspec, typename Tid, Tid Tmax> template <>
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults() /* static */ void ObjectClass::InsertDefaults()
{ {
ObjectClass::Get(ObjectClass::Allocate('LTHS'))->name = STR_OBJECT_CLASS_LTHS; ObjectClass::Get(ObjectClass::Allocate('LTHS'))->name = STR_OBJECT_CLASS_LTHS;
ObjectClass::Get(ObjectClass::Allocate('TRNS'))->name = STR_OBJECT_CLASS_TRNS; ObjectClass::Get(ObjectClass::Allocate('TRNS'))->name = STR_OBJECT_CLASS_TRNS;
} }
template <typename Tspec, typename Tid, Tid Tmax> template <>
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const bool ObjectClass::IsUIAvailable(uint index) const
{ {
return this->GetSpec(index)->IsEverAvailable(); return this->GetSpec(index)->IsEverAvailable();
} }
INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX) /* Instantiate ObjectClass. */
template class NewGRFClass<ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX>;
/* virtual */ uint32_t ObjectScopeResolver::GetRandomBits() const /* virtual */ uint32_t ObjectScopeResolver::GetRandomBits() const
{ {

View File

@ -163,8 +163,8 @@ private:
TownScopeResolver *GetTown(); TownScopeResolver *GetTown();
}; };
/** Struct containing information relating to object classes. */ /** Class containing information relating to object classes. */
typedef NewGRFClass<ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX> ObjectClass; using ObjectClass = NewGRFClass<ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX>;
static const size_t OBJECT_SPRITE_GROUP_DEFAULT = 0; static const size_t OBJECT_SPRITE_GROUP_DEFAULT = 0;
static const size_t OBJECT_SPRITE_GROUP_PURCHASE = 1; static const size_t OBJECT_SPRITE_GROUP_PURCHASE = 1;

View File

@ -27,8 +27,8 @@
#include "safeguards.h" #include "safeguards.h"
template <typename Tspec, typename Tid, Tid Tmax> template <>
void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults() void RoadStopClass::InsertDefaults()
{ {
/* Set up initial data */ /* Set up initial data */
RoadStopClass::Get(RoadStopClass::Allocate('DFLT'))->name = STR_STATION_CLASS_DFLT; RoadStopClass::Get(RoadStopClass::Allocate('DFLT'))->name = STR_STATION_CLASS_DFLT;
@ -37,13 +37,14 @@ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
RoadStopClass::Get(RoadStopClass::Allocate('WAYP'))->Insert(nullptr); RoadStopClass::Get(RoadStopClass::Allocate('WAYP'))->Insert(nullptr);
} }
template <typename Tspec, typename Tid, Tid Tmax> template <>
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint) const bool RoadStopClass::IsUIAvailable(uint) const
{ {
return true; return true;
} }
INSTANTIATE_NEWGRF_CLASS_METHODS(RoadStopClass, RoadStopSpec, RoadStopClassID, ROADSTOP_CLASS_MAX) /* Instantiate RoadStopClass. */
template class NewGRFClass<RoadStopSpec, RoadStopClassID, ROADSTOP_CLASS_MAX>;
static const uint NUM_ROADSTOPSPECS_PER_STATION = 63; ///< Maximum number of parts per station. static const uint NUM_ROADSTOPSPECS_PER_STATION = 63; ///< Maximum number of parts per station.

View File

@ -161,7 +161,7 @@ struct RoadStopSpec {
static const RoadStopSpec *Get(uint16_t index); static const RoadStopSpec *Get(uint16_t index);
}; };
typedef NewGRFClass<RoadStopSpec, RoadStopClassID, ROADSTOP_CLASS_MAX> RoadStopClass; using RoadStopClass = NewGRFClass<RoadStopSpec, RoadStopClassID, ROADSTOP_CLASS_MAX>;
void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view); void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view);

View File

@ -28,8 +28,8 @@
#include "safeguards.h" #include "safeguards.h"
template <typename Tspec, typename Tid, Tid Tmax> template <>
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults() /* static */ void StationClass::InsertDefaults()
{ {
/* Set up initial data */ /* Set up initial data */
StationClass::Get(StationClass::Allocate('DFLT'))->name = STR_STATION_CLASS_DFLT; StationClass::Get(StationClass::Allocate('DFLT'))->name = STR_STATION_CLASS_DFLT;
@ -38,13 +38,14 @@ template <typename Tspec, typename Tid, Tid Tmax>
StationClass::Get(StationClass::Allocate('WAYP'))->Insert(nullptr); StationClass::Get(StationClass::Allocate('WAYP'))->Insert(nullptr);
} }
template <typename Tspec, typename Tid, Tid Tmax> template <>
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint) const bool StationClass::IsUIAvailable(uint) const
{ {
return true; return true;
} }
INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX) /* Instantiate StationClass. */
template class NewGRFClass<StationSpec, StationClassID, STAT_CLASS_MAX>;
static const uint NUM_STATIONSSPECS_PER_STATION = 255; ///< Maximum number of parts per station. static const uint NUM_STATIONSSPECS_PER_STATION = 255; ///< Maximum number of parts per station.

View File

@ -175,8 +175,8 @@ struct StationSpec {
std::vector<std::vector<std::vector<uint8_t>>> layouts; std::vector<std::vector<std::vector<uint8_t>>> layouts;
}; };
/** Struct containing information relating to station classes. */ /** Class containing information relating to station classes. */
typedef NewGRFClass<StationSpec, StationClassID, STAT_CLASS_MAX> StationClass; using StationClass = NewGRFClass<StationSpec, StationClassID, STAT_CLASS_MAX>;
const StationSpec *GetStationSpec(TileIndex t); const StationSpec *GetStationSpec(TileIndex t);