LRAUV  revA
RingBuffer.h
Go to the documentation of this file.
1 
9 #ifndef RINGBUFFER_H_
10 #define RINGBUFFER_H_
11 
12 #include <cstdlib>
13 #include <cstdio>
14 
15 #include "ListEntry.h"
16 #include "Mutex.h"
17 
37 {
38 public:
39  RingBufferVoid( const bool freeOnCleanup = true );
40 
41  virtual ~RingBufferVoid();
42 
43  bool isEmpty();
44 
45  void freeAll();
46 
47  void push( void* item );
48 
49  void* pop();
50 
51  void* peek();
52 
53 protected:
56  const bool freeOnCleanup_;
58 
59 private:
60  // Note that the copy constructor below is private and not given a body.
61  // Any attempt to call it will return a compiler error.
62  RingBufferVoid( const RingBufferVoid& old ); // disallow copy constructor
63 
64 };
65 
71 template< typename T >
72 class RingBuffer : public RingBufferVoid
73 {
74 public:
75  RingBuffer( const bool deleteOnCleanup = true )
76  : RingBufferVoid( deleteOnCleanup )
77  {}
78  ;
79 
80  virtual ~RingBuffer()
81  {
82  deleteAll();
83  };
84 
85  void deleteAll()
86  {
87  while( freeOnCleanup_ && !isEmpty() )
88  {
89  T item( pop() );
90  if( NULL != item )
91  {
92  delete item;
93  }
94  }
95  };
96 
97  void push( T item )
98  {
99  RingBufferVoid::push( ( void* )item );
100  };
101 
102  T pop()
103  {
104  return ( T )RingBufferVoid::pop();
105  };
106 
107  T peek()
108  {
109  return ( T )RingBufferVoid::peek();
110  };
111 };
112 
113 #endif /*RINGBUFFER_H_*/
virtual ~RingBuffer()
Definition: RingBuffer.h:80
virtual ~RingBufferVoid()
Definition: RingBuffer.cpp:17
T peek()
Definition: RingBuffer.h:107
Contains the ListEntry class declaration.
void deleteAll()
Definition: RingBuffer.h:85
ListEntry< void * > * end_
Definition: RingBuffer.h:55
RingBufferVoid(const bool freeOnCleanup=true)
Definition: RingBuffer.cpp:11
A simple pthread Mutex abstraction.
Definition: Mutex.h:20
void freeAll()
Definition: RingBuffer.cpp:37
RingBuffer(const bool deleteOnCleanup=true)
Definition: RingBuffer.h:75
bool isEmpty()
Definition: RingBuffer.cpp:32
void push(void *item)
Definition: RingBuffer.cpp:49
void * pop()
Definition: RingBuffer.cpp:69
void * peek()
Definition: RingBuffer.cpp:88
void push(T item)
Definition: RingBuffer.h:97
Contains the Mutex, ThreadCondition, and MutexLocker class declarations.
ListEntry< void * > * start_
Definition: RingBuffer.h:54
const bool freeOnCleanup_
Definition: RingBuffer.h:56
Thread-safe ring buffer for storing class pointers.
Definition: RingBuffer.h:72
Mutex mutex_
Definition: RingBuffer.h:57
Thread-safe ring buffer for storing void pointers.
Definition: RingBuffer.h:36
T pop()
Definition: RingBuffer.h:102