summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMichael Welter <michael@welter-4d.de>2022-09-11 20:56:18 +0200
committerMichael Welter <michael@welter-4d.de>2022-12-20 15:36:13 +0100
commitf43e674bc0e47b7360c2a1ee335f7536e1638ae1 (patch)
treea9e418d2ffbdb0b791aa36c6f0ed1407771e99ab
parent00299649bf84c9f81764d36d49c01c254953f362 (diff)
Fix initialization order issues
-rw-r--r--migration/migration.cpp31
-rw-r--r--migration/migration.hpp3
-rw-r--r--video/camera.cpp14
3 files changed, 36 insertions, 12 deletions
diff --git a/migration/migration.cpp b/migration/migration.cpp
index 4ddb2c8c..f4b1739b 100644
--- a/migration/migration.cpp
+++ b/migration/migration.cpp
@@ -26,14 +26,25 @@ using namespace options::globals;
namespace migrations::detail {
-static std::vector<mptr> migration_list;
-static std::vector<mfun> migration_thunks;
+
+std::vector<mptr>& migrator::migration_list()
+{
+ static std::vector<mptr> v;
+ return v;
+}
+
+std::vector<mfun>& migrator::migration_thunks()
+{
+ static std::vector<mfun> v;
+ return v;
+}
+
void migrator::register_migration(mptr const& m)
{
const QString date = m->unique_date();
- for (mptr const& m2 : migration_list)
+ for (mptr const& m2 : migration_list())
if (m2->unique_date() == date)
std::abort();
@@ -65,35 +76,35 @@ void migrator::register_migration(mptr const& m)
if (day_ < 1 || day_ > 31)
abort();
- migration_list.push_back(m);
+ migration_list().push_back(m);
}
void migrator::eval_thunks()
{
- for (auto& fun : migration_thunks)
+ for (auto& fun : migration_thunks())
{
mptr m = fun();
register_migration(m);
}
- if (!migration_thunks.empty())
+ if (!migration_thunks().empty())
sort_migrations();
- migration_thunks.clear();
+ migration_thunks().clear();
}
void migrator::add_migration_thunk(mfun& thunk)
{
- migration_thunks.push_back(thunk);
+ migration_thunks().push_back(thunk);
}
std::vector<mptr>& migrator::migrations()
{
eval_thunks();
- return migration_list;
+ return migration_list();
}
void migrator::sort_migrations()
{
- std::sort(migration_list.begin(), migration_list.end(),
+ std::sort(migration_list().begin(), migration_list().end(),
[](const mptr x, const mptr y) {
return x->unique_date() < y->unique_date();
});
diff --git a/migration/migration.hpp b/migration/migration.hpp
index a3035247..7fc18c97 100644
--- a/migration/migration.hpp
+++ b/migration/migration.hpp
@@ -43,6 +43,9 @@ namespace detail {
static void set_last_migration_time(const QString& val);
static int to_int(const QString& str, bool& ok);
+
+ static std::vector<mptr>& migration_list();
+ static std::vector<mfun>& migration_thunks();
};
template<typename t>
diff --git a/video/camera.cpp b/video/camera.cpp
index 42320402..a66d8a59 100644
--- a/video/camera.cpp
+++ b/video/camera.cpp
@@ -4,8 +4,12 @@
#include <utility>
#include <QMutex>
-static std::vector<std::unique_ptr<video::impl::camera_>> metadata;
-static QMutex mtx;
+std::pair<std::vector<std::unique_ptr<video::impl::camera_>>&, QMutex&> get_metadata()
+{
+ static std::vector<std::unique_ptr<video::impl::camera_>> metadata;
+ static QMutex mtx;
+ return { metadata, mtx };
+}
namespace video::impl {
@@ -17,6 +21,7 @@ camera::~camera() = default;
void register_camera(std::unique_ptr<impl::camera_> camera)
{
+ auto [metadata, mtx] = get_metadata();
QMutexLocker l(&mtx);
metadata.push_back(std::move(camera));
}
@@ -27,6 +32,7 @@ namespace video {
bool show_dialog(const QString& camera_name)
{
+ auto [metadata, mtx] = get_metadata();
QMutexLocker l(&mtx);
for (auto& camera : metadata)
@@ -39,6 +45,7 @@ bool show_dialog(const QString& camera_name)
std::unique_ptr<camera_impl> make_camera_(const QString& name)
{
+ auto [metadata, mtx] = get_metadata();
QMutexLocker l(&mtx);
for (auto& camera : metadata)
@@ -54,6 +61,7 @@ std::unique_ptr<camera_impl> make_camera(const QString& name)
if (auto ret = make_camera_(name))
return ret;
+ auto [metadata, mtx] = get_metadata();
for (auto& camera : metadata)
for (const QString& name_ : camera->camera_names())
if (auto ret = camera->make_camera(name_))
@@ -64,6 +72,8 @@ std::unique_ptr<camera_impl> make_camera(const QString& name)
std::vector<QString> camera_names()
{
+ auto [metadata, mtx] = get_metadata();
+
QMutexLocker l(&mtx);
std::vector<QString> names; names.reserve(32);