diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index 3f908b98d..480a115ae 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -244,7 +244,7 @@ gettext_path() { // Going to log informatively just once static bool first_time = true; - cPointerSetter( &first_time, false ); + cScopedAssignment( &first_time, false ); // TODO: distinguish between -d runs and normal runs // TODO: can we detect DESTDIR-installs? diff --git a/src/libcalamares/utils/RAII.h b/src/libcalamares/utils/RAII.h index 186775676..957e4fe42 100644 --- a/src/libcalamares/utils/RAII.h +++ b/src/libcalamares/utils/RAII.h @@ -48,10 +48,11 @@ using cSignalBlocker = QSignalBlocker; /** @brief Writes a value on destruction to a pointed-to location. * * If the pointer is non-null, write the last-given-value if there - * is one to the pointed-to object. + * is one to the pointed-to object. This is called the "then-value". + * */ template < typename T > -struct cPointerSetter +struct cScopedAssignment { std::optional< T > m_value; T* m_pointer; @@ -62,22 +63,36 @@ struct cPointerSetter * will do nothing on destruction, leaving the pointed-to * value unchanged. */ - cPointerSetter( T* p ) + cScopedAssignment( T* p ) : m_pointer( p ) { } - /** @brief Create a setter with a value already set + /** @brief Create a setter with a then-value already set * * This ensures that on destruction, the value @p v will be written; * it is equivalent to assigning @p v immediately. The pointed-to * value is **not** changed (until destruction). */ - cPointerSetter( T* p, T v ) - : m_value( v ) + cScopedAssignment( T* p, T then ) + : m_value( then ) , m_pointer( p ) { } - ~cPointerSetter() + /** @brief Create a setter with a then-value and assign a new value now + * + * As above, but also assign @p now to the thing pointed-to. + */ + cScopedAssignment( T* p, T now, T then ) + : m_value( then ) + , m_pointer( p ) + { + if ( p ) + { + *p = now; + } + } + + ~cScopedAssignment() { if ( m_pointer && m_value.has_value() ) { @@ -85,13 +100,13 @@ struct cPointerSetter } } - const T& operator=( const T& v ) + const T& operator=( const T& then ) { - m_value = v; - return v; + m_value = then; + return then; } }; template < typename T > -cPointerSetter( T p )->cPointerSetter< decltype( *p ) >; +cScopedAssignment( T p )->cScopedAssignment< decltype( *p ) >; #endif diff --git a/src/libcalamares/utils/Tests.cpp b/src/libcalamares/utils/Tests.cpp index bb1bf9520..c652571b4 100644 --- a/src/libcalamares/utils/Tests.cpp +++ b/src/libcalamares/utils/Tests.cpp @@ -346,35 +346,35 @@ LibCalamaresTests::testPointerSetter() QCOMPARE( special, 17 ); { - cPointerSetter p( &special ); + cScopedAssignment p( &special ); } QCOMPARE( special, 17 ); { - cPointerSetter p( &special ); + cScopedAssignment p( &special ); p = 18; } QCOMPARE( special, 18 ); { - cPointerSetter p( &special ); + cScopedAssignment p( &special ); p = 20; p = 3; } QCOMPARE( special, 3 ); { - cPointerSetter< int > p( nullptr ); + cScopedAssignment< int > p( nullptr ); } QCOMPARE( special, 3 ); { // "don't do this" .. order of destructors is important - cPointerSetter p( &special ); - cPointerSetter q( &special ); + cScopedAssignment p( &special ); + cScopedAssignment q( &special ); p = 17; } QCOMPARE( special, 17 ); { // "don't do this" .. order of destructors is important - cPointerSetter p( &special ); - cPointerSetter q( &special ); + cScopedAssignment p( &special ); + cScopedAssignment q( &special ); p = 34; q = 2; // q destroyed first, then p diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 7140bd790..f1b6efeba 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -275,7 +275,7 @@ Config::detectCurrentKeyboardLayout() { return; } - cPointerSetter returnToIntial( &m_state, State::Initial ); + cScopedAssignment returnToIntial( &m_state, State::Initial ); m_state = State::Guessing; //### Detect current keyboard layout and variant @@ -427,7 +427,7 @@ Config::guessLocaleKeyboardLayout() { return; } - cPointerSetter returnToIntial( &m_state, State::Initial ); + cScopedAssignment returnToIntial( &m_state, State::Initial ); m_state = State::Guessing; /* Guessing a keyboard layout based on the locale means diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index 027d50a38..236f63ec3 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -174,8 +174,7 @@ LocalePage::locationChanged( const CalamaresUtils::Locale::TimeZoneData* locatio { return; } - m_blockTzWidgetSet = true; // Blocked until we go out of scope - cPointerSetter b( &m_blockTzWidgetSet, false ); + cScopedAssignment b( &m_blockTzWidgetSet, true, false ); // Set region index int index = m_regionCombo->findData( location->region() ); diff --git a/src/modules/partition/core/PartUtils.cpp b/src/modules/partition/core/PartUtils.cpp index c99884ce6..806c0ceb3 100644 --- a/src/modules/partition/core/PartUtils.cpp +++ b/src/modules/partition/core/PartUtils.cpp @@ -529,7 +529,7 @@ efiFilesystemMinimumSize() QString canonicalFilesystemName( const QString& fsName, FileSystem::Type* fsType ) { - cPointerSetter type( fsType ); + cScopedAssignment type( fsType ); if ( fsName.isEmpty() ) { type = FileSystem::Ext4;