Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/ObjectListBase.h @ 8472

Last change on this file since 8472 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 6.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    @ingroup Object ObjectList
32    @brief Declaration of the ObjectListBase class which stores all objects of each class.
33
34    orxonox::ObjectListBase is a double-linked list, used by @ref orxonox::Identifier "Identifiers"
35    to store all objects of a given class. Newly created objects are added to the list through the
36    @c RegisterObject() macro in the constructor.
37*/
38
39#ifndef _ObjectListBase_H__
40#define _ObjectListBase_H__
41
42#include "CorePrereqs.h"
43
44#include <vector>
45#include "OrxonoxClass.h"
46
47namespace orxonox
48{
49    // ###############################
50    // ###  ObjectListBaseElement  ###
51    // ###############################
52    /// The list-element of the ObjectListBase
53    class _CoreExport ObjectListBaseElement
54    {
55        public:
56            /**
57                @brief Constructor: Creates the list-element with an object.
58                @param objectBase The object to store
59            */
60            ObjectListBaseElement(OrxonoxClass* objectBase) : next_(0), prev_(0), objectBase_(objectBase) {}
61
62            ObjectListBaseElement* next_;       //!< The next element in the list
63            ObjectListBaseElement* prev_;       //!< The previous element in the list
64            OrxonoxClass* objectBase_;
65    };
66
67
68    // ###############################
69    // ###    ObjectListElement    ###
70    // ###############################
71    /// The list-element that actually contains the object
72    template <class T>
73    class ObjectListElement : public ObjectListBaseElement
74    {
75        public:
76            ObjectListElement(T* object) : ObjectListBaseElement(static_cast<OrxonoxClass*>(object)), object_(object) {}
77            T* object_;              //!< The object
78    };
79
80
81    // ###############################
82    // ###     ObjectListBase      ###
83    // ###############################
84    /**
85        @brief The ObjectListBase contains all objects of a given class.
86
87        The ObjectListBase is used by Identifiers to store all objects of their class.
88        You can use Identifier::getObjects() to get the object-list from an Identifier.
89        Use @ref Iterator "Iterator<T>" to iterate through them.
90
91        Alternatively you can also use the static helper class @ref orxonox::ObjectList "ObjectList<T>"
92        to get the list of all objects of type @a T. Use @ref ObjectListIterator "ObjectListIterator<T>"
93        or @ref Iterator "Iterator<T>" to iterate through them.
94    */
95    class _CoreExport ObjectListBase
96    {
97        friend class MetaObjectListElement;
98
99        public:
100            ObjectListBase(Identifier* identifier);
101            ~ObjectListBase();
102
103            ObjectListBaseElement* add(ObjectListBaseElement* element);
104
105            /// Helper struct, used to export an element and the list to an instance of Iterator.
106            struct Export
107            {
108                Export(ObjectListBase* list, ObjectListBaseElement* element) : list_(list), element_(element) {}
109                ObjectListBase* list_;
110                ObjectListBaseElement* element_;
111            };
112
113            /// Returns a pointer to the first element in the list. Works only with Iterator.
114            inline Export begin() { return ObjectListBase::Export(this, this->first_); }
115            /// Returns a pointer to the element after the last element in the list. Works only with Iterator.
116            inline Export end() { return ObjectListBase::Export(this, 0); }
117            /// Returns a pointer to the last element in the list. Works only with Iterator.
118            inline Export rbegin() { return ObjectListBase::Export(this, this->last_); }
119            /// Returns a pointer to the element in front of the first element in the list. Works only with Iterator.
120            inline Export rend() { return ObjectListBase::Export(this, 0); }
121
122            inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
123            inline void unregisterIterator(void* iterator)
124            {
125                for (unsigned int i = 0; i < this->iterators_.size(); ++i)
126                {
127                    if (iterators_[i] == iterator)
128                    {
129                        iterators_.erase(iterators_.begin() + i);
130                        break;
131                    }
132                }
133            }
134            inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }
135            inline void unregisterObjectListIterator(void* iterator)
136            {
137                for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)
138                {
139                    if (objectListIterators_[i] == iterator)
140                    {
141                        objectListIterators_.erase(objectListIterators_.begin() + i);
142                        break;
143                    }
144                }
145            }
146            void notifyIterators(OrxonoxClass* object) const;
147
148            inline Identifier* getIdentifier() const { return this->identifier_; }
149
150        private:
151            Identifier* identifier_;                 //!< The Iterator owning this list
152            ObjectListBaseElement* first_;           //!< The first element in the list
153            ObjectListBaseElement* last_;            //!< The last element in the list
154            std::vector<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
155            std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
156    };
157}
158
159#endif /* _ObjectListBase_H__ */
Note: See TracBrowser for help on using the repository browser.