Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_command_class.cc @ 7388

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

orxonox/trunk: fixed a notNULL definition

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