yat  0.8.3pre
StreamRedirect.h
00001 #ifndef _theplu_yat_utility_stream_readirect_ 
00002 #define _theplu_yat_utility_stream_readirect_ 
00003 
00004 // $Id: StreamRedirect.h 2061 2009-09-11 17:54:01Z peter $
00005 
00006 /*
00007   Copyright (C) 2009 Peter Johansson
00008 
00009   This file is part of the yat library, http://dev.thep.lu.se/yat
00010 
00011   The yat library is free software; you can redistribute it and/or
00012   modify it under the terms of the GNU General Public License as
00013   published by the Free Software Foundation; either version 3 of the
00014   License, or (at your option) any later version.
00015 
00016   The yat library is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019   General Public License for more details.
00020 
00021   You should have received a copy of the GNU General Public License
00022   along with yat. If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #include <fstream>
00026 #include <istream>
00027 #include <ostream>
00028 #include <streambuf>
00029 #include <string>
00030 
00031 namespace theplu {
00032 namespace yat {
00033 namespace utility {
00034 
00053   template<class charT, class traits = std::char_traits<charT> >
00054   class BasicStreamRedirect
00055   {
00056   public:
00066     BasicStreamRedirect(std::basic_istream<charT, traits>& is1, 
00067                         std::basic_istream<charT, traits>& is2,
00068                         bool active=true);
00069     
00078     BasicStreamRedirect(std::basic_istream<charT, traits>& is, 
00079                         const std::string& file, bool active=true);
00080     
00090     BasicStreamRedirect(std::basic_ostream<charT, traits>& os1, 
00091                         std::basic_ostream<charT, traits>& os2,
00092                         bool active=true);
00093     
00102     BasicStreamRedirect(std::basic_ostream<charT, traits>& os, 
00103                         const std::string& file, bool active=true);
00104     
00120     ~BasicStreamRedirect(void);
00121     
00122   private:
00123     // no copying
00124     BasicStreamRedirect(const BasicStreamRedirect&);
00125     BasicStreamRedirect& operator=(const BasicStreamRedirect&);
00126     
00127     void init(std::basic_ios<charT, traits>& ios1, 
00128               std::basic_ios<charT, traits>& ios2,
00129               bool active);
00130     
00131     std::basic_streambuf<charT, traits>* buf_;
00132     std::basic_ifstream<charT, traits>* ifs_;
00133     std::basic_ios<charT, traits>* ios_;
00134     std::basic_ofstream<charT, traits>* ofs_;
00135   };
00136   
00140   typedef BasicStreamRedirect<char> StreamRedirect;
00141   
00145   typedef BasicStreamRedirect<wchar_t> wStreamRedirect;
00146   
00147   
00148   // template implementations
00149   
00150   template<class charT, class traits>
00151   BasicStreamRedirect<charT, traits>::
00152 	BasicStreamRedirect(std::basic_istream<charT, traits>& is1, 
00153                       std::basic_istream<charT, traits>& is2, bool active)
00154     : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00155   {
00156     init(is1, is2, active);
00157   }
00158   
00159   
00160   template<class charT, class traits>
00161   BasicStreamRedirect<charT, traits>::
00162 	BasicStreamRedirect(std::basic_istream<charT, traits>& is, 
00163                       const std::string& file, bool active)
00164     : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00165   {
00166     if (active) {
00167       ifs_ = new std::basic_ifstream<charT, traits>(file.c_str());
00168       init(is, *ofs_, active);
00169     }
00170   }
00171   
00172   
00173   template<class charT, class traits>
00174   BasicStreamRedirect<charT, traits>::
00175 	BasicStreamRedirect(std::basic_ostream<charT, traits>& os1, 
00176                       std::basic_ostream<charT, traits>& os2, bool active)
00177     : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00178   {
00179     init(os1, os2, active);
00180   }
00181   
00182   
00183   template<class charT, class traits>
00184   BasicStreamRedirect<charT, traits>::
00185 	BasicStreamRedirect(std::basic_ostream<charT, traits>& os, 
00186                       const std::string& file, bool active)
00187     : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00188   {
00189     if (active) {
00190       ofs_ = new std::basic_ofstream<charT, traits>(file.c_str());
00191       init(os, *ofs_, active);
00192     }
00193   }
00194   
00195   
00196   template<class charT, class traits>
00197   BasicStreamRedirect<charT, traits>::
00198 	~BasicStreamRedirect(void)
00199   {
00200     // only restore stream if active is true
00201     if (ios_) {
00202       ios_->rdbuf(buf_);
00203     }
00204     delete ifs_;
00205     delete ofs_;
00206   }
00207   
00208   
00209   template<class charT, class traits>
00210   void 
00211   BasicStreamRedirect<charT, traits>::init(std::basic_ios<charT, traits>& s1, 
00212                                            std::basic_ios<charT, traits>& s2,
00213                                            bool active)
00214   {
00215     if (active) {
00216       ios_ = &s1;
00217       // save the buffer 
00218       buf_ = s1.rdbuf();
00219       // redirect os1 to os2
00220       s1.rdbuf(s2.rdbuf());
00221     }
00222   }
00223   
00224 }}} // of namespace utility, yat, and theplu
00225 
00226 #endif

Generated on Thu Dec 20 2012 03:12:58 for yat by  doxygen 1.8.0-20120409