Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cycle-loading of LoadParam works…

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