Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/command/ConsoleCommandManager.cc @ 11247

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

merged branch cpp11_v3 back to trunk

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