Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12144 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 5.5 KB
RevLine 
[8765]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
[8848]29/**
30    @file
31    @brief Implementation of the OutputListener class.
32*/
33
[8765]34#include "OutputListener.h"
35
36#include "OutputManager.h"
37
38namespace orxonox
39{
[8848]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    */
[8834]45    OutputListener::OutputListener(bool bRegister)
[8765]46    {
47        this->levelMask_ = level::none;
[8833]48        this->additionalContextsLevelMask_ = level::none;
49        this->additionalContextsMask_ = context::none;
[8765]50
[8834]51        if (bRegister)
52            OutputManager::getInstance().registerListener(this);
[8765]53    }
54
[8848]55    /**
56        @brief Destructor, unregisters the instance from OutputManager.
57    */
[8765]58    OutputListener::~OutputListener()
59    {
[8776]60        OutputManager::getInstance().unregisterListener(this);
[8765]61    }
62
[8848]63    /**
[9550]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    /**
[8848]87        @brief Defines the level mask in a way which accepts all output up to the level \c max.
88    */
[8765]89    void OutputListener::setLevelMax(OutputLevel max)
90    {
[8808]91        this->setLevelRange(static_cast<OutputLevel>(0x1), max);
[8765]92    }
93
[8848]94    /**
95        @brief Defines the level mask in a way which accepts all output between the levels \c min and \c max.
96    */
[8765]97    void OutputListener::setLevelRange(OutputLevel min, OutputLevel max)
98    {
[8808]99        int mask = 0;
100        for (int level = min; level <= max; level = level << 1)
[8765]101            mask |= level;
102
[8808]103        this->setLevelMask(static_cast<OutputLevel>(mask));
[8765]104    }
105
[8848]106    /**
107        @brief Defines the level mask.
108    */
[8765]109    void OutputListener::setLevelMask(OutputLevel mask)
110    {
111        this->levelMask_ = mask;
112
[11071]113        for (AdditionalContextListener* listener : this->listeners_)
114            listener->updatedLevelMask(this);
[8765]115    }
116
[8848]117    /**
118        @brief Defines the level mask of additional contexts in a way which accepts all output up to the level \c max.
119    */
[8833]120    void OutputListener::setAdditionalContextsLevelMax(OutputLevel max)
[8765]121    {
[8833]122        this->setAdditionalContextsLevelRange(static_cast<OutputLevel>(0x1), max);
123    }
[8765]124
[8848]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    */
[8833]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));
[8765]135    }
[8833]136
[8848]137    /**
138        @brief Defines the level mask of additional contexts.
139    */
[8833]140    void OutputListener::setAdditionalContextsLevelMask(OutputLevel mask)
141    {
142        this->additionalContextsLevelMask_ = mask;
143
[11071]144        for (AdditionalContextListener* listener : this->listeners_)
145            listener->updatedAdditionalContextsLevelMask(this);
[8833]146    }
147
[8848]148    /**
149        @brief Defines the mask of additional contexts.
150    */
[8833]151    void OutputListener::setAdditionalContextsMask(OutputContextMask mask)
152    {
153        this->additionalContextsMask_ = mask;
154
[11071]155        for (AdditionalContextListener* listener : this->listeners_)
156            listener->updatedAdditionalContextsMask(this);
[8833]157    }
[8850]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    }
[8765]175}
Note: See TracBrowser for help on using the repository browser.