RAUL  0.8.0
EventRingBuffer.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_EVENT_RING_BUFFER_HPP
19 #define RAUL_EVENT_RING_BUFFER_HPP
20 
21 #include <cassert>
22 #include <algorithm>
23 #include "raul/RingBuffer.hpp"
24 #include "raul/TimeStamp.hpp"
25 
26 namespace Raul {
27 
28 
36 public:
37 
40  explicit EventRingBuffer(size_t capacity)
41  : RingBuffer(capacity)
42  {}
43 
44  size_t capacity() const { return _size; }
45 
46  size_t write(TimeStamp time, size_t size, const uint8_t* buf);
47  bool read(TimeStamp* time, size_t* size, uint8_t* buf);
48 };
49 
50 
51 inline bool
52 EventRingBuffer::read(TimeStamp* time, size_t* size, uint8_t* buf)
53 {
54  bool success = RingBuffer::full_read(sizeof(TimeStamp), (uint8_t*)time);
55  if (success)
56  success = RingBuffer::full_read(sizeof(size_t), (uint8_t*)size);
57  if (success)
58  success = RingBuffer::full_read(*size, buf);
59 
60  return success;
61 }
62 
63 
64 inline size_t
65 EventRingBuffer::write(TimeStamp time, size_t size, const uint8_t* buf)
66 {
67  assert(size > 0);
68 
69  if (write_space() < (sizeof(TimeStamp) + sizeof(size_t) + size)) {
70  return 0;
71  } else {
72  RingBuffer::write(sizeof(TimeStamp), (uint8_t*)&time);
73  RingBuffer::write(sizeof(size_t), (uint8_t*)&size);
74  RingBuffer::write(size, buf);
75  return size;
76  }
77 }
78 
79 
80 } // namespace Raul
81 
82 #endif // RAUL_EVENT_RING_BUFFER_HPP
83 
A RingBuffer of events (generic time-stamped binary "blobs").
Definition: EventRingBuffer.hpp:35
EventRingBuffer(size_t capacity)
Definition: EventRingBuffer.hpp:40
A lock-free RingBuffer.
Definition: RingBuffer.hpp:40
const uint32_t _size
Size (capacity) in bytes.
Definition: RingBuffer.hpp:106