[libcalamares] Slight refactor, move into NamedEnumTable

- expand documentation on find()
 - make find() methods
main
Adriaan de Groot 6 years ago
parent fb44113561
commit 9c2a6b03e4

@ -41,41 +41,63 @@ struct NamedEnumTable
using enum_t = T; using enum_t = T;
using pair_t = std::pair< string_t, enum_t >; using pair_t = std::pair< string_t, enum_t >;
using type = std::initializer_list< pair_t >; using type = std::initializer_list< pair_t >;
} ;
/** @brief Find a name @p s in the @p table. */ type table;
template<typename T>
typename NamedEnumTable<T>::enum_t find( const typename NamedEnumTable<T>::type& table, const QString& s, bool& ok )
{
ok = false;
for ( const auto p : table ) /** @brief Create a table of named enum values.
if ( s == p.first ) *
{ * Use braced-initialisation for NamedEnum, and remember that the
ok = true; * elements of the list are **pairs**, e.g.
return p.second; *
} * static const NamedEnumTable<Colors> c{ {"red", Colors::Red } };
*/
NamedEnumTable( type v ) : table( v ) { /* static_assert( v.size() > 0 ); */ };
// ok is still false /** @brief Find a name @p s in the table.
return table.begin()->second; *
} * 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. */ for ( const auto p : table )
template<typename T> if ( 0 == QString::compare( s, p.first, Qt::CaseInsensitive ) )
typename NamedEnumTable<T>::enum_t find( const typename NamedEnumTable<T>::type& table, const T s, bool& ok ) {
{ ok = true;
ok = false; return p.second;
}
for ( const auto p : table ) // ok is still false
if ( s == p.second) return table.begin()->second;
{ }
ok = true;
return p.first;
}
// ok is still false /** @brief Find a value @p s in the table.
return QString(); *
} * 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. */ /** @brief Smashes an enum value to its underlying type. */
template<typename E> template<typename E>

Loading…
Cancel
Save