Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/ClassTreeMask.cc @ 967

Last change on this file since 967 was 896, checked in by landauf, 16 years ago
  • added == and != operator to the ClassTreeMask
  • Included the Namespace in the Loader
File size: 29.9 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 ClassTreeMask.cc
30    @brief Implementation of the ClassTreeMask, ClassTreeMaskNode and ClassTreeMaskIterator classes.
31*/
32
33#include "ClassTreeMask.h"
34#include "Identifier.h"
35#include "BaseObject.h"
36
37namespace orxonox
38{
39    // ###############################
40    // ###    ClassTreeMaskNode    ###
41    // ###############################
42    /**
43        @brief Constructor: Creates the node, sets the subclass and the rule.
44        @param subclass The subclass the rule refers to
45        @param bIncluded The rule: included (true) or excluded (false)
46    */
47    ClassTreeMaskNode::ClassTreeMaskNode(const Identifier* subclass, bool bIncluded)
48    {
49        this->subclass_ = subclass;
50        this->bIncluded_ = bIncluded;
51    }
52
53    /**
54        @brief Destructor: Deletes all subnodes.
55    */
56    ClassTreeMaskNode::~ClassTreeMaskNode()
57    {
58        // Go through the list of all subnodes and delete them
59        this->deleteAllSubnodes();
60    }
61
62    /**
63        @brief Sets the rule for the node to "included".
64        @param overwrite True = overwrite previously added rules for inheriting classes
65    */
66    void ClassTreeMaskNode::include(bool overwrite)
67    {
68        this->setIncluded(true, overwrite);
69    }
70
71    /**
72        @brief Sets the rule for the node to "excluded".
73        @param overwrite True = overwrite previously added rules for inheriting classes
74    */
75    void ClassTreeMaskNode::exclude(bool overwrite)
76    {
77        this->setIncluded(false, overwrite);
78    }
79
80    /**
81        @brief Sets the rule for the node to a given value and erases all following rules.
82        @param bIncluded The rule: included (true) or excluded (false)
83        @param overwrite True = overwrite previously added rules for inheriting classes
84    */
85    void ClassTreeMaskNode::setIncluded(bool bIncluded, bool overwrite)
86    {
87        if (overwrite)
88            this->deleteAllSubnodes();
89
90        this->bIncluded_ = bIncluded;
91    }
92
93    /**
94        @brief Adds a new subnode to the list of subnodes.
95        @param subnode The new subnode
96    */
97    void ClassTreeMaskNode::addSubnode(ClassTreeMaskNode* subnode)
98    {
99        this->subnodes_.insert(this->subnodes_.end(), subnode);
100    }
101
102    /**
103        @brief Tells if the rule is "included" or not.
104        @return The rule: true = included, false = excluded
105    */
106    bool ClassTreeMaskNode::isIncluded() const
107    {
108        return this->bIncluded_;
109    }
110
111    /**
112        @brief Tells if the rule is "excluded" or not.
113        @return The inverted rule: true = excluded, false = included
114    */
115    bool ClassTreeMaskNode::isExcluded() const
116    {
117        return (!this->bIncluded_);
118    }
119
120    /**
121        @brief Returns the Identifier of the class the rule refers to.
122        @return The Identifier representing the class
123    */
124    const Identifier* ClassTreeMaskNode::getClass() const
125    {
126        return this->subclass_;
127    }
128
129    /**
130        @brief Deletes all subnodes of this node.
131    */
132    void ClassTreeMaskNode::deleteAllSubnodes()
133    {
134        // Go through the list of all subnodes and delete them
135        for (std::list<ClassTreeMaskNode*>::iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); )
136            delete (*(it++));
137
138        // Clear the list
139        this->subnodes_.clear();
140    }
141
142
143    // ###############################
144    // ###  ClassTreeMaskIterator  ###
145    // ###############################
146    /**
147        @brief Constructor: Initializes the iterator by creating a helper-list with the root-node and putting it to the stack.
148        @param node The root-node
149    */
150    ClassTreeMaskIterator::ClassTreeMaskIterator(ClassTreeMaskNode* node)
151    {
152        // Create a list and put the root-note into it
153        std::list<ClassTreeMaskNode*>::iterator it = this->rootlist_.insert(this->rootlist_.end(), node);
154
155        // Put the iterator to the only element of the list and the corresponding end()-iterator on the stack
156        this->nodes_.push(std::pair<std::list<ClassTreeMaskNode*>::iterator, std::list<ClassTreeMaskNode*>::iterator>(it, this->rootlist_.end()));
157    }
158
159    /**
160        @brief Destructor: Does nothing.
161    */
162    ClassTreeMaskIterator::~ClassTreeMaskIterator()
163    {
164    }
165
166    /**
167        @brief Iterates through the rule-tree.
168        @return A reference to the iterator itself
169    */
170    ClassTreeMaskIterator& ClassTreeMaskIterator::operator++()
171    {
172        // Check if the actual node has subnodes
173        if ((*this->nodes_.top().first)->subnodes_.begin() != (*this->nodes_.top().first)->subnodes_.end())
174        {
175            // Yes it has: Push an iterator, pointing at the first subnode, on the stack
176            this->nodes_.push(std::pair<std::list<ClassTreeMaskNode*>::iterator, std::list<ClassTreeMaskNode*>::iterator>((*this->nodes_.top().first)->subnodes_.begin(), (*this->nodes_.top().first)->subnodes_.end()));
177        }
178        else
179        {
180            // No subnodes, meaning we reached a leaf: Go to the next node
181            do
182            {
183                // Iterate one step in the current list
184                ++this->nodes_.top().first;
185
186                // Check if we reached the end of the list (the second item in the stored pair always represents the end)
187                if (this->nodes_.top().first == this->nodes_.top().second)
188                {
189                    // Yes we've reached the end: Pop the list-iterator from the stack
190                    this->nodes_.pop();
191
192                    // Continue with the loop, meaning: Try to iterate through the previous list
193                    continue;
194                }
195
196                // If we reached this point, we aren't yet at the end of the list: Leave the loop
197                break;
198            } while (!this->nodes_.empty()); // Stop looping if the stack is empty, meaning: We've iterated to the end
199        }
200
201        // Finally return a reference to the iterator itself
202        return *this;
203    }
204
205    /**
206        @brief Returns a pointer to the ClassTreeMaskNode whereon the iterator points.
207        @return The pointer to the node
208    */
209    ClassTreeMaskNode* ClassTreeMaskIterator::operator*() const
210    {
211        return (*this->nodes_.top().first);
212    }
213
214    /**
215        @brief Returns a pointer to the ClassTreeMaskNode whereon the iterator points.
216        @return The pointer to the node
217    */
218    ClassTreeMaskNode* ClassTreeMaskIterator::operator->() const
219    {
220        return (*this->nodes_.top().first);
221    }
222
223    /**
224        @brief Returns true if the stack is empty, meaning we've reached the end of the tree.
225        @return True if we've reached the end of the tree
226    */
227    ClassTreeMaskIterator::operator bool()
228    {
229        return (!this->nodes_.empty());
230    }
231
232    /**
233        @brief Compares the current node with the given one and returns true if they match.
234        @param compare The node to compare with
235        @return The result of the comparison (true if they match)
236    */
237    bool ClassTreeMaskIterator::operator==(ClassTreeMaskNode* compare)
238    {
239        if (!this->nodes_.empty())
240            return ((*this->nodes_.top().first) == compare);
241        else
242            return (compare == 0);
243    }
244
245    /**
246        @brief Compares the current node with the given one and returns true if they don't match.
247        @param compare The node to compare with
248        @return The result of the comparison (true if they don't match)
249    */
250    bool ClassTreeMaskIterator::operator!=(ClassTreeMaskNode* compare)
251    {
252        if (!this->nodes_.empty())
253            return ((*this->nodes_.top().first) != compare);
254        else
255            return (compare != 0);
256    }
257
258
259    // ###############################
260    // ###      ClassTreeMask      ###
261    // ###############################
262    /**
263        @brief Constructor: Adds the root-node of the tree with the first rule ("include everything").
264    */
265    ClassTreeMask::ClassTreeMask()
266    {
267        this->root_ = new ClassTreeMaskNode(ClassManager<BaseObject>::getIdentifier(), true);
268    }
269
270    /**
271        @brief Copyconstructor: Adds the root-node of the tree with the first rule ("include everything") and adds all rules from the other mask.
272        @param other The other mask
273    */
274    ClassTreeMask::ClassTreeMask(const ClassTreeMask& other)
275    {
276        this->root_ = new ClassTreeMaskNode(ClassManager<BaseObject>::getIdentifier(), true);
277        for (ClassTreeMaskIterator it = other.root_; it; ++it)
278            this->add(it->getClass(), it->isIncluded(), false);
279    }
280
281    /**
282        @brief Destructor: Deletes the root node (which will delete all subnodes too).
283    */
284    ClassTreeMask::~ClassTreeMask()
285    {
286        delete this->root_;
287    }
288
289    /**
290        @brief Adds a new "include" rule for a given subclass to the mask.
291        @param subclass The subclass
292        @param overwrite True = overwrite previously added rules for inheriting classes
293        @param clean True = clean the tree after adding the new rule
294    */
295    void ClassTreeMask::include(const Identifier* subclass, bool overwrite, bool clean)
296    {
297        this->add(subclass, true, overwrite, clean);
298    }
299
300    /**
301        @brief Adds a new "exclude" rule for a given subclass to the mask.
302        @param subclass The subclass
303        @param overwrite True = overwrite previously added rules for inheriting classes
304        @param clean True = clean the tree after adding the new rule
305    */
306    void ClassTreeMask::exclude(const Identifier* subclass, bool overwrite, bool clean)
307    {
308        this->add(subclass, false, overwrite, clean);
309    }
310
311    /**
312        @brief Adds a new rule for a given subclass to the mask.
313        @param subclass The subclass
314        @param bInclude The rule: include (true) or exclude (false)
315        @param overwrite True = overwrite previously added rules for inheriting classes
316        @param clean True = clean the tree after adding the new rule
317    */
318    void ClassTreeMask::add(const Identifier* subclass, bool bInclude, bool overwrite, bool clean)
319    {
320        // Check if the given subclass is a child of our root-class
321        if (subclass->isA(this->root_->getClass()))
322        {
323            // Yes it is: Just add the rule to the three
324            this->add(this->root_, subclass, bInclude, overwrite);
325        }
326        else
327        {
328            // No it's not: Search for classes inheriting from the given class and add the rules for them
329            for (std::set<const Identifier*>::const_iterator it = subclass->getDirectChildrenBegin(); it != subclass->getDirectChildrenEnd(); ++it)
330                if ((*it)->isA(this->root_->getClass()))
331                    if (overwrite || (!this->nodeExists(*it))) // If we don't want to overwrite, only add nodes that don't already exist
332                        this->add(this->root_, *it, bInclude, overwrite);
333        }
334
335        // Clean the rule-tree
336        if (clean)
337            this->clean();
338    }
339
340    /**
341        @brief Adds a new rule for a given subclass to a node of the internal rule-tree (recursive function).
342        @param node The node
343        @param subclass The subclass
344        @param bInclude The rule: include (true) or exclude (false)
345        @param overwrite True = overwrite previously added rules for inheriting classes
346    */
347    void ClassTreeMask::add(ClassTreeMaskNode* node, const Identifier* subclass, bool bInclude, bool overwrite)
348    {
349        // Check if the current node contains exactly the subclass we want to add
350        if (subclass == node->getClass())
351        {
352            // We're at the right place, just change the mask and return
353            node->setIncluded(bInclude, overwrite);
354            return;
355        }
356        else if (subclass->isA(node->getClass()))
357        {
358            // Search for an already existing node, containing the subclass we want to add
359            for (std::list<ClassTreeMaskNode*>::iterator it = node->subnodes_.begin(); it != node->subnodes_.end(); ++it)
360            {
361                if (subclass->isA((*it)->getClass()))
362                {
363                    // We've found an existing node -> delegate the work with a recursive function-call and return
364                    this->add(*it, subclass, bInclude, overwrite);
365                    return;
366                }
367            }
368
369            // There is no existing node satisfying our needs -> we create a new node
370            ClassTreeMaskNode* newnode = new ClassTreeMaskNode(subclass, bInclude);
371
372            // Search for nodes that should actually be subnodes of our new node and erase them
373            for (std::list<ClassTreeMaskNode*>::iterator it = node->subnodes_.begin(); it != node->subnodes_.end(); )
374            {
375                if ((*it)->getClass()->isChildOf(subclass))
376                {
377                    // We've found a subnode: add it to the new node and erase it from the current node
378                    if (!overwrite)
379                        newnode->addSubnode(*it);
380                    else
381                        delete (*it);
382
383                    node->subnodes_.erase(it++);
384                }
385                else
386                {
387                    ++it;
388                }
389            }
390
391            // Finally add the new node as a subnode to the current node
392            node->addSubnode(newnode);
393        }
394    }
395
396    /**
397        @brief Adds a new "include" rule for a single subclass. The new rule doesn't change the mask for inheriting classes.
398        @param subclass The subclass
399        @param clean True = clean the tree after adding the new rule
400    */
401    void ClassTreeMask::includeSingle(const Identifier* subclass, bool clean)
402    {
403        this->addSingle(subclass, true, clean);
404    }
405
406    /**
407        @brief Adds a new "exclude" rule for a single subclass. The new rule doesn't change the mask for inheriting classes.
408        @param subclass The subclass
409        @param clean True = clean the tree after adding the new rule
410    */
411    void ClassTreeMask::excludeSingle(const Identifier* subclass, bool clean)
412    {
413        this->addSingle(subclass, false, clean);
414    }
415
416    /**
417        @brief Adds a new rule for a single subclass. The new rule doesn't change the mask for inheriting classes.
418        @param bInclude The rule: include (true) or exclude (false)
419        @param subclass The subclass
420        @param clean True = clean the tree after adding the new rule
421    */
422    void ClassTreeMask::addSingle(const Identifier* subclass, bool bInclude, bool clean)
423    {
424        for (std::set<const Identifier*>::const_iterator it = subclass->getDirectChildrenBegin(); it != subclass->getDirectChildrenEnd(); ++it)
425            this->add(*it, this->isIncluded(*it), false, false);
426
427        this->add(subclass, bInclude, false, clean);
428    }
429
430    /**
431        @brief Resets the mask to "include everything".
432    */
433    void ClassTreeMask::reset()
434    {
435        delete this->root_;
436        this->root_ = new ClassTreeMaskNode(ClassManager<BaseObject>::getIdentifier(), true);
437    }
438
439    /**
440        @brief Tells if a given subclass is included or not.
441        @param subclass The subclass
442        @return Included (true) or excluded (false)
443    */
444    bool ClassTreeMask::isIncluded(const Identifier* subclass) const
445    {
446        return this->isIncluded(this->root_, subclass);
447    }
448
449    /**
450        @brief Tells if a given subclass of a node in the rule-tree is included or not (recursive function).
451        @param node The node
452        @param subclass The subclass
453        @return Included (true) or excluded (false)
454    */
455    bool ClassTreeMask::isIncluded(ClassTreeMaskNode* node, const Identifier* subclass) const
456    {
457        // Check if the searched subclass is of the same type as the class in the current node or a derivative
458        if (subclass->isA(node->getClass()))
459        {
460            // Check for the special case
461            if (subclass == node->getClass())
462                return node->isIncluded();
463
464            // Go through the list of subnodes and look for a node containing the searched subclass and delegate the request by a recursive function-call.
465            for (std::list<ClassTreeMaskNode*>::iterator it = node->subnodes_.begin(); it != node->subnodes_.end(); ++it)
466                if (subclass->isA((*it)->getClass()))
467                    return isIncluded(*it, subclass);
468
469            // There is no subnode containing our class -> the rule of the current node takes in effect
470            return node->isIncluded();
471        }
472        else
473        {
474            // The class is not included in the mask: return false
475            return false;
476        }
477    }
478
479    /**
480        @brief Tells if a given subclass is excluded or not.
481        @param subclass The subclass
482        @return The inverted rule: Excluded (true) or included (false)
483    */
484    bool ClassTreeMask::isExcluded(const Identifier* subclass) const
485    {
486        return (!this->isIncluded(subclass));
487    }
488
489    /**
490        @brief Removes all unneeded rules that don't change the information of the mask.
491    */
492    void ClassTreeMask::clean()
493    {
494        this->clean(this->root_);
495    }
496
497    /**
498        @brief Removes all unneded rules that don't change the information of a node of a mask (recursive function).
499        @param node The node
500    */
501    void ClassTreeMask::clean(ClassTreeMaskNode* node)
502    {
503        // Iterate through all subnodes of the given node
504        for (std::list<ClassTreeMaskNode*>::iterator it = node->subnodes_.begin(); it != node->subnodes_.end(); )
505        {
506            // Recursive function-call: Clean the subnode
507            this->clean(*it);
508
509            // Now check if the subnode contains the same rule as the current node
510            if ((*it)->isIncluded() == node->isIncluded())
511            {
512                // It does: Move all subnodes of the redundant subnode to the current node
513                node->subnodes_.insert(node->subnodes_.end(), (*it)->subnodes_.begin(), (*it)->subnodes_.end());
514                (*it)->subnodes_.clear();
515
516                // Remove the redundant subnode from the current node
517                node->subnodes_.erase(it++);
518            }
519            else
520            {
521                // The subnode is necessary: Move on to the next subnode
522                ++it;
523            }
524        }
525    }
526
527    /**
528        @brief Checks if a node for the given subclass exists.
529        @param subclass The Identifier of the subclass
530        @return True = the node exists
531    */
532    bool ClassTreeMask::nodeExists(const Identifier* subclass)
533    {
534        for (ClassTreeMaskIterator it = this->root_; it; ++it)
535            if ((*it)->getClass() == subclass)
536                return true;
537
538        return false;
539    }
540
541    /**
542        @brief Assignment operator: Adds all rules of the other mask.
543        @param other The other mask
544        @return A reference to the mask itself
545    */
546    ClassTreeMask& ClassTreeMask::operator=(const ClassTreeMask& other)
547    {
548        // Make a copy to avoid troubles with self-assignments (like A = A).
549        ClassTreeMask temp(other);
550
551        // Removes all current rules
552        this->reset();
553
554        // Copy all rules from the other mask
555        for (ClassTreeMaskIterator it = temp.root_; it; ++it)
556            this->add(it->getClass(), it->isIncluded(), false, false);
557
558        // Return a reference to the mask itself
559        return (*this);
560    }
561
562    /**
563        @brief Compares the mask with another mask and returns true if they represent the same logic.
564        @param other The other mask
565        @return True if both masks represent the same logic
566    */
567    bool ClassTreeMask::operator==(const ClassTreeMask& other) const
568    {
569        ClassTreeMask temp1 = other;
570        ClassTreeMask temp2 = (*this);
571
572        temp1.clean();
573        temp2.clean();
574
575        ClassTreeMaskIterator it1 = temp1.root_;
576        ClassTreeMaskIterator it2 = temp2.root_;
577
578        for ( ; it1 && it2; ++it1, ++it2)
579            if (it1->getClass() != it2->getClass())
580                return false;
581
582        return true;
583    }
584
585    /**
586        @brief Compares the mask with another mask and returns true if they represent different logics.
587        @param other The other mask
588        @return True if the masks represent different logics
589    */
590    bool ClassTreeMask::operator!=(const ClassTreeMask& other) const
591    {
592        return (!((*this) == other));
593    }
594
595    /**
596        @brief Prefix operator + does nothing.
597        @return A reference to the mask itself
598    */
599    ClassTreeMask& ClassTreeMask::operator+()
600    {
601        return (*this);
602    }
603
604    /**
605        @brief Prefix operator - inverts the mask.
606        @return The inverted mask
607    */
608    ClassTreeMask ClassTreeMask::operator-() const
609    {
610        return (!(*this));
611    }
612
613    /**
614        @brief Adds two masks in the sense of a union (all classes that are included in at least one of the masks will be included in the resulting mask too).
615        @param other The mask to unite with
616        @return The union
617    */
618    ClassTreeMask ClassTreeMask::operator+(const ClassTreeMask& other) const
619    {
620        // Create a new mask
621        ClassTreeMask newmask;
622
623        // Add all nodes from the first mask, calculate the rule with the or-operation
624        for (ClassTreeMaskIterator it = this->root_; it; ++it)
625        {
626            const Identifier* subclass = it->getClass();
627            newmask.add(subclass, this->isIncluded(subclass) or other.isIncluded(subclass), false, false);
628        }
629
630        // Add all nodes from the second mask, calculate the rule with the or-operation
631        for (ClassTreeMaskIterator it = other.root_; it; ++it)
632        {
633            const Identifier* subclass = it->getClass();
634            newmask.add(subclass, this->isIncluded(subclass) or other.isIncluded(subclass), false, false);
635        }
636
637        // Drop all redundant rules
638        newmask.clean();
639
640        // Return the new mask
641        return newmask;
642    }
643
644    /**
645        @brief Intersects two masks (only classes that are included in both masks will be included in the resulting mask too).
646        @param other The mask to intersect with
647        @return The intersection
648    */
649    ClassTreeMask ClassTreeMask::operator*(const ClassTreeMask& other) const
650    {
651        // Create a new mask
652        ClassTreeMask newmask;
653
654        // Add all nodes from the first mask, calculate the rule with the and-operation
655        for (ClassTreeMaskIterator it = this->root_; it; ++it)
656        {
657            const Identifier* subclass = it->getClass();
658            newmask.add(subclass, this->isIncluded(subclass) and other.isIncluded(subclass), false, false);
659        }
660
661        // Add all nodes from the second mask, calculate the rule with the and-operation
662        for (ClassTreeMaskIterator it = other.root_; it; ++it)
663        {
664            const Identifier* subclass = it->getClass();
665            newmask.add(subclass, this->isIncluded(subclass) and other.isIncluded(subclass), false, false);
666        }
667
668        // Drop all redundant rules
669        newmask.clean();
670
671        // Return the new mask
672        return newmask;
673    }
674
675    /**
676        @brief Removes all elements of the second mask from the first mask (all classes that are included in the first mask stay included, except those that are included in the second mask too).
677        @param other The mask to subtract.
678        @return The difference
679    */
680    ClassTreeMask ClassTreeMask::operator-(const ClassTreeMask& other) const
681    {
682        return ((*this) * (!other));
683    }
684
685    /**
686        @brief Inverts the mask (all included classes are now excluded and vice versa).
687        @return The complement
688    */
689    ClassTreeMask ClassTreeMask::operator!() const
690    {
691        // Create a new mask
692        ClassTreeMask newmask;
693
694        // Add all nodes from the other mask, inverting the rules
695        for (ClassTreeMaskIterator it = this->root_; it; ++it)
696        {
697            const Identifier* subclass = it->getClass();
698            newmask.add(subclass, !this->isIncluded(subclass), false, false);
699        }
700
701        // Return the new mask
702        return newmask;
703    }
704
705    /**
706        @brief Unites this mask with another mask.
707        @param other The other mask
708        @return A reference to the mask itself
709    */
710    ClassTreeMask& ClassTreeMask::operator+=(const ClassTreeMask& other)
711    {
712        (*this) = (*this) + other;
713        return (*this);
714    }
715
716    /**
717        @brief Intersects this mask with another mask.
718        @param other The other mask
719        @return A reference to the mask itself
720    */
721    ClassTreeMask& ClassTreeMask::operator*=(const ClassTreeMask& other)
722    {
723        (*this) = (*this) * other;
724        return (*this);
725    }
726
727    /**
728        @brief Subtracts another mask from this mask.
729        @param other The other mask
730        @return A reference to the mask itself
731    */
732    ClassTreeMask& ClassTreeMask::operator-=(const ClassTreeMask& other)
733    {
734        (*this) = (*this) - other;
735        return (*this);
736    }
737
738    /**
739        @brief Intersects two masks (only classes that are included in both masks will be included in the resulting mask too).
740        @param other The mask to intersect with
741        @return The intersection
742    */
743    ClassTreeMask ClassTreeMask::operator&(const ClassTreeMask& other) const
744    {
745        return ((*this) * other);
746    }
747
748    /**
749        @brief Adds two masks in the sense of a union (all classes that are included in at least one of the masks will be included in the resulting mask too).
750        @param other The mask to unite with
751        @return The union
752    */
753    ClassTreeMask ClassTreeMask::operator|(const ClassTreeMask& other) const
754    {
755        return ((*this) + other);
756    }
757
758    /**
759        @brief Joins two masks in the sense of a xor (exclusivity) operation (all classes that are included in exactly one of the masks, but not in both, will be included in the resulting mask too).
760        @param other The mask to join with
761        @return The result
762    */
763    ClassTreeMask ClassTreeMask::operator^(const ClassTreeMask& other) const
764    {
765        // Create a new mask
766        ClassTreeMask newmask;
767
768        // Add all nodes from the first mask, calculate the rule with the xor-operation
769        for (ClassTreeMaskIterator it = this->root_; it; ++it)
770        {
771            const Identifier* subclass = it->getClass();
772            newmask.add(subclass, this->isIncluded(subclass) xor other.isIncluded(subclass), false, false);
773        }
774
775        // Add all nodes from the second mask, calculate the rule with the xor-operation
776        for (ClassTreeMaskIterator it = other.root_; it; ++it)
777        {
778            const Identifier* subclass = it->getClass();
779            newmask.add(subclass, this->isIncluded(subclass) xor other.isIncluded(subclass), false, false);
780        }
781
782        // Drop all redundant rules
783        newmask.clean();
784
785        // Return the new mask
786        return newmask;
787    }
788
789    /**
790        @brief Inverts the mask (all included classes are now excluded and vice versa).
791        @return The complement
792    */
793    ClassTreeMask ClassTreeMask::operator~() const
794    {
795        return (!(*this));
796    }
797
798    /**
799        @brief Intersects this mask with another mask (and-operation)
800        @param other The other mask
801        @return A reference to the mask itself
802    */
803    ClassTreeMask& ClassTreeMask::operator&=(const ClassTreeMask& other)
804    {
805        (*this) = (*this) & other;
806        return (*this);
807    }
808
809    /**
810        @brief Unites this mask with another mask (or-operation).
811        @param other The other mask
812        @return A reference to the mask itself
813    */
814    ClassTreeMask& ClassTreeMask::operator|=(const ClassTreeMask& other)
815    {
816        (*this) = (*this) | other;
817        return (*this);
818    }
819
820    /**
821        @brief Joins this mask with another mask with a xor-operation.
822        @param other The other mask
823        @return A reference to the mask itself
824    */
825    ClassTreeMask& ClassTreeMask::operator^=(const ClassTreeMask& other)
826    {
827        (*this) = (*this) ^ other;
828        return (*this);
829    }
830
831    /**
832        @brief Converts the content of a mask into a human readable string and puts it on the ostream.
833        @param out The ostream
834        @param mask The mask
835        @return A reference to the ostream
836    */
837    std::ostream& operator<<(std::ostream& out, const ClassTreeMask& mask)
838    {
839        // Iterate through all rules
840        for (ClassTreeMaskIterator it = mask.root_; it; ++it)
841        {
842            // Calculate the prefix: + means included, - means excluded
843            if (it->isIncluded())
844                out << "+";
845            else
846                out << "-";
847
848            // Put the name of the corresponding class on the stream
849            out << it->getClass()->getName() << " ";
850        }
851
852        return out;
853    }
854}
Note: See TracBrowser for help on using the repository browser.