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/>.
8 /** @file newgrf_class.h Header file for classes to be used by e.g. NewGRF stations and airports */
10 #ifndef NEWGRF_CLASS_H
11 #define NEWGRF_CLASS_H
13 #include "strings_type.h"
15 /* Base for each type of NewGRF spec to be used with NewGRFClass. */
16 template <typename Tindex
>
17 struct NewGRFSpecBase
{
18 Tindex class_index
; ///< Class index of this spec, invalid until class is allocated.
19 uint16_t index
; ///< Index within class of this spec, invalid until inserted into class.
23 * Struct containing information relating to NewGRF classes for stations and airports.
25 template <typename Tspec
, typename Tindex
, Tindex Tmax
>
28 /* Tspec must be of NewGRFSpecBase<Tindex>. */
29 static_assert(std::is_base_of_v
<NewGRFSpecBase
<Tindex
>, Tspec
>);
31 uint ui_count
= 0; ///< Number of specs in this class potentially available to the user.
32 Tindex index
= static_cast<Tindex
>(0); ///< Index of class within the list of classes.
33 std::vector
<Tspec
*> spec
; ///< List of specifications.
37 * @note This may be reallocated during initialization so pointers may be invalidated.
39 static inline std::vector
<NewGRFClass
<Tspec
, Tindex
, Tmax
>> classes
;
41 /** Initialise the defaults. */
42 static void InsertDefaults();
45 using spec_type
= Tspec
;
46 using index_type
= Tindex
;
48 uint32_t global_id
; ///< Global ID for class, e.g. 'DFLT', 'WAYP', etc.
49 StringID name
; ///< Name of this class.
51 /* Public constructor as emplace_back needs access. */
52 NewGRFClass(uint32_t global_id
, StringID name
) : global_id(global_id
), name(name
) { }
55 * Get read-only span of specs of this class.
56 * @return Read-only span of specs.
58 std::span
<Tspec
* const> Specs() const { return this->spec
; }
61 * Get read-only span of all classes of this type.
62 * @return Read-only span of classes.
64 static std::span
<NewGRFClass
<Tspec
, Tindex
, Tmax
> const> Classes() { return NewGRFClass::classes
; }
66 void Insert(Tspec
*spec
);
68 Tindex
Index() const { return this->index
; }
69 /** Get the number of allocated specs within the class. */
70 uint
GetSpecCount() const { return static_cast<uint
>(this->spec
.size()); }
71 /** Get the number of potentially user-available specs within the class. */
72 uint
GetUISpecCount() const { return this->ui_count
; }
74 const Tspec
*GetSpec(uint index
) const;
76 /** Check whether the spec will be available to the user at some point in time. */
77 bool IsUIAvailable(uint index
) const;
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
);
86 static const Tspec
*GetByGrf(uint32_t grfid
, uint16_t local_id
);
89 #endif /* NEWGRF_CLASS_H */