diff options
author | David Robillard <d@drobilla.net> | 2007-07-24 19:26:47 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2007-07-24 19:26:47 +0000 |
commit | 3b4a308ff647def75e647ac8f97e1e48b57672c3 (patch) | |
tree | a33e3a5c78f2ee7f23e151f39fe17ab39b0b81c8 /raul/StampedChunkRingBuffer.hpp | |
parent | fcb302e6248527b2f1fb655f4d0a64af18f5f059 (diff) | |
download | raul-3b4a308ff647def75e647ac8f97e1e48b57672c3.tar.gz raul-3b4a308ff647def75e647ac8f97e1e48b57672c3.tar.bz2 raul-3b4a308ff647def75e647ac8f97e1e48b57672c3.zip |
Consistently rename all C++ files .cpp/.hpp.
Fix (some) inclusion guard names to not clash with other libs.
git-svn-id: http://svn.drobilla.net/lad/raul@613 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'raul/StampedChunkRingBuffer.hpp')
-rw-r--r-- | raul/StampedChunkRingBuffer.hpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/raul/StampedChunkRingBuffer.hpp b/raul/StampedChunkRingBuffer.hpp new file mode 100644 index 0000000..b4f9371 --- /dev/null +++ b/raul/StampedChunkRingBuffer.hpp @@ -0,0 +1,83 @@ +/* This file is part of Raul. + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * 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 <cassert> +#include <algorithm> +#include <glib.h> +#include <raul/types.hpp> +#include <raul/RingBuffer.hpp> + +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<Byte> { +public: + + /** @param size Size in bytes. + */ + StampedChunkRingBuffer(size_t size) + : RingBuffer<Byte>(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<Byte>::full_read(sizeof(TickTime), (Byte*)time); + if (success) + success = RingBuffer<Byte>::full_read(sizeof(size_t), (Byte*)size); + if (success) + success = RingBuffer<Byte>::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<Byte>::write(sizeof(TickTime), (Byte*)&time); + RingBuffer<Byte>::write(sizeof(size_t), (Byte*)&size); + RingBuffer<Byte>::write(size, buf); + return size; + } +} + + +} // namespace Raul + +#endif // RAUL_STAMPED_CHUNK_RING_BUFFER_HPP + |