Changeset 2048


Ignore:
Timestamp:
Sep 4, 2009, 4:47:18 PM (11 years ago)
Author:
Peter
Message:

adding bool parameter in constructors and fixed some docs typos

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/stream_redirect_test.cc

    r2047 r2048  
    3333void test1(test::Suite&);
    3434void test2(test::Suite&);
     35void test3(test::Suite&);
    3536void test_cout(test::Suite&);
    3637
     
    4142  test1(suite);
    4243  test2(suite);
     44  test3(suite);
    4345
    4446  // keep this last in case it screws up cout
     
    8991
    9092
     93void test3(test::Suite& suite)
     94{
     95  suite.out() << "testing active=false\n";
     96  std::ostringstream* os1 = new std::ostringstream;
     97  std::ostringstream* os2 = new std::ostringstream;
     98  StreamRedirect* sr = new StreamRedirect(*os1, *os2, false);
     99  delete os1;
     100  delete os2;
     101  delete sr;
     102}
     103
     104
    91105void test_cout(test::Suite& suite)
    92106{
  • trunk/yat/utility/StreamRedirect.h

    r2047 r2048  
    3939
    4040     \note It is important that an instance of this class is
    41      deallocated before the stream it holds (se StreamSwitcher~).
     41     deallocated before the stream it holds (see
     42     BasicStreamRedirect::~BasicStreamRedirect).
    4243
    43      \see StreamSwitcher and wStreamSwitcher
     44     \see StreamRedirect and wStreamRedirect
    4445
    4546     \since New in yat 0.6
     
    5556       (rather than its own buffer). A reference to \a is1 and its
    5657       buffer is stored, so \a is1 can be restored in the destructor.
     58
     59       If \a active is false, the class does nothing.
    5760     */
    5861    BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
    59                         std::basic_istream<charT, traits>& is2);
     62                        std::basic_istream<charT, traits>& is2,
     63                        bool active=true);
    6064   
    6165    /**
     
    6569       (rather than its own buffer). A reference to \a os1 and its
    6670       buffer is stored, so \a os1 can be restored in the destructor.
     71
     72       If \a active is false, the class does nothing.
    6773    */
    6874    BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
    69                         std::basic_ostream<charT, traits>& os2);
     75                        std::basic_ostream<charT, traits>& os2,
     76                        bool active=true);
    7077   
    7178    /**
    72        \brief Destructor - resets the switch
     79       \brief Destructor - resets the redirect
    7380
    7481       The stream (first argument in constructor) is restored to use
     
    9198   
    9299    void init(std::basic_ios<charT, traits>& ios1,
    93               std::basic_ios<charT, traits>& ios2);
     100              std::basic_ios<charT, traits>& ios2,
     101              bool active);
    94102   
    95103    std::basic_ios<charT, traits>* ios_;
     
    113121  BasicStreamRedirect<charT, traits>::
    114122  BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
    115                       std::basic_istream<charT, traits>& is2)
     123                      std::basic_istream<charT, traits>& is2, bool active)
    116124  {
    117     init(is1, is2);
     125    init(is1, is2, active);
    118126  }
    119127 
     
    122130  BasicStreamRedirect<charT, traits>::
    123131  BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
    124                       std::basic_ostream<charT, traits>& os2)
     132                      std::basic_ostream<charT, traits>& os2, bool active)
    125133  {
    126     init(os1, os2);
     134    init(os1, os2, active);
    127135  }
    128136 
     
    132140  ~BasicStreamRedirect(void)
    133141  {
    134     YAT_ASSERT(ios_);
    135     ios_->rdbuf(buf_);
     142    // only restore stream if active is true
     143    if (ios_) {
     144      ios_->rdbuf(buf_);
     145    }
    136146  }
    137147 
     
    140150  void
    141151  BasicStreamRedirect<charT, traits>::init(std::basic_ios<charT, traits>& s1,
    142                                            std::basic_ios<charT, traits>& s2)
     152                                           std::basic_ios<charT, traits>& s2,
     153                                           bool active)
    143154  {
    144     ios_ = &s1;
    145    
    146     // save the buffer
    147     buf_ = ios_->rdbuf();
    148    
    149     // redirect os1 to os2
    150     ios_->rdbuf(s2.rdbuf());
     155    if (active) {
     156      ios_ = &s1;
     157      // save the buffer
     158      buf_ = ios_->rdbuf();
     159      // redirect os1 to os2
     160      ios_->rdbuf(s2.rdbuf());
     161    }
     162    else {
     163      ios_ = NULL;
     164      buf_ = NULL;
     165    }
    151166  }
    152167 
Note: See TracChangeset for help on using the changeset viewer.