newgrf_class_func.h

Go to the documentation of this file.
00001 /* $Id: newgrf_class_func.h 20632 2010-08-26 22:01:16Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "newgrf_class.h"
00013 
00014 #include "table/strings.h"
00015 
00016 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
00017   template <typename Tspec, typename Tid, Tid Tmax> \
00018   type NewGRFClass<Tspec, Tid, Tmax>
00019 
00021 template <typename Tspec, typename Tid, Tid Tmax>
00022 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
00023 
00024 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
00025 {
00026   for (Tid i = (Tid)0; i < Tmax; i++) {
00027     classes[i].global_id = 0;
00028     classes[i].name      = STR_EMPTY;
00029     classes[i].count     = 0;
00030 
00031     free(classes[i].spec);
00032     classes[i].spec = NULL;
00033   }
00034 
00035   InsertDefaults();
00036 }
00037 
00038 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
00039 {
00040   for (Tid i = (Tid)0; i < Tmax; i++) {
00041     if (classes[i].global_id == global_id) {
00042       /* ClassID is already allocated, so reuse it. */
00043       return i;
00044     } else if (classes[i].global_id == 0) {
00045       /* This class is empty, so allocate it to the global id. */
00046       classes[i].global_id = global_id;
00047       return i;
00048     }
00049   }
00050 
00051   grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
00052   return (Tid)0;
00053 }
00054 
00055 DEFINE_NEWGRF_CLASS_METHOD(void)::SetName(Tid cls_id, StringID name)
00056 {
00057   assert(cls_id < Tmax);
00058   classes[cls_id].name = name;
00059 }
00060 
00061 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
00062 {
00063   assert(spec->cls_id < Tmax);
00064   NewGRFClass<Tspec, Tid, Tmax> *cls = &classes[spec->cls_id];
00065 
00066   uint i = cls->count++;
00067   cls->spec = ReallocT(cls->spec, cls->count);
00068 
00069   cls->spec[i] = spec;
00070 }
00071 
00072 DEFINE_NEWGRF_CLASS_METHOD(StringID)::GetName(Tid cls_id)
00073 {
00074   assert(cls_id < Tmax);
00075   return classes[cls_id].name;
00076 }
00077 
00078 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetCount()
00079 {
00080   uint i;
00081   for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
00082   return i;
00083 }
00084 
00085 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetCount(Tid cls_id)
00086 {
00087   assert(cls_id < Tmax);
00088   return classes[cls_id].count;
00089 }
00090 
00091 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::Get(Tid cls_id, uint index)
00092 {
00093   assert(cls_id < Tmax);
00094   if (index < classes[cls_id].count) return classes[cls_id].spec[index];
00095 
00096   /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
00097   return NULL;
00098 }
00099 
00100 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
00101 {
00102   uint j;
00103 
00104   for (Tid i = (Tid)0; i < Tmax; i++) {
00105     for (j = 0; j < classes[i].count; j++) {
00106       const Tspec *spec = classes[i].spec[j];
00107       if (spec == NULL) continue;
00108       if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
00109         if (index != NULL) *index = j;
00110         return spec;
00111       }
00112     }
00113   }
00114 
00115   return NULL;
00116 }
00117 
00118 #undef DEFINE_NEWGRF_CLASS_METHOD
00119 
00121 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
00122   template void name::Reset(); \
00123   template Tid name::Allocate(uint32 global_id); \
00124   template void name::SetName(Tid cls_id, StringID name); \
00125   template void name::Assign(Tspec *spec); \
00126   template StringID name::GetName(Tid cls_id); \
00127   template uint name::GetCount(); \
00128   template uint name::GetCount(Tid cls_id); \
00129   template const Tspec *name::Get(Tid cls_id, uint index); \
00130   template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);

Generated on Sun Jan 9 16:01:57 2011 for OpenTTD by  doxygen 1.6.1