SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
doublebuffer.h
Go to the documentation of this file.
1 // Copyright (C) 2016 Edouard Griffiths, F4EXB //
3 // //
4 // This program 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 as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program 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 V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #ifndef SDRBASE_UTIL_DOUBLEBUFFER_H_
19 #define SDRBASE_UTIL_DOUBLEBUFFER_H_
20 
21 #include <vector>
22 #include <algorithm>
23 
24 #include <QByteArray>
25 
26 #include "simpleserializer.h"
27 
28 template<typename T>
30 {
31 public:
33  {
34  m_size = 0;
35  m_current = m_data.end();
36  }
37 
39 
41  {
42  m_size = other.m_size;
43  m_data = other.m_data;
44  m_current = m_data.begin();
45  }
46 
48  {
49  if (&other == this) {
50  return *this;
51  }
52 
53  m_size = other.m_size;
54  m_data = other.m_data;
55  m_current = m_data.begin();
56  return *this;
57  }
58 
59  void resize(int size)
60  {
61  m_size = size;
62  m_data.resize(2*size);
63  m_current = m_data.begin();
64  }
65 
66  void write(const typename std::vector<T>::const_iterator& begin, const typename std::vector<T>::const_iterator& cend)
67  {
68  typename std::vector<T>::const_iterator end = cend;
69 
70  if ((end - begin) > m_size)
71  {
72  end = begin + m_size;
73  }
74 
75  int insize = end - begin;
76 
77  std::copy(begin, end, m_current);
78 
79  if (((m_current - m_data.begin()) + insize) > m_size)
80  {
81  int sizeLeft = m_size - (m_current - m_data.begin());
82  std::copy(begin, begin + sizeLeft, m_current + m_size);
83  std::copy(begin + sizeLeft, end, m_data.begin());
84  m_current = m_data.begin() + (insize - sizeLeft);
85  }
86  else
87  {
88  std::copy(begin, end, m_current + m_size);
89  m_current += insize;
90  }
91  }
92 
93  typename std::vector<T>::iterator getCurrent() const { return m_current + m_size; }
94  typename std::vector<T>::const_iterator begin() const { return m_data.begin(); }
95  typename std::vector<T>::iterator begin() { return m_data.begin(); }
96  unsigned int absoluteFill() const { return m_current - m_data.begin(); }
97  void reset() { m_current = m_data.begin(); }
98 
99  QByteArray serialize() const
100  {
101  SimpleSerializer s(1);
102 
103  QByteArray buf(reinterpret_cast<const char*>(m_data.data()), m_data.size()*sizeof(T));
104  s.writeS32(1, m_size);
105  s.writeU32(2, m_current - m_data.begin());
106  s.writeBlob(3, buf);
107 
108  return s.final();
109  }
110 
111  bool deserialize(const QByteArray& data)
112  {
113  SimpleDeserializer d(data);
114 
115  if(!d.isValid()) {
116  return false;
117  }
118 
119  if (d.getVersion() == 1)
120  {
121  unsigned int tmpUInt;
122  QByteArray buf;
123 
124  d.readS32(1, &m_size, m_data.size()/2);
125  m_data.resize(2*m_size);
126  d.readU32(2, &tmpUInt, 0);
127  m_current = m_data.begin() + tmpUInt;
128  d.readBlob(3, &buf);
129  //qDebug("DoubleBufferSimple::deserialize: m_data.size(): %u buf.size(): %d", m_data.size(), buf.size());
130  //std::copy(reinterpret_cast<char *>(m_data.data()), buf.data(), buf.data() + buf.size()); // bug
131  memcpy(reinterpret_cast<char *>(m_data.data()), buf.data(), buf.size());
132 
133  return true;
134  }
135  else
136  {
137  return false;
138  }
139  }
140 
141 private:
142  int m_size;
143  std::vector<T> m_data;
144  typename std::vector<T>::iterator m_current;
145 };
146 
147 #endif /* SDRBASE_UTIL_DOUBLEBUFFER_H_ */
void resize(int size)
Definition: doublebuffer.h:59
std::vector< T > m_data
Definition: doublebuffer.h:143
DoubleBufferSimple & operator=(const DoubleBufferSimple &other)
Definition: doublebuffer.h:47
std::vector< T >::iterator begin()
Definition: doublebuffer.h:95
void writeBlob(quint32 id, const QByteArray &value)
std::vector< T >::const_iterator begin() const
Definition: doublebuffer.h:94
bool readU32(quint32 id, quint32 *result, quint32 def=0) const
bool deserialize(const QByteArray &data)
Definition: doublebuffer.h:111
bool isValid() const
bool readS32(quint32 id, qint32 *result, qint32 def=0) const
bool readBlob(quint32 id, QByteArray *result, const QByteArray &def=QByteArray()) const
void write(const typename std::vector< T >::const_iterator &begin, const typename std::vector< T >::const_iterator &cend)
Definition: doublebuffer.h:66
DoubleBufferSimple(const DoubleBufferSimple &other)
Definition: doublebuffer.h:40
std::vector< T >::iterator m_current
Definition: doublebuffer.h:144
void writeS32(quint32 id, qint32 value)
quint32 getVersion() const
void writeU32(quint32 id, quint32 value)
unsigned int absoluteFill() const
Definition: doublebuffer.h:96
const QByteArray & final()
std::vector< T >::iterator getCurrent() const
Definition: doublebuffer.h:93
QByteArray serialize() const
Definition: doublebuffer.h:99