OpenTTD Source: src/newgrf_class.h Source File

OpenTTD Source 15.0-RC2

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
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>
17 struct NewGRFSpecBase {
18 Tindex class_index;
19 uint16_t index;
20};
21
25template <typename Tspec, typename Tindex, Tindex Tmax>
26 class 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;
49 StringID name;
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 */
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition newgrf_class.h:26
NewGRFClass::index
Tindex index
Index of class within the list of classes.
Definition newgrf_class.h:32
NewGRFClass::Classes
static std::span< NewGRFClass< Tspec, Tindex, Tmax > const > Classes()
Get read-only span of all classes of this type.
Definition newgrf_class.h:64
NewGRFClass::Assign
static void Assign(Tspec *spec)
Assign a spec to one of the classes.
Definition newgrf_class_func.h:70
NewGRFClass::GetUISpecCount
uint GetUISpecCount() const
Get the number of potentially user-available specs within the class.
Definition newgrf_class.h:72
NewGRFClass::name
StringID name
Name of this class.
Definition newgrf_class.h:49
NewGRFClass::IsUIAvailable
bool IsUIAvailable(uint index) const
Check whether the spec will be available to the user at some point in time.
Definition newgrf_airport.cpp:40
NewGRFClass::Insert
void Insert(Tspec *spec)
Insert a spec into the class, and update its index.
Definition newgrf_class_func.h:55
NewGRFClass::global_id
uint32_t global_id
Global ID for class, e.g. 'DFLT', 'WAYP', etc.
Definition newgrf_class.h:48
NewGRFClass::Specs
std::span< Tspec *const > Specs() const
Get read-only span of specs of this class.
Definition newgrf_class.h:58
NewGRFClass::Get
static NewGRFClass * Get(Tindex class_index)
Get a particular class.
Definition newgrf_class_func.h:82
NewGRFClass::GetUIClassCount
static uint GetUIClassCount()
Get the number of classes available to the user.
Definition newgrf_class_func.h:103
NewGRFClass::Allocate
static Tindex Allocate(uint32_t global_id)
Allocate a class with a given global class ID.
Definition newgrf_class_func.h:32
NewGRFClass::ui_count
uint ui_count
Number of specs in this class potentially available to the user.
Definition newgrf_class.h:31
NewGRFClass::GetByGrf
static const Tspec * GetByGrf(uint32_t grfid, uint16_t local_id)
Retrieve a spec by GRF location.
Definition newgrf_class_func.h:128
NewGRFClass::spec
std::vector< Tspec * > spec
List of specifications.
Definition newgrf_class.h:33
NewGRFClass::GetSpecCount
uint GetSpecCount() const
Get the number of allocated specs within the class.
Definition newgrf_class.h:70
NewGRFClass::GetClassCount
static uint GetClassCount()
Get the number of allocated classes.
Definition newgrf_class_func.h:93
NewGRFClass::InsertDefaults
static void InsertDefaults()
Initialise the defaults.
Definition newgrf_airport.cpp:31
NewGRFClass::GetSpec
const Tspec * GetSpec(uint index) const
Get a spec from the class at a given index.
Definition newgrf_class_func.h:114
NewGRFClass::classes
static std::vector< NewGRFClass< Tspec, Tindex, Tmax > > classes
The actual classes.
Definition newgrf_class.h:39
NewGRFClass::Reset
static void Reset()
Reset the classes, i.e.
Definition newgrf_class_func.h:16
strings_type.h
Types related to strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
NewGRFSpecBase
Definition newgrf_class.h:17
NewGRFSpecBase::index
uint16_t index
Index within class of this spec, invalid until inserted into class.
Definition newgrf_class.h:19
NewGRFSpecBase::class_index
Tindex class_index
Class index of this spec, invalid until class is allocated.
Definition newgrf_class.h:18