File indexing completed on 2025-01-05 04:35:06

0001 /*
0002  * This file is part of the mouse gesture package.
0003  * Copyright (C) 2006 Johan Thelin <e8johan@gmail.com>
0004  * All rights reserved.
0005  *
0006  * Redistribution and use in source and binary forms, with or
0007  * without modification, are permitted provided that the
0008  * following conditions are met:
0009  *
0010  *   - Redistributions of source code must retain the above
0011  *     copyright notice, this list of conditions and the
0012  *     following disclaimer.
0013  *   - Redistributions in binary form must reproduce the
0014  *     above copyright notice, this list of conditions and
0015  *     the following disclaimer in the documentation and/or
0016  *     other materials provided with the distribution.
0017  *   - The names of its contributors may be used to endorse
0018  *     or promote products derived from this software without
0019  *     specific prior written permission.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
0022  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
0023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0024  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0025  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0026  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0028  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
0029  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
0030  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0031  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0032  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0033  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  *
0036  */
0037 
0038 #ifndef RING_BUFFER_H
0039 #define RING_BUFFER_H
0040 
0041 #include <vector>
0042 
0043 /*
0044 * Implementation of Ring Buffer
0045 */
0046 template<typename T>
0047 class RingBuffer
0048 {
0049 public:
0050     typedef T*             iterator;
0051     typedef const T*       const_iterator;
0052 
0053     RingBuffer() {
0054         array = nullptr;
0055         size = 0;
0056         read = 0;
0057         write = 0;
0058         empty = true;
0059         overflow = false;
0060     }
0061     RingBuffer(int size) {
0062         size = 0;
0063         read = 0;
0064         write = 0;
0065         empty = true;
0066         overflow = false;
0067         resize(size);
0068     }
0069 
0070 
0071     void push_back(T item) {
0072         /*
0073                 if(overflow)
0074                 {
0075                     throw std::exception("container overflow!");
0076                 }
0077         */
0078 
0079         array[write++] = item;
0080         if (write >= size) write = 0;
0081         empty = false;
0082         if (write == read) {
0083             overflow = true;
0084         }
0085     }
0086     T &pop() {
0087         /*
0088                 if ( empty )
0089                 {
0090                     throw std::exception("container is empty");
0091                 }
0092         */
0093         int tmp = read;
0094         read++;
0095 
0096         if (read >= size) read = 0;
0097         overflow = false;
0098         if (write == read)
0099             empty = true;
0100         return array[tmp];
0101     }
0102 
0103     void setReadPointerTo(int index) {
0104         read = index;
0105         if (read >= size) read = 0;
0106         if (write != read) empty = false;
0107     }
0108 
0109     int getReadPointer() {
0110         return read;
0111     }
0112 
0113     bool is_empty() {
0114         return empty;
0115     }
0116 
0117     void resize(int s) {
0118         size = s;
0119         array = new T[size];
0120     }
0121 
0122 protected:
0123     T* array;
0124     int size;
0125     int read;
0126     int write;
0127     bool overflow;
0128     bool empty;
0129 };
0130 
0131 #endif