Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/shader/src/core/ClassTreeMask.h @ 12397

Last change on this file since 12397 was 1505, checked in by rgrieder, 17 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 9.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 ClassTreeMask.h
31    @brief Definition of the ClassTreeMask, ClassTreeMaskNode and ClassTreeMaskIterator classes.
32
33    ClassTreeMask is a class to define a mask of the class-tree beginning with BaseObject.
34    You can include or exclude classes by calling the corresponding functions with the
35    Identifier of the class.
36
37    You can work with a ClassTreeMask in the sense of the set-theory, meaning that you can create
38    unions, intersections, complements and differences by using overloaded operators.
39
40
41
42    The ClassTreeMask is internally represented by a tree. The nodes in the tree are
43    ClassTreeMaskNodes, containing the rule (included or excluded) for this class and all
44    subclasses and a list of all subnodes. To minimize the size, the tree contains only
45    nodes changing the mask. By adding new rules, the tree gets reordered dynamically.
46
47    Adding a new rule overwrites all rules assigned to inherited classes. Use overwrite = false
48    if you don't like this feature. Useless rules that don't change the information of the mask
49    aren't saved in the internal tree. Use clean = false if you wan't to save them.
50
51    With overwrite = false and clean = false it doesn't matter in which way you create the mask.
52    You can manually drop useless rules from the tree by calling clean().
53
54
55
56    Because of the complicated shape of the internal tree, there is an iterator to iterate
57    through all ClassTreeMaskNodes of a mask. It starts with the BaseObject and moves on to
58    the first subclass until it reaches a leaf of the tree. Then the iterator moves one step
59    back and iterates to the second subclass. If there are no more subclasses, it steps another
60    step back, and so on.
61
62    Example: A and B are childs of BaseObject, A1 and A2 are childs of A, B1 and B2 are childs of B.
63    The ClassTreeMaskIterator would move trough the tree in the following order:
64    BaseObject, A, A1, A2, B, B1, B2.
65
66    Note that the iterator doesn't move trough the whole class-tree, but only through the
67    internal tree of the mask, containing the minimal needed set of nodes to describe the mask.
68*/
69
70#ifndef _ClassTreeMask_H__
71#define _ClassTreeMask_H__
72
73#include "CorePrereqs.h"
74
75#include <list>
76#include <stack>
77
78namespace orxonox
79{
80    // ###############################
81    // ###    ClassTreeMaskNode    ###
82    // ###############################
83    //! The ClassTreeMaskNode is a node in the internal tree of the ClassTreeMask, containing the rules of the mask.
84    /**
85        The ClassTreeMaskNode is used to store the rule (included or excluded) for a given
86        class (described by the corresponding Identifier). The nodes are used in the internal
87        tree of ClassTreeMask. To build a tree, they store a list of all subnodes.
88    */
89    class _CoreExport ClassTreeMaskNode
90    {
91        friend class ClassTreeMask;
92        friend class ClassTreeMaskIterator;
93
94        public:
95            ClassTreeMaskNode(const Identifier* subclass, bool bIncluded = true);
96            ~ClassTreeMaskNode();
97
98            void include(bool overwrite = true);
99            void exclude(bool overwrite = true);
100            void setIncluded(bool bIncluded, bool overwrite = true);
101
102            void addSubnode(ClassTreeMaskNode* subnode);
103
104            bool isIncluded() const;
105            bool isExcluded() const;
106
107            const Identifier* getClass() const;
108
109        private:
110            void deleteAllSubnodes();
111
112            const Identifier* subclass_;                //!< The Identifier of the subclass the rule refers to
113            bool bIncluded_;                            //!< The rule: included or excluded
114            std::list<ClassTreeMaskNode*> subnodes_;    //!< A list containing all subnodes in the tree
115    };
116
117
118    // ###############################
119    // ###  ClassTreeMaskIterator  ###
120    // ###############################
121    //! The ClassTreeMaskIterator moves through all ClassTreeMaskNodes of the internal tree of a ClassTreeMask, containing the rules.
122    /**
123        Because of the complicated shape of the internal rule-tree of ClassTreeMask, an
124        iterator is used to move through all nodes of the tree. It starts with the BaseObject
125        and moves on to the first subclass until it reaches a leaf of the tree. Then the
126        iterator moves one step back and iterates to the second subclass. If there are no more
127        subclasses, it steps another step back, and so on.
128    */
129    class _CoreExport ClassTreeMaskIterator
130    {
131        public:
132            ClassTreeMaskIterator(ClassTreeMaskNode* node);
133            ~ClassTreeMaskIterator();
134
135            ClassTreeMaskIterator& operator++();
136            ClassTreeMaskNode* operator*() const;
137            ClassTreeMaskNode* operator->() const;
138            operator bool();
139            bool operator==(ClassTreeMaskNode* compare);
140            bool operator!=(ClassTreeMaskNode* compare);
141
142        private:
143            std::stack<std::pair<std::list<ClassTreeMaskNode*>::iterator, std::list<ClassTreeMaskNode*>::iterator> > nodes_;    //!< A stack to store list-iterators
144            std::list<ClassTreeMaskNode*> rootlist_;                                                                            //!< A list for internal use (it only stores the root-node)
145    };
146
147
148    // ###############################
149    // ###      ClassTreeMask      ###
150    // ###############################
151    //! The ClassTreeMask is a set of rules, containing the information for each class whether it's included or not.
152    /**
153        With a ClassTreeMask, you can include or exclude subtrees of the class-tree, starting
154        with a given subclass, described by the corresponding Identifier. To minimize the size
155        of the mask, the mask saves only relevant rules. But you can manually add rules that
156        don't change the information of the mask by using clean = false. If you want to drop
157        useless rules, call the clean() function.
158    */
159    class _CoreExport ClassTreeMask
160    {
161        public:
162            ClassTreeMask();
163            ClassTreeMask(const ClassTreeMask& other);
164            ~ClassTreeMask();
165
166            void include(const Identifier* subclass, bool overwrite = true, bool clean = true);
167            void exclude(const Identifier* subclass, bool overwrite = true, bool clean = true);
168            void add(const Identifier* subclass, bool bInclude, bool overwrite = true, bool clean = true);
169
170            void includeSingle(const Identifier* subclass, bool clean = true);
171            void excludeSingle(const Identifier* subclass, bool clean = true);
172            void addSingle(const Identifier* subclass, bool bInclude, bool clean = true);
173
174            void reset();
175            void clean();
176
177            bool isIncluded(const Identifier* subclass) const;
178            bool isExcluded(const Identifier* subclass) const;
179
180            ClassTreeMask& operator=(const ClassTreeMask& other);
181
182            bool operator==(const ClassTreeMask& other) const;
183            bool operator!=(const ClassTreeMask& other) const;
184
185            ClassTreeMask& operator+();
186            ClassTreeMask operator-() const;
187
188            ClassTreeMask operator+(const ClassTreeMask& other) const;
189            ClassTreeMask operator*(const ClassTreeMask& other) const;
190            ClassTreeMask operator-(const ClassTreeMask& other) const;
191            ClassTreeMask operator!() const;
192
193            ClassTreeMask& operator+=(const ClassTreeMask& other);
194            ClassTreeMask& operator*=(const ClassTreeMask& other);
195            ClassTreeMask& operator-=(const ClassTreeMask& other);
196
197            ClassTreeMask operator&(const ClassTreeMask& other) const;
198            ClassTreeMask operator|(const ClassTreeMask& other) const;
199            ClassTreeMask operator^(const ClassTreeMask& other) const;
200            ClassTreeMask operator~() const;
201
202            ClassTreeMask& operator&=(const ClassTreeMask& other);
203            ClassTreeMask& operator|=(const ClassTreeMask& other);
204            ClassTreeMask& operator^=(const ClassTreeMask& other);
205
206            friend std::ostream& operator<<(std::ostream& out, const ClassTreeMask& mask);
207
208        private:
209            void add(ClassTreeMaskNode* node, const Identifier* subclass, bool bInclude, bool overwrite = true);
210            bool isIncluded(ClassTreeMaskNode* node, const Identifier* subclass) const;
211            void clean(ClassTreeMaskNode* node);
212            bool nodeExists(const Identifier* subclass);
213
214            ClassTreeMaskNode* root_;   //!< The root-node of the internal rule-tree, usually BaseObject
215    };
216}
217
218#endif /* _ClassTreeMask_H__ */
Note: See TracBrowser for help on using the repository browser.