yat  0.13.2pre
StreamRedirect.h
1 #ifndef _theplu_yat_utility_stream_readirect_
2 #define _theplu_yat_utility_stream_readirect_
3 
4 // $Id: StreamRedirect.h 2061 2009-09-11 17:54:01Z peter $
5 
6 /*
7  Copyright (C) 2009 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include <fstream>
26 #include <istream>
27 #include <ostream>
28 #include <streambuf>
29 #include <string>
30 
31 namespace theplu {
32 namespace yat {
33 namespace utility {
34 
53  template<class charT, class traits = std::char_traits<charT> >
55  {
56  public:
66  BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
67  std::basic_istream<charT, traits>& is2,
68  bool active=true);
69 
78  BasicStreamRedirect(std::basic_istream<charT, traits>& is,
79  const std::string& file, bool active=true);
80 
90  BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
91  std::basic_ostream<charT, traits>& os2,
92  bool active=true);
93 
102  BasicStreamRedirect(std::basic_ostream<charT, traits>& os,
103  const std::string& file, bool active=true);
104 
120  ~BasicStreamRedirect(void);
121 
122  private:
123  // no copying
125  BasicStreamRedirect& operator=(const BasicStreamRedirect&);
126 
127  void init(std::basic_ios<charT, traits>& ios1,
128  std::basic_ios<charT, traits>& ios2,
129  bool active);
130 
131  std::basic_streambuf<charT, traits>* buf_;
132  std::basic_ifstream<charT, traits>* ifs_;
133  std::basic_ios<charT, traits>* ios_;
134  std::basic_ofstream<charT, traits>* ofs_;
135  };
136 
141 
146 
147 
148  // template implementations
149 
150  template<class charT, class traits>
152  BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
153  std::basic_istream<charT, traits>& is2, bool active)
154  : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
155  {
156  init(is1, is2, active);
157  }
158 
159 
160  template<class charT, class traits>
162  BasicStreamRedirect(std::basic_istream<charT, traits>& is,
163  const std::string& file, bool active)
164  : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
165  {
166  if (active) {
167  ifs_ = new std::basic_ifstream<charT, traits>(file.c_str());
168  init(is, *ofs_, active);
169  }
170  }
171 
172 
173  template<class charT, class traits>
175  BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
176  std::basic_ostream<charT, traits>& os2, bool active)
177  : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
178  {
179  init(os1, os2, active);
180  }
181 
182 
183  template<class charT, class traits>
185  BasicStreamRedirect(std::basic_ostream<charT, traits>& os,
186  const std::string& file, bool active)
187  : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
188  {
189  if (active) {
190  ofs_ = new std::basic_ofstream<charT, traits>(file.c_str());
191  init(os, *ofs_, active);
192  }
193  }
194 
195 
196  template<class charT, class traits>
199  {
200  // only restore stream if active is true
201  if (ios_) {
202  ios_->rdbuf(buf_);
203  }
204  delete ifs_;
205  delete ofs_;
206  }
207 
208 
209  template<class charT, class traits>
210  void
211  BasicStreamRedirect<charT, traits>::init(std::basic_ios<charT, traits>& s1,
212  std::basic_ios<charT, traits>& s2,
213  bool active)
214  {
215  if (active) {
216  ios_ = &s1;
217  // save the buffer
218  buf_ = s1.rdbuf();
219  // redirect os1 to os2
220  s1.rdbuf(s2.rdbuf());
221  }
222  }
223 
224 }}} // of namespace utility, yat, and theplu
225 
226 #endif
bool is(const std::string &s)
check if string is convertible to (numerical) type T
Definition: utility.h:464
BasicStreamRedirect(std::basic_istream< charT, traits > &is1, std::basic_istream< charT, traits > &is2, bool active=true)
redirect istream to another istream
Definition: StreamRedirect.h:152
BasicStreamRedirect< char > StreamRedirect
Definition: StreamRedirect.h:140
~BasicStreamRedirect(void)
Destructor - resets the redirect.
Definition: StreamRedirect.h:198
BasicStreamRedirect< wchar_t > wStreamRedirect
Definition: StreamRedirect.h:145
Redirect a stream to another stream.
Definition: StreamRedirect.h:54

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5