FIFOBuffer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. //
  2. // FIFOBuffer.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/FIFOBuffer.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: FIFOBuffer
  9. //
  10. // Definition of the FIFOBuffer class.
  11. //
  12. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_FIFOBuffer_INCLUDED
  18. #define Foundation_FIFOBuffer_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #include "Poco/Buffer.h"
  22. #include "Poco/BasicEvent.h"
  23. #include "Poco/Mutex.h"
  24. #include "Poco/Format.h"
  25. namespace Poco {
  26. template <class T>
  27. class BasicFIFOBuffer
  28. /// A simple buffer class with support for re-entrant,
  29. /// FIFO-style read/write operations, as well as (optional)
  30. /// empty/non-empty/full (i.e. writable/readable) transition
  31. /// notifications. Buffer can be flagged with end-of-file and
  32. /// error flags, which renders it un-readable/writable.
  33. ///
  34. /// Critical portions of code are protected by a recursive mutex.
  35. /// However, to achieve thread-safety in cases where multiple
  36. /// member function calls are involved and have to be atomic,
  37. /// the mutex must be locked externally.
  38. ///
  39. /// Buffer size, as well as amount of unread data and
  40. /// available space introspections are supported as well.
  41. ///
  42. /// This class is useful anywhere where a FIFO functionality
  43. /// is needed.
  44. {
  45. public:
  46. typedef T Type;
  47. mutable Poco::BasicEvent<bool> writable;
  48. /// Event indicating "writability" of the buffer,
  49. /// triggered as follows:
  50. ///
  51. /// * when buffer transitions from non-full to full,
  52. /// Writable event observers are notified, with
  53. /// false value as the argument
  54. ///
  55. /// * when buffer transitions from full to non-full,
  56. /// Writable event observers are notified, with
  57. /// true value as the argument
  58. mutable Poco::BasicEvent<bool> readable;
  59. /// Event indicating "readability" of the buffer,
  60. /// triggered as follows:
  61. ///
  62. /// * when buffer transitions from non-empty to empty,
  63. /// Readable event observers are notified, with false
  64. /// value as the argument
  65. ///
  66. /// * when FIFOBuffer transitions from empty to non-empty,
  67. /// Readable event observers are notified, with true value
  68. /// as the argument
  69. BasicFIFOBuffer(std::size_t size, bool notify = false):
  70. _buffer(size),
  71. _begin(0),
  72. _used(0),
  73. _notify(notify),
  74. _eof(false),
  75. _error(false)
  76. /// Creates the FIFOBuffer.
  77. {
  78. }
  79. BasicFIFOBuffer(T* pBuffer, std::size_t size, bool notify = false):
  80. _buffer(pBuffer, size),
  81. _begin(0),
  82. _used(0),
  83. _notify(notify),
  84. _eof(false),
  85. _error(false)
  86. /// Creates the FIFOBuffer.
  87. {
  88. }
  89. BasicFIFOBuffer(const T* pBuffer, std::size_t size, bool notify = false):
  90. _buffer(pBuffer, size),
  91. _begin(0),
  92. _used(size),
  93. _notify(notify),
  94. _eof(false),
  95. _error(false)
  96. /// Creates the FIFOBuffer.
  97. {
  98. }
  99. ~BasicFIFOBuffer()
  100. /// Destroys the FIFOBuffer.
  101. {
  102. }
  103. void resize(std::size_t newSize, bool preserveContent = true)
  104. /// Resizes the buffer. If preserveContent is true,
  105. /// the content of the old buffer is preserved.
  106. /// New size can be larger or smaller than
  107. /// the current size, but it must not be 0.
  108. /// Additionally, if the new length is smaller
  109. /// than currently used length and preserveContent
  110. /// is true, InvalidAccessException is thrown.
  111. {
  112. Mutex::ScopedLock lock(_mutex);
  113. if (preserveContent && (newSize < _used))
  114. throw InvalidAccessException("Can not resize FIFO without data loss.");
  115. std::size_t usedBefore = _used;
  116. _buffer.resize(newSize, preserveContent);
  117. if (!preserveContent) _used = 0;
  118. if (_notify) notify(usedBefore);
  119. }
  120. std::size_t peek(T* pBuffer, std::size_t length) const
  121. /// Peeks into the data currently in the FIFO
  122. /// without actually extracting it.
  123. /// If length is zero, the return is immediate.
  124. /// If length is greater than used length,
  125. /// it is substituted with the the current FIFO
  126. /// used length.
  127. ///
  128. /// Returns the number of elements copied in the
  129. /// supplied buffer.
  130. {
  131. if (0 == length) return 0;
  132. Mutex::ScopedLock lock(_mutex);
  133. if (!isReadable()) return 0;
  134. if (length > _used) length = _used;
  135. std::memcpy(pBuffer, _buffer.begin() + _begin, length * sizeof(T));
  136. return length;
  137. }
  138. std::size_t peek(Poco::Buffer<T>& buffer, std::size_t length = 0) const
  139. /// Peeks into the data currently in the FIFO
  140. /// without actually extracting it.
  141. /// Resizes the supplied buffer to the size of
  142. /// data written to it. If length is not
  143. /// supplied by the caller or is greater than length
  144. /// of currently used data, the current FIFO used
  145. /// data length is substituted for it.
  146. ///
  147. /// Returns the number of elements copied in the
  148. /// supplied buffer.
  149. {
  150. Mutex::ScopedLock lock(_mutex);
  151. if (!isReadable()) return 0;
  152. if (0 == length || length > _used) length = _used;
  153. buffer.resize(length);
  154. return peek(buffer.begin(), length);
  155. }
  156. std::size_t read(T* pBuffer, std::size_t length)
  157. /// Copies the data currently in the FIFO
  158. /// into the supplied buffer.
  159. /// Resizes the supplied buffer to the size of
  160. /// data written to it.
  161. ///
  162. /// Returns the reference to the buffer.
  163. {
  164. if (0 == length) return 0;
  165. Mutex::ScopedLock lock(_mutex);
  166. if (!isReadable()) return 0;
  167. std::size_t usedBefore = _used;
  168. std::size_t readLen = peek(pBuffer, length);
  169. poco_assert (_used >= readLen);
  170. _used -= readLen;
  171. if (0 == _used) _begin = 0;
  172. else _begin += length;
  173. if (_notify) notify(usedBefore);
  174. return readLen;
  175. }
  176. std::size_t read(Poco::Buffer<T>& buffer, std::size_t length = 0)
  177. /// Copies the data currently in the FIFO
  178. /// into the supplied buffer.
  179. /// Resizes the supplied buffer to the size of
  180. /// data written to it.
  181. ///
  182. /// Returns the reference to the buffer.
  183. {
  184. Mutex::ScopedLock lock(_mutex);
  185. if (!isReadable()) return 0;
  186. std::size_t usedBefore = _used;
  187. std::size_t readLen = peek(buffer, length);
  188. poco_assert (_used >= readLen);
  189. _used -= readLen;
  190. if (0 == _used) _begin = 0;
  191. else _begin += length;
  192. if (_notify) notify(usedBefore);
  193. return readLen;
  194. }
  195. std::size_t write(const T* pBuffer, std::size_t length)
  196. /// Writes data from supplied buffer to the FIFO buffer.
  197. /// If there is no sufficient space for the whole
  198. /// buffer to be written, data up to available
  199. /// length is written.
  200. /// The length of data to be written is determined from the
  201. /// length argument. Function does nothing and returns zero
  202. /// if length argument is equal to zero.
  203. ///
  204. /// Returns the length of data written.
  205. {
  206. if (0 == length) return 0;
  207. Mutex::ScopedLock lock(_mutex);
  208. if (!isWritable()) return 0;
  209. if (_buffer.size() - (_begin + _used) < length)
  210. {
  211. std::memmove(_buffer.begin(), _buffer.begin() + _begin, _used * sizeof(T));
  212. _begin = 0;
  213. }
  214. std::size_t usedBefore = _used;
  215. std::size_t available = _buffer.size() - _used - _begin;
  216. std::size_t len = length > available ? available : length;
  217. std::memcpy(_buffer.begin() + _begin + _used, pBuffer, len * sizeof(T));
  218. _used += len;
  219. poco_assert (_used <= _buffer.size());
  220. if (_notify) notify(usedBefore);
  221. return len;
  222. }
  223. std::size_t write(const Buffer<T>& buffer, std::size_t length = 0)
  224. /// Writes data from supplied buffer to the FIFO buffer.
  225. /// If there is no sufficient space for the whole
  226. /// buffer to be written, data up to available
  227. /// length is written.
  228. /// The length of data to be written is determined from the
  229. /// length argument or buffer size (when length argument is
  230. /// default zero or greater than buffer size).
  231. ///
  232. /// Returns the length of data written.
  233. {
  234. if (length == 0 || length > buffer.size())
  235. length = buffer.size();
  236. return write(buffer.begin(), length);
  237. }
  238. std::size_t size() const
  239. /// Returns the size of the buffer.
  240. {
  241. return _buffer.size();
  242. }
  243. std::size_t used() const
  244. /// Returns the size of the used portion of the buffer.
  245. {
  246. return _used;
  247. }
  248. std::size_t available() const
  249. /// Returns the size of the available portion of the buffer.
  250. {
  251. return _buffer.size() - _used;
  252. }
  253. void drain(std::size_t length = 0)
  254. /// Drains length number of elements from the buffer.
  255. /// If length is zero or greater than buffer current
  256. /// content length, buffer is emptied.
  257. {
  258. Mutex::ScopedLock lock(_mutex);
  259. std::size_t usedBefore = _used;
  260. if (0 == length || length >= _used)
  261. {
  262. _begin = 0;
  263. _used = 0;
  264. }
  265. else
  266. {
  267. _begin += length;
  268. _used -= length;
  269. }
  270. if (_notify) notify(usedBefore);
  271. }
  272. void copy(const T* ptr, std::size_t length)
  273. /// Copies the supplied data to the buffer and adjusts
  274. /// the used buffer size.
  275. {
  276. poco_check_ptr(ptr);
  277. if (0 == length) return;
  278. Mutex::ScopedLock lock(_mutex);
  279. if (length > available())
  280. throw Poco::InvalidAccessException("Cannot extend buffer.");
  281. if (!isWritable())
  282. throw Poco::InvalidAccessException("Buffer not writable.");
  283. std::memcpy(&_buffer[_used], ptr, length * sizeof(T));
  284. std::size_t usedBefore = _used;
  285. _used += length;
  286. if (_notify) notify(usedBefore);
  287. }
  288. void advance(std::size_t length)
  289. /// Advances buffer by length elements.
  290. /// Should be called AFTER the data
  291. /// was copied into the buffer.
  292. {
  293. Mutex::ScopedLock lock(_mutex);
  294. if (length > available())
  295. throw Poco::InvalidAccessException("Cannot extend buffer.");
  296. if (!isWritable())
  297. throw Poco::InvalidAccessException("Buffer not writable.");
  298. std::size_t usedBefore = _used;
  299. _used += length;
  300. if (_notify) notify(usedBefore);
  301. }
  302. T* begin()
  303. /// Returns the pointer to the beginning of the buffer.
  304. {
  305. return _buffer.begin();
  306. }
  307. T* next()
  308. /// Returns the pointer to the next available position in the buffer.
  309. {
  310. if (available() == 0)
  311. throw InvalidAccessException("Buffer is full.");
  312. return _buffer.begin() + _used;
  313. }
  314. T& operator [] (std::size_t index)
  315. /// Returns value at index position.
  316. /// Throws InvalidAccessException if index is larger than
  317. /// the last valid (used) buffer position.
  318. {
  319. Mutex::ScopedLock lock(_mutex);
  320. if (index >= _used)
  321. throw InvalidAccessException(format("Index out of bounds: %z (max index allowed: %z)", index, _used - 1));
  322. return _buffer[_begin + index];
  323. }
  324. const T& operator [] (std::size_t index) const
  325. /// Returns value at index position.
  326. /// Throws InvalidAccessException if index is larger than
  327. /// the last valid (used) buffer position.
  328. {
  329. Mutex::ScopedLock lock(_mutex);
  330. if (index >= _used)
  331. throw InvalidAccessException(format("Index out of bounds: %z (max index allowed: %z)", index, _used - 1));
  332. return _buffer[_begin + index];
  333. }
  334. const Buffer<T>& buffer() const
  335. /// Returns const reference to the underlying buffer.
  336. {
  337. return _buffer;
  338. }
  339. void setError(bool error = true)
  340. /// Sets the error flag on the buffer and empties it.
  341. /// If notifications are enabled, they will be triggered
  342. /// if appropriate.
  343. ///
  344. /// Setting error flag to true prevents reading and writing
  345. /// to the buffer; to re-enable FIFOBuffer for reading/writing,
  346. /// the error flag must be set to false.
  347. {
  348. if (error)
  349. {
  350. bool f = false;
  351. Mutex::ScopedLock lock(_mutex);
  352. if (error && isReadable() && _notify) readable.notify(this, f);
  353. if (error && isWritable() && _notify) writable.notify(this, f);
  354. _error = error;
  355. _used = 0;
  356. }
  357. else
  358. {
  359. bool t = true;
  360. Mutex::ScopedLock lock(_mutex);
  361. _error = false;
  362. if (_notify && !_eof) writable.notify(this, t);
  363. }
  364. }
  365. bool isValid() const
  366. /// Returns true if error flag is not set on the buffer,
  367. /// otherwise returns false.
  368. {
  369. return !_error;
  370. }
  371. void setEOF(bool eof = true)
  372. /// Sets end-of-file flag on the buffer.
  373. ///
  374. /// Setting EOF flag to true prevents writing to the
  375. /// buffer; reading from the buffer will still be
  376. /// allowed until all data present in the buffer at the
  377. /// EOF set time is drained. After that, to re-enable
  378. /// FIFOBuffer for reading/writing, EOF must be
  379. /// set to false.
  380. ///
  381. /// Setting EOF flag to false clears EOF state if it
  382. /// was previously set. If EOF was not set, it has no
  383. /// effect.
  384. {
  385. Mutex::ScopedLock lock(_mutex);
  386. bool flag = !eof;
  387. if (_notify) writable.notify(this, flag);
  388. _eof = eof;
  389. }
  390. bool hasEOF() const
  391. /// Returns true if EOF flag has been set.
  392. {
  393. return _eof;
  394. }
  395. bool isEOF() const
  396. /// Returns true if EOF flag has been set and buffer is empty.
  397. {
  398. return isEmpty() && _eof;
  399. }
  400. bool isEmpty() const
  401. /// Returns true is buffer is empty, false otherwise.
  402. {
  403. return 0 == _used;
  404. }
  405. bool isFull() const
  406. /// Returns true is buffer is full, false otherwise.
  407. {
  408. return size() == _used;
  409. }
  410. bool isReadable() const
  411. /// Returns true if buffer contains data and is not
  412. /// in error state.
  413. {
  414. return !isEmpty() && isValid();
  415. }
  416. bool isWritable() const
  417. /// Returns true if buffer is not full and is not
  418. /// in error state.
  419. {
  420. return !isFull() && isValid() && !_eof;
  421. }
  422. void setNotify(bool notify = true)
  423. /// Enables/disables notifications.
  424. {
  425. _notify = notify;
  426. }
  427. bool getNotify() const
  428. /// Returns true if notifications are enabled, false otherwise.
  429. {
  430. return _notify;
  431. }
  432. Mutex& mutex()
  433. /// Returns reference to mutex.
  434. {
  435. return _mutex;
  436. }
  437. private:
  438. void notify(std::size_t usedBefore)
  439. {
  440. bool t = true, f = false;
  441. if (usedBefore == 0 && _used > 0)
  442. readable.notify(this, t);
  443. else if (usedBefore > 0 && 0 == _used)
  444. readable.notify(this, f);
  445. if (usedBefore == _buffer.size() && _used < _buffer.size())
  446. writable.notify(this, t);
  447. else if (usedBefore < _buffer.size() && _used == _buffer.size())
  448. writable.notify(this, f);
  449. }
  450. BasicFIFOBuffer();
  451. BasicFIFOBuffer(const BasicFIFOBuffer&);
  452. BasicFIFOBuffer& operator = (const BasicFIFOBuffer&);
  453. Buffer<T> _buffer;
  454. std::size_t _begin;
  455. std::size_t _used;
  456. bool _notify;
  457. mutable Mutex _mutex;
  458. bool _eof;
  459. bool _error;
  460. };
  461. //
  462. // We provide an instantiation for char
  463. //
  464. typedef BasicFIFOBuffer<char> FIFOBuffer;
  465. } // namespace Poco
  466. #endif // Foundation_FIFOBuffer_INCLUDED