Mercurial > vba-linux
comparison 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 |
comparison
equal
deleted
inserted
replaced
27:b970226568d2 | 28:2efb971df515 |
---|---|
1 /*************************************************************************** | |
2 * Copyright (C) 2008 by Sindre AamÄs * | |
3 * aamas@stud.ntnu.no * | |
4 * * | |
5 * This program is free software; you can redistribute it and/or modify * | |
6 * it under the terms of the GNU General Public License version 2 as * | |
7 * published by the Free Software Foundation. * | |
8 * * | |
9 * This program is distributed in the hope that it will be useful, * | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
12 * GNU General Public License version 2 for more details. * | |
13 * * | |
14 * You should have received a copy of the GNU General Public License * | |
15 * version 2 along with this program; if not, write to the * | |
16 * Free Software Foundation, Inc., * | |
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * | |
18 ***************************************************************************/ | |
19 #ifndef RINGBUFFER_H | |
20 #define RINGBUFFER_H | |
21 | |
22 #include "Array.h" | |
23 #include <cstddef> | |
24 #include <algorithm> | |
25 #include <cstring> | |
26 | |
27 template<typename T> | |
28 class RingBuffer { | |
29 Array<T> buf; | |
30 std::size_t sz; | |
31 std::size_t rpos; | |
32 std::size_t wpos; | |
33 | |
34 public: | |
35 RingBuffer(const std::size_t sz_in = 0) : sz(0), rpos(0), wpos(0) { reset(sz_in); } | |
36 | |
37 std::size_t avail() const { | |
38 return (wpos < rpos ? 0 : sz) + rpos - wpos - 1; | |
39 } | |
40 | |
41 void clear() { | |
42 wpos = rpos = 0; | |
43 } | |
44 | |
45 void fill(T value); | |
46 | |
47 void read(T *out, std::size_t num); | |
48 | |
49 void reset(std::size_t sz_in); | |
50 | |
51 std::size_t size() const { | |
52 return sz - 1; | |
53 } | |
54 | |
55 std::size_t used() const { | |
56 return (wpos < rpos ? sz : 0) + wpos - rpos; | |
57 } | |
58 | |
59 void write(const T *in, std::size_t num); | |
60 }; | |
61 | |
62 template<typename T> | |
63 void RingBuffer<T>::fill(const T value) { | |
64 std::fill(buf + 0, buf + sz, value); | |
65 rpos = 0; | |
66 wpos = sz - 1; | |
67 } | |
68 | |
69 template<typename T> | |
70 void RingBuffer<T>::read(T *out, std::size_t num) { | |
71 if (rpos + num > sz) { | |
72 const std::size_t n = sz - rpos; | |
73 | |
74 std::memcpy(out, buf + rpos, n * sizeof(T)); | |
75 | |
76 rpos = 0; | |
77 num -= n; | |
78 out += n; | |
79 } | |
80 | |
81 std::memcpy(out, buf + rpos, num * sizeof(T)); | |
82 | |
83 if ((rpos += num) == sz) | |
84 rpos = 0; | |
85 } | |
86 | |
87 template<typename T> | |
88 void RingBuffer<T>::reset(const std::size_t sz_in) { | |
89 sz = sz_in + 1; | |
90 rpos = wpos = 0; | |
91 buf.reset(sz_in ? sz : 0); | |
92 } | |
93 | |
94 template<typename T> | |
95 void RingBuffer<T>::write(const T *in, std::size_t num) { | |
96 if (wpos + num > sz) { | |
97 const std::size_t n = sz - wpos; | |
98 | |
99 std::memcpy(buf + wpos, in, n * sizeof(T)); | |
100 | |
101 wpos = 0; | |
102 num -= n; | |
103 in += n; | |
104 } | |
105 | |
106 std::memcpy(buf + wpos, in, num * sizeof(T)); | |
107 | |
108 if ((wpos += num) == sz) | |
109 wpos = 0; | |
110 } | |
111 | |
112 #endif |