00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 }
00081
00082 #endif // RAUL_STAMPED_CHUNK_RING_BUFFER_HPP
00083