Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: dissected LoadParam and Executor completely. Now there is no more LoadParam<Template that noone understands>(many params that are still the same, but cleaner now)
This is a major improvement, since understanding Executor is hard work but loadParam should not be.

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