Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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