kwave  18.07.70
MultiTrackSource.h
Go to the documentation of this file.
1 /*************************************************************************
2  MultiTrackSource.h - template for multi-track sources
3  -------------------
4  begin : Sat Oct 20 2007
5  copyright : (C) 2007 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef MULTI_TRACK_SOURCE_H
19 #define MULTI_TRACK_SOURCE_H
20 
21 #include "config.h"
22 
23 #include <new>
24 
25 #include <QFutureSynchronizer>
26 #include <QList>
27 #include <QObject>
28 #include <QtConcurrentRun>
29 
30 #include "libkwave/SampleSource.h"
31 
32 namespace Kwave
33 {
34 
39  template <class SOURCE, const bool INITIALIZE>
40  class Q_DECL_EXPORT MultiTrackSource: public Kwave::SampleSource,
41  private QList<SOURCE *>
42  {
43  public:
53  MultiTrackSource(unsigned int tracks, QObject *parent = Q_NULLPTR)
54  :Kwave::SampleSource(parent),
55  QList<SOURCE *>()
56  {
57  Q_UNUSED(tracks);
58  Q_ASSERT(INITIALIZE || (tracks == 0));
59  Q_ASSERT(QList<SOURCE *>::size() == static_cast<int>(tracks));
60  }
61 
63  virtual ~MultiTrackSource() Q_DECL_OVERRIDE
64  {
65  clear();
66  }
67 
72  virtual void goOn() Q_DECL_OVERRIDE
73  {
74  QFutureSynchronizer<void> synchronizer;
75 
76  foreach (SOURCE *src, static_cast< QList<SOURCE *> >(*this)) {
77  if (!src) continue;
78  synchronizer.addFuture(QtConcurrent::run(
79  this,
81  src)
82  );
83  }
84 
85  synchronizer.waitForFinished();
86  }
87 
89  virtual bool done() const Q_DECL_OVERRIDE
90  {
91  foreach (SOURCE *src, static_cast< QList<SOURCE *> >(*this))
92  if (src && !src->done()) return false;
93  return true;
94  }
95 
100  virtual unsigned int tracks() const Q_DECL_OVERRIDE
101  {
102  return QList<SOURCE *>::size();
103  }
104 
110  inline virtual SOURCE *at(unsigned int track) const {
111  return QList<SOURCE *>::at(track);
112  }
113 
115  inline virtual SOURCE * operator [] (unsigned int track)
116  Q_DECL_OVERRIDE
117  {
118  return at(track);
119  }
120 
128  inline virtual bool insert(unsigned int track, SOURCE *source) {
129  QList<SOURCE *>::insert(track, source);
130  return (at(track) == source);
131  }
132 
134  inline virtual void clear() {
135  while (!QList<SOURCE *>::isEmpty())
136  delete QList<SOURCE *>::takeLast();
137  }
138 
139  private:
140 
142  void runSource(SOURCE *src) {
143  src->goOn();
144  }
145 
146  };
147 
152  template <class SOURCE>
153  class Q_DECL_EXPORT MultiTrackSource<SOURCE, true>
154  :public Kwave::MultiTrackSource<SOURCE, false>
155  {
156  public:
163  MultiTrackSource(unsigned int tracks,
164  QObject *parent = Q_NULLPTR)
165  :Kwave::MultiTrackSource<SOURCE, false>(0, parent)
166  {
167  for (unsigned int i = 0; i < tracks; i++)
168  this->insert(i, new(std::nothrow) SOURCE());
169  }
170 
172  virtual ~MultiTrackSource() { }
173  };
174 
175 }
176 
177 #endif /* MULTI_TRACK_SOURCE_H */
178 
179 //***************************************************************************
180 //***************************************************************************
MultiTrackSource(unsigned int tracks, QObject *parent=Q_NULLPTR)
Definition: App.h:33
virtual bool done() const Q_DECL_OVERRIDE
virtual unsigned int tracks() const Q_DECL_OVERRIDE
MultiTrackSource(unsigned int tracks, QObject *parent=Q_NULLPTR)
virtual void goOn() Q_DECL_OVERRIDE
virtual bool insert(unsigned int track, SOURCE *source)
virtual SOURCE * at(unsigned int track) const
virtual ~MultiTrackSource() Q_DECL_OVERRIDE
void runSource(SOURCE *src)