FIFOBufferStream.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // FIFOBufferStream.h
  3. //
  4. // Library: Foundation
  5. // Package: Streams
  6. // Module: FIFOBufferStream
  7. //
  8. // Definition of the FIFOBufferStream class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_FIFOBufferStream_INCLUDED
  16. #define Foundation_FIFOBufferStream_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/FIFOBuffer.h"
  19. #include "Poco/BufferedBidirectionalStreamBuf.h"
  20. #include <istream>
  21. #include <ostream>
  22. namespace Poco {
  23. class Foundation_API FIFOBufferStreamBuf: public BufferedBidirectionalStreamBuf
  24. /// This is the streambuf class used for reading from and writing to a FIFOBuffer.
  25. /// FIFOBuffer is enabled for emtpy/non-empty/full state transitions notifications.
  26. {
  27. public:
  28. FIFOBufferStreamBuf();
  29. /// Creates a FIFOBufferStreamBuf.
  30. explicit FIFOBufferStreamBuf(FIFOBuffer& fifoBuffer);
  31. /// Creates a FIFOBufferStreamBuf and assigns the given buffer to it.
  32. FIFOBufferStreamBuf(char* pBuffer, std::size_t length);
  33. /// Creates a FIFOBufferStreamBuf and assigns the given buffer to it.
  34. FIFOBufferStreamBuf(const char* pBuffer, std::size_t length);
  35. /// Creates a FIFOBufferStreamBuf and assigns the given buffer to it.
  36. explicit FIFOBufferStreamBuf(std::size_t length);
  37. /// Creates a FIFOBufferStreamBuf of the given length.
  38. ~FIFOBufferStreamBuf();
  39. /// Destroys the FIFOBufferStreamBuf.
  40. FIFOBuffer& fifoBuffer();
  41. /// Returns the underlying FIFO buffer reference.
  42. protected:
  43. int readFromDevice(char* buffer, std::streamsize length);
  44. int writeToDevice(const char* buffer, std::streamsize length);
  45. private:
  46. enum
  47. {
  48. STREAM_BUFFER_SIZE = 1024
  49. };
  50. FIFOBuffer* _pFIFOBuffer;
  51. FIFOBuffer& _fifoBuffer;
  52. };
  53. class Foundation_API FIFOIOS: public virtual std::ios
  54. /// The base class for FIFOBufferInputStream and
  55. /// FIFOBufferStream.
  56. ///
  57. /// This class is needed to ensure the correct initialization
  58. /// order of the stream buffer and base classes.
  59. {
  60. public:
  61. explicit FIFOIOS(FIFOBuffer& buffer);
  62. /// Creates a FIFOIOS and assigns the given buffer to it.
  63. FIFOIOS(char* pBuffer, std::size_t length);
  64. /// Creates a FIFOIOS and assigns the given buffer to it.
  65. FIFOIOS(const char* pBuffer, std::size_t length);
  66. /// Creates a FIFOIOS and assigns the given buffer to it.
  67. explicit FIFOIOS(std::size_t length);
  68. /// Creates a FIFOIOS of the given length.
  69. ~FIFOIOS();
  70. /// Destroys the FIFOIOS.
  71. ///
  72. /// Flushes the buffer.
  73. FIFOBufferStreamBuf* rdbuf();
  74. /// Returns a pointer to the internal FIFOBufferStreamBuf.
  75. void close();
  76. /// Flushes the stream.
  77. protected:
  78. FIFOBufferStreamBuf _buf;
  79. };
  80. class Foundation_API FIFOBufferStream: public FIFOIOS, public std::iostream
  81. /// An output stream for writing to a FIFO.
  82. {
  83. public:
  84. Poco::BasicEvent<bool>& readable;
  85. Poco::BasicEvent<bool>& writable;
  86. explicit FIFOBufferStream(FIFOBuffer& buffer);
  87. /// Creates the FIFOBufferStream with supplied buffer as initial value.
  88. FIFOBufferStream(char* pBuffer, std::size_t length);
  89. /// Creates a FIFOBufferStream and assigns the given buffer to it.
  90. FIFOBufferStream(const char* pBuffer, std::size_t length);
  91. /// Creates a FIFOBufferStream and assigns the given buffer to it.
  92. explicit FIFOBufferStream(std::size_t length);
  93. /// Creates a FIFOBufferStream of the given length.
  94. ~FIFOBufferStream();
  95. /// Destroys the FIFOBufferStream.
  96. ///
  97. /// Flushes the buffer.
  98. private:
  99. FIFOBufferStream();
  100. FIFOBufferStream(const FIFOBufferStream& other);
  101. FIFOBufferStream& operator =(const FIFOBufferStream& other);
  102. };
  103. ///
  104. /// inlines
  105. ///
  106. inline FIFOBuffer& FIFOBufferStreamBuf::fifoBuffer()
  107. {
  108. return _fifoBuffer;
  109. }
  110. } // namespace Poco
  111. #endif // Foundation_FIFOBufferStream_INCLUDED