[libcalamares] Document NamedSuffix

- Add more constructors
 - Make the table-constructor public, no need to force
   subclassing on everyone
 - Declare some useful API
 - Document the bits.
This commit is contained in:
Adriaan de Groot 2019-01-14 11:50:06 +01:00
parent ef33aa01f3
commit 99a19c7c6b

View File

@ -17,6 +17,23 @@
*/ */
/** @brief Support for unit-suffixed values. /** @brief Support for unit-suffixed values.
*
* This combines a value with an (enum) unit indicating what kind
* of value it is, e.g. 10 meters, or 64 pixels. Includes simple
* parsing support for the values written as strings like <value><unit>,
* e.g. "10m" or "64px".
*
* When a suffixed unit value needs validation, define an isValid()
* method; similarly for simple construction from a string (with a fixed
* table of suffixes). Typical use then looks like:
*
* class MyUnit : public NamedSuffix<MyUnitEnum, MyUnitEnum::None>
* {
* public:
* using NamedSuffix::NamedSuffix; // Keep existing constructors
* MyUnit( const QString& s );
* bool isValid() const;
* } ;
*/ */
#ifndef LIBCALAMARES_NAMEDSUFFIX_H #ifndef LIBCALAMARES_NAMEDSUFFIX_H
@ -24,24 +41,34 @@
#include "NamedEnum.h" #include "NamedEnum.h"
template<typename T, T none> /** @brief Template that takes the enum type to work with and a special none-enum. */
template<typename T, T _none>
class NamedSuffix class NamedSuffix
{ {
public: public:
using unit_t = T; using unit_t = T;
static constexpr unit_t none = _none;
/** @brief Empty value. */
NamedSuffix() NamedSuffix()
: m_value(0) : m_value(0)
, m_unit( none ) , m_unit( none )
{ {
} }
int value() const { return m_value; } /** @brief Specific value and unit. */
T unit() const { return m_unit; } NamedSuffix( int value, unit_t unit )
: m_value( value )
, m_unit( unit )
{
}
bool isValid() const { return m_unit != none; } /** @brief Construct value and unit from string.
*
protected: * This parses the given string @p s by comparing with the suffixes
* in @p table and uses the first matching suffix as the unit.
*/
NamedSuffix( const NamedEnumTable<T>& table, const QString& s ) NamedSuffix( const NamedEnumTable<T>& table, const QString& s )
: NamedSuffix() : NamedSuffix()
{ {
@ -54,8 +81,27 @@ protected:
} }
} }
/** @brief Construct value from string.
*
* This is not defined in the template, because it should probably
* delegate to the constructor above with a fixed table.
*/
NamedSuffix( const QString& s );
int value() const { return m_value; }
unit_t unit() const { return m_unit; }
/** @brief Check that a value-unit combination is valid.
*
* This is not defined in the template, because validity (e.g.
* range of acceptable values) depends on the kind of unit.
*/
bool isValid() const;
protected:
int m_value; int m_value;
T m_unit; unit_t m_unit;
}; };