diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-03-19 16:57:36 +0200 |
---|---|---|
committer | Alf Watt <alf.watt@mapbox.com> | 2019-03-21 12:25:12 -0700 |
commit | ba6dfea248b6fc5224c840ea536ac7abf57d80f1 (patch) | |
tree | 9ad248db128b3d1847985efb5c79bf13a4d56d36 /src/mbgl/storage/file_source.cpp | |
parent | 15c6dbe967f4150a0d9f555781c7e353ecf20043 (diff) | |
download | qtlocation-mapboxgl-ba6dfea248b6fc5224c840ea536ac7abf57d80f1.tar.gz |
[core] Remove file source from public Map ctor
Diffstat (limited to 'src/mbgl/storage/file_source.cpp')
-rw-r--r-- | src/mbgl/storage/file_source.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/mbgl/storage/file_source.cpp b/src/mbgl/storage/file_source.cpp new file mode 100644 index 0000000000..5854682771 --- /dev/null +++ b/src/mbgl/storage/file_source.cpp @@ -0,0 +1,37 @@ +#include <mbgl/storage/file_source.hpp> +#include <mbgl/storage/resource_options.hpp> +#include <mbgl/util/string.hpp> + +#include <mutex> +#include <map> + +namespace mbgl { + +std::shared_ptr<FileSource> FileSource::getSharedFileSource(const ResourceOptions& options) { + static std::mutex mutex; + static std::map<std::string, std::weak_ptr<mbgl::FileSource>> fileSources; + + std::lock_guard<std::mutex> lock(mutex); + + // Purge entries no longer in use. + for (auto it = fileSources.begin(); it != fileSources.end();) { + it = it->second.expired() ? fileSources.erase(it) : ++it; + } + + const uint64_t context = reinterpret_cast<uint64_t>(options.platformContext()); + const std::string key = options.baseURL() + '|' + options.accessToken() + '|' + options.cachePath() + '|' + util::toString(context); + + std::shared_ptr<mbgl::FileSource> fileSource; + auto tuple = fileSources.find(key); + if (tuple != fileSources.end()) { + fileSource = tuple->second.lock(); + } + + if (!fileSource) { + fileSources[key] = fileSource = createPlatformFileSource(options); + } + + return fileSource; +} + +} // namespace mbgl |