Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[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 template<typename Time>
38 class EventRingBuffer : public Evoral::RingBuffer<uint8_t>, public Evoral::EventSink<Time> {
39 public:
40
41         /** @param capacity Ringbuffer capacity in bytes.
42          */
43         EventRingBuffer(size_t capacity) : RingBuffer<uint8_t>(capacity)
44         {}
45
46         size_t capacity() const { return _size; }
47
48         bool peek_time(Time* time);
49
50         uint32_t write(Time  time, EventType  type, uint32_t  size, const uint8_t* buf);
51         bool     read (Time* time, EventType* type, uint32_t* size,       uint8_t* buf);
52 };
53
54
55 template<typename Time>
56 inline bool
57 EventRingBuffer<Time>::peek_time(Time* time)
58 {
59         bool success = RingBuffer<uint8_t>::full_peek(sizeof(Time), (uint8_t*)time);
60         return success;
61 }
62
63
64 template<typename Time>
65 inline bool
66 EventRingBuffer<Time>::read(Time* time, EventType* type, uint32_t* size, uint8_t* buf)
67 {
68         bool success = RingBuffer<uint8_t>::full_read(sizeof(Time), (uint8_t*)time);
69         if (success)
70                 success = RingBuffer<uint8_t>::full_read(sizeof(EventType), (uint8_t*)type);
71         if (success)
72                 success = RingBuffer<uint8_t>::full_read(sizeof(uint32_t), (uint8_t*)size);
73         if (success)
74                 success = RingBuffer<uint8_t>::full_read(*size, buf);
75
76         return success;
77 }
78
79
80 template<typename Time>
81 inline uint32_t
82 EventRingBuffer<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
83 {
84         if (write_space() < (sizeof(Time) + sizeof(EventType) + sizeof(uint32_t) + size)) {
85                 return 0;
86         } else {
87                 RingBuffer<uint8_t>::write(sizeof(Time), (uint8_t*)&time);
88                 RingBuffer<uint8_t>::write(sizeof(EventType), (uint8_t*)&type);
89                 RingBuffer<uint8_t>::write(sizeof(uint32_t), (uint8_t*)&size);
90                 RingBuffer<uint8_t>::write(size, buf);
91                 return size;
92         }
93 }
94
95
96 } // namespace Evoral
97
98 #endif // EVORAL_EVENT_RING_BUFFER_HPP
99