Pass FileSource by reference

This commit is contained in:
John Firebaugh
2014-11-20 17:13:35 -08:00
parent a9b20fc09a
commit 9e971095d3
5 changed files with 16 additions and 19 deletions

View File

@@ -34,11 +34,11 @@ public:
class Sprite : public std::enable_shared_from_this<Sprite>, private util::noncopyable {
private:
struct Key {};
void load(const util::ptr<FileSource> &fileSource);
void load(FileSource& fileSource);
public:
Sprite(const Key &, const std::string& base_url, float pixelRatio);
static util::ptr<Sprite> Create(const std::string& base_url, float pixelRatio, const util::ptr<FileSource> &fileSource);
static util::ptr<Sprite> Create(const std::string& base_url, float pixelRatio, FileSource& fileSource);
const SpritePosition &getSpritePosition(const std::string& name) const;

View File

@@ -48,7 +48,7 @@ private:
class GlyphPBF {
public:
GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange, const util::ptr<FileSource> &fileSource);
GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange, FileSource& fileSource);
private:
GlyphPBF(const GlyphPBF &) = delete;
@@ -71,7 +71,7 @@ private:
// Manages Glyphrange PBF loading.
class GlyphStore {
public:
GlyphStore(const util::ptr<FileSource> &fileSource);
GlyphStore(FileSource& fileSource);
// Block until all specified GlyphRanges of the specified font stack are loaded.
void waitForGlyphRanges(const std::string &fontStack, const std::set<GlyphRange> &glyphRanges);
@@ -86,11 +86,8 @@ private:
FontStack &createFontStack(const std::string &fontStack);
public:
std::string glyphURL;
private:
const util::ptr<FileSource> fileSource;
FileSource& fileSource;
std::unordered_map<std::string, std::map<GlyphRange, std::unique_ptr<GlyphPBF>>> ranges;
std::unordered_map<std::string, std::unique_ptr<FontStack>> stacks;
std::mutex mtx;

View File

@@ -346,7 +346,7 @@ void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) {
style->loadJSON((const uint8_t *)styleJSON.c_str());
if (!fileSource) {
fileSource = std::make_shared<FileSource>(**loop, platform::defaultCacheDatabase());
glyphStore = std::make_shared<GlyphStore>(fileSource);
glyphStore = std::make_shared<GlyphStore>(*fileSource);
}
fileSource->setBase(base);
glyphStore->setURL(util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken()));
@@ -370,7 +370,7 @@ util::ptr<Sprite> Map::getSprite() {
const float pixelRatio = state.getPixelRatio();
const std::string &sprite_url = style->getSpriteURL();
if (!sprite || sprite->pixelRatio != pixelRatio) {
sprite = Sprite::Create(sprite_url, pixelRatio, fileSource);
sprite = Sprite::Create(sprite_url, pixelRatio, *fileSource);
}
return sprite;
@@ -649,7 +649,7 @@ void Map::updateTiles() {
void Map::prepare() {
if (!fileSource) {
fileSource = std::make_shared<FileSource>(**loop, platform::defaultCacheDatabase());
glyphStore = std::make_shared<GlyphStore>(fileSource);
glyphStore = std::make_shared<GlyphStore>(*fileSource);
}
if (!style) {

View File

@@ -22,7 +22,7 @@ SpritePosition::SpritePosition(uint16_t x_, uint16_t y_, uint16_t width_, uint16
sdf(sdf_) {
}
util::ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio, const util::ptr<FileSource> &fileSource) {
util::ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio, FileSource& fileSource) {
util::ptr<Sprite> sprite(std::make_shared<Sprite>(Key(), base_url, pixelRatio));
sprite->load(fileSource);
return sprite;
@@ -51,7 +51,7 @@ Sprite::operator bool() const {
// Note: This is a separate function that must be called exactly once after creation
// The reason this isn't part of the constructor is that calling shared_from_this() in
// the constructor fails.
void Sprite::load(const util::ptr<FileSource> &fileSource) {
void Sprite::load(FileSource& fileSource) {
if (!valid) {
// Treat a non-existent sprite as a successfully loaded empty sprite.
loadedImage = true;
@@ -62,7 +62,7 @@ void Sprite::load(const util::ptr<FileSource> &fileSource) {
util::ptr<Sprite> sprite = shared_from_this();
fileSource->request(ResourceType::JSON, jsonURL)->onload([sprite](const Response &res) {
fileSource.request(ResourceType::JSON, jsonURL)->onload([sprite](const Response &res) {
if (res.code == 200) {
sprite->body = res.data;
sprite->parseJSON();
@@ -75,7 +75,7 @@ void Sprite::load(const util::ptr<FileSource> &fileSource) {
}
});
fileSource->request(ResourceType::Image, spriteURL)->onload([sprite](const Response &res) {
fileSource.request(ResourceType::Image, spriteURL)->onload([sprite](const Response &res) {
if (res.code == 200) {
sprite->image = res.data;
sprite->parseImage();

View File

@@ -137,7 +137,7 @@ void FontStack::lineWrap(Shaping &shaping, const float lineHeight, const float m
align(shaping, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, line);
}
GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange, const util::ptr<FileSource> &fileSource)
GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange, FileSource& fileSource)
: future(promise.get_future().share())
{
// Load the glyph set URL
@@ -148,8 +148,8 @@ GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, Gl
});
// The prepare call jumps back to the main thread.
fileSource->prepare([&, url, fileSource] {
auto request = fileSource->request(ResourceType::Glyphs, url);
fileSource.prepare([&, url] {
auto request = fileSource.request(ResourceType::Glyphs, url);
request->onload([&, url](const Response &res) {
if (res.code != 200) {
// Something went wrong with loading the glyph pbf. Pass on the error to the future listeners.
@@ -228,7 +228,7 @@ void GlyphPBF::parse(FontStack &stack) {
data.clear();
}
GlyphStore::GlyphStore(const util::ptr<FileSource> &fileSource_) : fileSource(fileSource_) {}
GlyphStore::GlyphStore(FileSource& fileSource_) : fileSource(fileSource_) {}
void GlyphStore::setURL(const std::string &url) {
glyphURL = url;