Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: list to vector

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