[core] Add global settings object

This commit is contained in:
Alexander Shalamov
2020-01-29 16:42:19 +02:00
parent d4d96fd88c
commit f30aec6c1a
3 changed files with 108 additions and 0 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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