Add templated versions of CeilDiv and Ceil maths functions
[openttd-joker.git] / src / newgrf_class_func.h
bloba28ed2167bf0b816da764f5f85ca4b22bb5ce3fb
1 /* $Id: newgrf_class_func.h 24170 2012-04-22 16:28:32Z frosch $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file newgrf_class_func.h Implementation of the NewGRF class' functions. */
12 #include "newgrf_class.h"
14 #include "table/strings.h"
16 /**
17 * Helper for defining the class method's signatures.
18 * @param type The type of the class.
20 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
21 template <typename Tspec, typename Tid, Tid Tmax> \
22 type NewGRFClass<Tspec, Tid, Tmax>
24 /** Instantiate the array. */
25 template <typename Tspec, typename Tid, Tid Tmax>
26 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
28 /** Reset the class, i.e. clear everything. */
29 DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
31 this->global_id = 0;
32 this->name = STR_EMPTY;
33 this->count = 0;
34 this->ui_count = 0;
36 free(this->spec);
37 this->spec = NULL;
40 /** Reset the classes, i.e. clear everything. */
41 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
43 for (Tid i = (Tid)0; i < Tmax; i++) {
44 classes[i].ResetClass();
47 InsertDefaults();
50 /**
51 * Allocate a class with a given global class ID.
52 * @param cls_id The global class id, such as 'DFLT'.
53 * @return The (non global!) class ID for the class.
54 * @note Upon allocating the same global class ID for a
55 * second time, this first allocation will be given.
57 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
59 for (Tid i = (Tid)0; i < Tmax; i++) {
60 if (classes[i].global_id == global_id) {
61 /* ClassID is already allocated, so reuse it. */
62 return i;
63 } else if (classes[i].global_id == 0) {
64 /* This class is empty, so allocate it to the global id. */
65 classes[i].global_id = global_id;
66 return i;
70 grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
71 return (Tid)0;
74 /**
75 * Insert a spec into the class.
76 * @param spec The spec to insert.
78 DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
80 uint i = this->count++;
81 this->spec = ReallocT(this->spec, this->count);
83 this->spec[i] = spec;
85 if (this->IsUIAvailable(i)) this->ui_count++;
88 /**
89 * Assign a spec to one of the classes.
90 * @param spec The spec to assign.
91 * @note The spec must have a valid class id set.
93 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
95 assert(spec->cls_id < Tmax);
96 Get(spec->cls_id)->Insert(spec);
99 /**
100 * Get a particular class.
101 * @param cls_id The id for the class.
102 * @pre cls_id < Tmax
104 template <typename Tspec, typename Tid, Tid Tmax>
105 NewGRFClass<Tspec, Tid, Tmax> *NewGRFClass<Tspec, Tid, Tmax>::Get(Tid cls_id)
107 assert(cls_id < Tmax);
108 return classes + cls_id;
112 * Get the number of allocated classes.
113 * @return The number of classes.
115 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount()
117 uint i;
118 for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
119 return i;
123 * Get the number of classes available to the user.
124 * @return The number of classes.
126 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount()
128 uint cnt = 0;
129 for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
130 if (classes[i].GetUISpecCount() > 0) cnt++;
132 return cnt;
136 * Get the nth-class with user available specs.
137 * @param index UI index of a class.
138 * @return The class ID of the class.
140 DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index)
142 for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
143 if (classes[i].GetUISpecCount() == 0) continue;
144 if (index-- == 0) return (Tid)i;
146 NOT_REACHED();
150 * Get a spec from the class at a given index.
151 * @param index The index where to find the spec.
152 * @return The spec at given location.
154 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const
156 /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
157 return index < this->GetSpecCount() ? this->spec[index] : NULL;
161 * Translate a UI spec index into a spec index.
162 * @param ui_index UI index of the spec.
163 * @return index of the spec, or -1 if out of range.
165 DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const
167 if (ui_index < 0) return -1;
168 for (uint i = 0; i < this->GetSpecCount(); i++) {
169 if (!this->IsUIAvailable(i)) continue;
170 if (ui_index-- == 0) return i;
172 return -1;
176 * Translate a spec index into a UI spec index.
177 * @param index index of the spec.
178 * @return UI index of the spec, or -1 if out of range.
180 DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const
182 if ((uint)index >= this->GetSpecCount()) return -1;
183 uint ui_index = 0;
184 for (int i = 0; i < index; i++) {
185 if (this->IsUIAvailable(i)) ui_index++;
187 return ui_index;
191 * Retrieve a spec by GRF location.
192 * @param grfid GRF ID of spec.
193 * @param local_id Index within GRF file of spec.
194 * @param index Pointer to return the index of the spec in its class. If NULL then not used.
195 * @return The spec.
197 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
199 uint j;
201 for (Tid i = (Tid)0; i < Tmax; i++) {
202 for (j = 0; j < classes[i].count; j++) {
203 const Tspec *spec = classes[i].spec[j];
204 if (spec == NULL) continue;
205 if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
206 if (index != NULL) *index = j;
207 return spec;
212 return NULL;
215 #undef DEFINE_NEWGRF_CLASS_METHOD
217 /** Force instantiation of the methods so we don't get linker errors. */
218 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
219 template void name::ResetClass(); \
220 template void name::Reset(); \
221 template Tid name::Allocate(uint32 global_id); \
222 template void name::Insert(Tspec *spec); \
223 template void name::Assign(Tspec *spec); \
224 template NewGRFClass<Tspec, Tid, Tmax> *name::Get(Tid cls_id); \
225 template uint name::GetClassCount(); \
226 template uint name::GetUIClassCount(); \
227 template Tid name::GetUIClass(uint index); \
228 template const Tspec *name::GetSpec(uint index) const; \
229 template int name::GetUIFromIndex(int index) const; \
230 template int name::GetIndexFromUI(int ui_index) const; \
231 template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);