Fix broken whitespace.
[ardour.git] / libs / evoral / evoral / EventRingBuffer.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave 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 "evoral/RingBuffer.hpp"
22 #include "evoral/EventSink.hpp"
23 #include "evoral/types.hpp"
24
25 #include <iostream>
26 using namespace std;
27
28 namespace Evoral {
29
30
31 /** A RingBuffer of events (generic time-stamped binary "blobs").
32  *
33  * This packs a timestamp, size, and size bytes of data flat into the buffer.
34  * Useful for MIDI events, OSC messages, etc.
35  */
36 template<typename Time>
37 class EventRingBuffer : public Evoral::RingBuffer<uint8_t>, public Evoral::EventSink<Time> {
38 public:
39
40         /** @param capacity Ringbuffer capacity in bytes.
41          */
42         EventRingBuffer(size_t capacity) : RingBuffer<uint8_t>(capacity)
43         {}
44
45         size_t capacity() const { return _size; }
46
47         bool peek_time(Time* time);
48
49         uint32_t write(Time  time, EventType  type, uint32_t  size, const uint8_t* buf);
50         bool     read (Time* time, EventType* type, uint32_t* size,       uint8_t* buf);
51 };
52
53
54 template<typename Time>
55 inline bool
56 EventRingBuffer<Time>::peek_time(Time* time)
57 {
58         bool success = RingBuffer<uint8_t>::full_peek(sizeof(Time), (uint8_t*)time);
59         return success;
60 }
61
62
63 template<typename Time>
64 inline bool
65 EventRingBuffer<Time>::read(Time* time, EventType* type, uint32_t* size, uint8_t* buf)
66 {
67         bool success = RingBuffer<uint8_t>::full_read(sizeof(Time), (uint8_t*)time);
68         if (success)
69                 success = RingBuffer<uint8_t>::full_read(sizeof(EventType), (uint8_t*)type);
70         if (success)
71                 success = RingBuffer<uint8_t>::full_read(sizeof(uint32_t), (uint8_t*)size);
72         if (success)
73                 success = RingBuffer<uint8_t>::full_read(*size, buf);
74
75         return success;
76 }
77
78
79 template<typename Time>
80 inline uint32_t
81 EventRingBuffer<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
82 {
83         if (write_space() < (sizeof(Time) + sizeof(EventType) + sizeof(uint32_t) + size)) {
84                 return 0;
85         } else {
86                 RingBuffer<uint8_t>::write(sizeof(Time), (uint8_t*)&time);
87                 RingBuffer<uint8_t>::write(sizeof(EventType), (uint8_t*)&type);
88                 RingBuffer<uint8_t>::write(sizeof(uint32_t), (uint8_t*)&size);
89                 RingBuffer<uint8_t>::write(size, buf);
90                 return size;
91         }
92 }
93
94
95 } // namespace Evoral
96
97 #endif // EVORAL_EVENT_RING_BUFFER_HPP
98