Simplify RunLoop

Instead of transforming between return value and callback, pass
the wrapped callback on to the invoked function as the last argument.

This eliminates the need for multiple invokeWithArgument overloads
and permits invoked functions to be asynchronous.
This commit is contained in:
John Firebaugh
2015-06-11 19:03:19 -04:00
parent 3ab682e627
commit fc64e97bce
8 changed files with 149 additions and 216 deletions

View File

@@ -20,25 +20,30 @@ public:
util::ThreadContext::setFileSource(fs);
}
bool parseRasterTile(RasterBucket* bucket, std::string data) {
return bucket->setImage(data);
void parseRasterTile(RasterBucket* bucket, std::string data, std::function<void (bool)> callback) {
callback(bucket->setImage(data));
}
TileParseResult parseVectorTile(TileWorker* worker, std::string data) {
void parseVectorTile(TileWorker* worker, std::string data, std::function<void (TileParseResult)> callback) {
try {
pbf tilePBF(reinterpret_cast<const unsigned char *>(data.data()), data.size());
return worker->parse(VectorTile(tilePBF));
callback(worker->parse(VectorTile(tilePBF)));
} catch (const std::exception& ex) {
return TileParseResult(ex.what());
callback(TileParseResult(ex.what()));
}
}
TileParseResult parseLiveTile(TileWorker* worker, const LiveTile* tile) {
return worker->parse(*tile);
void parseLiveTile(TileWorker* worker, const LiveTile* tile, std::function<void (TileParseResult)> callback) {
try {
callback(worker->parse(*tile));
} catch (const std::exception& ex) {
callback(TileParseResult(ex.what()));
}
}
void redoPlacement(TileWorker* worker, float angle, bool collisionDebug) {
void redoPlacement(TileWorker* worker, float angle, bool collisionDebug, std::function<void ()> callback) {
worker->redoPlacement(angle, collisionDebug);
callback();
}
};
@@ -53,22 +58,22 @@ Worker::~Worker() = default;
std::unique_ptr<WorkRequest> Worker::parseRasterTile(RasterBucket& bucket, std::string data, std::function<void (bool)> callback) {
current = (current + 1) % threads.size();
return threads[current]->invokeWithResult(&Worker::Impl::parseRasterTile, callback, &bucket, data);
return threads[current]->invokeWithCallback(&Worker::Impl::parseRasterTile, callback, &bucket, data);
}
std::unique_ptr<WorkRequest> Worker::parseVectorTile(TileWorker& worker, std::string data, std::function<void (TileParseResult)> callback) {
current = (current + 1) % threads.size();
return threads[current]->invokeWithResult(&Worker::Impl::parseVectorTile, callback, &worker, data);
return threads[current]->invokeWithCallback(&Worker::Impl::parseVectorTile, callback, &worker, data);
}
std::unique_ptr<WorkRequest> Worker::parseLiveTile(TileWorker& worker, const LiveTile& tile, std::function<void (TileParseResult)> callback) {
current = (current + 1) % threads.size();
return threads[current]->invokeWithResult(&Worker::Impl::parseLiveTile, callback, &worker, &tile);
return threads[current]->invokeWithCallback(&Worker::Impl::parseLiveTile, callback, &worker, &tile);
}
std::unique_ptr<WorkRequest> Worker::redoPlacement(TileWorker& worker, float angle, bool collisionDebug, std::function<void ()> callback) {
current = (current + 1) % threads.size();
return threads[current]->invokeWithResult(&Worker::Impl::redoPlacement, callback, &worker, angle, collisionDebug);
return threads[current]->invokeWithCallback(&Worker::Impl::redoPlacement, callback, &worker, angle, collisionDebug);
}
} // end namespace mbgl