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