OpenTTD Source 20260208-master-g43af8e94d0
newgrf_class.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NEWGRF_CLASS_H
11#define NEWGRF_CLASS_H
12
13#include "strings_type.h"
14
15/* Base for each type of NewGRF spec to be used with NewGRFClass. */
16template <typename Tindex>
18 Tindex class_index;
19 uint16_t index;
20};
21
25template <typename Tspec, typename Tindex, Tindex Tmax>
26class NewGRFClass {
27private:
28 /* Tspec must be of NewGRFSpecBase<Tindex>. */
29 static_assert(std::is_base_of_v<NewGRFSpecBase<Tindex>, Tspec>);
30
31 uint ui_count = 0;
32 Tindex index = static_cast<Tindex>(0);
33 std::vector<Tspec *> spec;
34
39 static inline std::vector<NewGRFClass<Tspec, Tindex, Tmax>> classes;
40
42 static void InsertDefaults();
43
44public:
45 using spec_type = Tspec;
46 using index_type = Tindex;
47
48 uint32_t global_id;
50
51 /* Public constructor as emplace_back needs access. */
52 NewGRFClass(uint32_t global_id, StringID name) : global_id(global_id), name(name) { }
53
58 std::span<Tspec * const> Specs() const { return this->spec; }
59
64 static std::span<NewGRFClass<Tspec, Tindex, Tmax> const> Classes() { return NewGRFClass::classes; }
65
66 void Insert(Tspec *spec);
67
68 Tindex Index() const { return this->index; }
70 uint GetSpecCount() const { return static_cast<uint>(this->spec.size()); }
72 uint GetUISpecCount() const { return this->ui_count; }
73
74 const Tspec *GetSpec(uint index) const;
75
77 bool IsUIAvailable(uint index) const;
78
79 static void Reset();
80 static Tindex Allocate(uint32_t global_id);
81 static void Assign(Tspec *spec);
82 static uint GetClassCount();
83 static uint GetUIClassCount();
84 static NewGRFClass *Get(Tindex class_index);
85
86 static const Tspec *GetByGrf(uint32_t grfid, uint16_t local_id);
87};
88
89#endif /* NEWGRF_CLASS_H */
static std::span< NewGRFClass< Tspec, Tindex, Tmax > const > Classes()
Get read-only span of all classes of this type.
static void Assign(Tspec *spec)
Assign a spec to one of the classes.
uint GetUISpecCount() const
Get the number of potentially user-available specs within the class.
void Insert(Tspec *spec)
Insert a spec into the class, and update its index.
bool IsUIAvailable(uint index) const
Check whether the spec will be available to the user at some point in time.
std::span< Tspec *const > Specs() const
Get read-only span of specs of this class.
static NewGRFClass * Get(Tindex class_index)
Get a particular class.
static void InsertDefaults()
Initialise the defaults.
static uint GetUIClassCount()
Get the number of classes available to the user.
static Tindex Allocate(uint32_t global_id)
Allocate a class with a given global class ID.
static const Tspec * GetByGrf(uint32_t grfid, uint16_t local_id)
Retrieve a spec by GRF location.
uint GetSpecCount() const
Get the number of allocated specs within the class.
static uint GetClassCount()
Get the number of allocated classes.
const Tspec * GetSpec(uint index) const
Get a spec from the class at a given index.
static std::vector< NewGRFClass< AirportSpec, AirportClassID, Tmax > > classes
static void Reset()
Reset the classes, i.e.
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
uint16_t index
Index within class of this spec, invalid until inserted into class.
Tindex class_index
Class index of this spec, invalid until class is allocated.