summaryrefslogtreecommitdiff
path: root/src/mbgl/storage/file_source.cpp
blob: 58546827714f50ad026dccd1f5ac9163dda49343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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