PipeStream.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // PipeStream.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/PipeStream.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: PipeStream
  9. //
  10. // Definition of the PipeStream class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_PipeStream_INCLUDED
  18. #define Foundation_PipeStream_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Pipe.h"
  21. #include "Poco/BufferedStreamBuf.h"
  22. #include <istream>
  23. #include <ostream>
  24. namespace Poco {
  25. class Foundation_API PipeStreamBuf: public BufferedStreamBuf
  26. /// This is the streambuf class used for reading from and writing to a Pipe.
  27. {
  28. public:
  29. typedef BufferedStreamBuf::openmode openmode;
  30. PipeStreamBuf(const Pipe& pipe, openmode mode);
  31. /// Creates a PipeStreamBuf with the given Pipe.
  32. ~PipeStreamBuf();
  33. /// Destroys the PipeStreamBuf.
  34. void close();
  35. /// Closes the pipe.
  36. protected:
  37. int readFromDevice(char* buffer, std::streamsize length);
  38. int writeToDevice(const char* buffer, std::streamsize length);
  39. private:
  40. enum
  41. {
  42. STREAM_BUFFER_SIZE = 1024
  43. };
  44. Pipe _pipe;
  45. };
  46. class Foundation_API PipeIOS: public virtual std::ios
  47. /// The base class for PipeInputStream and
  48. /// PipeOutputStream.
  49. ///
  50. /// This class is needed to ensure the correct initialization
  51. /// order of the stream buffer and base classes.
  52. {
  53. public:
  54. PipeIOS(const Pipe& pipe, openmode mode);
  55. /// Creates the PipeIOS with the given Pipe.
  56. ~PipeIOS();
  57. /// Destroys the PipeIOS.
  58. ///
  59. /// Flushes the buffer, but does not close the pipe.
  60. PipeStreamBuf* rdbuf();
  61. /// Returns a pointer to the internal PipeStreamBuf.
  62. void close();
  63. /// Flushes the stream and closes the pipe.
  64. protected:
  65. PipeStreamBuf _buf;
  66. };
  67. class Foundation_API PipeOutputStream: public PipeIOS, public std::ostream
  68. /// An output stream for writing to a Pipe.
  69. {
  70. public:
  71. PipeOutputStream(const Pipe& pipe);
  72. /// Creates the PipeOutputStream with the given Pipe.
  73. ~PipeOutputStream();
  74. /// Destroys the PipeOutputStream.
  75. ///
  76. /// Flushes the buffer, but does not close the pipe.
  77. };
  78. class Foundation_API PipeInputStream: public PipeIOS, public std::istream
  79. /// An input stream for reading from a Pipe.
  80. ///
  81. /// Using formatted input from a PipeInputStream
  82. /// is not recommended, due to the read-ahead behavior of
  83. /// istream with formatted reads.
  84. {
  85. public:
  86. PipeInputStream(const Pipe& pipe);
  87. /// Creates the PipeInputStream with the given Pipe.
  88. ~PipeInputStream();
  89. /// Destroys the PipeInputStream.
  90. };
  91. } // namespace Poco
  92. #endif // Foundation_PipeStream_INCLUDED