Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/physics/src/util/loading/load_param.cc @ 4283

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

orxonox/branches/physics: merged the trunk back to branches/physics
merged with command
svn merge -r 4223:HEAD ../../trunk/ .
conflicts additively included

File size: 6.5 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: Christian Meyer
13   co-programmer: ...
14*/
15
16#include "load_param.h"
17
18#include "list.h"
19#include "base_object.h"
20
21#include <stdarg.h>
22
23/**
24   \param object The object this Parameter is loaded too.
25   \param paramName The name of the parameter loaded.
26   \param paramCount how many parameters this loading-function takes
27   \param ... the parameter information
28*/
29BaseLoadParam::BaseLoadParam(BaseObject* object, const char* paramName, int paramCount, ...)
30{
31  this->paramDesc = NULL;
32  if (LoadClassDescription::parametersDescription)
33    {
34      // locating the class
35      this->classDesc = LoadClassDescription::addClass(object->getClassName());
36      this->paramDesc = this->classDesc->addParam(paramName);
37
38      this->paramDesc->types = new char*[paramCount];
39      this->paramDesc->paramCount = paramCount;
40
41      va_list types;
42      va_start (types, paramCount);
43      for(int i = 0; i < paramCount; i++)
44        {
45          const char* tmpTypeName = va_arg (types, const char*);
46          this->paramDesc->types[i] = new char[strlen(tmpTypeName)+1];
47          strcpy(this->paramDesc->types[i], tmpTypeName);
48        }
49      va_end(types); 
50
51      int argCount = 0;
52    }
53}
54
55/**
56   \param descriptionText The text to set as a description for this Parameter
57   \returns a pointer to itself.
58*/
59BaseLoadParam* BaseLoadParam::describe(const char* descriptionText)
60{
61  if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription())
62    {
63      this->paramDesc->setDescription(descriptionText);
64    }
65  return this;
66}
67
68/**
69   \param paramName the name of the parameter to load
70*/
71LoadParamDescription::LoadParamDescription(const char* paramName)
72{
73  this->types = NULL;
74  this->description = NULL;
75  this->paramName = new char[strlen(paramName)+1];
76  strcpy(this->paramName, paramName);
77}
78
79/**
80   \brief removes all the alocated memory
81*/
82LoadParamDescription::~LoadParamDescription(void)
83{
84  for(int i = 0; i < this->paramCount; i++)
85    {
86      delete this->types[i];
87    }
88  delete []this->types;
89  delete []this->paramName;
90  delete []this->description;
91}
92
93/**
94   \param descriptionText The text to set as a description for this Parameter
95*/
96void LoadParamDescription::setDescription(const char* descriptionText)
97{
98  this->description = new char[strlen(descriptionText)+1];
99  strcpy(this->description, descriptionText);
100}
101
102/**
103   \brief prints out this parameter, its input method and the description (if availiable)
104*/
105void LoadParamDescription::print(void) const
106{
107  PRINT(3)(" <%s>", this->paramName);
108  for (int i = 0; i < this->paramCount; i++)
109    {
110      if (i > 0)
111        PRINT(3)(",");
112      PRINT(3)("%s", this->types[i]);
113    }
114  PRINT(3)("</%s>", this->paramName);
115  if (this->description)
116    PRINT(3)(" -- %s", this->description);
117  PRINT(3)("\n");
118}
119
120/**
121   \brief A list, that holds all the classes that are loadable (classes not objects!!)
122*/
123tList<LoadClassDescription>* LoadClassDescription::classList = new tList<LoadClassDescription>;
124
125/**
126   \brief if the description of Parameters should be executed
127*/
128bool LoadClassDescription::parametersDescription = true;
129
130/**
131   \param className the name of the class to be loadable
132*/
133LoadClassDescription::LoadClassDescription(const char* className)
134{
135  this->className = new char[strlen(className)+1];
136  strcpy(this->className, className);
137
138  classList->add(this);
139
140  this->paramList = new tList<LoadParamDescription>;
141}
142
143/**
144   \brief deletes a classDescription (deletes all the parameterDescriptions as well
145*/
146LoadClassDescription::~LoadClassDescription(void)
147{
148  delete []this->className;
149
150  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
151  LoadParamDescription* enumParamDesc = iterator->nextElement();
152  while (enumParamDesc)
153    {
154      delete enumParamDesc;
155      enumParamDesc = iterator->nextElement();
156    }
157  delete iterator;
158}
159
160/**
161   \brief adds a class to the list of loadable classes
162   \param className The name of the class to add
163
164   this function searches for the className string, and if found just returns the appropriate Class.
165   Otherwise it returns a new classDescription
166*/
167LoadClassDescription* LoadClassDescription::addClass(const char* className)
168{
169  tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
170  LoadClassDescription* enumClassDesc = iterator->nextElement();
171  while (enumClassDesc)
172    {
173      if (!strcmp(enumClassDesc->className, className))
174        {
175          delete iterator;
176          return enumClassDesc;
177        }
178      enumClassDesc = iterator->nextElement();
179    }
180  delete iterator;
181
182  return new LoadClassDescription(className);
183}
184
185/**
186   \brief does the same as addClass(const char* className), but with params
187   \param paramName the name of the parameter to add.
188*/
189LoadParamDescription* LoadClassDescription::addParam(const char* paramName)
190{
191  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
192  LoadParamDescription* enumParamDesc = iterator->nextElement();
193  while (enumParamDesc)
194    {
195      if (!strcmp(enumParamDesc->paramName, paramName))
196        {
197          delete iterator;
198          return enumParamDesc;
199        }
200      enumParamDesc = iterator->nextElement();
201    }
202  delete iterator;
203
204  this->paramList->add(new LoadParamDescription(paramName));
205  return paramList->lastElement();
206}
207
208/**
209   \brief prints out all loadable Classes, and their parameters
210*/
211void LoadClassDescription::printAll(const char* fileName)
212{
213  PRINT(3)("===============================================================\n");
214  PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n");
215  tIterator<LoadClassDescription>* classIT = LoadClassDescription::classList->getIterator();
216  LoadClassDescription* enumClassDesc = classIT->nextElement();
217  while (enumClassDesc)
218    {
219      PRINT(3)("<%s>\n", enumClassDesc->className);
220      tIterator<LoadParamDescription>* paramIT = enumClassDesc->paramList->getIterator();
221      LoadParamDescription* enumParamDesc = paramIT->nextElement();
222      while (enumParamDesc)
223        {
224          enumParamDesc->print();
225          enumParamDesc = paramIT->nextElement();
226        }
227      delete paramIT;
228
229      PRINT(3)("</%s>\n\n", enumClassDesc->className);
230      enumClassDesc = classIT->nextElement();
231    }
232  delete classIT;
233  PRINT(3)("===============================================================\n");
234}
Note: See TracBrowser for help on using the repository browser.