File indexing completed on 2024-04-28 15:31:26

0001 /*
0002  *   SPDX-FileCopyrightText: 2008-2009 Petri Damstén <damu@iki.fi>
0003  *   SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
0004  *
0005  *   SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #ifndef KUNITCONVERSION_CONVERTER_H
0009 #define KUNITCONVERSION_CONVERTER_H
0010 
0011 #include <kunitconversion/kunitconversion_export.h>
0012 
0013 #include "unitcategory.h"
0014 
0015 #include <QExplicitlySharedDataPointer>
0016 
0017 namespace KUnitConversion
0018 {
0019 class Value;
0020 class UnitCategory;
0021 class ConverterPrivate;
0022 
0023 /**
0024  * @short Class for converting values between units of measurement
0025  *
0026  * This is a class to convert values between different units of measurement.
0027  *
0028  * @see Unit, UnitCategory, Value
0029  *
0030  * @author Petri Damstén <damu@iki.fi>
0031  * @author John Layt <jlayt@kde.org>
0032  */
0033 
0034 class KUNITCONVERSION_EXPORT Converter
0035 {
0036 public:
0037     /**
0038      * Creates a Converter instance.
0039      */
0040     Converter();
0041     /**
0042      * Destroys this Converter instance.
0043      */
0044     ~Converter();
0045     /**
0046      * Copy constructor.
0047      * @param other existing Converter instance.
0048      */
0049     Converter(const Converter &other);
0050 
0051     /**
0052      * Assignment operator, assign @p other to this.
0053      **/
0054     Converter &operator=(const Converter &other);
0055 
0056     // TODO KF6 de-inline
0057 #ifdef Q_COMPILER_RVALUE_REFS
0058     /**
0059      * Move-assigns @p other to this Converter instance, transferring the
0060      * ownership of the managed pointer to this instance.
0061      **/
0062     Converter &operator=(Converter &&other)
0063     {
0064         swap(other);
0065         return *this;
0066     }
0067 #endif
0068 
0069     // TODO KF6 remove
0070     /**
0071      * Swaps this Converter with @p other. This function is very fast and never fails.
0072      **/
0073     void swap(Converter &other)
0074     {
0075         d.swap(other.d);
0076     }
0077 
0078 #if KUNITCONVERSION_ENABLE_DEPRECATED_SINCE(5, 91)
0079     /**
0080      * @return @c true if this Converter is equal to the @p other Converter.
0081      * @deprecated since 5.91, result is always true
0082      **/
0083     KUNITCONVERSION_DEPRECATED_VERSION(5, 91, "result is always true")
0084     bool operator==(const Converter &other) const;
0085 
0086     /**
0087      * @return @c true if this Converter is not equal to the @p other Converter.
0088      * @deprecated since 5.91, result is always false
0089      **/
0090     KUNITCONVERSION_DEPRECATED_VERSION(5, 91, "result is always false")
0091     bool operator!=(const Converter &other) const;
0092 #endif
0093 
0094     /**
0095      * Convert value to another unit.
0096      *
0097      * @param value value to convert
0098      * @param toUnit unit to convert to. If empty default unit is used.
0099      * @return converted value
0100      **/
0101     Value convert(const Value &value, const QString &toUnit = QString()) const;
0102     Value convert(const Value &value, UnitId toUnit) const;
0103     Value convert(const Value &value, const Unit &toUnit) const;
0104 
0105     /**
0106      * Find unit category for unit.
0107      *
0108      * @param unit unit to find category for.
0109      * @return unit category for unit
0110      **/
0111     UnitCategory categoryForUnit(const QString &unit) const;
0112 
0113     /**
0114      * Find unit for string unit.
0115      *
0116      * @param unitString unit string to find unit for.
0117      * @return unit for string unit
0118      **/
0119     Unit unit(const QString &unitString) const;
0120 
0121     /**
0122      * Find unit for unit enum.
0123      *
0124      * @param unitId unit enum to find unit for.
0125      * @return unit for string unit
0126      **/
0127     Unit unit(UnitId unitId) const;
0128 
0129     /**
0130      * Find unit category.
0131      *
0132      * @param category name of the category to find (length, area, mass, etc.).
0133      * @return unit category named category or invalid category.
0134      **/
0135     UnitCategory category(const QString &category) const;
0136 
0137     /**
0138      * Find unit category.
0139      *
0140      * @param categoryId id of the category to find (LengthCategory, AreaCategory, etc.).
0141      * @return unit category which id is categoryId or invalid category.
0142      **/
0143     UnitCategory category(CategoryId categoryId) const;
0144 
0145     /**
0146      * Returns a list of all unit categories.
0147      *
0148      * @return list of unit categories.
0149      **/
0150     QList<UnitCategory> categories() const;
0151 
0152 private:
0153     QExplicitlySharedDataPointer<ConverterPrivate> d;
0154 };
0155 
0156 } // KUnitConversion namespace
0157 
0158 #endif