From 78a504998bfd005992d875071f0a58eb5b4b1a59 Mon Sep 17 00:00:00 2001 From: Johan Smith Agudelo Rodriguez Date: Sun, 25 Dec 2016 18:09:56 -0500 Subject: [PATCH] utilities functions --- liblattedock/extras.cpp | 1 + liblattedock/extras.h | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 liblattedock/extras.cpp create mode 100644 liblattedock/extras.h diff --git a/liblattedock/extras.cpp b/liblattedock/extras.cpp new file mode 100644 index 000000000..2ae40bff2 --- /dev/null +++ b/liblattedock/extras.cpp @@ -0,0 +1 @@ +#include "extras.h" diff --git a/liblattedock/extras.h b/liblattedock/extras.h new file mode 100644 index 000000000..9d7417be0 --- /dev/null +++ b/liblattedock/extras.h @@ -0,0 +1,73 @@ +#ifndef EXTRAS_H +#define EXTRAS_H + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#if __GLIBCXX__ <= 20150623 +namespace std { +template +unique_ptr make_unique(Args &&... args) +{ + return std::unique_ptr(new T(std::forward(args)...)); +} +} +#endif + +/*! + * @brief convert a QRect to a QString with format `(, ) x` + */ +inline QString qRectToStr(const QRect &r) +{ + return "(" % QString::number(r.x()) % ", " % QString::number(r.y()) % ") " + % QString::number(r.width()) % "x" % QString::number(r.height()); +} + +/*! + * @brief convert a `Q_ENUM` to c-string + */ +template +inline const char *qEnumToStr(EnumType value) +{ + return QMetaEnum::fromType().valueToKey(value); +} + +/*! + * @brief convert a `Q_ENUMS` of `Plasma::Types::Location` to c-string + */ +inline const char *qEnumToStr(Plasma::Types::Location Enum) +{ + static const int Index = Plasma::Types::staticMetaObject.indexOfEnumerator("Location"); + return Plasma::Types::staticMetaObject.enumerator(Index).valueToKey(Enum); +} + +/*! + * @brief convert a `Q_ENUMS` of `Plasma::Types::FormFactor` to c-string + */ +inline const char *qEnumToStr(Plasma::Types::FormFactor Enum) +{ + static const int Index = Plasma::Types::staticMetaObject.indexOfEnumerator("FormFactor"); + return Plasma::Types::staticMetaObject.enumerator(Index).valueToKey(Enum); +} + +/*! + * @brief machine epsilon + */ +template +typename std::enable_if(), bool>::type almost_equal(T x, T y, int ulp) +{ + return std::abs(x - y) < std::numeric_limits::epsilon() * std::abs(x + y) * ulp + || std::abs(x - y) < std::numeric_limits::min(); +} + +#endif // EXTRAS_H