gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
BlockBuffer.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef BLOCKBUFFER_H_
18 #define BLOCKBUFFER_H_
19 
20 #include <queue>
21 #include <Thread.H>
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wignored-attributes"
24 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
25 #include <Eigen/Dense>
26 #pragma GCC diagnostic pop
27 
28 #define BLOCK_BUFFER_DEFAULT_COUNT 3
29 
34 class BlockBuffer {
35  std::vector<Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> > buffers;
36  std::queue<Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *> emptyBuffers;
37  std::queue<Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *> fullBuffers;
38 
41 
42  void init(int count) {
43  buffers.resize(count);
44  for (int c=0; c<count; c++)
45  emptyBuffers.push(&buffers[c]);
46  }
47 public:
51  BlockBuffer(int count) {
52  init(count);
53  }
54 
56  BlockBuffer(void) {
58  }
59 
64  Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *getEmptyBuffer(void){
65  Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *retBuf=NULL;
66  emptyBufferMutex.lock();
67  if (emptyBuffers.size()){
68  retBuf=emptyBuffers.front();
69  emptyBuffers.pop();
70  }
71  emptyBufferMutex.unLock();
72  return retBuf;
73  }
74 
79  Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *getFullBuffer(void){
80  Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *retBuf=NULL;
81  fullBufferMutex.lock();
82  if (fullBuffers.size()){
83  retBuf=fullBuffers.front();
84  fullBuffers.pop();
85  }
86  fullBufferMutex.unLock();
87  return retBuf;
88  }
89 
93  void putFullBuffer(Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *fb){
94  fullBufferMutex.lock();
95  fullBuffers.push(fb);
96  fullBufferMutex.unLock();
97  }
98 
102  void putEmptyBuffer(Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *eb){
103  emptyBufferMutex.lock();
104  emptyBuffers.push(eb);
105  emptyBufferMutex.unLock();
106  }
107 
112  return buffers.size();
113  }
114 
119  void resizeBuffers(int rows, int cols){
120  emptyBufferMutex.lock();
121  fullBufferMutex.lock();
122  for (int i=0; i<buffers.size(); i++)
123  buffers[i].resize(rows, cols);
124  emptyBufferMutex.unLock();
125  fullBufferMutex.unLock();
126  }
127 
133  void resize(int count){
134  buffers.resize(count);
135  int rows=0, cols=0;
136  // find the current matrix sizes and mimick - try either a full or empty buffer. All should be the same.
137  Eigen::Array<unsigned short, Eigen::Dynamic, Eigen::Dynamic> *b=getFullBuffer();
138  if (!b)
139  b=getEmptyBuffer();
140  if (b) {
141  rows=b->rows();
142  cols=b->cols();
143  }
144 
145  // remove any existing pointers
146  while ((b=getFullBuffer())!=NULL);
147  while ((b=getEmptyBuffer())!=NULL);
148 
149  // create all of the buffers and set them up as empty.
150  emptyBufferMutex.lock();
151  fullBufferMutex.lock();
152  init(count);
153  emptyBufferMutex.unLock();
154  fullBufferMutex.unLock();
155  resizeBuffers(rows, cols);
156  }
157 
158 // IF you plan to use a class which inherits from a stl container, then this will conflict, leaving out for now.
159 // /** Return a pointer to one of the buffers in the buffer vector which contains all buffers in constant disorder.
160 // \param i The i'th buffer to index from the buffer vector (0 <= i < getBufferCount()).
161 // \return A pointer to the i'th buffer or NULL on failure.
162 // */
163 // Eigen::DenseBase<Derived> *operator[](int i){
164 // if (i<buffers.size() & i>=0)
165 // return &buffers[i];
166 // return NULL;
167 // }
168 };
169 
170 #endif // BLOCKBUFFER_H_
void putFullBuffer(Eigen::Array< unsigned short, Eigen::Dynamic, Eigen::Dynamic > *fb)
Definition: BlockBuffer.H:93
Mutex emptyBufferMutex
Used for.
Definition: BlockBuffer.H:40
std::queue< Eigen::Array< unsigned short, Eigen::Dynamic, Eigen::Dynamic > * > fullBuffers
The full buffer queue.
Definition: BlockBuffer.H:37
std::queue< Eigen::Array< unsigned short, Eigen::Dynamic, Eigen::Dynamic > * > emptyBuffers
The empty buffer queue.
Definition: BlockBuffer.H:36
Definition: Thread.H:271
std::vector< Eigen::Array< unsigned short, Eigen::Dynamic, Eigen::Dynamic > > buffers
The vector of buffers.
Definition: BlockBuffer.H:35
BlockBuffer(int count)
Definition: BlockBuffer.H:51
void init(int count)
Definition: BlockBuffer.H:42
int getBufferCount()
Definition: BlockBuffer.H:111
int unLock()
Definition: Thread.H:325
void resizeBuffers(int rows, int cols)
Definition: BlockBuffer.H:119
BlockBuffer(void)
Constructor - creates BLOCK_BUFFER_DEFAULT_COUNT buffers.
Definition: BlockBuffer.H:56
Mutex fullBufferMutex
Used for.
Definition: BlockBuffer.H:39
void resize(int count)
Definition: BlockBuffer.H:133
#define BLOCK_BUFFER_DEFAULT_COUNT
Definition: BlockBuffer.H:28
void putEmptyBuffer(Eigen::Array< unsigned short, Eigen::Dynamic, Eigen::Dynamic > *eb)
Definition: BlockBuffer.H:102
int lock()
Definition: Thread.H:295
Eigen::Array< unsigned short, Eigen::Dynamic, Eigen::Dynamic > * getEmptyBuffer(void)
Definition: BlockBuffer.H:64
Eigen::Array< unsigned short, Eigen::Dynamic, Eigen::Dynamic > * getFullBuffer(void)
Definition: BlockBuffer.H:79
gtkIOStream: /tmp/gtkiostream/include/BlockBuffer.H Source File
GTK+ IOStream  Beta