开发者

Array of templated type in C++

I have the following :

QFutureWatcher<bool> *procwatcher;
procwatcher = new QFutureWatcher<bool>();
QFuture<bool> procfuture = QtConcurrent::run(this, &EraserBatch::processTile);
procwatcher->setFuture(procfuture);

  QFutureWatcher<bool> *procwatcher2;
  procwatcher2 = new QFutureWatcher<bool>();
  QFuture<bool> procfuture2 = QtConcurrent::run(this, &EraserBatch::processTile);
  procwatcher2->setFuture(procfuture2);

What is the syntax for creating a dynamically sized array of these two types - QFutureWatcher and QFuture so that I can say procwatcher[0] and procfuture[1], et开发者_Python百科c.

Thanks!


As long as the template is fully specialized (i.e. all template parameters specified), then you can simply do this:

#include <vector> // required for std::vector

std::vector<QFutureWatcher<bool>*> procWatchers;

Although according to how QFutureWatcher is used in these documentation examples, you probably want to store QFutureWatcher instances in the std::vector instead:

std::vector<QFutureWatcher<bool> > procWatchers;

That way you won't have to manually new and delete the QFutureWatcher instances.

Apparently QFutureWatcher inherits from QObject, which is uncopyable. That prevents std::vector<QFutureWatcher<bool> > from working.


You have this:

QFutureWatcher<bool> *procwatcher;
procwatcher = new QFutureWatcher<bool>();
QFuture<bool> procfuture = QtConcurrent::run(this, &EraserBatch::processTile);
procwatcher->setFuture(procfuture);

QFutureWatcher<bool> *procwatcher2;
procwatcher2 = new QFutureWatcher<bool>();
QFuture<bool> procfuture2 = QtConcurrent::run(this, &EraserBatch::processTile);
procwatcher2->setFuture(procfuture2);

You can do something like this:

// Not tested!

// Bundle QFutureWatcher and QFuture together.
template<typename T>
struct FutureStruct
{
    FutureStruct(QFutureWatcher<T>* w, const QFuture<T>& f)
        : watcher(w), future(f)
    {
        this->watcher->setFuture(this->future);
    }

    QFutureWatcher<T>* watcher; // Apparently QObjects can't be copied.
    QFuture<T> future;
};

// ...

std::vector< FutureStruct<bool> > futures;

// ...

void AddFuture()
{
    futures.push_back(FutureStruct<bool>(new QFutureWatcher<bool>(),
        QtConcurrent::run(this, &EraserBatch::processTile)));
}

// ...

futures[0].watcher; // gets you the first QFutureWatcher<bool>*
futures[0].future;  // gets you the first QFuture<bool>

futures[1].watcher; // gets you the second QFutureWatcher<bool>*
futures[1].future;  // gets you the second QFuture<bool>

// ...

Of course, because the QFutureWatcher<bool> was allocated with new, you need to delete it before the futures vector goes away:

for(std::vector< FutureStruct<bool> >::iterator i = futures.begin();
    i != futures.end(); ++i)
{
    delete i->watcher;
}


RAII way if watchers are owned not only by the vector:

typedef boost::shared_ptr<QFutureWatcher<bool> > ProcWatcherPtr;
std::vector<ProcWatcherPtr> procWatchers;

RAII way if watchers are owned only by the vector:

typedef QFutureWatcher<bool> ProcWatcher
boost::ptr_vector<ProcWatcher> procWatchers;

or without memory allocation if it fits your needs:

std::vector<QFutureWatcher<bool> > procWatchers;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜