diff --git a/src/libcalamares/utils/NamedSuffix.h b/src/libcalamares/utils/NamedSuffix.h new file mode 100644 index 000000000..db87026ae --- /dev/null +++ b/src/libcalamares/utils/NamedSuffix.h @@ -0,0 +1,62 @@ +/* === 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 unit-suffixed values. + */ + +#ifndef LIBCALAMARES_NAMEDSUFFIX_H +#define LIBCALAMARES_NAMEDSUFFIX_H + +#include "NamedEnum.h" + +template +class NamedSuffix +{ +public: + using unit_t = T; + + NamedSuffix() + : m_value(0) + , m_unit( none ) + { + } + + int value() const { return m_value; } + T unit() const { return m_unit; } + + bool isValid() const { return m_unit != none; } + +protected: + NamedSuffix( const NamedEnumTable& table, const QString& s ) + : NamedSuffix() + { + for( const auto suffix : table.table ) + if ( s.endsWith( suffix.first ) ) + { + m_value = s.left( s.length() - suffix.first.length() + 1 ).toInt(); + m_unit = suffix.second; + break; + } + } + + int m_value; + T m_unit; +}; + + +#endif