Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/output/src/libraries/util/output/OutputListener.cc @ 8850

Last change on this file since 8850 was 8850, checked in by landauf, 13 years ago

moved filtering of sub-contexts from BaseWriter to a new interface SubcontextOutputListener

  • Property svn:eol-style set to native
File size: 4.7 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 Defines the level mask in a way which accepts all output up to the level \c max.
65    */
66    void OutputListener::setLevelMax(OutputLevel max)
67    {
68        this->setLevelRange(static_cast<OutputLevel>(0x1), max);
69    }
70
71    /**
72        @brief Defines the level mask in a way which accepts all output between the levels \c min and \c max.
73    */
74    void OutputListener::setLevelRange(OutputLevel min, OutputLevel max)
75    {
76        int mask = 0;
77        for (int level = min; level <= max; level = level << 1)
78            mask |= level;
79
80        this->setLevelMask(static_cast<OutputLevel>(mask));
81    }
82
83    /**
84        @brief Defines the level mask.
85    */
86    void OutputListener::setLevelMask(OutputLevel mask)
87    {
88        this->levelMask_ = mask;
89
90        OutputManager::getInstance().updateCombinedLevelMask();
91    }
92
93    /**
94        @brief Defines the level mask of additional contexts in a way which accepts all output up to the level \c max.
95    */
96    void OutputListener::setAdditionalContextsLevelMax(OutputLevel max)
97    {
98        this->setAdditionalContextsLevelRange(static_cast<OutputLevel>(0x1), max);
99    }
100
101    /**
102        @brief Defines the level mask of additional contexts in a way which accepts all output between the levels \c min and \c max.
103    */
104    void OutputListener::setAdditionalContextsLevelRange(OutputLevel min, OutputLevel max)
105    {
106        int mask = 0;
107        for (int level = min; level <= max; level = level << 1)
108            mask |= level;
109
110        this->setAdditionalContextsLevelMask(static_cast<OutputLevel>(mask));
111    }
112
113    /**
114        @brief Defines the level mask of additional contexts.
115    */
116    void OutputListener::setAdditionalContextsLevelMask(OutputLevel mask)
117    {
118        this->additionalContextsLevelMask_ = mask;
119
120        OutputManager::getInstance().updateCombinedAdditionalContextsLevelMask();
121    }
122
123    /**
124        @brief Defines the mask of additional contexts.
125    */
126    void OutputListener::setAdditionalContextsMask(OutputContextMask mask)
127    {
128        this->additionalContextsMask_ = mask;
129
130        OutputManager::getInstance().updateCombinedAdditionalContextsMask();
131    }
132
133    /**
134        @brief Returns true if this listener accepts output of the given level and context, based on the levels and contexts masks.
135    */
136    bool OutputListener::acceptsOutput(OutputLevel level, const OutputContextContainer& context) const
137    {
138        // check if the output level is accepted by the level mask (independent of the context)
139        if (this->levelMask_ & level)
140            return true;
141
142        // check if the output context is accepted by the additional context mask and if the level matches as well
143        if ((this->additionalContextsMask_ & context.mask) && (this->additionalContextsLevelMask_ & level))
144            return true;
145
146        // otherwise we don't accept the output
147        return false;
148    }
149}
Note: See TracBrowser for help on using the repository browser.