OpenTTD Source  20241120-master-g6d3adc6169
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 <http://www.gnu.org/licenses/>.
6  */
7 
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. */
16 template <typename Tindex>
18  Tindex class_index;
19  uint16_t index;
20 };
21 
25 template <typename Tspec, typename Tindex, Tindex Tmax>
26 class NewGRFClass {
27 private:
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 
44 public:
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. */
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 */
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:26
Tindex index
Index of class within the list of classes.
Definition: newgrf_class.h:32
std::span< Tspec *const > Specs() const
Get read-only span of specs of this class.
Definition: newgrf_class.h:58
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.
Definition: newgrf_class.h:72
static std::span< NewGRFClass< Tspec, Tindex, Tmax > const > Classes()
Get read-only span of all classes of this type.
Definition: newgrf_class.h:64
StringID name
Name of this class.
Definition: newgrf_class.h:49
bool IsUIAvailable(uint index) const
Check whether the spec will be available to the user at some point in time.
void Insert(Tspec *spec)
Insert a spec into the class, and update its index.
uint32_t global_id
Global ID for class, e.g. 'DFLT', 'WAYP', etc.
Definition: newgrf_class.h:48
static NewGRFClass * Get(Tindex class_index)
Get a particular class.
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.
uint ui_count
Number of specs in this class potentially available to the user.
Definition: newgrf_class.h:31
static const Tspec * GetByGrf(uint32_t grfid, uint16_t local_id)
Retrieve a spec by GRF location.
std::vector< Tspec * > spec
List of specifications.
Definition: newgrf_class.h:33
uint GetSpecCount() const
Get the number of allocated specs within the class.
Definition: newgrf_class.h:70
static uint GetClassCount()
Get the number of allocated classes.
static void InsertDefaults()
Initialise the defaults.
const Tspec * GetSpec(uint index) const
Get a spec from the class at a given index.
static std::vector< NewGRFClass< Tspec, Tindex, Tmax > > classes
The actual classes.
Definition: newgrf_class.h:39
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.
Definition: strings_type.h:16
uint16_t index
Index within class of this spec, invalid until inserted into class.
Definition: newgrf_class.h:19
Tindex class_index
Class index of this spec, invalid until class is allocated.
Definition: newgrf_class.h:18