Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ggz/src/core/ObjectListBase.h @ 3114

Last change on this file since 3114 was 2784, checked in by rgrieder, 16 years ago
  • Using std::vector instead of std::list in register/unregister Iterator/ObjectListIterator. That is approximately 6 times faster because the list has very few elements.
  • Inlined getIdentifier() for sure by exporting the heavy part to another function.
  • Also eliminated the need of isFirstCall() because the "static initialisation chaos" only applies to variables that cannot be evaluated at compile time.
  • Property svn:eol-style set to native
File size: 5.8 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 Definition of the ObjectListBase class.
32
33    The ObjectListBase is a double-linked list, used by Identifiers to store all objects of a given class.
34    Newly created objects are added through the RegisterObject-macro in its constructor.
35*/
36
37#ifndef _ObjectListBase_H__
38#define _ObjectListBase_H__
39
40#include <vector>
41
42#include "CorePrereqs.h"
43
44namespace orxonox
45{
46    // ###############################
47    // ###  ObjectListBaseElement  ###
48    // ###############################
49    //! The list-element of the ObjectListBase
50    class _CoreExport ObjectListBaseElement
51    {
52        public:
53            /**
54                @brief Constructor: Creates the list-element with an object.
55                @param object The object to store
56            */
57            ObjectListBaseElement(OrxonoxClass* objectBase) : next_(0), prev_(0), objectBase_(objectBase) {}
58
59            ObjectListBaseElement* next_;       //!< The next element in the list
60            ObjectListBaseElement* prev_;       //!< The previous element in the list
61            OrxonoxClass* objectBase_;
62    };
63
64
65    // ###############################
66    // ###    ObjectListElement    ###
67    // ###############################
68    //! The list-element that actually contains the object
69    template <class T>
70    class ObjectListElement : public ObjectListBaseElement
71    {
72        public:
73            ObjectListElement(T* object) : ObjectListBaseElement((OrxonoxClass*)object), object_(object) {}
74            T* object_;              //!< The object
75    };
76
77
78    // ###############################
79    // ###     ObjectListBase      ###
80    // ###############################
81    //! The ObjectListBase contains all objects of a given class.
82    /**
83        The ObjectListBase is used by Identifiers to store all objects of their given class.
84        Use ObjectList<T> to get the list of all T's and Iterator<T> to iterate through them.
85    */
86    class _CoreExport ObjectListBase
87    {
88        friend class MetaObjectListElement;
89
90        public:
91            ObjectListBase(Identifier* identifier);
92            ~ObjectListBase();
93
94            ObjectListBaseElement* add(ObjectListBaseElement* element);
95
96            struct Export
97            {
98                Export(ObjectListBase* list, ObjectListBaseElement* element) : list_(list), element_(element) {}
99                ObjectListBase* list_;
100                ObjectListBaseElement* element_;
101            };
102
103            /** @brief Returns a pointer to the first element in the list. @return The element */
104            inline Export begin() { return ObjectListBase::Export(this, this->first_); }
105            /** @brief Returns a pointer to the element after the last element in the list. @return The element */
106            inline Export end() { return ObjectListBase::Export(this, 0); }
107            /** @brief Returns a pointer to the last element in the list. @return The element */
108            inline Export rbegin() { return ObjectListBase::Export(this, this->last_); }
109            /** @brief Returns a pointer to the element in front of the first element in the list. @return The element */
110            inline Export rend() { return ObjectListBase::Export(this, 0); }
111
112            inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
113            inline void unregisterIterator(void* iterator)
114            {
115                for (unsigned int i = 0; i < this->iterators_.size(); ++i)
116                {
117                    if (iterators_[i] == iterator)
118                    {
119                        iterators_.erase(iterators_.begin() + i);
120                        break;
121                    }
122                }
123            }
124            inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }
125            inline void unregisterObjectListIterator(void* iterator)
126            {
127                for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)
128                {
129                    if (objectListIterators_[i] == iterator)
130                    {
131                        objectListIterators_.erase(objectListIterators_.begin() + i);
132                        break;
133                    }
134                }
135            }
136            void notifyIterators(OrxonoxClass* object) const;
137
138            inline Identifier* getIdentifier() const { return this->identifier_; }
139
140        private:
141            Identifier* identifier_;               //!< The Iterator owning this list
142            ObjectListBaseElement* first_;         //!< The first element in the list
143            ObjectListBaseElement* last_;          //!< The last element in the list
144            std::vector<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
145            std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
146    };
147}
148
149#endif /* _ObjectListBase_H__ */
Note: See TracBrowser for help on using the repository browser.