Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/Identifier.cc @ 1427

Last change on this file since 1427 was 1390, checked in by landauf, 17 years ago

only girls sleep at this time :P

(started new implementation of ConsoleExecutor parser, but it's still buggy)

File size: 12.3 KB
RevLine 
[790]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[790]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
[871]29/**
[790]30    @file Identifier.cc
31    @brief Implementation of the Identifier class.
32*/
[871]33
[1062]34#include "Identifier.h"
35
[871]36#include <ostream>
[790]37
38#include "Factory.h"
[1341]39#include "ConsoleCommand.h"
[1052]40#include "CommandExecutor.h"
[790]41
42namespace orxonox
43{
44    // ###############################
45    // ###       Identifier        ###
46    // ###############################
[1052]47    int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero (this static member variable is ok: it's used in main(), not before)
[790]48
49    /**
50        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
51    */
52    Identifier::Identifier()
53    {
54        this->bCreatedOneObject_ = false;
[1052]55        this->factory_ = 0;
56
57        this->bHasConfigValues_ = false;
58        this->bHasConsoleCommands_ = false;
[790]59
[1052]60        this->children_ = new std::set<const Identifier*>();
61        this->directChildren_ = new std::set<const Identifier*>();
[197]62
[790]63        // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
64        static unsigned int classIDcounter_s = 0;
[871]65        this->classID_ = classIDcounter_s++;
[790]66    }
67
68    /**
[871]69        @brief Destructor: Deletes the list containing the children.
[790]70    */
71    Identifier::~Identifier()
72    {
[871]73        delete this->children_;
74        delete this->directChildren_;
[790]75    }
76
77    /**
[871]78        @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
79        @param parents A list containing all parents
[790]80    */
[1052]81    void Identifier::initialize(std::set<const Identifier*>* parents)
[790]82    {
[871]83        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
[790]84        this->bCreatedOneObject_ = true;
85
86        if (parents)
[871]87        {
88            this->parents_ = (*parents);
89            this->directParents_ = (*parents);
90
91            // Iterate through all parents
[1052]92            for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
[871]93            {
94                // Tell the parent we're one of it's children
95                (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this);
96
97                // Erase all parents of our parent from our direct-parent-list
[1052]98                for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
[871]99                {
100                    // Search for the parent's parent in our direct-parent-list
[1052]101                    for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
[871]102                    {
103                        if ((*it1) == (*it2))
104                        {
105                            // We've found a non-direct parent in our list: Erase it
106                            this->directParents_.erase(it2);
107                            break;
108                        }
109                    }
110                }
111            }
112
113            // Now iterate through all direct parents
[1052]114            for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
[871]115            {
116                // Tell the parent we're one of it's direct children
117                (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this);
[790]118            }
119        }
120    }
121
122    /**
123        @brief Creates an object of the type the Identifier belongs to.
124        @return The new object
125    */
126    BaseObject* Identifier::fabricate()
127    {
128        if (this->factory_)
129        {
130            return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type.
131        }
132        else
133        {
[871]134            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
135            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
[790]136            COUT(1) << "Aborting..." << std::endl;
137            abort();
138            return NULL;
139        }
140    }
141
142    /**
143        @brief Sets the network ID to a new value and changes the entry in the Factory.
144        @param id The new network ID
145    */
146    void Identifier::setNetworkID(unsigned int id)
147    {
148        Factory::changeNetworkID(this, this->classID_, id);
149        this->classID_ = id;
150    }
151
152    /**
[871]153        @brief Returns true, if the Identifier is at least of the given type.
[790]154        @param identifier The identifier to compare with
155    */
156    bool Identifier::isA(const Identifier* identifier) const
157    {
[1059]158        return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
[790]159    }
160
161    /**
[871]162        @brief Returns true, if the Identifier is exactly of the given type.
[790]163        @param identifier The identifier to compare with
164    */
[871]165    bool Identifier::isExactlyA(const Identifier* identifier) const
[790]166    {
167        return (identifier == this);
168    }
169
170    /**
[871]171        @brief Returns true, if the assigned identifier is a child of the given identifier.
[790]172        @param identifier The identifier to compare with
173    */
174    bool Identifier::isChildOf(const Identifier* identifier) const
175    {
[1060]176        return (this->parents_.find(identifier) != this->parents_.end());
[871]177    }
178
179    /**
180        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
181        @param identifier The identifier to compare with
182    */
183    bool Identifier::isDirectChildOf(const Identifier* identifier) const
184    {
[1060]185        return (this->directParents_.find(identifier) != this->directParents_.end());
[871]186    }
[790]187
188    /**
[871]189        @brief Returns true, if the assigned identifier is a parent of the given identifier.
[790]190        @param identifier The identifier to compare with
191    */
192    bool Identifier::isParentOf(const Identifier* identifier) const
193    {
[1052]194        return (this->children_->find(identifier) != this->children_->end());
[871]195    }
196
197    /**
198        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
199        @param identifier The identifier to compare with
200    */
201    bool Identifier::isDirectParentOf(const Identifier* identifier) const
202    {
[1060]203        return (this->directChildren_->find(identifier) != this->directChildren_->end());
[871]204    }
205
206    /**
[1052]207        @brief Returns the map that stores all Identifiers.
208        @return The map
209    */
210    std::map<std::string, Identifier*>& Identifier::getIdentifierMapIntern()
211    {
212        static std::map<std::string, Identifier*> identifierMap;
213        return identifierMap;
214    }
215
216    /**
217        @brief Returns the map that stores all Identifiers.
218        @return The map
219    */
220    std::map<std::string, Identifier*>& Identifier::getLowercaseIdentifierMapIntern()
221    {
222        static std::map<std::string, Identifier*> lowercaseIdentifierMap;
223        return lowercaseIdentifierMap;
224    }
225
226    /**
227        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
228        @param varname The name of the variablee
229        @param container The container
230    */
231    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
232    {
[1390]233        std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname);
234        if (it != this->configValues_.end())
235        {
236            COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << "." << std::endl;
237        }
238
[1052]239        this->bHasConfigValues_ = true;
240        this->configValues_[varname] = container;
241        this->configValues_LC_[getLowercase(varname)] = container;
242    }
243
244    /**
[871]245        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
246        @param varname The name of the variable
247        @return The ConfigValueContainer
248    */
249    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
250    {
251        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
252        if (it != configValues_.end())
253            return ((*it).second);
254        else
255            return 0;
256    }
257
258    /**
[1052]259        @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase.
260        @param varname The name of the variable in lowercase
261        @return The ConfigValueContainer
[871]262    */
[1052]263    ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname)
[871]264    {
[1052]265        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname);
266        if (it != configValues_LC_.end())
267            return ((*it).second);
268        else
269            return 0;
[871]270    }
271
272    /**
[1052]273        @brief Adds a new console command of this class.
274        @param executor The executor of the command
275        @param bCreateShortcut If this is true a shortcut gets created so you don't have to add the classname to access this command
276        @return The executor of the command
[871]277    */
[1341]278    ConsoleCommand& Identifier::addConsoleCommand(ConsoleCommand* command, bool bCreateShortcut)
[871]279    {
[1390]280        std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(command->getName());
281        if (it != this->consoleCommands_.end())
282        {
283            COUT(2) << "Warning: Overwriting console-command with name " << command->getName() << " in class " << this->getName() << "." << std::endl;
284        }
285
[1052]286        this->bHasConsoleCommands_ = true;
[1341]287        this->consoleCommands_[command->getName()] = command;
288        this->consoleCommands_LC_[getLowercase(command->getName())] = command;
[871]289
[1052]290        if (bCreateShortcut)
[1341]291            CommandExecutor::addConsoleCommandShortcut(command);
[1052]292
[1341]293        return (*command);
[871]294    }
295
[1052]296    /**
297        @brief Returns the executor of a console command with given name.
298        @brief name The name of the requested console command
299        @return The executor of the requested console command
300    */
[1341]301    ConsoleCommand* Identifier::getConsoleCommand(const std::string& name) const
[871]302    {
[1341]303        std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(name);
[1052]304        if (it != this->consoleCommands_.end())
305            return (*it).second;
306        else
307            return 0;
308    }
309
310    /**
311        @brief Returns the executor of a console command with given name in lowercase.
312        @brief name The name of the requested console command in lowercae
313        @return The executor of the requested console command
314    */
[1341]315    ConsoleCommand* Identifier::getLowercaseConsoleCommand(const std::string& name) const
[1052]316    {
[1341]317        std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_LC_.find(name);
[1052]318        if (it != this->consoleCommands_LC_.end())
319            return (*it).second;
320        else
321            return 0;
322    }
323
324    /**
325        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
326        @param out The outstream
327        @param list The list (or set) of Identifiers
328        @return The outstream
329    */
330    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
331    {
332        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
[871]333            out << (*it)->getName() << " ";
334
335        return out;
336    }
[790]337}
Note: See TracBrowser for help on using the repository browser.