libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2016 Kitsune Ral <kitsune-ral@users.sf.net>
2// SPDX-FileCopyrightText: 2019 Alexey Andreyev <aa13q@ya.ru>
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#pragma once
6
8
9#include <QtCore/QDebug>
10#include <QtCore/QElapsedTimer>
11#include <QtCore/QFuture>
12#include <QtCore/QHashFunctions>
13#include <QtCore/QLatin1String>
14#include <QtCore/QScopedPointer>
15#include <QtCore/QUrl>
16
17#include <memory>
18#include <optional>
19#include <source_location>
20#include <unordered_map>
21
22#define DECL_DEPRECATED_ENUMERATOR(Deprecated, Recommended)
23 Deprecated Q_DECL_ENUMERATOR_DEPRECATED_X("Use " #Recommended) = Recommended
24
25/// \brief Copy an object with slicing
26///
27/// Unintended slicing is bad, which is why there's a C++ Core Guideline that
28/// basically says "don't slice, or if you do, make it explicit". Sonar and
29/// clang-tidy have warnings matching this guideline; unfortunately, those
30/// warnings trigger even when you have a dedicated method (as the guideline
31/// recommends) that makes a slicing copy.
32///
33/// This macro is meant for cases when slicing is intended: the static cast
34/// silences the static analysis warning, and the macro appearance itself makes
35/// it very clear that slicing is wanted here. It is made as a macro
36/// (not as a function template) to support the case of private inheritance
37/// in which a function template would not be able to cast to the private base
38/// (see Uri::toUrl() for an example of just that situation).
39#define SLICE(Object, ToType) ToType{static_cast<const ToType&>(Object)}
40
41namespace Quotient {
42
43namespace _impl {
44 template <typename S>
45 constexpr inline auto toUtf8(S&& s)
46 {
47 if constexpr (std::convertible_to<S, std::string_view>)
48 return std::string_view(std::forward<S>(s));
49 else if constexpr (std::convertible_to<S, QByteArray>)
50 return QByteArray(std::forward<S>(s));
51 else //if constexpr (std::convertible_to<S, QString>)
52 return QString(std::forward<S>(s)).toUtf8();
53 }
54}
55
56//! \brief q(Utf8)Printable that can handle more than just QStrings
57//!
58//! This macro accepts all kinds of string-like input, from const char* all the way to raw
59//! QStringBuilder constructs. It returns a `const char*` pointer to a UTF-8 string; if the original
60//! input was QChar/u16-based, it creates a temporary buffer to store the UTF-8 representation that
61//! is destroyed once the statement containing QUO_CSTR() is done (therefore, ALWAYS copy the result
62//! based on QUO_CSTR() contents if you need to store it).
63#define QUO_CSTR(StringConvertible_) std::data(::Quotient::_impl::toUtf8(StringConvertible_))
64
65inline bool alarmX(bool alarmCondition, const auto& msg,
66 [[maybe_unused]] std::source_location loc = std::source_location::current())
67{
68 if (alarmCondition) [[unlikely]] {
69 qt_assert_x(loc.function_name(), QUO_CSTR(msg), loc.file_name(), loc.line());
70 qCritical() << msg;
71 }
72 return alarmCondition;
73}
74
75//! \brief A negative assertion facility that can be put in an if statement
76//!
77//! Unlike most other assertion functions or macros, this doesn't collapse to no-op in Release
78//! builds; rather, it sends a critical level message to the log and returns true so that you could
79//! immediately return or do some other damage control for Release builds. Also unlike most other
80//! assertion functions, \p AlarmCondition is the condition for failure, not for health; in other
81//! words, \p Message is sent to logs (and, in Debug configuration, the assertion fails)
82//! if \p AlarmCondition holds, not the other way around.
83//!
84//! This macro is a trivial wrapper around alarmX(), provided for API uniformity with QUO_ALARM()
85#define QUO_ALARM_X(...) ::Quotient::alarmX(__VA_ARGS__)
86
87#define QUO_ALARM(...) ::Quotient::alarmX((__VA_ARGS__) ? true : false, "Alarm: " #__VA_ARGS__)
88
89//! Evaluate the boolean expression and, in Debug mode, assert it to be true
90#define QUO_CHECK(...)
91 !::Quotient::alarmX(!(__VA_ARGS__) ? true : false, "Failing expression: " #__VA_ARGS__)
92
93//! A substitute for QtFuture::makeReadyVoidFuture() for compatibility with Qt pre-6.6
95{
96#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
97 return QtFuture::makeReadyVoidFuture();
98#else
99 return QtFuture::makeReadyFuture<void>();
100#endif
101}
102
103//! A substitute for QtFuture::makeReadyValueFuture() for compatibility with Qt pre-6.6
104template <typename T>
105inline auto makeReadyValueFuture(T&& value)
106{
107#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
108 return QtFuture::makeReadyValueFuture(std::forward<T>(value));
109#else
110 return QtFuture::makeReadyFuture(std::forward<T>(value));
111#endif
112}
113
114inline namespace Literals { using namespace Qt::Literals; }
115
116#if Quotient_VERSION_MAJOR == 0 && Quotient_VERSION_MINOR < 10
117/// This is only to make UnorderedMap alias work until we get rid of it
118template <typename T>
119struct HashQ {
120 size_t operator()(const T& s) const Q_DECL_NOEXCEPT
121 {
122 return qHash(s, uint(QHashSeed::globalSeed()));
123 }
124};
125/// A wrapper around std::unordered_map compatible with types that have qHash
126template <typename KeyT, typename ValT>
128 [[deprecated("Use std::unordered_map directly")]] = std::unordered_map<KeyT, ValT, HashQ<KeyT>>;
129
130template <typename ArrayT>
131class [[deprecated("Use std::ranges::subrange instead")]] Range {
132 using iterator = typename ArrayT::iterator;
133 using const_iterator = typename ArrayT::const_iterator;
134 using size_type = typename ArrayT::size_type;
135
136public:
137 constexpr Range(ArrayT& arr) : from(std::begin(arr)), to(std::end(arr)) {}
138 constexpr Range(iterator from, iterator to) : from(from), to(to) {}
139
140 constexpr size_type size() const
141 {
142 Q_ASSERT(std::distance(from, to) >= 0);
143 return size_type(std::distance(from, to));
144 }
145 constexpr bool empty() const { return from == to; }
146 constexpr const_iterator begin() const { return from; }
147 constexpr const_iterator end() const { return to; }
148 constexpr iterator begin() { return from; }
149 constexpr iterator end() { return to; }
150
151private:
152 iterator from;
153 iterator to;
154};
155
156namespace _impl {
157 template <typename T>
158 concept Holds_NonConst_LValue_Ref =
159 std::is_lvalue_reference_v<T> && !std::is_const_v<std::remove_reference<T>>;
160}
161
162//! \brief An adaptor for Qt (hash-)maps to make them iterable in STL style
163//!
164//! QMap/QHash container iterators returned by begin() and end() dereference
165//! to values, unlike STL where similar iterators dereference to key-value
166//! pairs. It is a problem in range-for if you want to also access map keys.
167//! This adaptor allows to use range-for syntax with access to both keys and
168//! values in QMap/QHash containers. Just use
169//! `for (auto&& [key, value] : asKeyValueRange(myMap)` instead of
170//! `for (auto&& value : myMap)`.
171//! \note When an rvalue is passed as the constructor argument, asKeyValueRange
172//! shallow-copies the map object to ensure its lifetime is maintained
173//! throughout the loop; with lvalues, no copying occurs, assuming that
174//! the map object outlives the range-for loop
175template <typename T>
176class [[deprecated(
177 "Use the member function with the same name of the respective container")]] asKeyValueRange {
178public:
179 explicit asKeyValueRange(T data)
180 : m_data { data }
181 {}
182
184 {
185 return m_data.keyValueBegin();
186 }
188 {
189 return m_data.keyValueEnd();
190 }
191 auto begin() const { return m_data.keyValueBegin(); }
192 auto end() const { return m_data.keyValueEnd(); }
193
194private:
195 T m_data;
196};
197
198// GCC complains about a deprecation even on deduction hints, so here's to suppress noise
200template <typename U>
202
203template <typename U>
205)
206#endif
207
208/** A replica of std::find_first_of that returns a pair of iterators
209 *
210 * Convenient for cases when you need to know which particular "first of"
211 * [sFirst, sLast) has been found in [first, last).
212 */
213template <typename InputIt, typename ForwardIt, typename Pred>
217{
218 for (; first != last; ++first)
219 for (auto it = sFirst; it != sLast; ++it)
220 if (pred(*first, *it))
221 return { first, it };
222
223 return { last, sLast };
224}
225
226//! \brief Common custom deleter for std::unique_ptr and QScopedPointer
227//!
228//! Since Qt 6, this is merely an alias for QScopedPointerDeleteLater (which is suitable
229//! for std::unique_ptr too).
231
232template <std::derived_from<QObject> T>
234
235//! \brief An owning implementation pointer
236//!
237//! This is basically std::unique_ptr<> to hold your pimpl's but without having
238//! to define default constructors/operator=() out of line.
239//! Thanks to https://oliora.github.io/2015/12/29/pimpl-and-rule-of-zero.html
240//! for inspiration
241template <typename ImplType, typename TypeToDelete = ImplType>
242using ImplPtr = std::unique_ptr<ImplType, void (*)(TypeToDelete*)>;
243
244// Why this works (see also the link above): because this defers the moment
245// of requiring sizeof of ImplType to the place where makeImpl is invoked
246// (which is located, necessarily, in the .cpp file after ImplType definition).
247// The stock unique_ptr deleter (std::default_delete) normally needs sizeof
248// at the same spot - as long as you defer definition of the owning type
249// constructors and operator='s to the .cpp file as well. Which means you
250// have to explicitly declare and define them (even if with = default),
251// formally breaking the rule of zero; informally, just adding boilerplate code.
252// The custom deleter itself is instantiated at makeImpl invocation - there's
253// no way earlier to even know how ImplType will be deleted and whether that
254// will need sizeof(ImplType) earlier. In theory it's a tad slower because
255// the deleter is called by the pointer; however, the difference will not
256// be noticeable (if exist at all) for any class with non-trivial contents.
257
258//! \brief make_unique for ImplPtr
259//!
260//! Since std::make_unique is not compatible with ImplPtr, this should be used
261//! in constructors of frontend classes to create implementation instances.
262template <typename ImplType, typename TypeToDelete = ImplType, typename... ArgTs>
263inline ImplPtr<ImplType, TypeToDelete> makeImpl(ArgTs&&... args)
264{
265 return ImplPtr<ImplType, TypeToDelete> {
266 new ImplType{std::forward<ArgTs>(args)...},
267 [](TypeToDelete* impl) { delete impl; }
268 };
269}
270
271template <typename ImplType, typename TypeToDelete = ImplType>
272inline ImplPtr<ImplType, TypeToDelete> acquireImpl(ImplType* from)
273{
274 return ImplPtr<ImplType, TypeToDelete> { from, [](TypeToDelete* impl) {
275 delete impl;
276 } };
277}
278
279template <typename ImplType, typename TypeToDelete = ImplType>
281{
282 return { nullptr, [](TypeToDelete*) { /* nullptr doesn't need deletion */ } };
283}
284
285template <typename T>
287 size_t (*destructor)(T*);
288
289 void operator()(T* toDelete)
290 {
291 destructor(toDelete);
292 delete[] reinterpret_cast<std::byte*>(toDelete);
293 }
294};
295
296//! \brief An owning pointer to a C structure
297//!
298//! This is intented to ease lifecycle management of Olm structures.
299//! \sa makeCStruct
300template <typename T>
301using CStructPtr = std::unique_ptr<T, CStructDeleter<T>>;
302
303//! \brief Create a C structure with pre-programmed deletion logic
304//!
305//! This facility function creates a CStructPtr that owns the pointer returned
306//! by \p constructor. The memory passed to \p constructor is allocated
307//! as an array of bytes; the size of that array is determined by calling
308//! \p sizeFn. Finally, since the returned pointer is owning, it also stores
309//! the corresponding CStructDeleter instance; when called at destruction of
310//! the owning pointer, this deleter first calls \p destructor passing the
311//! original C pointer returned by \p constructor; and then deletes the
312//! allocated array of bytes.
313template <typename T>
314inline auto makeCStruct(T* (*constructor)(void*), size_t (*sizeFn)(),
315 auto destructor)
316{
317 return CStructPtr<T>{ constructor(new std::byte[sizeFn()]), { destructor } };
318}
319
320//! \brief Multiplex several functors in one
321//!
322//! This is a well-known trick to wrap several lambdas into a single functor
323//! class that can be passed to std::visit.
324//! \sa https://en.cppreference.com/w/cpp/utility/variant/visit
325template <typename... FunctorTs>
326struct Overloads : FunctorTs... {
327 using FunctorTs::operator()...;
328};
329
330template <typename... FunctorTs>
331Overloads(FunctorTs&&...) -> Overloads<FunctorTs...>;
332
333/** Convert what looks like a URL or a Matrix ID to an HTML hyperlink */
334QUOTIENT_API void linkifyUrls(QString& htmlEscapedText);
335
336/** Sanitize the text before showing in HTML
337 *
338 * This does toHtmlEscaped() and removes Unicode BiDi marks.
339 */
341
342/** Pretty-print plain text into HTML
343 *
344 * This includes HTML escaping of <,>,",& and calling linkifyUrls()
345 */
347
348/** Return a path to cache directory after making sure that it exists
349 *
350 * The returned path has a trailing slash, clients don't need to append it.
351 * \param dirName path to cache directory relative to the standard cache path
352 */
354
355/** Hue color component of based of the hash of the string.
356 *
357 * The implementation is based on XEP-0392:
358 * https://xmpp.org/extensions/xep-0392.html
359 * Naming and range are the same as QColor's hueF method:
360 * https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
361 */
363
364/** Extract the serverpart from MXID */
366
371
372// QDebug manipulators
373
374//! \brief QDebug manipulator to setup the stream for JSON output
375//!
376//! Originally made to encapsulate the change in QDebug behavior in Qt 5.4
377//! and the respective addition of QDebug::noquote().
378//! Together with the operator<<() helper, the proposed usage is
379//! (similar to std:: I/O manipulators):
380//! `qCDebug(MAIN) << formatJson << json_object; // (QJsonObject etc.)`
381inline QDebug formatJson(QDebug dbg) { return dbg.noquote(); }
382
383//! Suppress full qualification of enums/QFlags when logging
384inline QDebug terse(QDebug dbg)
385{
386 return dbg.verbosity(QDebug::MinimumVerbosity);
387}
388
390#ifdef PROFILER_LOG_USECS
392#else
393 200
394#endif
395 * 1000;
396} // namespace Quotient
397
398//! \brief A helper operator for QDebug manipulators, e.g. formatJson
399//!
400//! \param dbg to output the json to
401//! \param manipFn a QDebug manipulator
402//! \return a copy of dbg that has its mode altered by manipFn
403inline QDebug operator<<(QDebug dbg, std::invocable<QDebug> auto manipFn)
404{
405 return std::invoke(manipFn, dbg);
406}
407
408inline QDebug operator<<(QDebug dbg, QElapsedTimer et)
409{
410 // NOLINTNEXTLINE(bugprone-integer-division)
411 dbg << static_cast<double>(et.nsecsElapsed() / 1000) / 1000
412 << "ms"; // Show in ms with 3 decimal digits precision
413 return dbg;
414}
415
416namespace Quotient {
417//! \brief Lift an operation into dereferenceable types (std::optional or pointers)
418//!
419//! This is a more generic version of std::optional::and_then() that accepts an arbitrary number of
420//! arguments of any type that is dereferenceable (i.e. unary operator*() can be applied to it) and
421//! (explicitly or implicitly) convertible to bool. This allows to streamline checking for
422//! nullptr/nullopt before applying the operation on the underlying types. \p fn is only invoked if
423//! all \p args are "truthy" (i.e. <tt>(... && bool(args)) == true</tt>).
424//! \param fn A callable that should accept the types stored inside optionals/pointers passed in
425//! \p args (NOT optionals/pointers themselves; they are unwrapped)
426//! \return Always an optional: if \p fn returns another type, lift() wraps it in std::optional;
427//! if \p fn returns std::optional, that return value (or std::nullopt) is returned as is.
428template <typename FnT>
429inline auto lift(FnT&& fn, auto&&... args)
430{
431 if constexpr (std::is_void_v<std::invoke_result_t<FnT, decltype(*args)...>>) {
432 if ((... && bool(args)))
433 std::invoke(std::forward<FnT>(fn), *args...);
434 } else
435 return (... && bool(args))
436 ? std::optional(std::invoke(std::forward<FnT>(fn), *args...))
437 : std::nullopt;
438}
439
440//! \brief Merge the value from an optional
441//!
442//! Assigns the value stored at \p rhs to \p lhs if, and only if, \p rhs is not omitted and
443//! `lhs != *rhs`. \p lhs can be either an optional or an ordinary variable.
444//! \return `true` if \p rhs is not omitted and the \p lhs value was different, in other words,
445//! if \p lhs has changed; `false` otherwise
446template <typename T1, typename T2>
447 requires std::is_assignable_v<T1&, const T2&>
448constexpr inline bool merge(T1& lhs, const std::optional<T2>& rhs)
449{
450 if (!rhs || lhs == *rhs)
451 return false;
452 lhs = *rhs;
453 return true;
454}
455
456//! \brief Merge structure-like types
457//!
458//! Merges fields in \p lhs from counterparts in \p rhs. The list of fields to merge is passed
459//! in additional parameters (\p fields). E.g.:
460//! \codeline mergeStruct(struct1, struct2, &Struct::field1, &Struct::field2, &Struct::field3)
461//! \return the number of fields in \p lhs that were changed
462template <typename StructT>
463constexpr inline size_t mergeStruct(StructT& lhs, const StructT& rhs, const auto... fields)
464{
465 return ((... + static_cast<size_t>(merge(lhs.*fields, rhs.*fields))));
466}
467
468// These are meant to eventually become separate classes derived from QString (or perhaps
469// QByteArray?), with their own construction and validation logic; for now they are just aliases
470// for QString to make numerous IDs at least semantically different in the code.
471
475
476QUOTIENT_API bool isGuestUserId(const UserId& uId);
477
482
484};
485} // namespace Quotient
constexpr iterator end()
Definition util.h:149
constexpr Range(iterator from, iterator to)
Definition util.h:138
constexpr bool empty() const
Definition util.h:145
constexpr const_iterator end() const
Definition util.h:147
constexpr const_iterator begin() const
Definition util.h:146
constexpr Range(ArrayT &arr)
Definition util.h:137
constexpr iterator begin()
Definition util.h:148
constexpr size_type size() const
Definition util.h:140
An adaptor for Qt (hash-)maps to make them iterable in STL style.
Definition util.h:177
QT_IGNORE_DEPRECATIONS(template< typename U > asKeyValueRange(U &) -> asKeyValueRange< U & >;template< typename U > asKeyValueRange(U &&) -> asKeyValueRange< U >;) template< typename InputIt
QUOTIENT_API bool isGuestUserId(const UserId &uId)
auto lift(FnT &&fn, auto &&... args)
Lift an operation into dereferenceable types (std::optional or pointers)
Definition util.h:429
auto makeReadyValueFuture(T &&value)
A substitute for QtFuture::makeReadyValueFuture() for compatibility with Qt pre-6....
Definition util.h:105
QUOTIENT_API int patchVersion()
QUOTIENT_API int minorVersion()
QDebug formatJson(QDebug dbg)
QDebug manipulator to setup the stream for JSON output.
Definition util.h:381
bool alarmX(bool alarmCondition, const auto &msg, std::source_location loc=std::source_location::current())
Definition util.h:65
Overloads(FunctorTs &&...) -> Overloads< FunctorTs... >
constexpr qint64 ProfilerMinNsecs
Definition util.h:389
constexpr ImplPtr< ImplType, TypeToDelete > ZeroImpl()
Definition util.h:280
QUOTIENT_API void linkifyUrls(QString &htmlEscapedText)
constexpr bool merge(T1 &lhs, const std::optional< T2 > &rhs)
Merge the value from an optional.
Definition util.h:448
constexpr size_t mergeStruct(StructT &lhs, const StructT &rhs, const auto... fields)
Merge structure-like types.
Definition util.h:463
ImplPtr< ImplType, TypeToDelete > makeImpl(ArgTs &&... args)
make_unique for ImplPtr
Definition util.h:263
QDebug terse(QDebug dbg)
Suppress full qualification of enums/QFlags when logging.
Definition util.h:384
auto makeReadyVoidFuture()
A substitute for QtFuture::makeReadyVoidFuture() for compatibility with Qt pre-6.6.
Definition util.h:94
auto makeCStruct(T *(*constructor)(void *), size_t(*sizeFn)(), auto destructor)
Create a C structure with pre-programmed deletion logic.
Definition util.h:314
QUOTIENT_API int majorVersion()
ImplPtr< ImplType, TypeToDelete > acquireImpl(ImplType *from)
Definition util.h:272
#define QUOTIENT_API
void operator()(T *toDelete)
Definition util.h:289
This is only to make UnorderedMap alias work until we get rid of it.
Definition util.h:119
size_t operator()(const T &s) const Q_DECL_NOEXCEPT
Definition util.h:120
Multiplex several functors in one.
Definition util.h:326
QDebug operator<<(QDebug dbg, std::invocable< QDebug > auto manipFn)
A helper operator for QDebug manipulators, e.g. formatJson.
Definition util.h:403
#define QUO_CSTR(StringConvertible_)
q(Utf8)Printable that can handle more than just QStrings
Definition util.h:63