Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/OutputBuffer.h @ 2493

Last change on this file since 2493 was 2171, checked in by landauf, 16 years ago

merged revisions 2111-2170 from objecthierarchy branch back to trunk.

  • Property svn:eol-style set to native
File size: 5.6 KB
RevLine 
[1505]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
[1791]29/**
[2171]30    @file
[1791]31    @brief Declaration of the OutputBuffer class.
32
33    The OutputBuffer acts almost like std::ostream. You can put text and other values to the
34    OutputBuffer by using the << operator. The OutputBuffer stores the text and calls registerd
35    listeners if new text gets assigned.
36    The listeners are then able to retrieve the text line by line.
37
38    It's important to know that getLine actually removes the line from the OutputBuffer, so it's
39    better to only have one "active" listener.
40*/
41
[1505]42#ifndef _OutputBuffer_H__
43#define _OutputBuffer_H__
44
45#include <list>
46#include <sstream>
47#include <iostream>
48
[1586]49#include "UtilPrereqs.h"
[1505]50
51namespace orxonox
52{
[1791]53    /**
54        @brief A pure virtual baseclass for classes that want to register as listener to an OutputBuffer.
55
56        This class is pure virtual, so an inheriting class has to implement the function on it's own.
57        The function get's called, if an instance of the inheriting class registers as a listener at
58        an OutputBuffer and this buffer changes.
59    */
[2171]60    class OutputBufferListener
[1505]61    {
62        friend class OutputBuffer;
63
64        public:
65            virtual ~OutputBufferListener() {}
66
67        private:
68            virtual void outputChanged() = 0;
69    };
70
[1791]71    /**
72        @brief The OutputBuffer acts almost like std::ostream and stores the assigned text.
73
74        If text gets assigned by using the << operator or another function, the OutputBuffer
75        calls it's listeners, allowing them to retrieve the text line by line.
76
77        It's important to know that getLine actually removes the line from the OutputBuffer, so it's
78        better to only have one "active" listener.
79    */
[1586]80    class _UtilExport OutputBuffer
[1505]81    {
82        public:
83            OutputBuffer() {}
84            ~OutputBuffer() {}
85
[1791]86            /**
87                @brief Puts some object/value to the OutputBuffer. The text gets assigned and the OutputBuffer calls it's listeners.
88                @param object The object/value to assign
89            */
[1505]90            template <class T>
91            inline OutputBuffer& operator<<(T object)
92            {
93                this->stream_ << object;
94                this->callListeners();
95                return *this;
96            }
97
[1791]98            /**
99                @brief Reads the stored text of the other OutputBuffer and calls the listeners.
100                @param object The other OutputBuffer
101            */
[1586]102            template <const OutputBuffer&>
103            inline OutputBuffer& operator<<(const OutputBuffer& object)
104            {
[1716]105                this->stream_ << object.stream_.rdbuf();
[1586]106                this->callListeners();
107                return *this;
108            }
109
[1505]110            OutputBuffer& operator<<(std::ostream& (*manipulator)(std::ostream&));
111            OutputBuffer& operator<<(std::ios& (*manipulator)(std::ios&));
112            OutputBuffer& operator<<(std::ios_base& (*manipulator)(std::ios_base&));
113
[1791]114            /**
115                @brief Does the same like operator<<: Assigns the object to the stream and calls the listeners.
116                @param object The object/value
117            */
[1505]118            template <class T>
119            inline void add(T object)
120            {
121                this->stream_ << object;
122                this->callListeners();
123            }
124
[1791]125            /**
126                @brief Assigns an object/value and adds std::endl.
127                @param object The object/value
128            */
[1505]129            template <class T>
130            inline void addLine(T object)
131            {
132                this->stream_ << object << std::endl;
133                this->callListeners();
134            }
135
[1791]136            /**
137                @brief Puts std::endl to the stream and calls the listeners.
138            */
[1505]139            inline void newline()
140            {
141                this->stream_ << std::endl;
142                this->callListeners();
143            }
144
[1791]145            /**
146                @brief Flushes the stored text (~empties the OutputBuffer).
147            */
[1505]148            inline void flush()
149            {
150                this->stream_.flush();
151            }
152
153            bool getLine(std::string* output);
154
155            void registerListener(OutputBufferListener* listener);
156            void unregisterListener(OutputBufferListener* listener);
157
[1791]158            /**
159                @brief Returns the internal stringstream object.
160            */
[1586]161            inline std::stringstream& getStream()
[1791]162            {
163                return this->stream_;
164            }
[1586]165
[1505]166        private:
167            void callListeners();
168
[1854]169            std::stringstream stream_;                   //!< The stringstream that stores the assigned text
170            std::list<OutputBufferListener*> listeners_; //!< A list of all listeners
[1505]171    };
172}
173
174#endif /* _OutputBuffer_H__ */
Note: See TracBrowser for help on using the repository browser.