From e28aeebb4890543979081c3f58617193669445e9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 11 Jan 2019 19:48:11 +0100 Subject: [PATCH] [libcalamares] Introduce helper NamedSuffix template For (all?) those cases where we have configuration with a value followed by a unit, introduce a class that uses the NamedEnum properties to make parsing and split-up easier. --- src/libcalamares/utils/NamedSuffix.h | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/libcalamares/utils/NamedSuffix.h 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