Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/output/OutputListener.cc @ 9550

Last change on this file since 9550 was 9550, checked in by landauf, 11 years ago

merged testing branch back to trunk. unbelievable it took me 13 months to finish this chore…

  • Property svn:eol-style set to native
File size: 5.5 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 Implementation of the OutputListener class.
32*/
33
34#include "OutputListener.h"
35
36#include "OutputManager.h"
37
38namespace orxonox
39{
40    /**
41        @brief Constructor, initializes the values and registers the instance at OutputManager if requested.
42        @param bRegister If \c true, the instance is automatically registered at OutputManager.
43                         Should be \c false if the constructor of the derived class generates output.
44    */
45    OutputListener::OutputListener(bool bRegister)
46    {
47        this->levelMask_ = level::none;
48        this->additionalContextsLevelMask_ = level::none;
49        this->additionalContextsMask_ = context::none;
50
51        if (bRegister)
52            OutputManager::getInstance().registerListener(this);
53    }
54
55    /**
56        @brief Destructor, unregisters the instance from OutputManager.
57    */
58    OutputListener::~OutputListener()
59    {
60        OutputManager::getInstance().unregisterListener(this);
61    }
62
63    /**
64        @brief Adds a listener to the list.
65    */
66    void OutputListener::registerListener(AdditionalContextListener* listener)
67    {
68        this->listeners_.push_back(listener);
69    }
70
71    /**
72        @brief Removes a listener from the list.
73    */
74    void OutputListener::unregisterListener(AdditionalContextListener* listener)
75    {
76        for (std::vector<AdditionalContextListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
77        {
78            if (*it == listener)
79            {
80                this->listeners_.erase(it);
81                break;
82            }
83        }
84    }
85
86    /**
87        @brief Defines the level mask in a way which accepts all output up to the level \c max.
88    */
89    void OutputListener::setLevelMax(OutputLevel max)
90    {
91        this->setLevelRange(static_cast<OutputLevel>(0x1), max);
92    }
93
94    /**
95        @brief Defines the level mask in a way which accepts all output between the levels \c min and \c max.
96    */
97    void OutputListener::setLevelRange(OutputLevel min, OutputLevel max)
98    {
99        int mask = 0;
100        for (int level = min; level <= max; level = level << 1)
101            mask |= level;
102
103        this->setLevelMask(static_cast<OutputLevel>(mask));
104    }
105
106    /**
107        @brief Defines the level mask.
108    */
109    void OutputListener::setLevelMask(OutputLevel mask)
110    {
111        this->levelMask_ = mask;
112
113        for (size_t i = 0; i < this->listeners_.size(); ++i)
114            this->listeners_[i]->updatedLevelMask(this);
115    }
116
117    /**
118        @brief Defines the level mask of additional contexts in a way which accepts all output up to the level \c max.
119    */
120    void OutputListener::setAdditionalContextsLevelMax(OutputLevel max)
121    {
122        this->setAdditionalContextsLevelRange(static_cast<OutputLevel>(0x1), max);
123    }
124
125    /**
126        @brief Defines the level mask of additional contexts in a way which accepts all output between the levels \c min and \c max.
127    */
128    void OutputListener::setAdditionalContextsLevelRange(OutputLevel min, OutputLevel max)
129    {
130        int mask = 0;
131        for (int level = min; level <= max; level = level << 1)
132            mask |= level;
133
134        this->setAdditionalContextsLevelMask(static_cast<OutputLevel>(mask));
135    }
136
137    /**
138        @brief Defines the level mask of additional contexts.
139    */
140    void OutputListener::setAdditionalContextsLevelMask(OutputLevel mask)
141    {
142        this->additionalContextsLevelMask_ = mask;
143
144        for (size_t i = 0; i < this->listeners_.size(); ++i)
145            this->listeners_[i]->updatedAdditionalContextsLevelMask(this);
146    }
147
148    /**
149        @brief Defines the mask of additional contexts.
150    */
151    void OutputListener::setAdditionalContextsMask(OutputContextMask mask)
152    {
153        this->additionalContextsMask_ = mask;
154
155        for (size_t i = 0; i < this->listeners_.size(); ++i)
156            this->listeners_[i]->updatedAdditionalContextsMask(this);
157    }
158
159    /**
160        @brief Returns true if this listener accepts output of the given level and context, based on the levels and contexts masks.
161    */
162    bool OutputListener::acceptsOutput(OutputLevel level, const OutputContextContainer& context) const
163    {
164        // check if the output level is accepted by the level mask (independent of the context)
165        if (this->levelMask_ & level)
166            return true;
167
168        // check if the output context is accepted by the additional context mask and if the level matches as well
169        if ((this->additionalContextsMask_ & context.mask) && (this->additionalContextsLevelMask_ & level))
170            return true;
171
172        // otherwise we don't accept the output
173        return false;
174    }
175}
Note: See TracBrowser for help on using the repository browser.