Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/Identifier.cc @ 953

Last change on this file since 953 was 947, checked in by landauf, 16 years ago
  • added CommandExecutor
  • added ConsoleCommand macros
  • added getTypename to all MultiTypes
  • added 2 static maps to Identifier that contain all existing Identifiers with their names and lowercase names respectively.
  • added 2 maps to each Identifier that contain all console commands of the Identifier with their names and lowercase names respectively
  • using tolower(.) and toupper(.) instead of selfmade hacks in String.h
  • added AccessLevel enum
  • added some test-console-commands to OutputHandler, Ambient and SpaceShip
File size: 11.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/**
29    @file Identifier.cc
30    @brief Implementation of the Identifier class.
31*/
32
33#include <ostream>
34
35#include "Identifier.h"
36#include "Factory.h"
37#include "Executor.h"
38#include "CommandExecutor.h"
39
40namespace orxonox
41{
42    // ###############################
43    // ###       Identifier        ###
44    // ###############################
45    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)
46
47    /**
48        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
49    */
50    Identifier::Identifier()
51    {
52        this->bCreatedOneObject_ = false;
53        this->factory_ = 0;
54
55        this->bHasConfigValues_ = false;
56        this->bHasConsoleCommands_ = false;
57
58        this->children_ = new std::set<const Identifier*>();
59        this->directChildren_ = new std::set<const Identifier*>();
60
61        // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
62        static unsigned int classIDcounter_s = 0;
63        this->classID_ = classIDcounter_s++;
64    }
65
66    /**
67        @brief Destructor: Deletes the list containing the children.
68    */
69    Identifier::~Identifier()
70    {
71        delete this->children_;
72        delete this->directChildren_;
73    }
74
75    /**
76        @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
77        @param parents A list containing all parents
78    */
79    void Identifier::initialize(std::set<const Identifier*>* parents)
80    {
81        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
82        this->bCreatedOneObject_ = true;
83
84        if (parents)
85        {
86            this->parents_ = (*parents);
87            this->directParents_ = (*parents);
88
89            // Iterate through all parents
90            for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
91            {
92                // Tell the parent we're one of it's children
93                (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this);
94
95                // Erase all parents of our parent from our direct-parent-list
96                for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
97                {
98                    // Search for the parent's parent in our direct-parent-list
99                    for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
100                    {
101                        if ((*it1) == (*it2))
102                        {
103                            // We've found a non-direct parent in our list: Erase it
104                            this->directParents_.erase(it2);
105                            break;
106                        }
107                    }
108                }
109            }
110
111            // Now iterate through all direct parents
112            for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
113            {
114                // Tell the parent we're one of it's direct children
115                (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this);
116            }
117        }
118    }
119
120    /**
121        @brief Creates an object of the type the Identifier belongs to.
122        @return The new object
123    */
124    BaseObject* Identifier::fabricate()
125    {
126        if (this->factory_)
127        {
128            return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type.
129        }
130        else
131        {
132            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
133            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
134            COUT(1) << "Aborting..." << std::endl;
135            abort();
136            return NULL;
137        }
138    }
139
140    /**
141        @brief Sets the network ID to a new value and changes the entry in the Factory.
142        @param id The new network ID
143    */
144    void Identifier::setNetworkID(unsigned int id)
145    {
146        Factory::changeNetworkID(this, this->classID_, id);
147        this->classID_ = id;
148    }
149
150    /**
151        @brief Returns true, if the Identifier is at least of the given type.
152        @param identifier The identifier to compare with
153    */
154    bool Identifier::isA(const Identifier* identifier) const
155    {
156        return (identifier == this || (this->parents_.find(identifier) != this->children_->end()));
157    }
158
159    /**
160        @brief Returns true, if the Identifier is exactly of the given type.
161        @param identifier The identifier to compare with
162    */
163    bool Identifier::isExactlyA(const Identifier* identifier) const
164    {
165        return (identifier == this);
166    }
167
168    /**
169        @brief Returns true, if the assigned identifier is a child of the given identifier.
170        @param identifier The identifier to compare with
171    */
172    bool Identifier::isChildOf(const Identifier* identifier) const
173    {
174        return (this->parents_.find(identifier) != this->children_->end());
175    }
176
177    /**
178        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
179        @param identifier The identifier to compare with
180    */
181    bool Identifier::isDirectChildOf(const Identifier* identifier) const
182    {
183        return (this->directParents_.find(identifier) != this->children_->end());
184    }
185
186    /**
187        @brief Returns true, if the assigned identifier is a parent of the given identifier.
188        @param identifier The identifier to compare with
189    */
190    bool Identifier::isParentOf(const Identifier* identifier) const
191    {
192        return (this->children_->find(identifier) != this->children_->end());
193    }
194
195    /**
196        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
197        @param identifier The identifier to compare with
198    */
199    bool Identifier::isDirectParentOf(const Identifier* identifier) const
200    {
201        return (this->directChildren_->find(identifier) != this->children_->end());
202    }
203
204    /**
205        @brief Returns the map that stores all Identifiers.
206        @return The map
207    */
208    std::map<std::string, Identifier*>& Identifier::getIdentifierMapIntern()
209    {
210        static std::map<std::string, Identifier*> identifierMap;
211        return identifierMap;
212    }
213
214    /**
215        @brief Returns the map that stores all Identifiers.
216        @return The map
217    */
218    std::map<std::string, Identifier*>& Identifier::getLowercaseIdentifierMapIntern()
219    {
220        static std::map<std::string, Identifier*> lowercaseIdentifierMap;
221        return lowercaseIdentifierMap;
222    }
223
224    /**
225        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
226        @param varname The name of the variablee
227        @param container The container
228    */
229    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
230    {
231        this->bHasConfigValues_ = true;
232        this->configValues_[varname] = container;
233        this->configValues_LC_[getLowercase(varname)] = container;
234    }
235
236    /**
237        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
238        @param varname The name of the variable
239        @return The ConfigValueContainer
240    */
241    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
242    {
243        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
244        if (it != configValues_.end())
245            return ((*it).second);
246        else
247            return 0;
248    }
249
250    /**
251        @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase.
252        @param varname The name of the variable in lowercase
253        @return The ConfigValueContainer
254    */
255    ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname)
256    {
257        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname);
258        if (it != configValues_LC_.end())
259            return ((*it).second);
260        else
261            return 0;
262    }
263
264    /**
265        @brief Adds a new console command of this class.
266        @param executor The executor of the command
267        @param bCreateShortcut If this is true a shortcut gets created so you don't have to add the classname to access this command
268        @return The executor of the command
269    */
270    ExecutorStatic& Identifier::addConsoleCommand(ExecutorStatic* executor, bool bCreateShortcut)
271    {
272        this->bHasConsoleCommands_ = true;
273        this->consoleCommands_[executor->getName()] = executor;
274        this->consoleCommands_LC_[getLowercase(executor->getName())] = executor;
275
276        if (bCreateShortcut)
277            CommandExecutor::addConsoleCommandShortcut(executor);
278
279        return (*executor);
280    }
281
282    /**
283        @brief Returns the executor of a console command with given name.
284        @brief name The name of the requested console command
285        @return The executor of the requested console command
286    */
287    ExecutorStatic* Identifier::getConsoleCommand(const std::string& name) const
288    {
289        std::map<std::string, ExecutorStatic*>::const_iterator it = this->consoleCommands_.find(name);
290        if (it != this->consoleCommands_.end())
291            return (*it).second;
292        else
293            return 0;
294    }
295
296    /**
297        @brief Returns the executor of a console command with given name in lowercase.
298        @brief name The name of the requested console command in lowercae
299        @return The executor of the requested console command
300    */
301    ExecutorStatic* Identifier::getLowercaseConsoleCommand(const std::string& name) const
302    {
303        std::map<std::string, ExecutorStatic*>::const_iterator it = this->consoleCommands_LC_.find(name);
304        if (it != this->consoleCommands_LC_.end())
305            return (*it).second;
306        else
307            return 0;
308    }
309
310    /**
311        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
312        @param out The outstream
313        @param list The list (or set) of Identifiers
314        @return The outstream
315    */
316    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
317    {
318        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
319            out << (*it)->getName() << " ";
320
321        return out;
322    }
323}
Note: See TracBrowser for help on using the repository browser.