Changeset 2051
- Timestamp:
- Sep 6, 2009, 6:47:19 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/StreamRedirect.h
r2050 r2051 81 81 Constructor for ostream 82 82 83 \a os1 is redirected to send its output to the buffer of \a os284 (rather than its own buffer). A reference to \a os1 and its85 buffer is stored, so \a os1 can be restored in the destructor.86 87 83 If \a active is false, the class does nothing. 88 84 */ … … 112 108 113 109 void init(std::basic_ios<charT, traits>& ios1, 114 std::basic_ios<charT, traits>& ios2); 110 std::basic_ios<charT, traits>& ios2, 111 bool active); 115 112 116 113 std::basic_streambuf<charT, traits>* buf_; 117 114 std::basic_ifstream<charT, traits>* ifs_; 118 std::basic_i stream<charT, traits>* is_;115 std::basic_ios<charT, traits>* ios_; 119 116 std::basic_ofstream<charT, traits>* ofs_; 120 std::basic_ostream<charT, traits>* os_;121 117 }; 122 118 … … 138 134 BasicStreamRedirect(std::basic_istream<charT, traits>& is1, 139 135 std::basic_istream<charT, traits>& is2, bool active) 140 : buf_(NULL), ifs_(NULL), i s_(NULL), ofs_(NULL), os_(NULL)136 : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL) 141 137 { 142 if (active) { 143 is_ = &is1; 144 init(is1, is2); 145 } 138 init(is1, is2, active); 146 139 } 147 140 … … 151 144 BasicStreamRedirect(std::basic_ostream<charT, traits>& os1, 152 145 std::basic_ostream<charT, traits>& os2, bool active) 153 : buf_(NULL), ifs_(NULL), i s_(NULL), ofs_(NULL), os_(NULL)146 : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL) 154 147 { 155 if (active) { 156 os_ = &os1; 157 init(os1, os2); 158 } 148 init(os1, os2, active); 159 149 } 160 150 … … 164 154 BasicStreamRedirect(std::basic_ostream<charT, traits>& os1, 165 155 const std::string& file, bool active) 166 : buf_(NULL), ifs_(NULL), i s_(NULL), ofs_(NULL), os_(NULL)156 : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL) 167 157 { 168 158 if (active) { 169 159 ofs_ = new std::basic_ofstream<charT, traits>(file.c_str()); 170 os_ = &os1; 171 init(os1, *ofs_); 160 init(os1, *ofs_, active); 172 161 } 173 162 } … … 178 167 ~BasicStreamRedirect(void) 179 168 { 180 YAT_ASSERT(os_==NULL || is_==NULL);181 182 169 // only restore stream if active is true 183 if (os_) { 184 std::flush(*os_); 185 os_->rdbuf(buf_); 186 } 187 else if (is_) { 188 is_->rdbuf(buf_); 170 if (ios_) { 171 ios_->rdbuf(buf_); 189 172 } 190 173 delete ifs_; … … 196 179 void 197 180 BasicStreamRedirect<charT, traits>::init(std::basic_ios<charT, traits>& s1, 198 std::basic_ios<charT, traits>& s2) 181 std::basic_ios<charT, traits>& s2, 182 bool active) 199 183 { 200 // save the buffer 201 buf_ = s1.rdbuf(); 202 // redirect os1 to os2 203 s1.rdbuf(s2.rdbuf()); 184 if (active) { 185 ios_ = &s1; 186 // save the buffer 187 buf_ = s1.rdbuf(); 188 // redirect os1 to os2 189 s1.rdbuf(s2.rdbuf()); 190 } 204 191 } 205 192
Note: See TracChangeset
for help on using the changeset viewer.