OpenTTD Source 20260721-master-g25ec12c62d
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 "core/label_type.hpp"
14#include "newgrf_type.h"
15#include "strings_type.h"
16
18template <typename Tindex>
20 Tindex class_index;
21 uint16_t index;
22};
23
27template <typename Tspec, typename Tindex>
29private:
30 /* Tspec must be of NewGRFSpecBase<Tindex>. */
31 static_assert(std::is_base_of_v<NewGRFSpecBase<Tindex>, Tspec>);
32
33 uint ui_count = 0;
34 Tindex index{};
35 std::vector<Tspec *> spec;
36
41 static inline std::vector<NewGRFClass<Tspec, Tindex>> classes;
42
44 static void InsertDefaults();
45
46public:
47 using spec_type = Tspec;
48 using index_type = Tindex;
50
53
60
65 std::span<Tspec * const> Specs() const { return this->spec; }
66
71 static std::span<NewGRFClass<Tspec, Tindex> const> Classes() { return NewGRFClass::classes; }
72
73 void Insert(Tspec *spec);
74
75 Tindex Index() const { return this->index; }
76
81 uint GetSpecCount() const { return static_cast<uint>(this->spec.size()); }
82
87 uint GetUISpecCount() const { return this->ui_count; }
88
89 const Tspec *GetSpec(uint index) const;
90
96 bool IsUIAvailable(uint index) const;
97
98 static void Reset();
99 static Tindex Allocate(GlobalID global_id);
100 static void Assign(Tspec *spec);
101 static uint GetClassCount();
102 static uint GetUIClassCount();
103 static NewGRFClass *Get(Tindex class_index);
104
105 static const Tspec *GetByGrf(GrfID grfid, uint16_t local_id);
106};
107
108#endif /* NEWGRF_CLASS_H */
const Tspec * GetSpec(uint index) const
Get a spec from the class at a given index.
bool IsUIAvailable(uint index) const
Check whether the spec will be available to the user at some point in time.
static void Reset()
Reset the classes, i.e.
static void InsertDefaults()
Initialise the defaults.
std::span< Tspec *const > Specs() const
Get read-only span of specs of this class.
uint GetUISpecCount() const
Get the number of potentially user-available specs within the class.
uint GetSpecCount() const
Get the number of allocated specs within the class.
void Insert(Tspec *spec)
Insert a spec into the class, and update its index.
std::vector< AirportSpec * > spec
static uint GetUIClassCount()
Get the number of classes available to the user.
static std::span< NewGRFClass< Tspec, Tindex > const > Classes()
Get read-only span of all classes of this type.
static std::vector< NewGRFClass< AirportSpec, AirportClassID > > classes
NewGRFClass(GlobalID global_id, StringID name)
Create the class.
static void Assign(Tspec *spec)
Assign a spec to one of the classes.
static NewGRFClass * Get(Tindex class_index)
Get a particular class.
static uint GetClassCount()
Get the number of allocated classes.
static const Tspec * GetByGrf(GrfID grfid, uint16_t local_id)
Retrieve a spec by GRF location.
Label< NewGRFClass< AirportSpec, AirportClassID > > GlobalID
static Tindex Allocate(GlobalID global_id)
Allocate a class with a given global class ID.
A type for 4 character labels/tags/ids in files that should be read/shown as is.
Commonly used types for the NewGRF implementation.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
A four character label/tag/id.
Base for each type of NewGRF spec to be used with NewGRFClass.
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.