diff options
-rw-r--r-- | include/mbgl/platform/settings.hpp | 48 | ||||
-rw-r--r-- | next/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/mbgl/platform/settings.cpp | 58 |
3 files changed, 108 insertions, 0 deletions
diff --git a/include/mbgl/platform/settings.hpp b/include/mbgl/platform/settings.hpp new file mode 100644 index 0000000000..80bfe1d60f --- /dev/null +++ b/include/mbgl/platform/settings.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include <mapbox/value.hpp> + +#include <memory> + +namespace mbgl { +namespace platform { + +#define DECLARE_MAPBOX_SETTING(name, value) constexpr const char* name = "mapbox_" #value + +// The value for EXPERIMENTAL_THREAD_PRIORITY_* keys, must be a double. +DECLARE_MAPBOX_SETTING(EXPERIMENTAL_THREAD_PRIORITY_WORKER, thread_priority_worker); +DECLARE_MAPBOX_SETTING(EXPERIMENTAL_THREAD_PRIORITY_FILE, thread_priority_file); +DECLARE_MAPBOX_SETTING(EXPERIMENTAL_THREAD_PRIORITY_NETWORK, thread_priority_network); +DECLARE_MAPBOX_SETTING(EXPERIMENTAL_THREAD_PRIORITY_DATABASE, thread_priority_database); + +// Settings class provides non-persistent, in-process key-value storage. +class Settings final { +public: + // Returns singleton instance + static Settings& getInstance() noexcept; + + // Sets setting value for a specified key. + void set(const std::string& key, mapbox::base::Value value) noexcept; + + // Sets multiple setting values by merging current Settings object + // with provided `values` object. + void set(mapbox::base::ValueObject values) noexcept; + + // Returns setting value for a specified key or NullValue if element + // for specified key is missing. + mapbox::base::Value get(const std::string& key) const noexcept; + + // Returns values for settings whose keys are equal to the ones provided in `keys` argument. + // Null values would be provided for keys whose elements are missing in Settings. + mapbox::base::ValueObject get(const std::vector<std::string>& keys) const noexcept; + +private: + Settings(); + +private: + class Impl; + std::unique_ptr<Impl> impl; +}; + +} // namespace platform +} // namespace mbgl diff --git a/next/CMakeLists.txt b/next/CMakeLists.txt index 11b4418aad..e956f3e5db 100644 --- a/next/CMakeLists.txt +++ b/next/CMakeLists.txt @@ -121,6 +121,7 @@ add_library( ${MBGL_ROOT}/include/mbgl/math/log2.hpp ${MBGL_ROOT}/include/mbgl/math/minmax.hpp ${MBGL_ROOT}/include/mbgl/math/wrap.hpp + ${MBGL_ROOT}/include/mbgl/platform/settings.hpp ${MBGL_ROOT}/include/mbgl/platform/thread.hpp ${MBGL_ROOT}/include/mbgl/renderer/query.hpp ${MBGL_ROOT}/include/mbgl/renderer/renderer.hpp @@ -353,6 +354,7 @@ add_library( ${MBGL_ROOT}/src/mbgl/map/transform_state.hpp ${MBGL_ROOT}/src/mbgl/map/zoom_history.hpp ${MBGL_ROOT}/src/mbgl/math/log2.cpp + ${MBGL_ROOT}/src/mbgl/platform/settings.cpp ${MBGL_ROOT}/src/mbgl/programs/attributes.hpp ${MBGL_ROOT}/src/mbgl/programs/background_pattern_program.hpp ${MBGL_ROOT}/src/mbgl/programs/background_program.cpp 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 |