Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better LoadParam

File size: 7.5 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#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
17
18#include "load_param.h"
19#include "load_param_description.h"
20
21#include "list.h"
22
23#include <stdarg.h>
24
25/**
26 * Constructs a new LoadParameter
27 * @param root the XML-element to load this Parameter from
28 * @param paramName the Parameter to load
29 * @param object the BaseObject, to load this parameter on to (will be cast to executor's Parameter)
30 * @param executor the Executor, that executes the loading procedure.
31 */
32CLoadParam::CLoadParam(const TiXmlElement* root, const char* paramName, BaseObject* object, const Executor& executor, bool inLoadCycle)
33{
34  this->paramName = paramName;
35  this->object = object;
36  this->inLoadCycle = inLoadCycle;
37
38  // determin the LoadString.
39  if (likely(!inLoadCycle))
40    this->loadElem = grabParameterElement(root, paramName);
41  //this->loadString = grabParameter(root, paramName);
42  else if (!strcmp(root->Value(), paramName))
43    this->loadElem = (TiXmlElement*)root->FirstChild();
44  else
45    this->loadElem = NULL;
46
47  // set the Executor.
48  this->executor = executor.clone();
49}
50
51/**
52 * This is a VERY SPECIAL deconsrtuctor.
53 * It is made, so that it loads the Parameters on destruction.
54 * meaning, if an Executor a valid Object exist, and all
55 * Execution-Conditions are met, they are executed here.
56 */
57CLoadParam::~CLoadParam()
58{
59  if (likely(this->executor != NULL))
60  {
61    if (this->loadElem != NULL &&  this->loadElem->ToText())
62      this->loadString = this->loadElem->Value();
63    else
64      this->loadString = NULL;
65    if (likely(this->object != NULL) &&
66        ( this->loadString != NULL ||
67         ((this->executor->getType() & Executor_NoLoadString) == Executor_NoLoadString)))
68    {
69      PRINTF(4)("Loading '%s' with Parameters '%s' onto: '%s'(%s)\n", this->paramName, this->loadString, this->object->getName(), this->object->getClassName());
70      this->executor->execute(this->object, this->loadString);
71    }
72    delete this->executor;
73  }
74}
75
76/**
77 * set the default values of the executor
78 * @param count how many default values to set.
79 * @param ... the default values !! must be at least count parameters!!
80 */
81CLoadParam& CLoadParam::defaultValues(unsigned int count, ...)
82{
83  va_list values;
84  va_start(values, count);
85
86  assert(executor != NULL);
87  this->executor->defaultValues(count, values);
88
89  return *this;
90}
91
92
93
94/**
95 * @param descriptionText The text to set as a description for this Parameter
96 * @returns a pointer to itself.
97*/
98CLoadParam& CLoadParam::describe(const char* descriptionText)
99{
100  if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription())
101    {
102      this->paramDesc->setDescription(descriptionText);
103    }
104  return *this;
105}
106
107// const LoadParamDescription* LoadParamDescription::getClass(const char* className)
108// {
109//   tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
110//   LoadClassDescription* enumClassDesc = iterator->firstElement();
111//   while (enumClassDesc)
112//   {
113//     if (!strcmp(enumClassDesc->className, classNameBegin, className))
114//     {
115//       delete iterator;
116//       return enumClassDesc;
117//     }
118//     enumClassDesc = iterator->nextElement();
119//   }
120//   delete iterator;
121//
122//   return NULL;
123// }
124
125
126
127
128/*
129 * @param object The object this Parameter is loaded too.
130 * @param root: the XML-element to load this option from.
131 * @param paramName: The name of the parameter loaded.
132 * @param paramCount: how many parameters this loading-function takes
133 * @param multi: if false LoadParam assumes only one occurence of this parameter in root, if true it assumes multiple occurences.
134 * @param ...: the parameter information (1. Parameter, 2. Default Value for the Parameter, ...)
135*/
136/*LoadParam::LoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName,
137                             int paramCount, bool multi, const void* pointerToParam, ...)
138{
139  this->setClassID(CL_LOAD_PARAM, "LoadParam");
140  this->executor = NULL;
141
142  this->loadString = NULL;
143  this->pointerToParam = pointerToParam;
144
145  if (paramCount == 0 || this->pointerToParam != NULL)
146    this->loadString = "none";
147  else
148{
149      if (likely(!multi))
150        this->loadString = grabParameter(root, paramName);
151      else
152{
153          if (!strcmp(root->Value(), paramName))
154{
155              const TiXmlNode* val = root->FirstChild();
156              if( val->ToText())
157                this->loadString = val->Value();
158}
159}
160}
161
162  this->paramDesc = NULL;
163  if (LoadClassDescription::parametersDescription)
164{
165    // locating the class
166    this->classDesc = LoadClassDescription::addClass(object->getClassName());
167
168    if ((this->paramDesc = this->classDesc->addParam(paramName)) != NULL)
169{
170
171      this->paramDesc->paramCount = paramCount;
172      this->paramDesc->types = new int[paramCount];
173      this->paramDesc->defaultValues = new char*[paramCount];
174
175      va_list types;
176      va_start (types, pointerToParam);
177      char defaultVal[512];
178      for(int i = 0; i < paramCount; i++)
179{
180        defaultVal[0] = '\0';
181          // parameters parsed
182        int tmpType = va_arg (types, int);
183        this->paramDesc->types[i] = tmpType;
184        switch (tmpType)
185{
186  case MT_INT:
187            sprintf(defaultVal, "%d", va_arg(types, int));
188            break;
189//          case MT_LONG:
190//            sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE));
191//            break;
192  case MT_FLOAT:
193            sprintf(defaultVal, "%0.3f", va_arg(types, double));
194            break;
195  case MT_STRING:
196            sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE));
197            break;
198  case MT_EXT1:
199            sprintf(defaultVal, "");
200            break;
201}
202        this->paramDesc->defaultValues[i] = new char[strlen(defaultVal)+1];
203        strcpy(this->paramDesc->defaultValues[i], defaultVal);
204}
205      va_end(types);
206
207      int argCount = 0;
208}
209}
210}*/
211
212
213
214
215
216
217
218
219
220
221//////////////////////
222// HELPER FUNCTIONS //
223//////////////////////
224/**
225 * @param root: The XML-element to grab a parameter from
226 * @param parameterName: the parameter to grab
227 * @returns the Value of the parameter if found, NULL otherwise
228*/
229const char* grabParameter(const TiXmlElement* root, const char* parameterName)
230{
231  const TiXmlElement* element;
232  const TiXmlNode* node;
233
234  if (root == NULL || parameterName == NULL)
235    return NULL;
236  assert( parameterName != NULL);
237
238  element = root->FirstChildElement( parameterName);
239  if( element == NULL) return NULL;
240
241  node = element->FirstChild();
242  while( node != NULL)
243    {
244      if( node->ToText()) return node->Value();
245      node = node->NextSibling();
246    }
247  return NULL;
248}
249
250/**
251 * @param root: The XML-element to grab a parameter from
252 * @param parameterName: the parameter to grab
253 * @returns the Element of the parameter if found, NULL otherwise
254 */
255const TiXmlElement* grabParameterElement(const TiXmlElement* root, const char* parameterName)
256{
257  const TiXmlElement* element;
258  const TiXmlNode* node;
259
260  if (root == NULL || parameterName == NULL)
261    return NULL;
262  assert( parameterName != NULL);
263
264  element = root->FirstChildElement( parameterName);
265  if( element == NULL) return NULL;
266
267  node = element->FirstChild();
268  while( node != NULL)
269  {
270    if( node->ToText()) return (TiXmlElement*)node;
271    node = node->NextSibling();
272  }
273  return NULL;
274}
275
276
277
Note: See TracBrowser for help on using the repository browser.