FIFOBufferStream.h 3.8 KB

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