From a57d74a38c84de41fec36a738e1b7e2ad6fdb59a Mon Sep 17 00:00:00 2001 From: Scott Theisen Date: Thu, 21 Nov 2024 23:03:05 -0500 Subject: [PATCH] merge CleanupGuard into MythContext --- mythtv/libs/libmyth/mythcontext.cpp | 5 +++++ mythtv/libs/libmyth/mythcontext.h | 8 ++++++++ mythtv/libs/libmythbase/CMakeLists.txt | 2 -- mythtv/libs/libmythbase/cleanupguard.cpp | 11 ---------- mythtv/libs/libmythbase/cleanupguard.h | 20 ------------------- mythtv/libs/libmythbase/libmythbase.pro | 4 ++-- mythtv/programs/mythbackend/mythbackend.cpp | 3 +-- mythtv/programs/mythfrontend/mythfrontend.cpp | 3 +-- 8 files changed, 17 insertions(+), 39 deletions(-) delete mode 100644 mythtv/libs/libmythbase/cleanupguard.cpp delete mode 100644 mythtv/libs/libmythbase/cleanupguard.h diff --git a/mythtv/libs/libmyth/mythcontext.cpp b/mythtv/libs/libmyth/mythcontext.cpp index 8f506bc9667..22136ce7554 100644 --- a/mythtv/libs/libmyth/mythcontext.cpp +++ b/mythtv/libs/libmyth/mythcontext.cpp @@ -1664,6 +1664,11 @@ bool MythContext::Init(const bool gui, MythContext::~MythContext() { + if (m_cleanup != nullptr) + { + m_cleanup(); + } + if (m_impl->m_gui) { DestroyMythMainWindow(); diff --git a/mythtv/libs/libmyth/mythcontext.h b/mythtv/libs/libmyth/mythcontext.h index fafb11bf3c5..ee0bc4d433c 100644 --- a/mythtv/libs/libmyth/mythcontext.h +++ b/mythtv/libs/libmyth/mythcontext.h @@ -27,6 +27,9 @@ class MPUBLIC MythContext bool disableAutoDiscovery = false, bool ignoreDB = false); + using CleanupFunction = void (*)(); + void setCleanup(CleanupFunction cleanup) { m_cleanup = cleanup; } + bool saveSettingsCache(); void SetDisableEventPopup(bool check); @@ -37,6 +40,11 @@ class MPUBLIC MythContext class Impl; Impl *m_impl {nullptr}; ///< PIMPL idiom QString m_appBinaryVersion; + /** + This is used to destroy global state before main() returns. It is called + first before anything else is done in ~MythContext() if it is not nullptr. + */ + CleanupFunction m_cleanup {nullptr}; }; #endif diff --git a/mythtv/libs/libmythbase/CMakeLists.txt b/mythtv/libs/libmythbase/CMakeLists.txt index 5c21db5e32d..5ea35ac93c2 100644 --- a/mythtv/libs/libmythbase/CMakeLists.txt +++ b/mythtv/libs/libmythbase/CMakeLists.txt @@ -84,7 +84,6 @@ set(LIBMYTHBASE_HEADERS set(LIBMYTHBASE_HEADERS_NOT_INSTALLED mythbinaryplist.h - cleanupguard.h configuration.h housekeeper.h logging.h @@ -146,7 +145,6 @@ add_library( ${LIBMYTHBASE_HEADERS} ${LIBMYTHBASE_HEADERS_NOT_INSTALLED} ${LIBMYTHBASE_HTTP_HEADERS} - cleanupguard.cpp configuration.cpp dbcheckcommon.cpp dbutil.cpp diff --git a/mythtv/libs/libmythbase/cleanupguard.cpp b/mythtv/libs/libmythbase/cleanupguard.cpp deleted file mode 100644 index 2ab010e6cf3..00000000000 --- a/mythtv/libs/libmythbase/cleanupguard.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "cleanupguard.h" - -CleanupGuard::CleanupGuard(CleanupFunc cleanFunction) : - m_cleanFunction(cleanFunction) -{ -} - -CleanupGuard::~CleanupGuard() -{ - m_cleanFunction(); -} diff --git a/mythtv/libs/libmythbase/cleanupguard.h b/mythtv/libs/libmythbase/cleanupguard.h deleted file mode 100644 index aff5a821b04..00000000000 --- a/mythtv/libs/libmythbase/cleanupguard.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CLEANUPGUARD_H -#define CLEANUPGUARD_H - -#include "mythbaseexp.h" - -class MBASE_PUBLIC CleanupGuard -{ - public: - using CleanupFunc = void (*)(); - - public: - explicit CleanupGuard(CleanupFunc cleanFunction); - - ~CleanupGuard(); - - private: - CleanupFunc m_cleanFunction; -}; - -#endif // CLEANUPGUARD_H diff --git a/mythtv/libs/libmythbase/libmythbase.pro b/mythtv/libs/libmythbase/libmythbase.pro index 734a3546607..533fec00404 100644 --- a/mythtv/libs/libmythbase/libmythbase.pro +++ b/mythtv/libs/libmythbase/libmythbase.pro @@ -37,7 +37,7 @@ HEADERS += mythsystemlegacy.h mythtypes.h HEADERS += threadedfilewriter.h mythsingledownload.h HEADERS += ternarycompare.h HEADERS += mythsession.h -HEADERS += cleanupguard.h portchecker.h +HEADERS += portchecker.h HEADERS += mythsorthelper.h mythdbcheck.h HEADERS += mythpower.h HEADERS += configuration.h @@ -76,7 +76,7 @@ SOURCES += mythrandom.cpp SOURCES += stringutil.cpp SOURCES += threadedfilewriter.cpp mythsingledownload.cpp SOURCES += mythsession.cpp -SOURCES += cleanupguard.cpp portchecker.cpp +SOURCES += portchecker.cpp SOURCES += mythsorthelper.cpp dbcheckcommon.cpp SOURCES += mythpower.cpp SOURCES += configuration.cpp diff --git a/mythtv/programs/mythbackend/mythbackend.cpp b/mythtv/programs/mythbackend/mythbackend.cpp index 45c418bc097..5153b55d2cb 100644 --- a/mythtv/programs/mythbackend/mythbackend.cpp +++ b/mythtv/programs/mythbackend/mythbackend.cpp @@ -25,7 +25,6 @@ // MythTV #include "libmyth/mythcontext.h" -#include "libmythbase/cleanupguard.h" #include "libmythbase/compat.h" #include "libmythbase/configuration.h" #include "libmythbase/exitcodes.h" @@ -156,7 +155,7 @@ int main(int argc, char **argv) LOG(VB_GENERAL, LOG_CRIT, "Failed to init MythContext."); return GENERIC_EXIT_NO_MYTHCONTEXT; } - CleanupGuard callCleanup(cleanup); + context.setCleanup(cleanup); MythTranslation::load("mythfrontend"); diff --git a/mythtv/programs/mythfrontend/mythfrontend.cpp b/mythtv/programs/mythfrontend/mythfrontend.cpp index d5d2d9bdb20..7710758af9d 100644 --- a/mythtv/programs/mythfrontend/mythfrontend.cpp +++ b/mythtv/programs/mythfrontend/mythfrontend.cpp @@ -36,7 +36,6 @@ #include "libmyth/mythcontext.h" #include "libmyth/mythmediamonitor.h" #include "libmyth/standardsettings.h" -#include "libmythbase/cleanupguard.h" #include "libmythbase/compat.h" // For SIG* on MinGW #include "libmythbase/exitcodes.h" #include "libmythbase/hardwareprofile.h" @@ -2024,7 +2023,7 @@ Q_DECL_EXPORT int main(int argc, char **argv) gCoreContext->SetExiting(true); return GENERIC_EXIT_NO_MYTHCONTEXT; } - CleanupGuard callCleanup(cleanup); + context.setCleanup(cleanup); SignalHandler::SetHandler(SIGUSR1, handleSIGUSR1); SignalHandler::SetHandler(SIGUSR2, handleSIGUSR2);