Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/loading/load_param.cc @ 4597

Last change on this file since 4597 was 4597, checked in by bensch, 19 years ago

orxonox/trunk: setClassID implemented in all files

File size: 8.0 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#include "load_param.h"
17
18#include "list.h"
19#include "base_object.h"
20
21#include <stdarg.h>
22
23/**
24   \param object The object this Parameter is loaded too.
25   \param root: the XML-element to load this option from.
26   \param paramName: The name of the parameter loaded.
27   \param paramCount: how many parameters this loading-function takes
28   \param multi: if false LoadParam assumes only one occurence of this parameter in root, if true it assumes multiple occurences.
29   \param ...: the parameter information
30*/
31BaseLoadParam::BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName,
32                             int paramCount, bool multi, ...)
33{
34  this->setClassID(CL_LOAD_PARAM, "LoadParam");
35  this->loadString = NULL;
36
37  if (paramCount == 0)
38    this->loadString = "none";
39  else
40    {
41      if (likely(!multi))
42        this->loadString = grabParameter(root, paramName);
43      else
44        {
45          if (!strcmp(root->Value(), paramName))
46            {
47              const TiXmlNode* val = root->FirstChild();
48              if( val->ToText())
49                this->loadString = val->Value();
50            }
51        }
52    }
53
54  this->paramDesc = NULL;
55  if (LoadClassDescription::parametersDescription)
56    {
57      // locating the class
58      this->classDesc = LoadClassDescription::addClass(object->getClassName());
59      this->paramDesc = this->classDesc->addParam(paramName);
60
61      this->paramDesc->types = new char*[paramCount];
62      this->paramDesc->paramCount = paramCount;
63
64      va_list types;
65      va_start (types, multi);
66      for(int i = 0; i < paramCount; i++)
67        {
68          const char* tmpTypeName = va_arg (types, const char*);
69          this->paramDesc->types[i] = new char[strlen(tmpTypeName)+1];
70          strcpy(this->paramDesc->types[i], tmpTypeName);
71        }
72      va_end(types);
73
74      int argCount = 0;
75    }
76}
77
78/**
79   \param descriptionText The text to set as a description for this Parameter
80   \returns a pointer to itself.
81*/
82BaseLoadParam* BaseLoadParam::describe(const char* descriptionText)
83{
84  if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription())
85    {
86      this->paramDesc->setDescription(descriptionText);
87    }
88  return this;
89}
90
91/**
92   \param paramName the name of the parameter to load
93*/
94LoadParamDescription::LoadParamDescription(const char* paramName)
95{
96  this->types = NULL;
97  this->description = NULL;
98  this->paramName = new char[strlen(paramName)+1];
99  strcpy(this->paramName, paramName);
100}
101
102/**
103   \brief removes all the alocated memory
104*/
105LoadParamDescription::~LoadParamDescription(void)
106{
107  for(int i = 0; i < this->paramCount; i++)
108    {
109      delete this->types[i];
110    }
111  delete []this->types;
112  delete []this->paramName;
113  delete []this->description;
114}
115
116/**
117   \param descriptionText The text to set as a description for this Parameter
118*/
119void LoadParamDescription::setDescription(const char* descriptionText)
120{
121  this->description = new char[strlen(descriptionText)+1];
122  strcpy(this->description, descriptionText);
123}
124
125/**
126   \brief prints out this parameter, its input method and the description (if availiable)
127*/
128void LoadParamDescription::print(void) const
129{
130  PRINT(3)(" <%s>", this->paramName);
131  for (int i = 0; i < this->paramCount; i++)
132    {
133      if (i > 0)
134        PRINT(3)(",");
135      PRINT(3)("%s", this->types[i]);
136    }
137  PRINT(3)("</%s>", this->paramName);
138  if (this->description)
139    PRINT(3)(" -- %s", this->description);
140  PRINT(3)("\n");
141}
142
143/**
144   \brief A list, that holds all the classes that are loadable (classes not objects!!)
145*/
146tList<LoadClassDescription>* LoadClassDescription::classList = new tList<LoadClassDescription>;
147
148/**
149   \brief if the description of Parameters should be executed
150*/
151bool LoadClassDescription::parametersDescription = true;
152
153/**
154   \param className the name of the class to be loadable
155*/
156LoadClassDescription::LoadClassDescription(const char* className)
157{
158  this->className = new char[strlen(className)+1];
159  strcpy(this->className, className);
160
161  classList->add(this);
162
163  this->paramList = new tList<LoadParamDescription>;
164}
165
166/**
167   \brief deletes a classDescription (deletes all the parameterDescriptions as well
168*/
169LoadClassDescription::~LoadClassDescription(void)
170{
171  delete []this->className;
172
173  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
174  LoadParamDescription* enumParamDesc = iterator->nextElement();
175  while (enumParamDesc)
176    {
177      delete enumParamDesc;
178      enumParamDesc = iterator->nextElement();
179    }
180  delete iterator;
181}
182
183/**
184   \brief adds a class to the list of loadable classes
185   \param className The name of the class to add
186
187   this function searches for the className string, and if found just returns the appropriate Class.
188   Otherwise it returns a new classDescription
189*/
190LoadClassDescription* LoadClassDescription::addClass(const char* className)
191{
192  tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
193  LoadClassDescription* enumClassDesc = iterator->nextElement();
194  while (enumClassDesc)
195    {
196      if (!strcmp(enumClassDesc->className, className))
197        {
198          delete iterator;
199          return enumClassDesc;
200        }
201      enumClassDesc = iterator->nextElement();
202    }
203  delete iterator;
204
205  return new LoadClassDescription(className);
206}
207
208/**
209   \brief does the same as addClass(const char* className), but with params
210   \param paramName the name of the parameter to add.
211*/
212LoadParamDescription* LoadClassDescription::addParam(const char* paramName)
213{
214  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
215  LoadParamDescription* enumParamDesc = iterator->nextElement();
216  while (enumParamDesc)
217    {
218      if (!strcmp(enumParamDesc->paramName, paramName))
219        {
220          delete iterator;
221          return enumParamDesc;
222        }
223      enumParamDesc = iterator->nextElement();
224    }
225  delete iterator;
226
227  this->paramList->add(new LoadParamDescription(paramName));
228  return paramList->lastElement();
229}
230
231/**
232   \brief prints out all loadable Classes, and their parameters
233*/
234void LoadClassDescription::printAll(const char* fileName)
235{
236  PRINT(3)("===============================================================\n");
237  PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n");
238  tIterator<LoadClassDescription>* classIT = LoadClassDescription::classList->getIterator();
239  LoadClassDescription* enumClassDesc = classIT->nextElement();
240  while (enumClassDesc)
241    {
242      PRINT(3)("<%s>\n", enumClassDesc->className);
243      tIterator<LoadParamDescription>* paramIT = enumClassDesc->paramList->getIterator();
244      LoadParamDescription* enumParamDesc = paramIT->nextElement();
245      while (enumParamDesc)
246        {
247          enumParamDesc->print();
248          enumParamDesc = paramIT->nextElement();
249        }
250      delete paramIT;
251
252      PRINT(3)("</%s>\n\n", enumClassDesc->className);
253      enumClassDesc = classIT->nextElement();
254    }
255  delete classIT;
256  PRINT(3)("===============================================================\n");
257}
258
259
260
261/**
262   \param root: The XML-element to grab a parameter from
263   \param parameterName: the parameter to grab
264   \returns the Value of the parameter if found, NULL otherwise
265*/
266const char* grabParameter(const TiXmlElement* root, const char* parameterName)
267{
268  const TiXmlElement* element;
269  const TiXmlNode* node;
270
271  if (root == NULL)
272    return NULL;
273  assert( parameterName != NULL);
274
275  element = root->FirstChildElement( parameterName);
276  if( element == NULL) return NULL;
277
278  node = element->FirstChild();
279  while( node != NULL)
280    {
281      if( node->ToText()) return node->Value();
282      node = node->NextSibling();
283    }
284  return NULL;
285}
Note: See TracBrowser for help on using the repository browser.