[libcalamares] Rename cPointerSetter
This class doesn't really set a pointer -- it is a scoped assignment through a pointer, which **can** set a value on destruction (when it leaves scope). Rename it. While here, extend the API so that it can do an assignment to the underlying object **now**, while also doing a scoped assignment later when it leaves scope. This makes some code a bit easier to read ("in this scope, X is now <v> and then it becomes <v'>")
This commit is contained in:
parent
bba5b21873
commit
8f65a644a9
@ -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?
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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() );
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user