Changeset 2051


Ignore:
Timestamp:
Sep 6, 2009, 6:47:19 PM (14 years ago)
Author:
Peter
Message:

the conclusion that the stream needed to flushed was nonsense, so I changed back to having and ios* as member variable. refs #521

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/StreamRedirect.h

    r2050 r2051  
    8181       Constructor for ostream
    8282
    83        \a os1 is redirected to send its output to the buffer of \a os2
    84        (rather than its own buffer). A reference to \a os1 and its
    85        buffer is stored, so \a os1 can be restored in the destructor.
    86 
    8783       If \a active is false, the class does nothing.
    8884    */
     
    112108   
    113109    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);
    115112   
    116113    std::basic_streambuf<charT, traits>* buf_;
    117114    std::basic_ifstream<charT, traits>* ifs_;
    118     std::basic_istream<charT, traits>* is_;
     115    std::basic_ios<charT, traits>* ios_;
    119116    std::basic_ofstream<charT, traits>* ofs_;
    120     std::basic_ostream<charT, traits>* os_;
    121117  };
    122118 
     
    138134  BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
    139135                      std::basic_istream<charT, traits>& is2, bool active)
    140     : buf_(NULL), ifs_(NULL), is_(NULL), ofs_(NULL), os_(NULL)
     136    : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
    141137  {
    142     if (active) {
    143       is_ = &is1;
    144       init(is1, is2);
    145     }
     138    init(is1, is2, active);
    146139  }
    147140 
     
    151144  BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
    152145                      std::basic_ostream<charT, traits>& os2, bool active)
    153     : buf_(NULL), ifs_(NULL), is_(NULL), ofs_(NULL), os_(NULL)
     146    : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
    154147  {
    155     if (active) {
    156       os_ = &os1;
    157       init(os1, os2);
    158     }
     148    init(os1, os2, active);
    159149  }
    160150 
     
    164154  BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
    165155                      const std::string& file, bool active)
    166     : buf_(NULL), ifs_(NULL), is_(NULL), ofs_(NULL), os_(NULL)
     156    : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
    167157  {
    168158    if (active) {
    169159      ofs_ = new std::basic_ofstream<charT, traits>(file.c_str());
    170       os_ = &os1;
    171       init(os1, *ofs_);
     160      init(os1, *ofs_, active);
    172161    }
    173162  }
     
    178167  ~BasicStreamRedirect(void)
    179168  {
    180     YAT_ASSERT(os_==NULL || is_==NULL);
    181 
    182169    // 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_);
    189172    }
    190173    delete ifs_;
     
    196179  void
    197180  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)
    199183  {
    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    }
    204191  }
    205192 
Note: See TracChangeset for help on using the changeset viewer.