File indexing completed on 2024-04-28 16:49:37

0001 /*
0002 *  Copyright 2018  Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock is distributed in the hope that it will be useful,
0012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 *  GNU General Public License for more details.
0015 *
0016 *  You should have received a copy of the GNU General Public License
0017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #ifndef EXTRAS_H
0021 #define EXTRAS_H
0022 
0023 // local
0024 #include "../liblatte2/config-latte-lib.h"
0025 
0026 // C++
0027 #include <type_traits>
0028 #include <numeric>
0029 #include <memory>
0030 #include <cmath>
0031 
0032 // Qt
0033 #include <QObject>
0034 #include <QString>
0035 #include <QStringBuilder>
0036 #include <QRect>
0037 #include <QMetaEnum>
0038 #include <QMetaType>
0039 
0040 // Plasma
0041 #include <Plasma>
0042 
0043 //! There are gcc versions that don't support yet that function even though they
0044 //! publish themselves as C++14 compatible. Such a case is gcc 4.8.x that openSUSE
0045 //! LEAP 42.2-3 is using. By enabling this flag such systems can be build correctly.
0046 #if ENABLE_MAKE_UNIQUE
0047 namespace std {
0048 template<class T, class... Args>
0049 unique_ptr<T> make_unique(Args &&... args)
0050 {
0051     return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
0052 }
0053 }
0054 #endif
0055 
0056 /*!
0057  * @brief convert a QRect to a QString with format `(<x>, <y>) <width>x<height>`
0058  */
0059 inline QString qRectToStr(const QRect &r)
0060 {
0061     return "(" % QString::number(r.x()) % ", " % QString::number(r.y()) % ") "
0062            % QString::number(r.width()) % "x" % QString::number(r.height());
0063 }
0064 
0065 /*!
0066  * @brief convert a `Q_ENUM` to c-string
0067  */
0068 template<typename EnumType>
0069 inline const char *qEnumToStr(EnumType value)
0070 {
0071     return QMetaEnum::fromType<EnumType>().valueToKey(value);
0072 }
0073 
0074 /*!
0075  * @brief convert a `Q_ENUMS` of `Plasma::Types::Location` to c-string
0076  */
0077 inline const char *qEnumToStr(Plasma::Types::Location Enum)
0078 {
0079     static const int Index = Plasma::Types::staticMetaObject.indexOfEnumerator("Location");
0080     return Plasma::Types::staticMetaObject.enumerator(Index).valueToKey(Enum);
0081 }
0082 
0083 /*!
0084  * @brief convert a `Q_ENUMS` of `Plasma::Types::FormFactor` to c-string
0085  */
0086 inline const char *qEnumToStr(Plasma::Types::FormFactor Enum)
0087 {
0088     static const int Index = Plasma::Types::staticMetaObject.indexOfEnumerator("FormFactor");
0089     return Plasma::Types::staticMetaObject.enumerator(Index).valueToKey(Enum);
0090 }
0091 
0092 /*!
0093  * @brief machine epsilon
0094  */
0095 template<class T>
0096 typename std::enable_if < !std::is_integral<T>(), bool >::type almost_equal(T x, T y, int ulp)
0097 {
0098     return std::abs(x - y) < std::numeric_limits<T>::epsilon() * std::abs(x + y) * ulp
0099            || std::abs(x - y) < std::numeric_limits<T>::min();
0100 }
0101 
0102 #endif // EXTRAS_H