Move event specific ringbuffer stuff to evoral.
[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 <glib.h>
22 #include <evoral/RingBuffer.hpp>
23 #include <evoral/EventSink.hpp>
24 #include <evoral/types.hpp>
25
26 #include <iostream>
27 using namespace std;
28
29 namespace Evoral {
30
31
32 /** A RingBuffer of events (generic time-stamped binary "blobs").
33  *
34  * This packs a timestamp, size, and size bytes of data flat into the buffer.
35  * Useful for MIDI events, OSC messages, etc.
36  */
37 class EventRingBuffer : public Evoral::RingBuffer<uint8_t>, public Evoral::EventSink {
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(EventTime* time);
48
49         uint32_t write(EventTime  time, EventType  type, uint32_t  size, const uint8_t* buf);
50         bool     read (EventTime* time, EventType* type, uint32_t* size,       uint8_t* buf);
51 };
52
53
54 inline bool
55 EventRingBuffer::peek_time(EventTime* time)
56 {
57         bool success = RingBuffer<uint8_t>::full_peek(sizeof(EventTime), (uint8_t*)time);
58         return success;
59 }
60
61
62 inline bool
63 EventRingBuffer::read(EventTime* time, EventType* type, uint32_t* size, uint8_t* buf)
64 {
65         bool success = RingBuffer<uint8_t>::full_read(sizeof(EventTime), (uint8_t*)time);
66         if (success)
67                 success = RingBuffer<uint8_t>::full_read(sizeof(EventType), (uint8_t*)type);
68         if (success)
69                 success = RingBuffer<uint8_t>::full_read(sizeof(uint32_t), (uint8_t*)size);
70         if (success)
71                 success = RingBuffer<uint8_t>::full_read(*size, buf);
72         
73         return success;
74 }
75
76
77 inline uint32_t
78 EventRingBuffer::write(EventTime time, EventType type, uint32_t size, const uint8_t* buf)
79 {
80         if (write_space() < (sizeof(EventTime) + sizeof(EventType) + sizeof(uint32_t) + size)) {
81                 return 0;
82         } else {
83                 RingBuffer<uint8_t>::write(sizeof(EventTime), (uint8_t*)&time);
84                 RingBuffer<uint8_t>::write(sizeof(EventType), (uint8_t*)&type);
85                 RingBuffer<uint8_t>::write(sizeof(uint32_t), (uint8_t*)&size);
86                 RingBuffer<uint8_t>::write(size, buf);
87                 return size;
88         }
89 }
90
91
92 } // namespace Evoral
93
94 #endif // EVORAL_EVENT_RING_BUFFER_HPP
95