DABC (Data Acquisition Backbone Core)  2.9.9
BuffersQueue.h
Go to the documentation of this file.
1 // $Id: BuffersQueue.h 4210 2019-02-12 19:55:03Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_BuffersQueue
17 #define DABC_BuffersQueue
18 
19 #ifndef DABC_Buffer
20 #include "dabc/Buffer.h"
21 #endif
22 
23 #ifndef DABC_Queue
24 #include "dabc/Queue.h"
25 #endif
26 
27 #include <vector>
28 
29 namespace dabc {
30 
39  class BuffersQueue {
40 
41  protected:
42  std::vector<dabc::Buffer> vect;
43 
44  unsigned front;
45  unsigned tail;
46  unsigned size;
47 
48  public:
49  BuffersQueue(unsigned capacity) :
50  vect(),
51  front(0),
52  tail(0),
53  size(0)
54  {
55  for (unsigned n=0;n<capacity;n++)
56  vect.push_back(dabc::Buffer());
57  }
58 
59  virtual ~BuffersQueue() { Cleanup(); }
60 
61  bool PushBuffer(Buffer& buf)
62  {
63  if (size == vect.size()) return false;
64  vect[tail] << buf;
65  size++;
66  tail = (tail+1) % vect.size();
67  return true;
68  }
69 
70  bool PopBuffer(Buffer& buf)
71  {
72  if (size == 0) return false;
73  buf << vect[front];
74  size--;
75  front = (front+1) % vect.size();
76  return true;
77  }
78 
79  unsigned Size() const { return size; }
80 
81  unsigned Capacity() const { return vect.size(); }
82 
83  bool Full() const { return Size() == Capacity(); }
84 
85  bool Empty() const { return size == 0; }
86 
87  void Cleanup();
88 
89  void Cleanup(Mutex* m);
90 
93  Buffer Item(unsigned n) const
94  {
95  return vect[(n+front) % vect.size()];
96  }
97 
99  {
100  BufferSize_t sum = 0;
101  for (unsigned n=0;n<Size();++n)
102  sum += vect[(n+front) % vect.size()].GetTotalSize();
103  return sum;
104  }
105 
106  };
107 
108 }
109 
110 
111 #endif
Reference on memory from memory pool.
Definition: Buffer.h:135
Queue of buffers
Definition: BuffersQueue.h:39
BuffersQueue(unsigned capacity)
Definition: BuffersQueue.h:49
bool PushBuffer(Buffer &buf)
Definition: BuffersQueue.h:61
std::vector< dabc::Buffer > vect
Definition: BuffersQueue.h:42
bool Empty() const
Definition: BuffersQueue.h:85
virtual ~BuffersQueue()
Definition: BuffersQueue.h:59
BufferSize_t TotalBuffersSize() const
Definition: BuffersQueue.h:98
bool PopBuffer(Buffer &buf)
Definition: BuffersQueue.h:70
unsigned Size() const
Definition: BuffersQueue.h:79
unsigned Capacity() const
Definition: BuffersQueue.h:81
Buffer Item(unsigned n) const
Returns reference on the Buffer in the queue, one can create any kind of buffer copies from it.
Definition: BuffersQueue.h:93
bool Full() const
Definition: BuffersQueue.h:83
posix pthread mutex
Definition: threads.h:61
Event manipulation API.
Definition: api.h:23
uint32_t BufferSize_t
Definition: Buffer.h:32