• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

EventRingBuffer.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007-2009 David 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_EVENT_RING_BUFFER_HPP
00019 #define RAUL_EVENT_RING_BUFFER_HPP
00020 
00021 #include <cassert>
00022 #include <algorithm>
00023 #include "raul/RingBuffer.hpp"
00024 #include "raul/TimeStamp.hpp"
00025 
00026 namespace Raul {
00027 
00028 
00035 class EventRingBuffer : private Raul::RingBuffer {
00036 public:
00037 
00040     explicit EventRingBuffer(size_t capacity)
00041         : RingBuffer(capacity)
00042     {}
00043 
00044     size_t capacity() const { return _size; }
00045 
00046     size_t write(TimeStamp time, size_t size, const uint8_t* buf);
00047     bool   read(TimeStamp* time, size_t* size, uint8_t* buf);
00048 };
00049 
00050 
00051 inline bool
00052 EventRingBuffer::read(TimeStamp* time, size_t* size, uint8_t* buf)
00053 {
00054     bool success = RingBuffer::full_read(sizeof(TimeStamp), (uint8_t*)time);
00055     if (success)
00056         success = RingBuffer::full_read(sizeof(size_t), (uint8_t*)size);
00057     if (success)
00058         success = RingBuffer::full_read(*size, buf);
00059 
00060     return success;
00061 }
00062 
00063 
00064 inline size_t
00065 EventRingBuffer::write(TimeStamp time, size_t size, const uint8_t* buf)
00066 {
00067     assert(size > 0);
00068 
00069     if (write_space() < (sizeof(TimeStamp) + sizeof(size_t) + size)) {
00070         return 0;
00071     } else {
00072         RingBuffer::write(sizeof(TimeStamp), (uint8_t*)&time);
00073         RingBuffer::write(sizeof(size_t), (uint8_t*)&size);
00074         RingBuffer::write(size, buf);
00075         return size;
00076     }
00077 }
00078 
00079 
00080 } // namespace Raul
00081 
00082 #endif // RAUL_EVENT_RING_BUFFER_HPP
00083 

Generated on Tue Jan 11 2011 18:26:17 for RAUL by  doxygen 1.7.1