Add missing namespace qualifiers.
[ardour.git] / libs / evoral / evoral / EventRingBuffer.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  *
4  * Evoral 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  * Evoral 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 EVORAL_EVENT_RING_BUFFER_HPP
19 #define EVORAL_EVENT_RING_BUFFER_HPP
20
21 #include <iostream>
22
23 #include "pbd/ringbufferNPT.h"
24
25 #include "evoral/visibility.h"
26 #include "evoral/EventSink.hpp"
27 #include "evoral/types.hpp"
28
29 using namespace std;
30
31 namespace Evoral {
32
33 /** A RingBuffer of events (generic time-stamped binary "blobs").
34  *
35  * This packs a timestamp, size, and size bytes of data flat into the buffer.
36  * Useful for MIDI events, OSC messages, etc.
37  *
38  * Note: the uint8_t template argument to RingBufferNPT<> indicates "byte
39  * oriented data", not anything particular linked to MIDI or any other
40  * possible interpretation of uint8_t.
41  */
42 template<typename Time>
43 class /*LIBEVORAL_API*/ EventRingBuffer : public PBD::RingBufferNPT<uint8_t>, public Evoral::EventSink<Time> {
44 public:
45
46         /** @param capacity Ringbuffer capacity in bytes.
47          */
48         EventRingBuffer(size_t capacity) : PBD::RingBufferNPT<uint8_t>(capacity)
49         {}
50
51         inline size_t capacity() const { return bufsize(); }
52
53         /** Peek at the ringbuffer (read w/o advancing read pointer).
54          * @return how much has been peeked (wraps around if read exceeds
55          * the end of the buffer):
56          * <pre>
57          * |===========--------------R=============================|
58          *            read-pointer---^
59          * </pre>
60          */
61         inline bool peek (uint8_t*, size_t size);
62
63         inline uint32_t write(Time  time, EventType  type, uint32_t  size, const uint8_t* buf);
64         inline bool     read (Time* time, EventType* type, uint32_t* size,       uint8_t* buf);
65 };
66
67 template<typename Time>
68 inline bool
69 EventRingBuffer<Time>::peek (uint8_t* buf, size_t size)
70 {
71         PBD::RingBufferNPT<uint8_t>::rw_vector vec;
72
73         get_read_vector (&vec);
74
75         if (vec.len[0] + vec.len[1] < size) {
76                 return false;
77         }
78
79         if (vec.len[0] > 0) {
80                 memcpy (buf, vec.buf[0], min (vec.len[0], size));
81         }
82
83         if (vec.len[0] < size) {
84                 if (vec.len[1]) {
85                         memcpy (buf + vec.len[0], vec.buf[1], size - vec.len[0]);
86                 }
87         }
88
89         return true;
90 }
91
92 template<typename Time>
93 inline bool
94 EventRingBuffer<Time>::read(Time* time, EventType* type, uint32_t* size, uint8_t* buf)
95 {
96         if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)time, sizeof (Time)) != sizeof (Time)) {
97                 return false;
98         }
99
100         if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)type, sizeof(EventType)) != sizeof (EventType)) {
101                 return false;
102         }
103
104         if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)size, sizeof(uint32_t)) != sizeof (uint32_t)) {
105                 return false;
106         }
107
108         if (PBD::RingBufferNPT<uint8_t>::read (buf, *size) != *size) {
109                 return false;
110         }
111
112         return true;
113 }
114
115
116 template<typename Time>
117 inline uint32_t
118 EventRingBuffer<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
119 {
120         if (!buf || write_space() < (sizeof(Time) + sizeof(EventType) + sizeof(uint32_t) + size)) {
121                 return 0;
122         } else {
123                 PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&time, sizeof(Time));
124                 PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&type, sizeof(EventType));
125                 PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&size, sizeof(uint32_t));
126                 PBD::RingBufferNPT<uint8_t>::write (buf, size);
127                 return size;
128         }
129 }
130
131
132 } // namespace Evoral
133
134 #endif // EVORAL_EVENT_RING_BUFFER_HPP
135