1 // This file is part of Desktop App Toolkit,
2 // a set of libraries for developing nice desktop applications.
3 //
4 // For license and copyright information please follow this link:
5 // https://github.com/desktop-app/legal/blob/master/LEGAL
6 //
7 #pragma once
8 
9 #include <QtCore/QLatin1String>
10 #include <memory>
11 
12 namespace base {
13 
14 template <typename Type>
take(Type & value)15 inline constexpr Type take(Type &value) noexcept {
16 	return std::exchange(value, Type {});
17 }
18 
19 template <typename Type>
duplicate(const Type & value)20 inline constexpr Type duplicate(const Type &value) {
21 	return value;
22 }
23 
24 template <typename Type, size_t Size>
array_size(const Type (&)[Size])25 inline constexpr size_t array_size(const Type(&)[Size]) noexcept {
26 	return Size;
27 }
28 
29 template <typename Container, typename T>
contains(const Container & container,const T & value)30 inline bool contains(const Container &container, const T &value) {
31 	const auto end = std::end(container);
32 	return std::find(std::begin(container), end, value) != end;
33 }
34 
35 template <typename Container>
reorder(Container & container,int oldPosition,int newPosition)36 inline void reorder(Container &container, int oldPosition, int newPosition) {
37 	const auto b = begin(container);
38 	if (oldPosition < newPosition) {
39 		std::rotate(
40 			b + oldPosition,
41 			b + oldPosition + 1,
42 			b + newPosition + 1);
43 	} else if (newPosition < oldPosition) {
44 		std::rotate(
45 			b + newPosition,
46 			b + oldPosition,
47 			b + oldPosition + 1);
48 	}
49 
50 }
51 
52 template <typename D, typename T>
up_cast(T object)53 inline constexpr D up_cast(T object) {
54 	using DV = std::decay_t<decltype(*D())>;
55 	using TV = std::decay_t<decltype(*T())>;
56 	if constexpr (std::is_base_of_v<DV, TV>) {
57 		return object;
58 	} else {
59 		return nullptr;
60 	}
61 }
62 
63 // We need a custom comparator for set<std::unique_ptr<T>>::find to work with pointers.
64 // thanks to http://stackoverflow.com/questions/18939882/raw-pointer-lookup-for-sets-of-unique-ptrs
65 template <typename T>
66 struct pointer_comparator {
67 	using is_transparent = std::true_type;
68 
69 	// helper does some magic in order to reduce the number of
70 	// pairs of types we need to know how to compare: it turns
71 	// everything into a pointer, and then uses `std::less<T*>`
72 	// to do the comparison:
73 	struct helper {
74 		const T *ptr = nullptr;
75 		helper() = default;
76 		helper(const helper &other) = default;
helperpointer_comparator::helper77 		helper(const T *p) : ptr(p) {
78 		}
79 		template <typename ...Ts>
helperpointer_comparator::helper80 		helper(const std::shared_ptr<Ts...> &other) : ptr(other.get()) {
81 		}
82 		template <typename ...Ts>
helperpointer_comparator::helper83 		helper(const std::unique_ptr<Ts...> &other) : ptr(other.get()) {
84 		}
85 		bool operator<(helper other) const {
86 			return std::less<const T*>()(ptr, other.ptr);
87 		}
88 	};
89 
90 	// without helper, we'd need 2^n different overloads, where
91 	// n is the number of types we want to support (so, 8 with
92 	// raw pointers, unique pointers, and shared pointers).  That
93 	// seems silly.
94 	// && helps enforce rvalue use only
operatorpointer_comparator95 	bool operator()(const helper &&lhs, const helper &&rhs) const {
96 		return lhs < rhs;
97 	}
98 
99 };
100 
101 inline QString FromUtf8Safe(const char *string, int size = -1) {
102 	if (!string || !size) {
103 		return QString();
104 	} else if (size < 0) {
105 		size = strlen(string);
106 	}
107 	const auto result = QString::fromUtf8(string, size);
108 	const auto back = result.toUtf8();
109 	return (back.size() != size || memcmp(back.constData(), string, size))
110 		? QString::fromLocal8Bit(string, size)
111 		: result;
112 }
113 
FromUtf8Safe(const QByteArray & string)114 inline QString FromUtf8Safe(const QByteArray &string) {
115 	return FromUtf8Safe(string.constData(), string.size());
116 }
117 
118 [[nodiscard]] double SafeRound(double value);
119 
120 [[nodiscard]] QString CleanAndSimplify(QString text);
121 
122 } // namespace base
123 
124 template <typename T>
accumulate_max(T & a,const T & b)125 inline void accumulate_max(T &a, const T &b) { if (a < b) a = b; }
126 
127 template <typename T>
accumulate_min(T & a,const T & b)128 inline void accumulate_min(T &a, const T &b) { if (a > b) a = b; }
129 
130 template <size_t Size>
qstr(const char (& string)[Size])131 QLatin1String qstr(const char(&string)[Size]) {
132 	return QLatin1String(string, int(Size) - 1);
133 }
134