Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: register unregister much better

File size: 5.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: 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  std::vector<ShellCommandClass*> ShellCommandClass::commandClassList;
29
30  /**
31   * creates a new ShellCommandClass
32   * @param className the Name of the command-class to create
33   */
34  ShellCommandClass::ShellCommandClass(const std::string& className)
35      : className(className)
36  {
37    this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
38    this->setName(className);
39
40    this->classID = CL_NULL;
41
42    ShellCommandClass::commandClassList.push_back(this);
43  }
44
45  /**
46   * destructs the shellCommandClass again
47   */
48  ShellCommandClass::~ShellCommandClass()
49  {
50    while(!this->commandList.empty())
51      delete this->commandList.back();
52  }
53
54  /**
55   * @param command the Command to register.
56   */
57  void ShellCommandClass::registerCommand(ShellCommand* command)
58  {
59    this->commandList.push_back(command);
60  }
61
62  /**
63   * @brief unregister command.
64   * @param command the Command to unregister.
65   */
66  void ShellCommandClass::unregisterCommand(ShellCommand* command)
67  {
68    std::vector<ShellCommand*>::iterator delC = std::find(this->commandList.begin(), this->commandList.end(), command);
69    if (delC != this->commandList.end())
70      this->commandList.erase(delC);
71  }
72
73  /**
74   * @brief unregisters all Commands that exist
75   */
76  void ShellCommandClass::unregisterAllCommands()
77  {
78    // unregister all commands and Classes
79    std::vector<ShellCommandClass*>::iterator classIT;
80    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
81      delete (*classIT);
82  }
83
84
85  /**
86   * @brief collects the Commands registered to some class.
87   * @param className the name of the Class to collect the Commands from.
88   * @param stringList a List to paste the Commands into.
89   * @returns true on success, false otherwise
90   */
91  bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList)
92  {
93    std::vector<ShellCommandClass*>::iterator elem;
94    for(elem = ShellCommandClass::commandClassList.begin(); elem != ShellCommandClass::commandClassList.end(); elem++)
95    {
96      if (className == (*elem)->getName())
97      {
98        std::vector<ShellCommand*>::iterator command;
99        for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++)
100          stringList.push_back((*command)->getName());
101      }
102    }
103    return true;
104  }
105
106
107  /**
108   * @brief checks if a Class is already registered to the Commands' class-stack
109   * @param className the Name of the Class to check for
110   * @returns the CommandClass if found, NULL otherwise
111   */
112  const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className)
113  {
114    std::vector<ShellCommandClass*>::const_iterator classIT;
115    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
116    {
117      if (className == (*classIT)->className)
118      {
119        if ((*classIT)->classID == CL_NULL)
120          (*classIT)->classID = ClassList::StringToID(className);
121
122        return (*classIT);
123      }
124    }
125    return NULL;
126  }
127
128  /**
129   * @brief searches for a CommandClass
130   * @param className the name of the CommandClass
131   * @returns the CommandClass if found, or a new CommandClass if not
132   */
133  ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className)
134  {
135    std::vector<ShellCommandClass*>::iterator classIT;
136    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
137      if (className == (*classIT)->className)
138        return (*classIT);
139    return new ShellCommandClass(className);
140  }
141
142  /**
143   * @brief displays help about ShellCommandClass
144   * @param className: the Class of Commands to show help about
145   */
146  void ShellCommandClass::help(const std::string& className)
147  {
148    std::vector<ShellCommandClass*>::iterator classIT;
149    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
150    {
151      if (className == (*classIT)->className)
152      {
153        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
154        std::vector<ShellCommand*>::const_iterator cmdIT;
155        for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
156        {
157          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
158          /// FIXME
159          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
160            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
161          if (!(*cmdIT)->description.empty())
162            PRINT(0)("- %s", (*cmdIT)->description.c_str());
163          PRINT(0)("\n");
164        }
165        return;
166      }
167    }
168    PRINTF(3)("Class %s not found in Command's classes\n", className.c_str());
169  }
170
171}
172
173
174
175
Note: See TracBrowser for help on using the repository browser.