/builddir/build/BUILD/raul-0.4.0/raul/StampedChunkRingBuffer.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_STAMPED_CHUNK_RING_BUFFER_HPP
00019 #define RAUL_STAMPED_CHUNK_RING_BUFFER_HPP
00020 
00021 #include <cassert>
00022 #include <algorithm>
00023 #include <glib.h>
00024 #include <raul/types.hpp>
00025 #include <raul/RingBuffer.hpp>
00026 
00027 namespace Raul {
00028 
00029 
00035 class StampedChunkRingBuffer : private Raul::RingBuffer<Byte> {
00036 public:
00037 
00040     StampedChunkRingBuffer(size_t size)
00041         : RingBuffer<Byte>(size)
00042     {}
00043 
00044     size_t capacity() const { return _size; }
00045 
00046     size_t write(TickTime time, size_t size, const Byte* buf);
00047     bool   read(TickTime* time, size_t* size, Byte* buf);
00048 };
00049 
00050 
00051 inline bool
00052 StampedChunkRingBuffer::read(TickTime* time, size_t* size, Byte* buf)
00053 {
00054     bool success = RingBuffer<Byte>::full_read(sizeof(TickTime), (Byte*)time);
00055     if (success)
00056         success = RingBuffer<Byte>::full_read(sizeof(size_t), (Byte*)size);
00057     if (success)
00058         success = RingBuffer<Byte>::full_read(*size, buf);
00059 
00060     return success;
00061 }
00062 
00063 
00064 inline size_t
00065 StampedChunkRingBuffer::write(TickTime time, size_t size, const Byte* buf)
00066 {
00067     assert(size > 0);
00068 
00069     if (write_space() < (sizeof(TickTime) + sizeof(size_t) + size)) {
00070         return 0;
00071     } else {
00072         RingBuffer<Byte>::write(sizeof(TickTime), (Byte*)&time);
00073         RingBuffer<Byte>::write(sizeof(size_t), (Byte*)&size);
00074         RingBuffer<Byte>::write(size, buf);
00075         return size;
00076     }
00077 }
00078 
00079 
00080 } // namespace Raul
00081 
00082 #endif // RAUL_STAMPED_CHUNK_RING_BUFFER_HPP
00083 

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1