Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRedirectStream.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JREDIRECTSTREAM__
2 #define __JLANG__JREDIRECTSTREAM__
3 
4 #include <istream>
5 #include <ostream>
6 
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JLANG {}
15 namespace JPP { using namespace JLANG; }
16 
17 namespace JLANG {
18 
19 
20  /**
21  * This class can be used to temporarily redirect one output (input) stream to another output (input) stream.
22  * The destructor restores the internal buffer of the first output (input) stream.
23  */
26  {
27  public:
28  /**
29  * Constructor.
30  * The output stream from is redirected to output stream to.
31  *
32  * \param from output stream
33  * \param to output stream
34  */
35  JRedirectStream(std::ostream& from,
36  std::ostream& to) :
37  ios(from)
38  {
39  p = ios.rdbuf();
40 
41  ios.rdbuf(to.rdbuf());
42  }
43 
44 
45  /**
46  * Constructor.
47  * The input stream from is redirected to input stream to.
48  *
49  * \param from input stream
50  * \param to input stream
51  */
52  JRedirectStream(std::istream& from,
53  std::istream& to) :
54  ios(from)
55  {
56  p = ios.rdbuf();
57 
58  ios.rdbuf(to.rdbuf());
59  }
60 
61 
62  /**
63  * Destructor.
64  * Restore internal buffer.
65  */
67  {
68  ios.rdbuf(p);
69  }
70 
71 
72  /**
73  * Get status of object.
74  *
75  * \return true
76  */
77  virtual bool getStatus() const
78  {
79  return true;
80  }
81 
82  private:
83  std::ios& ios;
84  std::streambuf* p;
85  };
86 }
87 
88 #endif
~JRedirectStream()
Destructor.
This class can be used to temporarily redirect one output (input) stream to another output (input) st...
virtual bool getStatus() const
Get status of object.
Interface for status of object.
JRedirectStream(std::istream &from, std::istream &to)
Constructor.
JRedirectStream(std::ostream &from, std::ostream &to)
Constructor.