diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2017-10-20 18:06:24 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2017-10-20 18:06:33 +0200 |
commit | b947e4a4ae0e06633b114dd59d447973fc5aaa56 (patch) | |
tree | 7933c169ed263f30ef0389f2059da14e3c5ad933 /options | |
parent | 39e209983bd1f04fb0beefef754d7430c8b7fb9f (diff) |
options/scoped: use raii
Diffstat (limited to 'options')
-rw-r--r-- | options/scoped.cpp | 29 | ||||
-rw-r--r-- | options/scoped.hpp | 16 |
2 files changed, 32 insertions, 13 deletions
diff --git a/options/scoped.cpp b/options/scoped.cpp index ef2b1b02..2974dfdb 100644 --- a/options/scoped.cpp +++ b/options/scoped.cpp @@ -9,9 +9,13 @@ namespace options { -std::atomic_bool opts::teardown_flag(false); +// XXX hack: the flag shouldn't be here as action at distance -sh 20160926 +static std::atomic_bool teardown_flag(false); +static void set_teardown_flag(bool value); +static void ensure_thread(); +static bool is_tracker_teardown(); -void opts::set_teardown_flag(bool value) +static void set_teardown_flag(bool value) { // flag being set means "opts" is about to go out of scope due to tracker stop // in this case we don't reload options. we only want to reload when cancel is pressed. @@ -19,10 +23,13 @@ void opts::set_teardown_flag(bool value) teardown_flag = value; } -void opts::ensure_thread() +static void ensure_thread() { // only as a bug check + if (qApp == nullptr) + abort(); + const QThread* ui_thread = qApp->thread(); const QThread* curthread = QThread::currentThread(); @@ -33,6 +40,11 @@ void opts::ensure_thread() abort(); } +static bool is_tracker_teardown() +{ + return teardown_flag; +} + opts::~opts() { if (!is_tracker_teardown()) @@ -43,13 +55,18 @@ opts::~opts() #endif } -bool opts::is_tracker_teardown() +opts::opts(const QString &name) : b(make_bundle(name)) { - return teardown_flag; } -opts::opts(const QString &name) : b(make_bundle(name)) +with_tracker_teardown::with_tracker_teardown() : old_value(teardown_flag) +{ + set_teardown_flag(true); +} + +with_tracker_teardown::~with_tracker_teardown() { + set_teardown_flag(old_value); } } // ns options diff --git a/options/scoped.hpp b/options/scoped.hpp index f43e9712..117bbf56 100644 --- a/options/scoped.hpp +++ b/options/scoped.hpp @@ -9,6 +9,15 @@ namespace options { +struct OTR_OPTIONS_EXPORT with_tracker_teardown final +{ + with_tracker_teardown(); + ~with_tracker_teardown(); + +private: + bool old_value; +}; + struct OTR_OPTIONS_EXPORT opts { bundle b; @@ -16,13 +25,6 @@ struct OTR_OPTIONS_EXPORT opts opts& operator=(const opts&) = delete; opts(const opts&) = delete; virtual ~opts(); - - // XXX hack: the flag shouldn't be here as action at distance -sh 20160926 - static void set_teardown_flag(bool value); - static bool is_tracker_teardown(); -private: - static std::atomic_bool teardown_flag; - static void ensure_thread(); }; } |