Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Merge_HS18/src/libraries/util/output/OutputStream.h @ 12178

Last change on this file since 12178 was 9550, checked in by landauf, 12 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: 4.9 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    @ingroup Output
32    @brief Declaration of the OutputStream class which is used to send output to orxonox::OutputManager.
33*/
34
35#ifndef _OutputStream_H__
36#define _OutputStream_H__
37
38#include "util/UtilPrereqs.h"
39
40#include <sstream>
41
42#include "OutputDefinitions.h"
43
44namespace orxonox
45{
46    /**
47        @brief This class is used to buffer output and send it to OutputManager whenever std::endl is passed to it.
48
49        OutputStream inherits from std::ostringstream and acts like std::cout.
50        This means you can use the << operator to write output to the stream.
51        This class is used by the orxout() function.
52
53        @attention
54        You must end an output message with std::endl, otherwise the message
55        won't be flushed. '\\n' only adds a new line to the message.
56
57        The following code samples are all equivalent:
58        @code
59        OutputStream stream;
60        stream.setOutputAttributes(user_info, context::example());
61        stream << "Hello World" << endl;
62        @endcode
63
64        @code
65        OutputStream stream(user_info, context::example());
66        stream << "Hello World" << endl;
67        @endcode
68
69        @code
70        orxout(user_info, context::example) << "Hello World" << endl;
71        @endcode
72    */
73    class OutputStream : public std::ostringstream
74    {
75        typedef std::ostream& (*EndlType)(std::ostream&);
76
77        public:
78            _UtilExport OutputStream();
79            _UtilExport OutputStream(OutputLevel level, const OutputContextContainer& context);
80            _UtilExport ~OutputStream();
81
82            void _UtilExport setOutputAttributes(OutputLevel level, const OutputContextContainer& context);
83
84            /// @brief Generic << operator which adds output to the stream.
85            template <class T>
86            inline OutputStream& operator<<(const T& val) { return this->output(val); }
87            /// @brief Sends a manipulator to the output stream.
88            inline OutputStream& operator<<(std::ios_base& (*manipulator)(std::ios_base&)) { return this->output(manipulator); }
89            /// @brief Sends a manipulator to the output stream.
90            inline OutputStream& operator<<(std::ios&      (*manipulator)(std::ios&))      { return this->output(manipulator); }
91            /// @brief Sends a manipulator to the output stream and flushes the message if the manipulator is std::endl.
92            inline OutputStream& operator<<(std::ostream&  (*manipulator)(std::ostream&))
93            {
94                if (this->bAcceptsOutput_)
95                {
96                    if (manipulator == static_cast<EndlType>(std::endl))
97                        this->sendMessage(); // send the message to OutputManager
98                    else
99                        return this->output(manipulator); // apply the manipulator
100                }
101                return *this;
102            }
103
104            inline const OutputLevel getOutputLevel() const { return this->level_; }
105            inline const OutputContextContainer* getOutputContext() const { return this->context_; }
106            inline bool acceptsOutput() const { return this->bAcceptsOutput_; }
107
108        private:
109            /// @brief Generic function to add values to the output stream, using the inherited << operator from std::ostringstream.
110            template <class T>
111            inline OutputStream& output(const T& val)
112            {
113                if (this->bAcceptsOutput_)
114                    static_cast<std::ostringstream&>(*this) << val;
115                return *this;
116            }
117
118            void _UtilExport sendMessage();
119
120            OutputLevel level_;                     ///< The output level of the current message
121            const OutputContextContainer* context_; ///< The output context of the current message
122            bool bAcceptsOutput_;                   ///< After defining level and context of the following message, this flag is set according to the masks defined in OutputManager. If it is false, the OutputStream will throw away every output sent using the << operator.
123    };
124}
125
126#endif /* _OutputStream_H__ */
Note: See TracBrowser for help on using the repository browser.