Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/core/ObjectListBase.h @ 5671

Last change on this file since 5671 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

  • 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 "CorePrereqs.h"
41
42#include <vector>
43#include "OrxonoxClass.h"
44
45namespace orxonox
46{
47    // ###############################
48    // ###  ObjectListBaseElement  ###
49    // ###############################
50    //! The list-element of the ObjectListBase
51    class _CoreExport ObjectListBaseElement
52    {
53        public:
54            /**
55                @brief Constructor: Creates the list-element with an object.
56                @param object The object to store
57            */
58            ObjectListBaseElement(OrxonoxClass* objectBase) : next_(0), prev_(0), objectBase_(objectBase) {}
59
60            ObjectListBaseElement* next_;       //!< The next element in the list
61            ObjectListBaseElement* prev_;       //!< The previous element in the list
62            OrxonoxClass* objectBase_;
63    };
64
65
66    // ###############################
67    // ###    ObjectListElement    ###
68    // ###############################
69    //! The list-element that actually contains the object
70    template <class T>
71    class ObjectListElement : public ObjectListBaseElement
72    {
73        public:
74            ObjectListElement(T* object) : ObjectListBaseElement(static_cast<OrxonoxClass*>(object)), object_(object) {}
75            T* object_;              //!< The object
76    };
77
78
79    // ###############################
80    // ###     ObjectListBase      ###
81    // ###############################
82    //! The ObjectListBase contains all objects of a given class.
83    /**
84        The ObjectListBase is used by Identifiers to store all objects of their given class.
85        Use ObjectList<T> to get the list of all T's and Iterator<T> to iterate through them.
86    */
87    class _CoreExport ObjectListBase
88    {
89        friend class MetaObjectListElement;
90
91        public:
92            ObjectListBase(Identifier* identifier);
93            ~ObjectListBase();
94
95            ObjectListBaseElement* add(ObjectListBaseElement* element);
96
97            struct Export
98            {
99                Export(ObjectListBase* list, ObjectListBaseElement* element) : list_(list), element_(element) {}
100                ObjectListBase* list_;
101                ObjectListBaseElement* element_;
102            };
103
104            /** @brief Returns a pointer to the first element in the list. @return The element */
105            inline Export begin() { return ObjectListBase::Export(this, this->first_); }
106            /** @brief Returns a pointer to the element after the last element in the list. @return The element */
107            inline Export end() { return ObjectListBase::Export(this, 0); }
108            /** @brief Returns a pointer to the last element in the list. @return The element */
109            inline Export rbegin() { return ObjectListBase::Export(this, this->last_); }
110            /** @brief Returns a pointer to the element in front of the first element in the list. @return The element */
111            inline Export rend() { return ObjectListBase::Export(this, 0); }
112
113            inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
114            inline void unregisterIterator(void* iterator)
115            {
116                for (unsigned int i = 0; i < this->iterators_.size(); ++i)
117                {
118                    if (iterators_[i] == iterator)
119                    {
120                        iterators_.erase(iterators_.begin() + i);
121                        break;
122                    }
123                }
124            }
125            inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }
126            inline void unregisterObjectListIterator(void* iterator)
127            {
128                for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)
129                {
130                    if (objectListIterators_[i] == iterator)
131                    {
132                        objectListIterators_.erase(objectListIterators_.begin() + i);
133                        break;
134                    }
135                }
136            }
137            void notifyIterators(OrxonoxClass* object) const;
138
139            inline Identifier* getIdentifier() const { return this->identifier_; }
140
141        private:
142            Identifier* identifier_;                 //!< The Iterator owning this list
143            ObjectListBaseElement* first_;           //!< The first element in the list
144            ObjectListBaseElement* last_;            //!< The last element in the list
145            std::vector<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
146            std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
147    };
148}
149
150#endif /* _ObjectListBase_H__ */
Note: See TracBrowser for help on using the repository browser.