diff options
Diffstat (limited to 'options/globals.hpp')
| -rw-r--r-- | options/globals.hpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/options/globals.hpp b/options/globals.hpp new file mode 100644 index 00000000..af242dc9 --- /dev/null +++ b/options/globals.hpp @@ -0,0 +1,74 @@ +#pragma once + +#include "export.hpp" +#include "compat/macros.h" + +#include <optional> + +#include <QString> +#include <QSettings> +#include <QMutex> + +namespace options::globals::detail { + +struct saver_; + +struct OTR_OPTIONS_EXPORT ini_ctx +{ + std::optional<QSettings> qsettings { std::in_place }; + QString pathname; + QMutex mtx { QMutex::Recursive }; + + unsigned refcount = 0; + bool modifiedp = false; + + ini_ctx(); +}; + +struct OTR_OPTIONS_EXPORT saver_ final +{ + ini_ctx& ctx; + + never_inline ~saver_(); + explicit never_inline saver_(ini_ctx& ini); +}; + +template<typename F> +never_inline +auto with_settings_object_(ini_ctx& ini, F&& fun) +{ + saver_ saver { ini }; + + return fun(*ini.qsettings); +} + +OTR_OPTIONS_EXPORT ini_ctx& cur_settings(); +OTR_OPTIONS_EXPORT ini_ctx& global_settings(); + +OTR_OPTIONS_EXPORT void mark_ini_modified(bool value = true); + +} // ns options::globals::detail + +namespace options::globals +{ + OTR_OPTIONS_EXPORT void mark_global_ini_modified(bool value = true); + OTR_OPTIONS_EXPORT QString ini_directory(); + OTR_OPTIONS_EXPORT QString ini_filename(); + OTR_OPTIONS_EXPORT QString ini_pathname(); + OTR_OPTIONS_EXPORT QString ini_combine(const QString& filename); + OTR_OPTIONS_EXPORT QStringList ini_list(); + + template<typename F> + auto with_settings_object(F&& fun) + { + using namespace detail; + return with_settings_object_(cur_settings(), fun); + } + + template<typename F> + auto with_global_settings_object(F&& fun) + { + using namespace detail; + return with_settings_object_(global_settings(), fun); + } +} // ns options::globals |
