Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 3 : 4 : // This library is free software; you can redistribute it and/or 5 : // modify it under the terms of the GNU Lesser General Public 6 : // License as published by the Free Software Foundation; either 7 : // version 2.1 of the License, or (at your option) any later version. 8 : 9 : // This library is distributed in the hope that it will be useful, 10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : // Lesser General Public License for more details. 13 : 14 : // You should have received a copy of the GNU Lesser General Public 15 : // License along with this library; if not, write to the Free Software 16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 : 18 : 19 : 20 : #ifndef LIBMESH_OSTREAM_PROXY_H 21 : #define LIBMESH_OSTREAM_PROXY_H 22 : 23 : 24 : 25 : // C++ includes 26 : #include <iostream> 27 : 28 : namespace libMesh 29 : { 30 : 31 : /** 32 : * This class is intended to be reseatable like a pointer-to-ostream 33 : * for flexibility, but to look like a reference when used to produce 34 : * less awkward user code. 35 : * 36 : * It is up to the user to ensure that the target ostream remains valid. 37 : * 38 : * \author Roy Stogner 39 : * \date 2010 40 : */ 41 : template <typename charT=char, typename traits=std::char_traits<charT>> 42 : class BasicOStreamProxy 43 : { 44 : public: 45 : /** 46 : * This class is going to be used to proxy for ostream, but other 47 : * character and traits types are possible 48 : */ 49 : typedef std::basic_ostream<charT,traits> streamT; 50 : 51 : /** 52 : * This class is going to be used to proxy for ostream, but other 53 : * character and traits types are possible 54 : */ 55 : typedef std::basic_streambuf<charT,traits> streambufT; 56 : 57 : /** 58 : * Default constructor. Takes a reference to the \p target ostream 59 : * to which we pass output. The user is responsible for ensuring 60 : * that this target exists for as long as the proxy does. 61 : */ 62 1100 : BasicOStreamProxy (streamT & target) : _target(&target) {} 63 : 64 : /** 65 : * Shallow copy constructor. Output in the new object is passed to 66 : * the same target ostream as in the old object. The user is 67 : * responsible for ensuring that this target exists for as long as 68 : * the proxies do. 69 : */ 70 0 : BasicOStreamProxy (BasicOStreamProxy & old) : _target(old._target) {} 71 : 72 : /** 73 : * Reset the internal target to a new \p target output stream. 74 : */ 75 0 : BasicOStreamProxy & operator= (streamT & target) 76 : { 77 0 : _target = ⌖ 78 0 : return *this; 79 : } 80 : 81 : /** 82 : * Reset the target to the same output stream as in \p old 83 : */ 84 : BasicOStreamProxy & operator= (const BasicOStreamProxy & old) 85 : { 86 : _target = old._target; 87 : return *this; 88 : } 89 : 90 : /** 91 : * Default destructor. 92 : */ 93 : ~BasicOStreamProxy () = default; 94 : 95 : // 96 : // Functions that get passed to the proxied target: 97 : // 98 : 99 : /** 100 : * Conversion to ostream &, for when we get passed to a function 101 : * requesting one. 102 : */ 103 15791 : operator streamT & () { return *_target; } 104 : 105 : /** 106 : * Conversion to const ostream &, for when we get passed to a 107 : * function requesting one. 108 : */ 109 : operator const streamT &() const { return *_target; } 110 : 111 : /** 112 : * Redirect any output to the target. 113 : */ 114 : template<typename T> 115 14421 : BasicOStreamProxy & operator<< (const T & in) 116 : { 117 534327 : (*_target) << in; return *this; 118 : } 119 : 120 : /** 121 : * Redirect any ostream manipulators to the target. 122 : */ 123 0 : BasicOStreamProxy & operator<< (streamT & (*in)(streamT &)) 124 : { 125 374789 : (*_target) << in; return *this; 126 : } 127 : 128 : /** 129 : * Redirect any ios manipulators to the target. 130 : */ 131 : BasicOStreamProxy & operator<< (std::basic_ios<charT,traits> & (*in)(std::basic_ios<charT,traits> &)) 132 : { 133 : (*_target) << in; return *this; 134 : } 135 : 136 : /** 137 : * Redirect any ios_base manipulators to the target. 138 : */ 139 2896 : BasicOStreamProxy & operator<< (std::ios_base & (*in)(std::ios_base &)) 140 : { 141 105241 : (*_target) << in; return *this; 142 : } 143 : 144 : /** 145 : * Get the associated stream buffer 146 : */ 147 : streambufT * rdbuf () const { return _target->rdbuf(); } 148 : 149 : /** 150 : * Set the associated stream buffer 151 : */ 152 13620 : streambufT * rdbuf ( streambufT * sb ) { return _target->rdbuf(sb); } 153 : 154 : /** 155 : * Flush the associated stream buffer 156 : */ 157 0 : BasicOStreamProxy & flush () { _target->flush(); return *this; } 158 : 159 : /** 160 : * Get the associated format flags 161 : */ 162 : std::ios_base::fmtflags flags ( ) const 163 : { return _target->flags(); } 164 : 165 : /** 166 : * Set/get the associated format flags 167 : */ 168 : std::ios_base::fmtflags flags ( std::ios_base::fmtflags fmtfl ) 169 : { return _target->flags(fmtfl); } 170 : 171 : /** 172 : * Set the associated flags 173 : */ 174 0 : std::ios_base::fmtflags setf ( std::ios_base::fmtflags fmtfl ) 175 0 : { return _target->setf(fmtfl); } 176 : 177 : /** 178 : * Set the associated flags 179 : */ 180 : std::ios_base::fmtflags setf ( std::ios_base::fmtflags fmtfl, 181 : std::ios_base::fmtflags mask ) 182 : { return _target->setf(fmtfl, mask); } 183 : 184 : /** 185 : * Clear the associated flags 186 : */ 187 0 : void unsetf ( std::ios_base::fmtflags mask ) 188 0 : { _target->unsetf(mask); } 189 : 190 : /** 191 : * Get the associated write precision 192 : */ 193 0 : std::streamsize precision () const 194 0 : { return _target->precision(); } 195 : 196 : /** 197 : * Set the associated write precision 198 : */ 199 0 : std::streamsize precision ( std::streamsize prec ) 200 0 : { return _target->precision(prec); } 201 : 202 : // 203 : // Functions that affect the Proxy class: 204 : // 205 : 206 : /** 207 : * Reset the proxy to point to a different \p target. 208 : * 209 : * \note This does not delete the previous target. 210 : */ 211 0 : void reset (streamT & target) { _target = ⌖ } 212 : 213 : /** 214 : * Rather than implement every ostream/ios/ios_base function, we'll 215 : * be lazy and make esoteric uses go through a \p get() function. 216 : */ 217 0 : streamT * get() 218 : { 219 0 : return _target; 220 : } 221 : 222 : /** 223 : * Rather than implement every ostream/ios/ios_base function, we'll 224 : * be lazy and make esoteric uses go through a \p get() function. 225 : */ 226 : const streamT * get() const 227 : { 228 : return _target; 229 : } 230 : 231 : /** 232 : * Returns the position of the character in the current stream. 233 : */ 234 : std::streampos tellp() { return _target->tellp(); } 235 : 236 : private: 237 : /** 238 : * The pointer to the "real" ostream we send everything to. 239 : */ 240 : streamT * _target; 241 : }; 242 : 243 : typedef BasicOStreamProxy<> OStreamProxy; 244 : 245 : } // namespace libMesh 246 : 247 : #endif // LIBMESH_OSTREAM_PROXY_H