Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc @ 10464

Last change on this file since 10464 was 10354, checked in by landauf, 9 years ago

moved code from static initializer into new function in ConsoleCommandManager

File size: 8.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @brief Implementation of the ConsoleCommand class.
32*/
33
34#include "ConsoleCommandManager.h"
35
36#include "ConsoleCommand.h"
37#include "util/StringUtils.h"
38
39namespace orxonox
40{
41    /**
42        @brief Returns the command with given group an name.
43        @param group The group of the requested command
44        @param name The group of the requested command
45        @param bPrintError If true, an error is printed if the command doesn't exist
46    */
47    /* static */ ConsoleCommand* ConsoleCommandManager::getCommand(const std::string& group, const std::string& name, bool bPrintError)
48    {
49        // find the group
50        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMap().find(group);
51        if (it_group != ConsoleCommandManager::getCommandMap().end())
52        {
53            // find the name
54            std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
55            if (it_name != it_group->second.end())
56            {
57                // return the pointer
58                return it_name->second;
59            }
60        }
61        if (bPrintError)
62        {
63            if (group == "")
64                orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
65            else
66                orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
67        }
68        return 0;
69    }
70
71    /**
72        @brief Returns the command with given group an name in lowercase.
73        @param group The group of the requested command in lowercase
74        @param name The group of the requested command in lowercase
75        @param bPrintError If true, an error is printed if the command doesn't exist
76    */
77    /* static */ ConsoleCommand* ConsoleCommandManager::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
78    {
79        std::string groupLC = getLowercase(group);
80        std::string nameLC = getLowercase(name);
81
82        // find the group
83        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMapLC().find(groupLC);
84        if (it_group != ConsoleCommandManager::getCommandMapLC().end())
85        {
86            // find the name
87            std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC);
88            if (it_name != it_group->second.end())
89            {
90                // return the pointer
91                return it_name->second;
92            }
93        }
94        if (bPrintError)
95        {
96            if (group == "")
97                orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
98            else
99                orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
100        }
101        return 0;
102    }
103
104    /**
105        @brief Returns the static map that stores all console commands.
106    */
107    /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMap()
108    {
109        static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap;
110        return commandMap;
111    }
112
113    /**
114        @brief Returns the static map that stores all console commands in lowercase.
115    */
116    /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMapLC()
117    {
118        static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC;
119        return commandMapLC;
120    }
121
122    /**
123        @brief Registers a new command with the groups and names that are defined by ConsoleCommand::getNames().
124    */
125    /* static */ void ConsoleCommandManager::registerCommand(ConsoleCommand* command)
126    {
127        for (size_t i = 0; i < command->getNames().size(); ++i)
128        {
129            const ConsoleCommand::CommandName& name = command->getNames()[i];
130            ConsoleCommandManager::registerCommand(name.group_, name.name_, command);
131        }
132    }
133
134    /**
135        @brief Registers a new command with given group an name by adding it to the command map.
136    */
137    /* static */ void ConsoleCommandManager::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
138    {
139        if (name == "")
140            return;
141
142        // check if a command with this name already exists
143        if (ConsoleCommandManager::getCommand(group, name) != 0)
144        {
145            if (group == "")
146                orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl;
147            else
148                orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl;
149        }
150        else
151        {
152            // add the command to the map
153            ConsoleCommandManager::getCommandMap()[group][name] = command;
154            ConsoleCommandManager::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command;
155        }
156    }
157
158    /**
159        @brief Removes the command from the command map.
160    */
161    /* static */ void ConsoleCommandManager::unregisterCommand(ConsoleCommand* command)
162    {
163        // iterate through all groups
164        for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMap().begin(); it_group != ConsoleCommandManager::getCommandMap().end(); )
165        {
166            // iterate through all commands of each group
167            for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
168            {
169                // erase the command
170                if (it_name->second == command)
171                    it_group->second.erase(it_name++);
172                else
173                    ++it_name;
174            }
175
176            // erase the group if it is empty now
177            if (it_group->second.empty())
178                ConsoleCommandManager::getCommandMap().erase(it_group++);
179            else
180                ++it_group;
181        }
182
183        // now the same for the lowercase-map:
184
185        // iterate through all groups
186        for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMapLC().begin(); it_group != ConsoleCommandManager::getCommandMapLC().end(); )
187        {
188            // iterate through all commands of each group
189            for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
190            {
191                // erase the command
192                if (it_name->second == command)
193                    it_group->second.erase(it_name++);
194                else
195                    ++it_name;
196            }
197
198            // erase the group if it is empty now
199            if (it_group->second.empty())
200                ConsoleCommandManager::getCommandMapLC().erase(it_group++);
201            else
202                ++it_group;
203        }
204    }
205
206    /**
207        @brief Deletes all commands
208    */
209    /* static */ void ConsoleCommandManager::destroyAll()
210    {
211        // delete entries until the map is empty
212        while (!ConsoleCommandManager::getCommandMap().empty() && !ConsoleCommandManager::getCommandMap().begin()->second.empty())
213            delete ConsoleCommandManager::getCommandMap().begin()->second.begin()->second;
214    }
215}
Note: See TracBrowser for help on using the repository browser.