Ardour  9.0-pre0-582-g084a23a80d
event_ring_buffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 David Robillard <d@drobilla.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <iostream>
24 
25 #include "pbd/ringbufferNPT.h"
26 
27 #include "evoral/EventSink.h"
28 #include "evoral/types.h"
29 
30 namespace ARDOUR {
31 
41 template<typename Time>
42 class EventRingBuffer : public PBD::RingBufferNPT<uint8_t>
43  , public Evoral::EventSink<Time> {
44 public:
45 
49  {}
50 
51  inline size_t capacity() const { return bufsize(); }
52 
61  inline bool peek (uint8_t*, size_t size);
62 
63  inline uint32_t write(Time time, Evoral::EventType type, uint32_t size, const uint8_t* buf);
64  inline bool read (Time* time, Evoral::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 {
72 
73  get_read_vector (&vec);
74 
75  if (vec.len[0] + vec.len[1] < size) {
76  return false;
77  }
78 
79  assert (vec.len[0] > 0 || vec.len[1] > 0);
80 
81  if (vec.len[0] > 0) {
82  memcpy (buf, vec.buf[0], std::min (vec.len[0], size));
83  }
84 
85  if (vec.len[0] < size) {
86  if (vec.len[1]) {
87  memcpy (buf + vec.len[0], vec.buf[1], size - vec.len[0]);
88  }
89  }
90 
91  return true;
92 }
93 
94 template<typename Time>
95 inline bool
96 EventRingBuffer<Time>::read(Time* time, Evoral::EventType* type, uint32_t* size, uint8_t* buf)
97 {
98  if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)time, sizeof (Time)) != sizeof (Time)) {
99  return false;
100  }
101 
102  if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)type, sizeof(Evoral::EventType)) != sizeof (Evoral::EventType)) {
103  return false;
104  }
105 
106  if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)size, sizeof(uint32_t)) != sizeof (uint32_t)) {
107  return false;
108  }
109 
110  if (PBD::RingBufferNPT<uint8_t>::read (buf, *size) != *size) {
111  return false;
112  }
113 
114  return true;
115 }
116 
117 template<typename Time>
118 inline uint32_t
119 EventRingBuffer<Time>::write(Time time, Evoral::EventType type, uint32_t size, const uint8_t* buf)
120 {
121  assert (size > 0);
122  if (!buf || size == 0 || write_space() < (sizeof(Time) + sizeof(Evoral::EventType) + sizeof(uint32_t) + size)) {
123  return 0;
124  } else {
125  PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&time, sizeof(Time));
126  PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&type, sizeof(Evoral::EventType));
127  PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&size, sizeof(uint32_t));
129  return size;
130  }
131 }
132 
133 } // namespace ARDOUR
134 
bool read(Time *time, Evoral::EventType *type, uint32_t *size, uint8_t *buf)
uint32_t write(Time time, Evoral::EventType type, uint32_t size, const uint8_t *buf)
EventRingBuffer(size_t capacity)
bool peek(uint8_t *, size_t size)
size_t write(const T *src, size_t cnt)
Definition: axis_view.h:42