Mercurial > vba-linux
diff src/sdl/RingBuffer.h @ 28:2efb971df515
bringing in SDL package
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 04 Mar 2012 21:06:50 -0600 |
parents | f9f4f1b99eed |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/sdl/RingBuffer.h Sun Mar 04 21:06:50 2012 -0600 1.3 @@ -0,0 +1,112 @@ 1.4 +/*************************************************************************** 1.5 + * Copyright (C) 2008 by Sindre AamÄs * 1.6 + * aamas@stud.ntnu.no * 1.7 + * * 1.8 + * This program is free software; you can redistribute it and/or modify * 1.9 + * it under the terms of the GNU General Public License version 2 as * 1.10 + * published by the Free Software Foundation. * 1.11 + * * 1.12 + * This program is distributed in the hope that it will be useful, * 1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of * 1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 1.15 + * GNU General Public License version 2 for more details. * 1.16 + * * 1.17 + * You should have received a copy of the GNU General Public License * 1.18 + * version 2 along with this program; if not, write to the * 1.19 + * Free Software Foundation, Inc., * 1.20 + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 1.21 + ***************************************************************************/ 1.22 +#ifndef RINGBUFFER_H 1.23 +#define RINGBUFFER_H 1.24 + 1.25 +#include "Array.h" 1.26 +#include <cstddef> 1.27 +#include <algorithm> 1.28 +#include <cstring> 1.29 + 1.30 +template<typename T> 1.31 +class RingBuffer { 1.32 + Array<T> buf; 1.33 + std::size_t sz; 1.34 + std::size_t rpos; 1.35 + std::size_t wpos; 1.36 + 1.37 +public: 1.38 + RingBuffer(const std::size_t sz_in = 0) : sz(0), rpos(0), wpos(0) { reset(sz_in); } 1.39 + 1.40 + std::size_t avail() const { 1.41 + return (wpos < rpos ? 0 : sz) + rpos - wpos - 1; 1.42 + } 1.43 + 1.44 + void clear() { 1.45 + wpos = rpos = 0; 1.46 + } 1.47 + 1.48 + void fill(T value); 1.49 + 1.50 + void read(T *out, std::size_t num); 1.51 + 1.52 + void reset(std::size_t sz_in); 1.53 + 1.54 + std::size_t size() const { 1.55 + return sz - 1; 1.56 + } 1.57 + 1.58 + std::size_t used() const { 1.59 + return (wpos < rpos ? sz : 0) + wpos - rpos; 1.60 + } 1.61 + 1.62 + void write(const T *in, std::size_t num); 1.63 +}; 1.64 + 1.65 +template<typename T> 1.66 +void RingBuffer<T>::fill(const T value) { 1.67 + std::fill(buf + 0, buf + sz, value); 1.68 + rpos = 0; 1.69 + wpos = sz - 1; 1.70 +} 1.71 + 1.72 +template<typename T> 1.73 +void RingBuffer<T>::read(T *out, std::size_t num) { 1.74 + if (rpos + num > sz) { 1.75 + const std::size_t n = sz - rpos; 1.76 + 1.77 + std::memcpy(out, buf + rpos, n * sizeof(T)); 1.78 + 1.79 + rpos = 0; 1.80 + num -= n; 1.81 + out += n; 1.82 + } 1.83 + 1.84 + std::memcpy(out, buf + rpos, num * sizeof(T)); 1.85 + 1.86 + if ((rpos += num) == sz) 1.87 + rpos = 0; 1.88 +} 1.89 + 1.90 +template<typename T> 1.91 +void RingBuffer<T>::reset(const std::size_t sz_in) { 1.92 + sz = sz_in + 1; 1.93 + rpos = wpos = 0; 1.94 + buf.reset(sz_in ? sz : 0); 1.95 +} 1.96 + 1.97 +template<typename T> 1.98 +void RingBuffer<T>::write(const T *in, std::size_t num) { 1.99 + if (wpos + num > sz) { 1.100 + const std::size_t n = sz - wpos; 1.101 + 1.102 + std::memcpy(buf + wpos, in, n * sizeof(T)); 1.103 + 1.104 + wpos = 0; 1.105 + num -= n; 1.106 + in += n; 1.107 + } 1.108 + 1.109 + std::memcpy(buf + wpos, in, num * sizeof(T)); 1.110 + 1.111 + if ((wpos += num) == sz) 1.112 + wpos = 0; 1.113 +} 1.114 + 1.115 +#endif