Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5653 was 5653, checked in by bensch, 20 years ago

orxonox/trunk: further cleanup

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