Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trying to fix a strange bug of the static initialisation

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