RingBuf.h
Go to the documentation of this file.
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 D. Aaron Wisner
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #ifndef EM_RINGBUF_CPP_H
26 #define EM_RINGBUF_CPP_H
27 
29 #include "main.h"
30 
31 template <typename Type, uint16_t MaxElements>
33 {
34 public:
35 
36 RingBufCPP(pros::Mutex& mut): rosvexMutex(mut)
37 {
39  {
40  _numElements = 0;
41 
42  _head = 0;
43  }
45 }
46 
51 bool add(Type &obj)
52 {
53  bool ret = false;
55  {
56  if (!isFull()) {
57  _buf[_head] = obj;
58  _head = (_head + 1)%MaxElements;
59  _numElements++;
60 
61  ret = true;
62  }
63  }
65 
66  return ret;
67 }
68 
69 
74 bool pull(Type *dest)
75 {
76  bool ret = false;
77  uint16_t tail;
78 
80  {
81  if (!isEmpty()) {
82  tail = getTail();
83  *dest = _buf[tail];
84  _numElements--;
85 
86  ret = true;
87  }
88  }
90 
91  return ret;
92 }
93 
94 
99 Type peek(uint16_t num)
100 {
101  Type *ret = NULL;
102 
104  {
105  if (num < _numElements) //make sure not out of bounds
106  ret = &_buf[(getTail() + num)%MaxElements];
107  }
109 
110  return ret;
111 }
112 
113 
117 bool isFull()
118 {
119  bool ret;
120 
122  {
123  ret = _numElements >= MaxElements;
124  }
126 
127  return ret;
128 }
129 
130 
134 uint16_t numElements()
135 {
136  uint16_t ret;
137 
139  {
140  ret = _numElements;
141  }
143 
144  return ret;
145 }
146 
147 
151 bool isEmpty()
152 {
153  bool ret;
154 
156  {
157  ret = !_numElements;
158  }
160 
161  return ret;
162 }
163 
164 protected:
169 uint16_t getTail()
170 {
171  return (_head + (MaxElements - _numElements))%MaxElements;
172 }
173 
174 
175 // underlying array
176 Type _buf[MaxElements];
177 
178 uint16_t _head;
179 uint16_t _numElements;
180 private:
181  pros::Mutex& rosvexMutex;
182 };
183 
184 #endif
#define NULL
Type _buf[MaxElements]
Definition: RingBuf.h:176
RingBufCPP(pros::Mutex &mut)
Definition: RingBuf.h:36
#define RB_ATOMIC_END
bool isEmpty()
Definition: RingBuf.h:151
bool isFull()
Definition: RingBuf.h:117
bool pull(Type *dest)
Definition: RingBuf.h:74
pros::Mutex & rosvexMutex
Definition: RingBuf.h:181
uint16_t _numElements
Definition: RingBuf.h:179
bool add(Type &obj)
Definition: RingBuf.h:51
uint16_t getTail()
Definition: RingBuf.h:169
uint16_t numElements()
Definition: RingBuf.h:134
uint16_t _head
Definition: RingBuf.h:178
#define RB_ATOMIC_START
Type peek(uint16_t num)
Definition: RingBuf.h:99


rosserial_vex_v5
Author(s): Cannon
autogenerated on Fri Jun 7 2019 22:03:08