/* This file is part of Raul. * Copyright (C) 2007 Dave Robillard * * Raul is free software; you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) any later * version. * * Raul is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef RAUL_STAMPED_CHUNK_RING_BUFFER_HPP #define RAUL_STAMPED_CHUNK_RING_BUFFER_HPP #include #include #include #include #include namespace Raul { /** A RingBuffer of timestamped binary "chunks". * * This packs a timestamp, size, and size bytes of data flat into the buffer. * Useful for MIDI events, OSC messages, etc. */ class StampedChunkRingBuffer : private Raul::RingBuffer { public: /** @param size Size in bytes. */ StampedChunkRingBuffer(size_t size) : RingBuffer(size) {} size_t capacity() const { return _size; } size_t write(TickTime time, size_t size, const Byte* buf); bool read(TickTime* time, size_t* size, Byte* buf); }; inline bool StampedChunkRingBuffer::read(TickTime* time, size_t* size, Byte* buf) { bool success = RingBuffer::full_read(sizeof(TickTime), (Byte*)time); if (success) success = RingBuffer::full_read(sizeof(size_t), (Byte*)size); if (success) success = RingBuffer::full_read(*size, buf); return success; } inline size_t StampedChunkRingBuffer::write(TickTime time, size_t size, const Byte* buf) { assert(size > 0); if (write_space() < (sizeof(TickTime) + sizeof(size_t) + size)) { return 0; } else { RingBuffer::write(sizeof(TickTime), (Byte*)&time); RingBuffer::write(sizeof(size_t), (Byte*)&size); RingBuffer::write(size, buf); return size; } } } // namespace Raul #endif // RAUL_STAMPED_CHUNK_RING_BUFFER_HPP