// This file is part of Desktop App Toolkit, // a set of libraries for developing nice desktop applications. // // For license and copyright information please follow this link: // https://github.com/desktop-app/legal/blob/master/LEGAL // #pragma once #include #include #include namespace base { template inline constexpr Type take(Type &value) noexcept { return std::exchange(value, Type {}); } template inline constexpr Type duplicate(const Type &value) { return value; } template inline constexpr size_t array_size(const Type(&)[Size]) noexcept { return Size; } template inline bool contains(const Container &container, const T &value) { const auto end = std::end(container); return std::find(std::begin(container), end, value) != end; } template inline void reorder(Container &container, int oldPosition, int newPosition) { const auto b = container.begin(); if (oldPosition < newPosition) { std::rotate( b + oldPosition, b + oldPosition + 1, b + newPosition + 1); } else if (newPosition < oldPosition) { std::rotate( b + newPosition, b + oldPosition, b + oldPosition + 1); } } [[nodiscard]] inline int reorder_index( int index, int oldPosition, int newPosition) { return (index == oldPosition) ? newPosition : (oldPosition < index && index <= newPosition) ? (index - 1) : (oldPosition > index && index >= newPosition) ? (index + 1) : index; } template inline constexpr D up_cast(T object) { using DV = std::decay_t; using TV = std::decay_t; if constexpr (std::is_base_of_v) { return object; } else { return nullptr; } } // We need a custom comparator for set>::find to work with pointers. // thanks to http://stackoverflow.com/questions/18939882/raw-pointer-lookup-for-sets-of-unique-ptrs template struct pointer_comparator { using is_transparent = std::true_type; // helper does some magic in order to reduce the number of // pairs of types we need to know how to compare: it turns // everything into a pointer, and then uses `std::less` // to do the comparison: struct helper { const T *ptr = nullptr; helper() = default; helper(const helper &other) = default; helper(const T *p) : ptr(p) { } template helper(const std::shared_ptr &other) : ptr(other.get()) { } template helper(const std::unique_ptr &other) : ptr(other.get()) { } bool operator<(helper other) const { return std::less()(ptr, other.ptr); } }; // without helper, we'd need 2^n different overloads, where // n is the number of types we want to support (so, 8 with // raw pointers, unique pointers, and shared pointers). That // seems silly. // && helps enforce rvalue use only bool operator()(const helper &&lhs, const helper &&rhs) const { return lhs < rhs; } }; inline QString FromUtf8Safe(const char *string, int size = -1) { if (!string || !size) { return QString(); } else if (size < 0) { size = strlen(string); } const auto result = QString::fromUtf8(string, size); const auto back = result.toUtf8(); return (back.size() != size || memcmp(back.constData(), string, size)) ? QString::fromLocal8Bit(string, size) : result; } inline QString FromUtf8Safe(const QByteArray &string) { return FromUtf8Safe(string.constData(), string.size()); } [[nodiscard]] double SafeRound(double value); [[nodiscard]] QString CleanAndSimplify(QString text); template [[nodiscard]] bool OppositeSigns(T a, T b) { // For zero returns false! return (a < 0 && b > 0) || (a > 0 && b < 0); } } // namespace base template inline void accumulate_max(T &a, const T &b) { if (a < b) a = b; } template inline void accumulate_min(T &a, const T &b) { if (a > b) a = b; } template QLatin1String qstr(const char(&string)[Size]) { return QLatin1String(string, int(Size) - 1); }