Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/load_param.cc @ 9727

Last change on this file since 9727 was 9727, checked in by bensch, 18 years ago

orxonox/new_class_id: new Executor construct, that is much more typesafe, faster, and easier to extend…

Also changed the LoadParam process, and adapted ScriptEngine calls

Then at the end, some missing headers appeared, and appended them to all the cc-files again.

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