TDiscreteDataSequence.h
Go to the documentation of this file.
1 #ifndef imeas_TDiscreteDataSequence_included
2 #define imeas_TDiscreteDataSequence_included
3 
4 
5 // STL incldues
6 #include <cstring>
7 
8 // Qt includes
9 #include <QtCore/qmath.h>
10 
11 // ACF includes
12 #include "istd/TChangeNotifier.h"
13 #include "istd/TOptDelPtr.h"
14 #include "istd/TSmartPtr.h"
15 #include "iser/IArchive.h"
16 #include "iser/CArchiveTag.h"
17 
19 
20 
21 namespace imeas
22 {
23 
24 
25 template <typename Element>
27 {
28 public:
30 
31  // reimplemented (imeas::IDiscreteDataSequence)
32  virtual bool CreateDiscreteSequence(
33  int samplesCount,
34  void* dataPtr,
35  bool releaseFlag,
36  int sampleDiff,
37  int channelDiff,
38  int sampleDepth,
39  int channelsCount = 1);
40  virtual bool CreateDiscreteSequenceWithInfo(
42  int samplesCount,
43  void* dataPtr,
44  bool releaseFlag,
45  int sampleDiff,
46  int channelDiff,
47  int sampleDepth,
48  int channelsCount = 1);
49 
50  virtual int GetSampleDepth() const;
51  virtual quint32 GetDiscreteSample(int position, int channel = 0) const;
52  virtual bool SetDiscreteSample(int position, int channel, quint32 sample);
53 
54  // reimplemented (imeas::IDataSequence)
55  virtual bool CreateSequence(int samplesCount, int channelsCount = 1);
56  virtual bool CreateSequenceWithInfo(
58  int samplesCount = -1,
59  int channelsCount = -1);
60  virtual const IDataSequenceInfo* GetSequenceInfo() const;
61  virtual bool IsEmpty() const;
62  virtual void ResetSequence();
63  virtual int GetSamplesCount() const;
64  virtual int GetChannelsCount() const;
65  virtual double GetSample(int index, int channel = 0) const;
66  virtual void SetSample(int index, int channel, double value);
67 
68  // reimplemented (iser::ISerializable)
69  virtual bool Serialize(iser::IArchive& archive);
70 
71  // reimplemented (istd::IChangeable)
72  virtual bool CopyFrom(const istd::IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS);
73 
74 private:
75  istd::TOptDelPtr<Element, true> m_sampleBuffer;
76 
77  int m_allocatedElementsCount;
78  int m_samplesCount;
79  int m_channelsCount;
80  int m_sampleDiff;
81  int m_channelDiff;
82  double m_normFactor;
83 
85 };
86 
87 
88 // public methods
89 
90 template <typename Element>
92 : m_allocatedElementsCount(0),
93  m_samplesCount(0),
94  m_channelsCount(0),
95  m_sampleDiff(0),
96  m_channelDiff(0)
97 {
98  m_normFactor = qPow(2.0, double(sizeof(Element) * 8)) - 1;
99 }
100 
101 
102 // reimplemented (imeas::IDiscreteDataSequence)
103 
104 template <typename Element>
106  int samplesCount,
107  void* dataPtr,
108  bool releaseFlag,
109  int sampleDiff,
110  int channelDiff,
111  int sampleDepth,
112  int channelsCount)
113 {
114  if (sampleDepth != sizeof(Element) * 8){
115  return false;
116  }
117 
118  m_sampleBuffer.SetPtr((Element*)dataPtr, releaseFlag);
119 
120  m_allocatedElementsCount = 0;
121  m_samplesCount = samplesCount;
122  m_channelsCount = channelsCount;
123  m_sampleDiff = (sampleDiff != 0) ? sampleDiff : int(channelsCount * sizeof(Element));
124  m_channelDiff = (channelDiff != 0) ? channelDiff: sizeof(Element);
125 
126  return true;
127 }
128 
129 
130 template <typename Element>
133  int samplesCount,
134  void* dataPtr,
135  bool releaseFlag,
136  int sampleDiff,
137  int channelDiff,
138  int sampleDepth,
139  int channelsCount)
140 {
141  m_sequenceInfoPtr = infoPtr;
142 
143  return CreateDiscreteSequence(samplesCount, dataPtr, releaseFlag, sampleDiff, channelDiff, sampleDepth, channelsCount);
144 }
145 
146 
147 template <typename Element>
149 {
150  return sizeof(Element) * 8;
151 }
152 
153 
154 template <typename Element>
155 quint32 TDiscreteDataSequence<Element>::GetDiscreteSample(int position, int channel) const
156 {
157  const Element& element = *(const Element*)((const quint8*)m_sampleBuffer.GetPtr() + position * m_sampleDiff + channel * m_channelDiff);
158 
159  return element;
160 }
161 
162 
163 template <typename Element>
164 bool TDiscreteDataSequence<Element>::SetDiscreteSample(int position, int channel, quint32 sample)
165 {
166  Element& element = *(Element*)((quint8*)m_sampleBuffer.GetPtr() + position * m_sampleDiff + channel * m_channelDiff);
167 
168  element = Element(sample);
169 
170  return true;
171 }
172 
173 
174 // reimplemented (imeas::IDataSequence)
175 
176 template <typename Element>
177 bool TDiscreteDataSequence<Element>::CreateSequence(int samplesCount, int channelsCount)
178 {
179  if (m_sequenceInfoPtr.IsValid()){
180  int flags = m_sequenceInfoPtr->GetSequenceInfoFlags();
181 
182  if (samplesCount < 0){
183  samplesCount = m_sequenceInfoPtr->GetDefaultSamplesCount();
184  }
185  else if (((flags & IDataSequenceInfo::SIF_SAMPLES_COUNT_FIXED) != 0) && (samplesCount != m_sequenceInfoPtr->GetDefaultSamplesCount())){
186  return false;
187  }
188 
189  if (channelsCount < 0){
190  channelsCount = m_sequenceInfoPtr->GetDefaultChannelsCount();
191  }
192  else if (((flags & IDataSequenceInfo::SIF_CHANNELS_COUNT_FIXED) != 0) && (channelsCount != m_sequenceInfoPtr->GetDefaultChannelsCount())){
193  return false;
194  }
195  }
196 
197  if ((samplesCount < 0) || (channelsCount < 0)){
198  return false;
199  }
200 
201  int elementsCount = samplesCount * channelsCount;
202  if ( (elementsCount > m_allocatedElementsCount) ||
203  (elementsCount * 2 < m_allocatedElementsCount)){
204  m_sampleBuffer.SetPtr(new Element[elementsCount], true);
205  m_allocatedElementsCount = elementsCount;
206  }
207 
208  m_samplesCount = samplesCount;
209  m_channelsCount = channelsCount;
210  m_sampleDiff = channelsCount * sizeof(Element);
211  m_channelDiff = sizeof(Element);
212 
213  return true;
214 }
215 
216 
217 template <typename Element>
220  int samplesCount,
221  int channelsCount)
222 {
223  m_sequenceInfoPtr = infoPtr;
224 
225  return CreateSequence(samplesCount, channelsCount);
226 }
227 
228 
229 template <typename Element>
231 {
232  return m_sequenceInfoPtr.GetPtr();
233 }
234 
235 
236 template <typename Element>
238 {
239  return m_samplesCount <= 0;
240 }
241 
242 
243 template <typename Element>
245 {
246  m_sampleBuffer.Reset();
247  m_samplesCount = 0;
248  m_channelsCount = 0;
249 }
250 
251 
252 template <typename Element>
254 {
255  return m_samplesCount;
256 }
257 
258 
259 template <typename Element>
261 {
262  return m_channelsCount;
263 }
264 
265 
266 template <typename Element>
267 double TDiscreteDataSequence<Element>::GetSample(int index, int channel) const
268 {
269  const Element& element = *(const Element*)((const quint8*)m_sampleBuffer.GetPtr() + index * m_sampleDiff + channel * m_channelDiff);
270 
271  return double(element) / m_normFactor;
272 }
273 
274 
275 template <typename Element>
276 void TDiscreteDataSequence<Element>::SetSample(int index, int channel, double value)
277 {
278  Element& element = *(Element*)((quint8*)m_sampleBuffer.GetPtr() + index * m_sampleDiff + channel * m_channelDiff);
279 
280  element = Element(value * m_normFactor - I_BIG_EPSILON);
281 }
282 
283 
284 // reimplemented (iser::ISerializable)
285 
286 template <typename Element>
288 {
289  bool retVal = true;
290 
291  int channelsCount = GetChannelsCount();
292  static iser::CArchiveTag channelsCountTag("ChannelsCount", "Number of channels");
293  retVal = retVal && archive.BeginTag(channelsCountTag);
294  retVal = retVal && archive.Process(channelsCount);
295  retVal = retVal && archive.EndTag(channelsCountTag);
296 
297  int samplesCount = GetSamplesCount();
298 
299  static iser::CArchiveTag samplesListTag("SampleList", "List of sample values");
300  static iser::CArchiveTag channelsTag("Channels", "List of sample values");
301  retVal = retVal && archive.BeginMultiTag(samplesListTag, channelsTag, samplesCount);
302 
303  if (!retVal){
304  return false;
305  }
306 
307  bool isStoring = archive.IsStoring();
308 
309  istd::CChangeNotifier notifier(NULL);
310 
311  if (isStoring){
312  for (int i = 0; i < samplesCount; ++i){
313  retVal = retVal && archive.BeginTag(channelsTag);
314  for (int channelIndex = 0; channelIndex < channelsCount; ++channelIndex){
315  Element sample = Element(GetDiscreteSample(i, channelIndex));
316 
317  retVal = retVal && archive.Process(sample);
318  }
319  retVal = retVal && archive.EndTag(channelsTag);
320  }
321  }
322  else{
323  notifier.SetPtr(this);
324  if ((samplesCount < 0) || (channelsCount < 0) || !CreateSequence(samplesCount, channelsCount)){
325  return false;
326  }
327 
328  for (int i = 0; i < samplesCount; ++i){
329  retVal = retVal && archive.BeginTag(channelsTag);
330  for (int channelIndex = 0; channelIndex < channelsCount; ++channelIndex){
331  Element sample = 0;
332 
333  retVal = retVal && archive.Process(sample);
334 
335  SetDiscreteSample(i, channelIndex, sample);
336  }
337  retVal = retVal && archive.EndTag(channelsTag);
338  }
339  }
340 
341  retVal = retVal && archive.EndTag(samplesListTag);
342 
343  return retVal;
344 }
345 
346 
347 // reimplemented (istd::IChangeable)
348 
349 template <typename Element>
350 bool TDiscreteDataSequence<Element>::CopyFrom(const istd::IChangeable& object, CompatibilityMode /*mode*/)
351 {
352  const IDataSequence* sequencePtr = dynamic_cast<const IDataSequence*>(&object);
353  if (sequencePtr != NULL){
354  const TDiscreteDataSequence<Element>* nativeSequencePtr = dynamic_cast<const TDiscreteDataSequence<Element>*>(sequencePtr);
355  if ( (nativeSequencePtr != NULL) &&
356  (nativeSequencePtr->m_channelDiff == sizeof(Element)) &&
357  (nativeSequencePtr->m_sampleDiff == int(nativeSequencePtr->m_channelsCount * sizeof(Element)))){
358  m_samplesCount = nativeSequencePtr->m_samplesCount;
359  m_channelsCount = nativeSequencePtr->m_channelsCount;
360  m_sampleDiff = sizeof(Element) * m_channelsCount;
361  m_channelDiff = sizeof(Element);
362 
363  m_allocatedElementsCount = 0;
364  m_allocatedElementsCount = m_samplesCount * m_channelsCount;
365  if (m_allocatedElementsCount > 0){
366  m_sampleBuffer.SetPtr(new Element[m_allocatedElementsCount], true);
367  if (m_sampleBuffer.IsValid()){
368  std::memcpy( m_sampleBuffer.GetPtr(),
369  nativeSequencePtr->m_sampleBuffer.GetPtr(),
370  m_allocatedElementsCount * sizeof(Element));
371  }
372  else{
373  ResetSequence();
374 
375  return false;
376  }
377  }
378  else{
379  m_sampleBuffer.Reset();
380  }
381 
382  }
383  else{
384  int samplesCount = sequencePtr->GetSamplesCount();
385  int channelsCount = sequencePtr->GetChannelsCount();
386 
387  Q_ASSERT(samplesCount >= 0);
388  Q_ASSERT(channelsCount >= 0);
389 
390  if (!CreateSequence(samplesCount, channelsCount)){
391  return false;
392  }
393 
394  for (int sampleIndex = 0; sampleIndex < samplesCount; ++sampleIndex){
395  for (int channelIndex = 0; channelIndex < channelsCount; ++channelIndex){
396  double sample = sequencePtr->GetSample(sampleIndex, channelIndex);
397 
398  SetSample(sampleIndex, channelIndex, sample);
399  }
400  }
401  }
402 
403  return true;
404  }
405 
406  return false;
407 }
408 
409 
414 
415 
416 } // namespace imeas
417 
418 
419 #endif // !imeas_TDiscreteDataSequence_included
420 
421 
TDiscreteDataSequence< quint8 > CSimpleSamplesSequence8
virtual int GetChannelsCount() const =0
Get number of channels.
TDiscreteDataSequence< quint32 > CSimpleSamplesSequence32
virtual int GetSamplesCount() const =0
Get size of this raster sequence.
virtual bool CreateSequence(int samplesCount, int channelsCount=1)
Create container for sample sequence with specified number of samples, channels and sample depth...
virtual int GetSampleDepth() const
Get number of bits used to represent single sample.
TDiscreteDataSequence< quint16 > CSimpleSamplesSequence16
virtual double GetSample(int index, int channel=0) const
Get sample value at specified index.
virtual bool CreateDiscreteSequence(int samplesCount, void *dataPtr, bool releaseFlag, int sampleDiff, int channelDiff, int sampleDepth, int channelsCount=1)
Create container for sample sequence using external samples buffer.
virtual bool CopyFrom(const istd::IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS)
virtual const IDataSequenceInfo * GetSequenceInfo() const
Get additional information about this sequence.
virtual void SetSample(int index, int channel, double value)
Set sample value at specified index.
virtual double GetSample(int index, int channel=0) const =0
Get sample value at specified index.
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
General definition of sequence contains discrete sample values in regular time grid.
virtual bool SetDiscreteSample(int position, int channel, quint32 sample)
Set discrete sample at specified position.
virtual bool Serialize(iser::IArchive &archive)
virtual bool IsEmpty() const
Return true if this sequence has no sample.
Stores additional data sequence data used to interpret samples value correctly.
#define NULL
virtual bool CreateDiscreteSequenceWithInfo(const istd::TTransPtr< const IDataSequenceInfo > &infoPtr, int samplesCount, void *dataPtr, bool releaseFlag, int sampleDiff, int channelDiff, int sampleDepth, int channelsCount=1)
Create container for sample sequence with additional sequence info using external samples buffer...
TDiscreteDataSequence< quint64 > CSimpleSamplesSequence64
Number of channels is fixed by this info object.
General definition of sequence contains samples in regular time grid.
Definition: IDataSequence.h:20
virtual void ResetSequence()
Reset this sequence.
virtual int GetSamplesCount() const
Get size of this raster sequence.
Number of samples is fixed by this info object.
virtual quint32 GetDiscreteSample(int position, int channel=0) const
Get discrete sample at specified position.
virtual int GetChannelsCount() const
Get number of channels.
virtual bool CreateSequenceWithInfo(const istd::TTransPtr< const IDataSequenceInfo > &infoPtr, int samplesCount=-1, int channelsCount=-1)
Create sequence and set the info object.
void SetPtr(Changeable *changeablePtr)

© 2007-2011 Witold Gantzke and Kirill Lepskiy