Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: first Element gets loaded through the new LoadParam procedure.

File size: 5.2 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#include "functor_list.h"
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 * @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, ...)
32*/
33LoadParamBase::LoadParamBase(const TiXmlElement* root, BaseObject* object, const char* paramName,
34                             int paramCount, bool multi, const void* pointerToParam, ...)
35{
36  this->setClassID(CL_LOAD_PARAM, "LoadParam");
37  this->executor = NULL;
38
39  this->loadString = NULL;
40  this->pointerToParam = pointerToParam;
41
42  if (paramCount == 0 || this->pointerToParam != NULL)
43    this->loadString = "none";
44  else
45    {
46      if (likely(!multi))
47        this->loadString = grabParameter(root, paramName);
48      else
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        }
57    }
58
59  this->paramDesc = NULL;
60  if (LoadClassDescription::parametersDescription)
61  {
62    // locating the class
63    this->classDesc = LoadClassDescription::addClass(object->getClassName());
64
65    if ((this->paramDesc = this->classDesc->addParam(paramName)) != NULL)
66    {
67
68      this->paramDesc->paramCount = paramCount;
69      this->paramDesc->types = new int[paramCount];
70      this->paramDesc->defaultValues = new char*[paramCount];
71
72      va_list types;
73      va_start (types, pointerToParam);
74      char defaultVal[512];
75      for(int i = 0; i < paramCount; i++)
76      {
77        defaultVal[0] = '\0';
78          // parameters parsed
79        int tmpType = va_arg (types, int);
80        this->paramDesc->types[i] = tmpType;
81        switch (tmpType)
82        {
83          case MT_INT:
84            sprintf(defaultVal, "%d", va_arg(types, int));
85            break;
86/*          case MT_LONG:
87            sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE));
88            break;*/
89          case MT_FLOAT:
90            sprintf(defaultVal, "%0.3f", va_arg(types, double));
91            break;
92          case MT_STRING:
93            sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE));
94            break;
95          case MT_EXT1:
96            sprintf(defaultVal, "");
97            break;
98        }
99        this->paramDesc->defaultValues[i] = new char[strlen(defaultVal)+1];
100        strcpy(this->paramDesc->defaultValues[i], defaultVal);
101      }
102      va_end(types);
103
104      int argCount = 0;
105    }
106  }
107}
108
109LoadParamBase::LoadParamBase(const TiXmlElement* root, const char* paramName, BaseObject* object, const Executor& executor)
110{
111  this->loadString = grabParameter(root, paramName);
112
113  if (loadString != NULL && root != NULL)
114  {
115    this->executor = executor.clone();
116    this->executor->execute(object, loadString);
117  }
118  else
119  {
120    this->executor = NULL;
121  }
122}
123LoadParamBase::~LoadParamBase()
124{
125  if (likely(this->executor != NULL))
126    delete this->executor;
127
128}
129
130
131
132/**
133 * @param descriptionText The text to set as a description for this Parameter
134 * @returns a pointer to itself.
135*/
136LoadParamBase* LoadParamBase::describe(const char* descriptionText)
137{
138  if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription())
139    {
140      this->paramDesc->setDescription(descriptionText);
141    }
142  return this;
143}
144
145// const LoadParamDescription* LoadParamDescription::getClass(const char* className)
146// {
147//   tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
148//   LoadClassDescription* enumClassDesc = iterator->firstElement();
149//   while (enumClassDesc)
150//   {
151//     if (!strcmp(enumClassDesc->className, classNameBegin, className))
152//     {
153//       delete iterator;
154//       return enumClassDesc;
155//     }
156//     enumClassDesc = iterator->nextElement();
157//   }
158//   delete iterator;
159//
160//   return NULL;
161// }
162
163/**
164 * @param root: The XML-element to grab a parameter from
165 * @param parameterName: the parameter to grab
166 * @returns the Value of the parameter if found, NULL otherwise
167*/
168const char* grabParameter(const TiXmlElement* root, const char* parameterName)
169{
170  const TiXmlElement* element;
171  const TiXmlNode* node;
172
173  if (root == NULL)
174    return NULL;
175  assert( parameterName != NULL);
176
177  element = root->FirstChildElement( parameterName);
178  if( element == NULL) return NULL;
179
180  node = element->FirstChild();
181  while( node != NULL)
182    {
183      if( node->ToText()) return node->Value();
184      node = node->NextSibling();
185    }
186  return NULL;
187}
Note: See TracBrowser for help on using the repository browser.