From fb4411356128a92e0c79b61fa6a617f81ca2dd9d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 11 Jan 2019 14:15:18 +0100 Subject: [PATCH 1/3] [libcalamares] Named enumeration support --- src/libcalamares/utils/NamedEnum.h | 88 ++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/libcalamares/utils/NamedEnum.h diff --git a/src/libcalamares/utils/NamedEnum.h b/src/libcalamares/utils/NamedEnum.h new file mode 100644 index 000000000..b28797288 --- /dev/null +++ b/src/libcalamares/utils/NamedEnum.h @@ -0,0 +1,88 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +/** @brief Support for "named" enumerations + * + * For tables which map string names to enum values, provide a NamedEnumTable + * which hangs on to an initializer_list of pairs of names and values. + * This table can be used with find() to map names to values, or + * values to names. A convenience function smash() is provided to help + * in printing integer (underlying) values of an enum. + */ + +#ifndef LIBCALAMARES_NAMEDENUM_H +#define LIBCALAMARES_NAMEDENUM_H + +#include + +#include +#include + +/** @brief Type for collecting parts of a named enum. */ +template +struct NamedEnumTable +{ + using string_t = QString; + using enum_t = T; + using pair_t = std::pair< string_t, enum_t >; + using type = std::initializer_list< pair_t >; +} ; + +/** @brief Find a name @p s in the @p table. */ +template +typename NamedEnumTable::enum_t find( const typename NamedEnumTable::type& table, const QString& s, bool& ok ) +{ + ok = false; + + for ( const auto p : table ) + if ( s == p.first ) + { + ok = true; + return p.second; + } + + // ok is still false + return table.begin()->second; +} + +/** @brief Find a value @p s in the @p table. */ +template +typename NamedEnumTable::enum_t find( const typename NamedEnumTable::type& table, const T s, bool& ok ) +{ + ok = false; + + for ( const auto p : table ) + if ( s == p.second) + { + ok = true; + return p.first; + } + + // ok is still false + return QString(); +} + +/** @brief Smashes an enum value to its underlying type. */ +template +constexpr typename std::underlying_type::type smash( const E e ) +{ + return static_cast::type>( e ); +} + + +#endif From 9c2a6b03e409d34e752f8cbcbee0acc7dbf67305 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 11 Jan 2019 14:30:21 +0100 Subject: [PATCH 2/3] [libcalamares] Slight refactor, move into NamedEnumTable - expand documentation on find() - make find() methods --- src/libcalamares/utils/NamedEnum.h | 80 +++++++++++++++++++----------- 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/src/libcalamares/utils/NamedEnum.h b/src/libcalamares/utils/NamedEnum.h index b28797288..c7b3ed9cf 100644 --- a/src/libcalamares/utils/NamedEnum.h +++ b/src/libcalamares/utils/NamedEnum.h @@ -41,41 +41,63 @@ struct NamedEnumTable using enum_t = T; using pair_t = std::pair< string_t, enum_t >; using type = std::initializer_list< pair_t >; -} ; -/** @brief Find a name @p s in the @p table. */ -template -typename NamedEnumTable::enum_t find( const typename NamedEnumTable::type& table, const QString& s, bool& ok ) -{ - ok = false; + type table; - for ( const auto p : table ) - if ( s == p.first ) - { - ok = true; - return p.second; - } + /** @brief Create a table of named enum values. + * + * Use braced-initialisation for NamedEnum, and remember that the + * elements of the list are **pairs**, e.g. + * + * static const NamedEnumTable c{ {"red", Colors::Red } }; + */ + NamedEnumTable( type v ) : table( v ) { /* static_assert( v.size() > 0 ); */ }; - // ok is still false - return table.begin()->second; -} + /** @brief Find a name @p s in the table. + * + * Searches case-insensitively. + * + * If the name @p s is not found, @p ok is set to false and + * the first enum value in the table is returned. Otherwise, + * @p ok is set to true and the corresponding value is returned. + * + */ + enum_t find( const string_t& s, bool& ok ) const + { + ok = false; -/** @brief Find a value @p s in the @p table. */ -template -typename NamedEnumTable::enum_t find( const typename NamedEnumTable::type& table, const T s, bool& ok ) -{ - ok = false; + for ( const auto p : table ) + if ( 0 == QString::compare( s, p.first, Qt::CaseInsensitive ) ) + { + ok = true; + return p.second; + } - for ( const auto p : table ) - if ( s == p.second) - { - ok = true; - return p.first; - } + // ok is still false + return table.begin()->second; + } - // ok is still false - return QString(); -} + /** @brief Find a value @p s in the table. + * + * If the value @p s is not found, @p ok is set to false and + * an empty string is returned. Otherwise, @p is set to true + * and the corresponding name is returned. + */ + string_t find( enum_t s, bool& ok ) const + { + ok = false; + + for ( const auto p : table ) + if ( s == p.second) + { + ok = true; + return p.first; + } + + // ok is still false + return string_t(); + } +} ; /** @brief Smashes an enum value to its underlying type. */ template From 194f693412bd35613162706e4887ea2017c418e6 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 11 Jan 2019 14:32:45 +0100 Subject: [PATCH 3/3] [partition] Use new NamedEnum approach --- .../partition/gui/PartitionViewStep.cpp | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/modules/partition/gui/PartitionViewStep.cpp b/src/modules/partition/gui/PartitionViewStep.cpp index 0152f8bec..7d1791342 100644 --- a/src/modules/partition/gui/PartitionViewStep.cpp +++ b/src/modules/partition/gui/PartitionViewStep.cpp @@ -35,6 +35,7 @@ #include "CalamaresVersion.h" #include "utils/CalamaresUtilsGui.h" #include "utils/Logger.h" +#include "utils/NamedEnum.h" #include "utils/Retranslator.h" #include "widgets/WaitingWidget.h" #include "GlobalStorage.h" @@ -474,25 +475,17 @@ PartitionViewStep::onLeave() static PartitionActions::Choices::SwapChoice nameToChoice( QString name, bool& ok ) { - ok = false; - name = name.toLower(); - using namespace PartitionActions::Choices; - // 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; + 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.find( name, ok ); }