[8765] | 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 | |
---|
[8848] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @ingroup Output |
---|
| 32 | @brief Declaration of the OutputStream class which is used to send output to orxonox::OutputManager. |
---|
| 33 | */ |
---|
| 34 | |
---|
[8765] | 35 | #ifndef _OutputStream_H__ |
---|
| 36 | #define _OutputStream_H__ |
---|
| 37 | |
---|
| 38 | #include "util/UtilPrereqs.h" |
---|
| 39 | |
---|
| 40 | #include <sstream> |
---|
| 41 | |
---|
| 42 | #include "OutputDefinitions.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[8848] | 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 | */ |
---|
[8771] | 73 | class OutputStream : public std::ostringstream |
---|
[8765] | 74 | { |
---|
| 75 | typedef std::ostream& (*EndlType)(std::ostream&); |
---|
| 76 | |
---|
| 77 | public: |
---|
[8771] | 78 | _UtilExport OutputStream(); |
---|
[8833] | 79 | _UtilExport OutputStream(OutputLevel level, const OutputContextContainer& context); |
---|
[8849] | 80 | _UtilExport ~OutputStream(); |
---|
[8765] | 81 | |
---|
[8833] | 82 | void _UtilExport setOutputAttributes(OutputLevel level, const OutputContextContainer& context); |
---|
[8765] | 83 | |
---|
[8848] | 84 | /// @brief Generic << operator which adds output to the stream. |
---|
[8765] | 85 | template <class T> |
---|
| 86 | inline OutputStream& operator<<(const T& val) { return this->output(val); } |
---|
[8848] | 87 | /// @brief Sends a manipulator to the output stream. |
---|
[8765] | 88 | inline OutputStream& operator<<(std::ios_base& (*manipulator)(std::ios_base&)) { return this->output(manipulator); } |
---|
[8848] | 89 | /// @brief Sends a manipulator to the output stream. |
---|
[8765] | 90 | inline OutputStream& operator<<(std::ios& (*manipulator)(std::ios&)) { return this->output(manipulator); } |
---|
[8848] | 91 | /// @brief Sends a manipulator to the output stream and flushes the message if the manipulator is std::endl. |
---|
[8765] | 92 | inline OutputStream& operator<<(std::ostream& (*manipulator)(std::ostream&)) |
---|
| 93 | { |
---|
| 94 | if (this->bAcceptsOutput_) |
---|
| 95 | { |
---|
| 96 | if (manipulator == static_cast<EndlType>(std::endl)) |
---|
[8848] | 97 | this->sendMessage(); // send the message to OutputManager |
---|
[8765] | 98 | else |
---|
[8848] | 99 | return this->output(manipulator); // apply the manipulator |
---|
[8765] | 100 | } |
---|
| 101 | return *this; |
---|
| 102 | } |
---|
| 103 | |
---|
[9529] | 104 | inline const OutputLevel getOutputLevel() const { return this->level_; } |
---|
| 105 | inline const OutputContextContainer* getOutputContext() const { return this->context_; } |
---|
| 106 | |
---|
[8765] | 107 | private: |
---|
[8848] | 108 | /// @brief Generic function to add values to the output stream, using the inherited << operator from std::ostringstream. |
---|
[8765] | 109 | template <class T> |
---|
| 110 | inline OutputStream& output(const T& val) |
---|
| 111 | { |
---|
| 112 | if (this->bAcceptsOutput_) |
---|
| 113 | static_cast<std::ostringstream&>(*this) << val; |
---|
| 114 | return *this; |
---|
| 115 | } |
---|
| 116 | |
---|
[8771] | 117 | void _UtilExport sendMessage(); |
---|
[8765] | 118 | |
---|
[8848] | 119 | OutputLevel level_; ///< The output level of the current message |
---|
| 120 | const OutputContextContainer* context_; ///< The output context of the current message |
---|
| 121 | 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. |
---|
[8765] | 122 | }; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | #endif /* _OutputStream_H__ */ |
---|