Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

StaticallyInitializedInstances are now responsible to delete the wrapped object.

File size: 7.4 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    /* static */ ConsoleCommandManager& ConsoleCommandManager::getInstance()
42    {
43        static ConsoleCommandManager instance;
44        return instance;
45    }
46
47    /**
48        @brief Returns the command with given group an name.
49        @param group The group of the requested command
50        @param name The group of the requested command
51        @param bPrintError If true, an error is printed if the command doesn't exist
52    */
53    ConsoleCommand* ConsoleCommandManager::getCommand(const std::string& group, const std::string& name, bool bPrintError)
54    {
55        // find the group
56        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = this->commandMap_.find(group);
57        if (it_group != this->commandMap_.end())
58        {
59            // find the name
60            std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
61            if (it_name != it_group->second.end())
62            {
63                // return the pointer
64                return it_name->second;
65            }
66        }
67        if (bPrintError)
68        {
69            if (group == "")
70                orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
71            else
72                orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
73        }
74        return 0;
75    }
76
77    /**
78        @brief Returns the command with given group an name in lowercase.
79        @param group The group of the requested command in lowercase
80        @param name The group of the requested command in lowercase
81        @param bPrintError If true, an error is printed if the command doesn't exist
82    */
83    ConsoleCommand* ConsoleCommandManager::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
84    {
85        std::string groupLC = getLowercase(group);
86        std::string nameLC = getLowercase(name);
87
88        // find the group
89        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = this->commandMapLC_.find(groupLC);
90        if (it_group != this->commandMapLC_.end())
91        {
92            // find the name
93            std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC);
94            if (it_name != it_group->second.end())
95            {
96                // return the pointer
97                return it_name->second;
98            }
99        }
100        if (bPrintError)
101        {
102            if (group == "")
103                orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
104            else
105                orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
106        }
107        return 0;
108    }
109
110    /**
111        @brief Registers a new command with the groups and names that are defined by ConsoleCommand::getNames().
112    */
113    void ConsoleCommandManager::registerCommand(ConsoleCommand* command)
114    {
115        for (size_t i = 0; i < command->getNames().size(); ++i)
116        {
117            const ConsoleCommand::CommandName& name = command->getNames()[i];
118            this->registerCommand(name.group_, name.name_, command);
119        }
120    }
121
122    /**
123        @brief Registers a new command with given group an name by adding it to the command map.
124    */
125    void ConsoleCommandManager::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
126    {
127        if (name == "")
128            return;
129
130        // check if a command with this name already exists
131        if (this->getCommand(group, name) != 0)
132        {
133            if (group == "")
134                orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl;
135            else
136                orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl;
137        }
138        else
139        {
140            // add the command to the map
141            this->commandMap_[group][name] = command;
142            this->commandMapLC_[getLowercase(group)][getLowercase(name)] = command;
143        }
144    }
145
146    /**
147        @brief Removes the command from the command map.
148    */
149    void ConsoleCommandManager::unregisterCommand(ConsoleCommand* command)
150    {
151        // iterate through all groups
152        for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = this->commandMap_.begin(); it_group != this->commandMap_.end(); )
153        {
154            // iterate through all commands of each group
155            for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
156            {
157                // erase the command
158                if (it_name->second == command)
159                    it_group->second.erase(it_name++);
160                else
161                    ++it_name;
162            }
163
164            // erase the group if it is empty now
165            if (it_group->second.empty())
166                this->commandMap_.erase(it_group++);
167            else
168                ++it_group;
169        }
170
171        // now the same for the lowercase-map:
172
173        // iterate through all groups
174        for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = this->commandMapLC_.begin(); it_group != this->commandMapLC_.end(); )
175        {
176            // iterate through all commands of each group
177            for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
178            {
179                // erase the command
180                if (it_name->second == command)
181                    it_group->second.erase(it_name++);
182                else
183                    ++it_name;
184            }
185
186            // erase the group if it is empty now
187            if (it_group->second.empty())
188                this->commandMapLC_.erase(it_group++);
189            else
190                ++it_group;
191        }
192    }
193}
Note: See TracBrowser for help on using the repository browser.