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 
8 /** @file newgrf_class.h Header file for classes to be used by e.g. NewGRF stations and airports */
9 
10 #ifndef NEWGRF_CLASS_H
11 #define NEWGRF_CLASS_H
12 
13 #include "strings_type.h"
14 
15 /**
16  * Struct containing information relating to NewGRF classes for stations and airports.
17  */
18 template <typename Tspec, typename Tid, Tid Tmax>
19 struct NewGRFClass {
20 private:
21 	uint count;       ///< Number of specs in this class.
22 	uint ui_count;    ///< Number of specs in this class potentially available to the user.
23 	Tspec **spec;     ///< Array of specifications.
24 
25 	/**
26 	 * The actual classes.
27 	 * @note We store pointers to members of this array in various places outside this class (e.g. to 'name' for GRF string resolving).
28 	 *       Thus this must be a static array, and cannot be a self-resizing vector or similar.
29 	 */
30 	static NewGRFClass<Tspec, Tid, Tmax> classes[Tmax];
31 
32 	void ResetClass();
33 
34 	/** Initialise the defaults. */
35 	static void InsertDefaults();
36 
37 public:
38 	uint32 global_id; ///< Global ID for class, e.g. 'DFLT', 'WAYP', etc.
39 	StringID name;    ///< Name of this class.
40 
41 	void Insert(Tspec *spec);
42 
43 	/** Get the number of allocated specs within the class. */
GetSpecCountNewGRFClass44 	uint GetSpecCount() const { return this->count; }
45 	/** Get the number of potentially user-available specs within the class. */
GetUISpecCountNewGRFClass46 	uint GetUISpecCount() const { return this->ui_count; }
47 	int GetUIFromIndex(int index) const;
48 	int GetIndexFromUI(int ui_index) const;
49 
50 	const Tspec *GetSpec(uint index) const;
51 
52 	/** Check whether the spec will be available to the user at some point in time. */
53 	bool IsUIAvailable(uint index) const;
54 
55 	static void Reset();
56 	static Tid Allocate(uint32 global_id);
57 	static void Assign(Tspec *spec);
58 	static uint GetClassCount();
59 	static uint GetUIClassCount();
60 	static Tid GetUIClass(uint index);
61 	static NewGRFClass *Get(Tid cls_id);
62 
63 	static const Tspec *GetByGrf(uint32 grfid, byte local_id, int *index);
64 };
65 
66 #endif /* NEWGRF_CLASS_H */
67