https://developer.mbed.org/users/ds4279/code/WiflySocket/

Committer:
ds4279
Date:
Wed Feb 22 18:49:17 2017 +0000
Revision:
0:374a8a31f262
Added modified library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ds4279 0:374a8a31f262 1 /* Copyright (C) 2012 mbed.org, MIT License
ds4279 0:374a8a31f262 2 *
ds4279 0:374a8a31f262 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ds4279 0:374a8a31f262 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ds4279 0:374a8a31f262 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ds4279 0:374a8a31f262 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ds4279 0:374a8a31f262 7 * furnished to do so, subject to the following conditions:
ds4279 0:374a8a31f262 8 *
ds4279 0:374a8a31f262 9 * The above copyright notice and this permission notice shall be included in all copies or
ds4279 0:374a8a31f262 10 * substantial portions of the Software.
ds4279 0:374a8a31f262 11 *
ds4279 0:374a8a31f262 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ds4279 0:374a8a31f262 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ds4279 0:374a8a31f262 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ds4279 0:374a8a31f262 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ds4279 0:374a8a31f262 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ds4279 0:374a8a31f262 17 */
ds4279 0:374a8a31f262 18
ds4279 0:374a8a31f262 19 #ifndef CIRCBUFFER_H_
ds4279 0:374a8a31f262 20 #define CIRCBUFFER_H_
ds4279 0:374a8a31f262 21
ds4279 0:374a8a31f262 22 template <class T>
ds4279 0:374a8a31f262 23 class CircBuffer {
ds4279 0:374a8a31f262 24 public:
ds4279 0:374a8a31f262 25 CircBuffer(int length) {
ds4279 0:374a8a31f262 26 write = 0;
ds4279 0:374a8a31f262 27 read = 0;
ds4279 0:374a8a31f262 28 size = length + 1;
ds4279 0:374a8a31f262 29 buf = (T *)malloc(size * sizeof(T));
ds4279 0:374a8a31f262 30 };
ds4279 0:374a8a31f262 31
ds4279 0:374a8a31f262 32 bool isFull() {
ds4279 0:374a8a31f262 33 return (((write + 1) % size) == read);
ds4279 0:374a8a31f262 34 };
ds4279 0:374a8a31f262 35
ds4279 0:374a8a31f262 36 bool isEmpty() {
ds4279 0:374a8a31f262 37 return (read == write);
ds4279 0:374a8a31f262 38 };
ds4279 0:374a8a31f262 39
ds4279 0:374a8a31f262 40 void queue(T k) {
ds4279 0:374a8a31f262 41 if (isFull()) {
ds4279 0:374a8a31f262 42 read++;
ds4279 0:374a8a31f262 43 read %= size;
ds4279 0:374a8a31f262 44 }
ds4279 0:374a8a31f262 45 buf[write++] = k;
ds4279 0:374a8a31f262 46 write %= size;
ds4279 0:374a8a31f262 47 }
ds4279 0:374a8a31f262 48
ds4279 0:374a8a31f262 49 void flush() {
ds4279 0:374a8a31f262 50 read = 0;
ds4279 0:374a8a31f262 51 write = 0;
ds4279 0:374a8a31f262 52 }
ds4279 0:374a8a31f262 53
ds4279 0:374a8a31f262 54
ds4279 0:374a8a31f262 55 uint32_t available() {
ds4279 0:374a8a31f262 56 return (write >= read) ? write - read : size - read + write;
ds4279 0:374a8a31f262 57 };
ds4279 0:374a8a31f262 58
ds4279 0:374a8a31f262 59 bool dequeue(T * c) {
ds4279 0:374a8a31f262 60 bool empty = isEmpty();
ds4279 0:374a8a31f262 61 if (!empty) {
ds4279 0:374a8a31f262 62 *c = buf[read++];
ds4279 0:374a8a31f262 63 read %= size;
ds4279 0:374a8a31f262 64 }
ds4279 0:374a8a31f262 65 return(!empty);
ds4279 0:374a8a31f262 66 };
ds4279 0:374a8a31f262 67
ds4279 0:374a8a31f262 68 private:
ds4279 0:374a8a31f262 69 volatile uint32_t write;
ds4279 0:374a8a31f262 70 volatile uint32_t read;
ds4279 0:374a8a31f262 71 uint32_t size;
ds4279 0:374a8a31f262 72 T * buf;
ds4279 0:374a8a31f262 73 };
ds4279 0:374a8a31f262 74
ds4279 0:374a8a31f262 75 #endif
ds4279 0:374a8a31f262 76