summaryrefslogtreecommitdiff
path: root/src/mbgl/platform/settings.cpp
blob: 10c8d109c6cf4afa81c4f231fd70b4fd70a73bb7 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <mbgl/platform/settings.hpp>

#include <mutex>

namespace mbgl {
namespace platform {

namespace {
mapbox::base::Value getValue(const std::string& key, const mapbox::base::ValueObject& object) {
    auto it = object.find(key);
    if (it != object.end()) {
        return it->second;
    }
    return {};
}
} // namespace

class Settings::Impl {
public:
    mapbox::base::ValueObject settings;
    std::mutex mutex;
};

Settings::Settings() : impl(std::make_unique<Impl>()) {}

Settings& Settings::getInstance() noexcept {
    static Settings instance;
    return instance;
}

void Settings::set(const std::string& key, mapbox::base::Value value) noexcept {
    std::lock_guard<std::mutex> lock(impl->mutex);
    impl->settings[key] = std::move(value);
}

void Settings::set(const mapbox::base::ValueObject& values) noexcept {
    std::lock_guard<std::mutex> lock(impl->mutex);
    for (const auto& pair : values) {
        impl->settings[pair.first] = pair.second;
    }
}

mapbox::base::ValueObject Settings::get(const std::vector<std::string>& keys) const noexcept {
    std::lock_guard<std::mutex> lock(impl->mutex);
    mapbox::base::ValueObject result;
    for (const auto& key : keys) {
        result[key] = getValue(key, impl->settings);
    }
    return result;
}

mapbox::base::Value Settings::get(const std::string& key) const noexcept {
    std::lock_guard<std::mutex> lock(impl->mutex);
    return getValue(key, impl->settings);
}

} // namespace platform
} // namespace mbgl