Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/shell/shell_command_class.cc @ 9692

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

orxonox/new_class_id: some thoughts on a BaseIterator class, that can travers through ObejectLists without knowing the Polymorph type.
This is all virtual, and since templated virutal functions are not allowed, quite hard to implements…
hpe it will work

File size: 6.7 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_SHELL
17
18#include "shell_command_class.h"
19
20#include "shell_command.h"
21
22#include "debug.h"
23#include "compiler.h"
24
25
26
27namespace OrxShell
28{
29  NewObjectListDefinition(ShellCommandClass);
30
31  CmdClassList* ShellCommandClass::commandClassList = NULL;
32
33  /**
34   * @brief creates a new ShellCommandClass
35   * @param className the Name of the command-class to create
36   */
37  ShellCommandClass::ShellCommandClass(const std::string& className)
38      : _className(className)
39  {
40    this->registerObject(this, ShellCommandClass::_objectList);
41    this->setName(className);
42
43    this->classID = CL_NULL;
44
45    if (ShellCommandClass::commandClassList == NULL)
46      ShellCommandClass::commandClassList = new CmdClassList;
47    ShellCommandClass::commandClassList->push_back(this);
48  }
49
50  /**
51   * destructs the shellCommandClass again
52   */
53  ShellCommandClass::~ShellCommandClass()
54  {
55    while(!this->commandList.empty())
56      delete this->commandList.back();
57
58    if (ShellCommandClass::commandClassList != NULL)
59    {
60      CmdClassList::iterator delClass = std::find(ShellCommandClass::commandClassList->begin(), ShellCommandClass::commandClassList->end(), this);
61      if (delClass != ShellCommandClass::commandClassList->end())
62        ShellCommandClass::commandClassList->erase(delClass);
63    }
64  }
65
66  /**
67   * @param command the Command to register.
68   */
69  void ShellCommandClass::registerCommand(ShellCommand* command)
70  {
71    this->commandList.push_back(command);
72  }
73
74  /**
75   * @brief Unregisters a command.
76   * @param command the Command to unregister.
77   */
78  void ShellCommandClass::unregisterCommand(ShellCommand* command)
79  {
80    CmdList::iterator delC = std::find(this->commandList.begin(), this->commandList.end(), command);
81    if (delC != this->commandList.end())
82      this->commandList.erase(delC);
83  }
84
85  /**
86   * @brief unregisters all Commands that exist
87   */
88  void ShellCommandClass::unregisterAllCommands()
89  {
90    // unregister all commands and Classes
91    CmdClassList::iterator classIT;
92    if (ShellCommandClass::commandClassList == NULL)
93      return;
94
95    while (!ShellCommandClass::commandClassList->empty())
96      delete ShellCommandClass::commandClassList->back();
97    delete ShellCommandClass::commandClassList;
98    ShellCommandClass::commandClassList = NULL;
99  }
100
101
102  /**
103   * @brief collects the Commands registered to some class.
104   * @param className the name of the Class to collect the Commands from.
105   * @param stringList a List to paste the Commands into.
106   * @returns true on success, false otherwise
107   */
108  bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList)
109  {
110    if (ShellCommandClass::commandClassList == NULL)
111      return false;
112
113
114    CmdClassList::const_iterator elem;
115    for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++)
116    {
117      if (className == (*elem)->getName())
118      {
119        CmdList::iterator command;
120        for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++)
121          stringList.push_back((*command)->getName());
122        return true;
123      }
124    }
125    return false;
126  }
127
128
129  /**
130   * @brief checks if a Class is already registered to the Commands' class-stack
131   * @param className the Name of the Class to check for
132   * @returns the CommandClass if found, NULL otherwise
133   */
134  ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className)
135  {
136    if (ShellCommandClass::commandClassList == NULL)
137      return false;
138
139
140    CmdClassList::const_iterator classIT;
141    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
142      if (className == (*classIT)->className)
143        return (*classIT);
144    return NULL;
145  }
146
147  /**
148   * @brief checks if a Class is already registered to the Commands' class-stack
149   * @param className the Name of the Class to check for
150   * @returns the CommandClass if found, NULL otherwise
151   */
152  bool ShellCommandClass::exists(const std::string& className)
153  {
154    return (ShellCommandClass::getCommandClass(className) != NULL);
155  }
156
157  ClassID ShellCommandClass::getClassID()
158  {
159    if (this->classID == CL_NULL)
160      this->classID = ClassList::StringToID(this->className);
161    return this->classID;
162  }
163
164
165  /**
166   * @brief searches for a CommandClass
167   * @param className the name of the CommandClass
168   * @returns the CommandClass if found, or a new CommandClass if not
169   */
170  ShellCommandClass* ShellCommandClass::acquireCommandClass(const std::string& className)
171  {
172    ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(className);
173    if (cmdClass != NULL)
174      return (cmdClass);
175    return new ShellCommandClass(className);
176  }
177
178  /**
179   * @brief displays help about ShellCommandClass
180   * @param className: the Class of Commands to show help about
181   */
182  void ShellCommandClass::help(const std::string& className)
183  {
184    if (ShellCommandClass::commandClassList == NULL)
185    {
186      PRINT(0)("No Commands Registered\n");
187      return;
188    }
189    if (className.empty())
190      PRINT(0)("===== Displaying %d registered Classes:\n", ShellCommandClass::commandClassList->size());
191
192
193    CmdClassList::iterator classIT;
194    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
195    {
196      if (className.empty() || className == (*classIT)->className)
197      {
198        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
199        CmdList::const_iterator cmdIT;
200        for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
201        {
202          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getCName(), (*cmdIT)->executor->getParamCount());
203          /// FIXME
204          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
205            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
206          if (!(*cmdIT)->description.empty())
207            PRINT(0)("- %s", (*cmdIT)->description.c_str());
208          PRINT(0)("\n");
209        }
210        if (likely(!className.empty()))
211          return;
212      }
213    }
214    PRINTF(3)("Class '%s' not found in Command's classes\n", className.c_str());
215  }
216}
217
218
219
220
Note: See TracBrowser for help on using the repository browser.