diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2020-01-29 16:42:19 +0200 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2020-02-06 23:54:19 +0200 |
commit | f30aec6c1a4397e524dc885767ccb349856fed0b (patch) | |
tree | 5aed895a2237a64dc8ffc5d565ff0cb73487fa44 /src/mbgl | |
parent | d4d96fd88c83a5640fdef408f1ff3f69db0a584c (diff) | |
download | qtlocation-mapboxgl-f30aec6c1a4397e524dc885767ccb349856fed0b.tar.gz |
[core] Add global settings object
Diffstat (limited to 'src/mbgl')
-rw-r--r-- | src/mbgl/platform/settings.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/mbgl/platform/settings.cpp b/src/mbgl/platform/settings.cpp new file mode 100644 index 0000000000..f586157919 --- /dev/null +++ b/src/mbgl/platform/settings.cpp @@ -0,0 +1,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(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 |