Change: Use vector/iterators/algorithms instead of C-array/loops for NewGRF classes.

This commit is contained in:
Peter Nelson
2024-04-09 17:54:42 +01:00
committed by Peter Nelson
parent 34758d0921
commit 052f421327
4 changed files with 35 additions and 58 deletions

View File

@@ -19,17 +19,14 @@
template <typename Tspec, typename Tid, Tid Tmax>
class NewGRFClass {
private:
uint ui_count; ///< Number of specs in this class potentially available to the user.
uint ui_count = 0; ///< Number of specs in this class potentially available to the user.
std::vector<Tspec *> spec; ///< List of specifications.
/**
* The actual classes.
* @note We store pointers to members of this array in various places outside this class (e.g. to 'name' for GRF string resolving).
* Thus this must be a static array, and cannot be a self-resizing vector or similar.
* @note This may be reallocated during initialization so pointers may be invalidated.
*/
static NewGRFClass<Tspec, Tid, Tmax> classes[Tmax];
void ResetClass();
static inline std::vector<NewGRFClass<Tspec, Tid, Tmax>> classes;
/** Initialise the defaults. */
static void InsertDefaults();
@@ -38,8 +35,12 @@ public:
uint32_t global_id; ///< Global ID for class, e.g. 'DFLT', 'WAYP', etc.
StringID name; ///< Name of this class.
/* Public constructor as emplace_back needs access. */
NewGRFClass(uint32_t global_id, StringID name) : global_id(global_id), name(name) { }
void Insert(Tspec *spec);
Tid Index() const { return static_cast<Tid>(std::distance(&*std::cbegin(NewGRFClass::classes), this)); }
/** Get the number of allocated specs within the class. */
uint GetSpecCount() const { return static_cast<uint>(this->spec.size()); }
/** Get the number of potentially user-available specs within the class. */