Home | Develop | Download | Contact
CircBuffer.hpp
1/*
2 * CircBuffer.hpp
3 *
4 * Copyright 2018 Fernando Pujaico Rivera <fernando.pujaico.rivera@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 */
22
32#ifndef __PDS_CIRCBUFFER_HPP__
33#define __PDS_CIRCBUFFER_HPP__
34
35#include <string>
36
46namespace Pds{
47
55template<typename Datum>
57{
58
59private:
61 unsigned int nel;
62
64 Datum *data;
65
67 unsigned int front;
68
69
70public:
71
83 CircBuffer(void);
84
92 CircBuffer(unsigned int Nel);
93
104
105 ~CircBuffer();
106
111public:
134 Datum &operator[](const int &id);
135
156 bool Push(const Datum &Dat);
157
162public:
183 bool IsEmpty(void) const;
184
190 unsigned int Nel(void) const;
191
195}; // Class CircBuffer
196
197} // namespace Pds
198
199
205template<typename Datum>
207{
208 this->nel = 0;
209 this->data = NULL;
210 this->front = 0;
211}
212
213template<typename Datum>
215{
216 if (Nel==0) return;
217
218 this->data = new Datum[Nel];
219 if(this->data==NULL) return;
220
221 //for(unsigned int i=0;i<Nel;i++) this->data[i]=0;
222 this->nel = Nel;
223 this->front = 0;
224}
225
226template<typename Datum>
228{
229 if(this!=&B) //Comprueba que no se esté intentando igualar un objeto a sí mismo
230 {
231 if (B.nel==0) return;
232
233 this->data = new Datum[B.nel];
234 if(this->data==NULL) return;
235
236 for(unsigned int i=0;i<B.nel;i++) this->data[i]=B.data[i];
237 this->nel = B.nel;
238 this->front = B.front;
239 }
240
241 return;
242}
243
244template<typename Datum>
246{
247 delete[] this->data;
248 this->data = NULL;
249 this->front = 0;
250 this->nel = 0;
251}
252
253template<typename Datum>
255{
256 int ID=id;
257 while(ID<0) ID=this->nel+ID;
258 return this->data[(this->front+ID)%this->nel];
259}
260
261template<typename Datum>
262bool Pds::CircBuffer<Datum>::Push(const Datum &Dat)
263{
264 if(this->nel==0) return false;
265
266 this->front = (this->nel+this->front -1) % this->nel;
267 this->data[this->front] = Dat;
268 return true;
269}
270
271
272template<typename Datum>
274{
275 if((this->nel==0)||(this->data==NULL)) return true;
276 return false;
277}
278
279template<typename Datum>
280unsigned int Pds::CircBuffer<Datum>::Nel(void) const
281{
282 if((this->nel==0)||(this->data==NULL)) return 0;
283 return this->nel;
284}
285
286#endif
287
La clase tipo Pds::CircBuffer . Esta clase genera un objeto con un buffer circular de tamaño fijo....
Definition: CircBuffer.hpp:57
unsigned int front
Definition: CircBuffer.hpp:67
unsigned int nel
Definition: CircBuffer.hpp:61
Datum & operator[](const int &id)
Lee y escribe datos en la memoria en el Pds::CircBuffer.
Definition: CircBuffer.hpp:254
CircBuffer(void)
Crea un objeto vacio de tipo Pds::CircBuffer.
Definition: CircBuffer.hpp:206
bool Push(const Datum &Dat)
Empuja un nuevo dato en la memoria Buff[0] del Pds::CircBuffer y descarta Buff[-1] Buff[N-1].
Definition: CircBuffer.hpp:262
bool IsEmpty(void) const
Retorna true si el objeto esta vacio y false si no.
Definition: CircBuffer.hpp:273
unsigned int Nel(void) const
Retorna el número de elementos del circular buffer.
Definition: CircBuffer.hpp:280
Nombre de espacio para Pds (Procesamiento Digital de Senales)
Definition: AbstractRV.hpp:42

Enlaces de interés

HomePage Bazaar Download Bug report Ayuda Developer Feed