2017-08-14 12:47:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-08-14 12:47:46 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-08-14 12:47:46 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-07-10 15:03:15 +00:00
|
|
|
#include <QtCore/QLatin1String>
|
|
|
|
|
2017-08-14 12:47:46 +00:00
|
|
|
namespace base {
|
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
template <typename Type>
|
|
|
|
inline Type take(Type &value) {
|
|
|
|
return std::exchange(value, Type {});
|
|
|
|
}
|
|
|
|
|
2017-11-23 09:58:12 +00:00
|
|
|
template <typename Type>
|
|
|
|
inline Type duplicate(const Type &value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
template <typename Type, size_t Size>
|
|
|
|
inline constexpr size_t array_size(const Type(&)[Size]) {
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2019-01-15 11:57:45 +00:00
|
|
|
template <typename Container, typename T>
|
|
|
|
inline bool contains(const Container &container, const T &value) {
|
|
|
|
const auto end = std::end(container);
|
|
|
|
return std::find(std::begin(container), end, value) != end;
|
|
|
|
}
|
|
|
|
|
2019-09-13 12:22:54 +00:00
|
|
|
template <typename D, typename T>
|
|
|
|
inline constexpr D up_cast(T object) {
|
|
|
|
using DV = std::decay_t<decltype(*D())>;
|
|
|
|
using TV = std::decay_t<decltype(*T())>;
|
|
|
|
if constexpr (std::is_base_of_v<DV, TV>) {
|
|
|
|
return object;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:24:33 +00:00
|
|
|
// We need a custom comparator for set<std::unique_ptr<T>>::find to work with pointers.
|
|
|
|
// thanks to http://stackoverflow.com/questions/18939882/raw-pointer-lookup-for-sets-of-unique-ptrs
|
|
|
|
template <typename T>
|
|
|
|
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<T*>`
|
|
|
|
// to do the comparison:
|
|
|
|
struct helper {
|
|
|
|
const T *ptr = nullptr;
|
|
|
|
helper() = default;
|
|
|
|
helper(const helper &other) = default;
|
|
|
|
helper(const T *p) : ptr(p) {
|
|
|
|
}
|
|
|
|
template <typename ...Ts>
|
|
|
|
helper(const std::shared_ptr<Ts...> &other) : ptr(other.get()) {
|
|
|
|
}
|
|
|
|
template <typename ...Ts>
|
|
|
|
helper(const std::unique_ptr<Ts...> &other) : ptr(other.get()) {
|
|
|
|
}
|
|
|
|
bool operator<(helper other) const {
|
|
|
|
return std::less<const T*>()(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-08-14 12:47:46 +00:00
|
|
|
} // namespace base
|
2018-08-08 20:11:28 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void accumulate_max(T &a, const T &b) { if (a < b) a = b; }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void accumulate_min(T &a, const T &b) { if (a > b) a = b; }
|
|
|
|
|
|
|
|
template <size_t Size>
|
|
|
|
QLatin1String qstr(const char(&string)[Size]) {
|
|
|
|
return QLatin1String(string, Size - 1);
|
|
|
|
}
|