| 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 OutputBuffer. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "OutputBuffer.h" | 
|---|
| 35 |  | 
|---|
| 36 | namespace orxonox | 
|---|
| 37 | { | 
|---|
| 38 |     /** | 
|---|
| 39 |         @brief Adds a new listener to the list. | 
|---|
| 40 |         @param listener The new listener | 
|---|
| 41 |     */ | 
|---|
| 42 |     void OutputBuffer::registerListener(OutputBufferListener* listener) | 
|---|
| 43 |     { | 
|---|
| 44 |         this->listeners_.push_back(listener); | 
|---|
| 45 |     } | 
|---|
| 46 |  | 
|---|
| 47 |     /** | 
|---|
| 48 |         @brief Removes a listener from the list. | 
|---|
| 49 |         @param listener The listener | 
|---|
| 50 |     */ | 
|---|
| 51 |     void OutputBuffer::unregisterListener(OutputBufferListener* listener) | 
|---|
| 52 |     { | 
|---|
| 53 |         for (std::vector<OutputBufferListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) | 
|---|
| 54 |         { | 
|---|
| 55 |             if ((*it) == listener) | 
|---|
| 56 |                 it = this->listeners_.erase(it); | 
|---|
| 57 |             else | 
|---|
| 58 |                 ++it; | 
|---|
| 59 |         } | 
|---|
| 60 |     } | 
|---|
| 61 |  | 
|---|
| 62 |     /** | 
|---|
| 63 |         @brief Puts a stream manipulator to the stream. | 
|---|
| 64 |         @param manipulator The manipulator | 
|---|
| 65 |     */ | 
|---|
| 66 |     OutputBuffer& OutputBuffer::operator<<(std::ostream& (*manipulator)(std::ostream&)) | 
|---|
| 67 |     { | 
|---|
| 68 |         this->stream_ << manipulator; | 
|---|
| 69 |         this->callListeners(); | 
|---|
| 70 |         return *this; | 
|---|
| 71 |     } | 
|---|
| 72 |  | 
|---|
| 73 |     /** | 
|---|
| 74 |         @brief Puts a stream manipulator to the stream. | 
|---|
| 75 |         @param manipulator The manipulator | 
|---|
| 76 |     */ | 
|---|
| 77 |     OutputBuffer& OutputBuffer::operator<<(std::ios& (*manipulator)(std::ios&)) | 
|---|
| 78 |     { | 
|---|
| 79 |         this->stream_ << manipulator; | 
|---|
| 80 |         this->callListeners(); | 
|---|
| 81 |         return *this; | 
|---|
| 82 |     } | 
|---|
| 83 |  | 
|---|
| 84 |     /** | 
|---|
| 85 |         @brief Puts a stream manipulator to the stream. | 
|---|
| 86 |         @param manipulator The manipulator | 
|---|
| 87 |     */ | 
|---|
| 88 |     OutputBuffer& OutputBuffer::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) | 
|---|
| 89 |     { | 
|---|
| 90 |         this->stream_ << manipulator; | 
|---|
| 91 |         this->callListeners(); | 
|---|
| 92 |         return *this; | 
|---|
| 93 |     } | 
|---|
| 94 |  | 
|---|
| 95 |     /** | 
|---|
| 96 |         @brief Removes the first line from the stream and assigns it to a given string object. | 
|---|
| 97 |         @param output The string object to assign the first line | 
|---|
| 98 |         @return True if there was at least one line in the stream and this line was successfully assigned | 
|---|
| 99 |  | 
|---|
| 100 |         It's important to know the returned line will be removed from the stream. If there are more than one | 
|---|
| 101 |         listener, they have to cooperate to avoid conflicts. | 
|---|
| 102 |     */ | 
|---|
| 103 |     bool OutputBuffer::getLine(std::string* output) | 
|---|
| 104 |     { | 
|---|
| 105 |         std::getline(this->stream_, *output); | 
|---|
| 106 |  | 
|---|
| 107 |         bool eof = this->stream_.eof(); | 
|---|
| 108 |         bool fail = this->stream_.fail(); | 
|---|
| 109 |  | 
|---|
| 110 |         if (eof) | 
|---|
| 111 |             this->stream_.flush(); | 
|---|
| 112 |  | 
|---|
| 113 |         if (eof || fail) | 
|---|
| 114 |             this->stream_.clear(); | 
|---|
| 115 |  | 
|---|
| 116 |         return (!eof && !fail); | 
|---|
| 117 |     } | 
|---|
| 118 |  | 
|---|
| 119 |     /** | 
|---|
| 120 |         @brief Calls the outputChanged() function of all registered listeners. | 
|---|
| 121 |     */ | 
|---|
| 122 |     void OutputBuffer::callListeners() | 
|---|
| 123 |     { | 
|---|
| 124 |         for (std::vector<OutputBufferListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 125 |             (*it)->outputChanged(); | 
|---|
| 126 |     } | 
|---|
| 127 | } | 
|---|