Falkon Develop
Cross-platform Qt-based web browser
ring_buffer.h
Go to the documentation of this file.
1/*
2 * This file is part of the mouse gesture package.
3 * Copyright (C) 2006 Johan Thelin <e8johan@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the
8 * following conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the
12 * following disclaimer.
13 * - Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions and
15 * the following disclaimer in the documentation and/or
16 * other materials provided with the distribution.
17 * - The names of its contributors may be used to endorse
18 * or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38#ifndef RING_BUFFER_H
39#define RING_BUFFER_H
40
41#include <vector>
42
43/*
44* Implementation of Ring Buffer
45*/
46template<typename T>
48{
49public:
50 typedef T* iterator;
51 typedef const T* const_iterator;
52
54 array = nullptr;
55 size = 0;
56 read = 0;
57 write = 0;
58 empty = true;
59 overflow = false;
60 }
62 size = 0;
63 read = 0;
64 write = 0;
65 empty = true;
66 overflow = false;
67 resize(size);
68 }
69
70
71 void push_back(T item) {
72 /*
73 if(overflow)
74 {
75 throw std::exception("container overflow!");
76 }
77 */
78
79 array[write++] = item;
80 if (write >= size) write = 0;
81 empty = false;
82 if (write == read) {
83 overflow = true;
84 }
85 }
86 T &pop() {
87 /*
88 if ( empty )
89 {
90 throw std::exception("container is empty");
91 }
92 */
93 int tmp = read;
94 read++;
95
96 if (read >= size) read = 0;
97 overflow = false;
98 if (write == read)
99 empty = true;
100 return array[tmp];
101 }
102
103 void setReadPointerTo(int index) {
104 read = index;
105 if (read >= size) read = 0;
106 if (write != read) empty = false;
107 }
108
110 return read;
111 }
112
113 bool is_empty() {
114 return empty;
115 }
116
117 void resize(int s) {
118 size = s;
119 array = new T[size];
120 }
121
122protected:
124 int size;
125 int read;
126 int write;
128 bool empty;
129};
130
131#endif
void resize(int s)
Definition: ring_buffer.h:117
bool is_empty()
Definition: ring_buffer.h:113
void push_back(T item)
Definition: ring_buffer.h:71
const T * const_iterator
Definition: ring_buffer.h:51
RingBuffer(int size)
Definition: ring_buffer.h:61
void setReadPointerTo(int index)
Definition: ring_buffer.h:103
T & pop()
Definition: ring_buffer.h:86
int getReadPointer()
Definition: ring_buffer.h:109
T * iterator
Definition: ring_buffer.h:50
bool overflow
Definition: ring_buffer.h:127