Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9824 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
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[7374]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
[1853]17
[5639]18#include "shell_command_class.h"
19
[5129]20#include "shell_command.h"
[1853]21
[5129]22#include "debug.h"
[5113]23#include "class_list.h"
[5780]24#include "compiler.h"
[5113]25
[7742]26
27
[7374]28namespace OrxShell
29{
[7743]30  CmdClassList* ShellCommandClass::commandClassList = NULL;
[5639]31
[7374]32  /**
[7407]33   * @brief creates a new ShellCommandClass
[7374]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);
[5188]41
[7374]42    this->classID = CL_NULL;
[5170]43
[7743]44    if (ShellCommandClass::commandClassList == NULL)
45      ShellCommandClass::commandClassList = new CmdClassList;
46    ShellCommandClass::commandClassList->push_back(this);
[7374]47  }
[5170]48
[7374]49  /**
50   * destructs the shellCommandClass again
51   */
52  ShellCommandClass::~ShellCommandClass()
[5170]53  {
[7388]54    while(!this->commandList.empty())
55      delete this->commandList.back();
[7743]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    }
[5170]63  }
64
[7374]65  /**
[7390]66   * @param command the Command to register.
[7374]67   */
[7390]68  void ShellCommandClass::registerCommand(ShellCommand* command)
[7374]69  {
[7390]70    this->commandList.push_back(command);
[5189]71  }
72
[7374]73  /**
[7742]74   * @brief Unregisters a command.
[7390]75   * @param command the Command to unregister.
76   */
77  void ShellCommandClass::unregisterCommand(ShellCommand* command)
78  {
[7742]79    CmdList::iterator delC = std::find(this->commandList.begin(), this->commandList.end(), command);
[7390]80    if (delC != this->commandList.end())
81      this->commandList.erase(delC);
82  }
83
84  /**
[7389]85   * @brief unregisters all Commands that exist
[7374]86   */
87  void ShellCommandClass::unregisterAllCommands()
88  {
[7394]89    // unregister all commands and Classes
[7742]90    CmdClassList::iterator classIT;
[7743]91    if (ShellCommandClass::commandClassList == NULL)
92      return;
[7742]93
[7743]94    while (!ShellCommandClass::commandClassList->empty())
95      delete ShellCommandClass::commandClassList->back();
96    delete ShellCommandClass::commandClassList;
97    ShellCommandClass::commandClassList = NULL;
[7374]98  }
99
[7390]100
[7374]101  /**
[7390]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  {
[7743]109    if (ShellCommandClass::commandClassList == NULL)
110      return false;
111
112
[7742]113    CmdClassList::const_iterator elem;
[7743]114    for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++)
[7390]115    {
116      if (className == (*elem)->getName())
117      {
[7742]118        CmdList::iterator command;
[7390]119        for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++)
120          stringList.push_back((*command)->getName());
[7742]121        return true;
[7390]122      }
123    }
[7742]124    return false;
[7390]125  }
126
127
128  /**
[7389]129   * @brief checks if a Class is already registered to the Commands' class-stack
[7374]130   * @param className the Name of the Class to check for
131   * @returns the CommandClass if found, NULL otherwise
132   */
[7742]133  ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className)
[5170]134  {
[7743]135    if (ShellCommandClass::commandClassList == NULL)
136      return false;
137
138
[7742]139    CmdClassList::const_iterator classIT;
[7743]140    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[7374]141      if (className == (*classIT)->className)
142        return (*classIT);
143    return NULL;
[5170]144  }
145
[7374]146  /**
[7408]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   */
[7411]151  bool ShellCommandClass::exists(const std::string& className)
[7408]152  {
[7411]153    return (ShellCommandClass::getCommandClass(className) != NULL);
154  }
[7408]155
[7411]156  ClassID ShellCommandClass::getClassID()
157  {
158    if (this->classID == CL_NULL)
159      this->classID = ClassList::StringToID(this->className);
160    return this->classID;
161  }
[7408]162
[7411]163
[7408]164  /**
[7390]165   * @brief searches for a CommandClass
[7374]166   * @param className the name of the CommandClass
167   * @returns the CommandClass if found, or a new CommandClass if not
168   */
[7408]169  ShellCommandClass* ShellCommandClass::acquireCommandClass(const std::string& className)
[7374]170  {
[7742]171    ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(className);
172    if (cmdClass != NULL)
173      return (cmdClass);
[7374]174    return new ShellCommandClass(className);
[5170]175  }
176
[7374]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)
[5204]182  {
[7743]183    if (ShellCommandClass::commandClassList == NULL)
184    {
185      PRINT(0)("No Commands Registered\n");
186      return;
187    }
[7742]188    if (className.empty())
[7743]189      PRINT(0)("===== Displaying %d registered Classes:\n", ShellCommandClass::commandClassList->size());
[7742]190
191
192    CmdClassList::iterator classIT;
[7743]193    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5204]194    {
[7742]195      if (className.empty() || className == (*classIT)->className)
[5204]196      {
[7394]197        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
[7742]198        CmdList::const_iterator cmdIT;
[7394]199        for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
[5204]200        {
[9406]201          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getCName(), (*cmdIT)->executor->getParamCount());
[7394]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");
[5204]208        }
[7742]209        if (likely(!className.empty()))
210          return;
[5204]211      }
212    }
[7742]213    PRINTF(3)("Class '%s' not found in Command's classes\n", className.c_str());
[5204]214  }
215}
[7388]216
217
218
219
Note: See TracBrowser for help on using the repository browser.