From fee17949245184d8284aeb2c89b549822146f5b8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 18 Jan 2019 15:56:51 +0100 Subject: [PATCH] [partition] Use NamedEnumTable support code - reduce amount of custom code by using the (new) generic implementation --- .../partition/core/PartitionActions.cpp | 46 ++++++++----------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 8b0303277..ae0e42838 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -26,6 +26,8 @@ #include "utils/CalamaresUtilsSystem.h" #include "utils/Units.h" +#include "utils/NamedEnum.h" + #include "JobQueue.h" #include "utils/Logger.h" @@ -305,43 +307,31 @@ doReplacePartition( PartitionCoreModule* core, namespace Choices { +static const NamedEnumTable& +nameTable() +{ + static const NamedEnumTable names{ + { QStringLiteral( "none" ), SwapChoice::NoSwap }, + { QStringLiteral( "small" ), SwapChoice::SmallSwap }, + { QStringLiteral( "suspend" ), SwapChoice::FullSwap }, + { QStringLiteral( "reuse" ), SwapChoice::ReuseSwap }, + { QStringLiteral( "file" ), SwapChoice::SwapFile } + }; + + return names; +} SwapChoice nameToChoice( QString name, bool& ok ) { - ok = false; - name = name.toLower(); - - // Each return here first sets ok to true, returns enum value - if ( name == QStringLiteral( "none" ) ) - return( ok=true, SwapChoice::NoSwap ); - else if ( name == QStringLiteral( "small" ) ) - return( ok=true, SwapChoice::SmallSwap); - else if ( name == QStringLiteral( "suspend" ) ) - return( ok=true, SwapChoice::FullSwap ); - else if ( name == QStringLiteral( "reuse" ) ) - return( ok=true, SwapChoice::ReuseSwap ); - else if ( name == QStringLiteral( "file" ) ) - return( ok=true, SwapChoice::SwapFile ); - - ok = false; - return SwapChoice::NoSwap; + return nameTable().find( name, ok ); } QString choiceToName( SwapChoice c ) { - switch ( c ) - { - case SwapChoice::NoSwap: return QStringLiteral( "none" ); - case SwapChoice::SmallSwap: return QStringLiteral( "small" ); - case SwapChoice::FullSwap: return QStringLiteral( "suspend" ); - case SwapChoice::ReuseSwap: return QStringLiteral( "reuse" ); - case SwapChoice::SwapFile: return QStringLiteral( "file" ); - } - - cWarning() << "Unknown SwapChoice" << c << "treated as none"; - return QStringLiteral( "none" ); + bool ok = false; + return nameTable().find( c, ok ); } } // namespace Choices