Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRedirectString.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JREDIRECTSTRING__
2 #define __JLANG__JREDIRECTSTRING__
3 
4 #include <istream>
5 #include <ostream>
6 #include <string>
7 #include <sstream>
8 
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JLANG {}
17 namespace JPP { using namespace JLANG; }
18 
19 namespace JLANG {
20 
21 
22  /**
23  * This class can be used to temporarily redirect an input stream to an input string.
24  * The destructor restores the internal buffer of the input stream.
25  */
28  {
29  public:
30  /**
31  * Constructor.
32  * The input stream is redirected to the given input string.
33  *
34  * \param is input stream
35  * \param buffer input string
36  */
37  JRedirectString(std::istream& is, const std::string& buffer) :
38  ios(is)
39  {
40  p = ios.rdbuf(new std::stringbuf(buffer));
41  }
42 
43 
44  /**
45  * Destructor.
46  * Restore internal buffer and delete temporary buffer.
47  */
49  {
50  delete ios.rdbuf(p);
51  }
52 
53 
54  /**
55  * Get status of object.
56  *
57  * \return true
58  */
59  virtual bool getStatus() const override
60  {
61  return true;
62  }
63 
64  private:
65  std::ios& ios;
66  std::streambuf* p;
67  };
68 }
69 
70 #endif
is
Definition: JDAQCHSM.chsm:167
JRedirectString(std::istream &is, const std::string &buffer)
Constructor.
~JRedirectString()
Destructor.
Interface for status of object.
This class can be used to temporarily redirect an input stream to an input string.
virtual bool getStatus() const override
Get status of object.