mbed-os
Fork of mbed-os by
features/unsupported/USBDevice/USBSerial/CircBuffer.h@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
elessair | 0:f269e3021894 | 2 | * |
elessair | 0:f269e3021894 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
elessair | 0:f269e3021894 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
elessair | 0:f269e3021894 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
elessair | 0:f269e3021894 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
elessair | 0:f269e3021894 | 7 | * Software is furnished to do so, subject to the following conditions: |
elessair | 0:f269e3021894 | 8 | * |
elessair | 0:f269e3021894 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
elessair | 0:f269e3021894 | 10 | * substantial portions of the Software. |
elessair | 0:f269e3021894 | 11 | * |
elessair | 0:f269e3021894 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
elessair | 0:f269e3021894 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
elessair | 0:f269e3021894 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
elessair | 0:f269e3021894 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
elessair | 0:f269e3021894 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
elessair | 0:f269e3021894 | 17 | */ |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #ifndef CIRCBUFFER_H |
elessair | 0:f269e3021894 | 20 | #define CIRCBUFFER_H |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | template <class T, int Size> |
elessair | 0:f269e3021894 | 23 | class CircBuffer { |
elessair | 0:f269e3021894 | 24 | public: |
elessair | 0:f269e3021894 | 25 | CircBuffer():write(0), read(0){} |
elessair | 0:f269e3021894 | 26 | bool isFull() { |
elessair | 0:f269e3021894 | 27 | return ((write + 1) % size == read); |
elessair | 0:f269e3021894 | 28 | }; |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | bool isEmpty() { |
elessair | 0:f269e3021894 | 31 | return (read == write); |
elessair | 0:f269e3021894 | 32 | }; |
elessair | 0:f269e3021894 | 33 | |
elessair | 0:f269e3021894 | 34 | void queue(T k) { |
elessair | 0:f269e3021894 | 35 | if (isFull()) { |
elessair | 0:f269e3021894 | 36 | read++; |
elessair | 0:f269e3021894 | 37 | read %= size; |
elessair | 0:f269e3021894 | 38 | } |
elessair | 0:f269e3021894 | 39 | buf[write++] = k; |
elessair | 0:f269e3021894 | 40 | write %= size; |
elessair | 0:f269e3021894 | 41 | } |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | uint16_t available() { |
elessair | 0:f269e3021894 | 44 | return (write >= read) ? write - read : size - read + write; |
elessair | 0:f269e3021894 | 45 | }; |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | bool dequeue(T * c) { |
elessair | 0:f269e3021894 | 48 | bool empty = isEmpty(); |
elessair | 0:f269e3021894 | 49 | if (!empty) { |
elessair | 0:f269e3021894 | 50 | *c = buf[read++]; |
elessair | 0:f269e3021894 | 51 | read %= size; |
elessair | 0:f269e3021894 | 52 | } |
elessair | 0:f269e3021894 | 53 | return(!empty); |
elessair | 0:f269e3021894 | 54 | }; |
elessair | 0:f269e3021894 | 55 | |
elessair | 0:f269e3021894 | 56 | private: |
elessair | 0:f269e3021894 | 57 | volatile uint16_t write; |
elessair | 0:f269e3021894 | 58 | volatile uint16_t read; |
elessair | 0:f269e3021894 | 59 | static const int size = Size+1; //a modern optimizer should be able to remove this so it uses no ram. |
elessair | 0:f269e3021894 | 60 | T buf[Size+1]; |
elessair | 0:f269e3021894 | 61 | }; |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | #endif |