• 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 
00034 class EventRingBuffer : private Raul::RingBuffer<uint8_t> {
00035 public:
00036 
00039     EventRingBuffer(size_t capacity)
00040         : RingBuffer<uint8_t>(capacity)
00041     {}
00042 
00043     size_t capacity() const { return _size; }
00044 
00045     size_t write(TimeStamp time, size_t size, const uint8_t* buf);
00046     bool   read(TimeStamp* time, size_t* size, uint8_t* buf);
00047 };
00048 
00049 
00050 inline bool
00051 EventRingBuffer::read(TimeStamp* time, size_t* size, uint8_t* buf)
00052 {
00053     bool success = RingBuffer<uint8_t>::full_read(sizeof(TimeStamp), (uint8_t*)time);
00054     if (success)
00055         success = RingBuffer<uint8_t>::full_read(sizeof(size_t), (uint8_t*)size);
00056     if (success)
00057         success = RingBuffer<uint8_t>::full_read(*size, buf);
00058 
00059     return success;
00060 }
00061 
00062 
00063 inline size_t
00064 EventRingBuffer::write(TimeStamp time, size_t size, const uint8_t* buf)
00065 {
00066     assert(size > 0);
00067 
00068     if (write_space() < (sizeof(TimeStamp) + sizeof(size_t) + size)) {
00069         return 0;
00070     } else {
00071         RingBuffer<uint8_t>::write(sizeof(TimeStamp), (uint8_t*)&time);
00072         RingBuffer<uint8_t>::write(sizeof(size_t), (uint8_t*)&size);
00073         RingBuffer<uint8_t>::write(size, buf);
00074         return size;
00075     }
00076 }
00077 
00078 
00079 } // namespace Raul
00080 
00081 #endif // RAUL_EVENT_RING_BUFFER_HPP
00082 

Generated on Sat Aug 27 2011 for RAUL by  doxygen 1.7.1