libQuotient
A Qt library for building matrix clients
util.h File Reference
#include "quotient_export.h"
#include <QtCore/QDebug>
#include <QtCore/QElapsedTimer>
#include <QtCore/QHashFunctions>
#include <QtCore/QLatin1String>
#include <QtCore/QUrl>
#include <memory>
#include <optional>
#include <source_location>
#include <unordered_map>
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Quotient::HashQ< T >
 This is only to make UnorderedMap alias work until we get rid of it. More...
 
class  Quotient::Range< ArrayT >
 
class  Quotient::asKeyValueRange< T >
 An adaptor for Qt (hash-)maps to make them iterable in STL style. More...
 
struct  Quotient::CStructDeleter< T >
 
struct  Quotient::Overloads< FunctorTs >
 Multiplex several functors in one. More...
 
struct  Quotient::HomeserverData
 

Namespaces

 Quotient
 
 Quotient::Literals
 

Macros

#define DECL_DEPRECATED_ENUMERATOR(Deprecated, Recommended)    Deprecated Q_DECL_ENUMERATOR_DEPRECATED_X("Use " #Recommended) = Recommended
 
#define SLICE(Object, ToType)   ToType{static_cast<const ToType&>(Object)}
 Copy an object with slicing. More...
 
#define QUO_CSTR(StringConvertible_)   std::data(::Quotient::_impl::toUtf8(StringConvertible_))
 q(Utf8)Printable that can handle more than just QStrings More...
 
#define QUO_ALARM_X(...)   ::Quotient::alarmX(__VA_ARGS__)
 A negative assertion facility that can be put in an if statement. More...
 
#define QUO_ALARM(...)   ::Quotient::alarmX((__VA_ARGS__) ? true : false, "Alarm: " #__VA_ARGS__)
 
#define QUO_CHECK(...)    !::Quotient::alarmX(!(__VA_ARGS__) ? true : false, "Failing expression: " #__VA_ARGS__)
 Evaluate the boolean expression and, in Debug mode, assert it to be true. More...
 

Typedefs

template<typename KeyT , typename ValT >
using Quotient::UnorderedMap = std::unordered_map< KeyT, ValT, HashQ< KeyT > >
 A wrapper around std::unordered_map compatible with types that have qHash. More...
 
template<typename ImplType , typename TypeToDelete = ImplType>
using Quotient::ImplPtr = std::unique_ptr< ImplType, void(*)(TypeToDelete *)>
 An owning implementation pointer. More...
 
template<typename T >
using Quotient::CStructPtr = std::unique_ptr< T, CStructDeleter< T > >
 An owning pointer to a C structure. More...
 
using Quotient::UserId = QString
 
using Quotient::RoomId = QString
 
using Quotient::EventId = QString
 

Functions

bool Quotient::alarmX (bool alarmCondition, const auto &msg, [[maybe_unused]] std::source_location loc=std::source_location::current())
 
constexpr auto Quotient::operator""_ls (const char *s, std::size_t size)
 
 Quotient::QT_IGNORE_DEPRECATIONS (template< typename U > asKeyValueRange(U &) -> asKeyValueRange< U & >;template< typename U > asKeyValueRange(U &&) -> asKeyValueRange< U >;) template< typename InputIt
 
Pred std::pair< InputIt, ForwardIt > Quotient::findFirstOf (InputIt first, InputIt last, ForwardIt sFirst, ForwardIt sLast, Pred pred)
 
template<typename ImplType , typename TypeToDelete = ImplType, typename... ArgTs>
ImplPtr< ImplType, TypeToDelete > Quotient::makeImpl (ArgTs &&... args)
 make_unique for ImplPtr More...
 
template<typename ImplType , typename TypeToDelete = ImplType>
ImplPtr< ImplType, TypeToDelete > Quotient::acquireImpl (ImplType *from)
 
template<typename ImplType , typename TypeToDelete = ImplType>
constexpr ImplPtr< ImplType, TypeToDelete > Quotient::ZeroImpl ()
 
template<typename T >
auto Quotient::makeCStruct (T *(*constructor)(void *), size_t(*sizeFn)(), auto destructor)
 Create a C structure with pre-programmed deletion logic. More...
 
template<typename... FunctorTs>
 Quotient::Overloads (FunctorTs &&...) -> Overloads< FunctorTs... >
 
QUOTIENT_API void Quotient::linkifyUrls (QString &htmlEscapedText)
 
QUOTIENT_API QString Quotient::sanitized (const QString &plainText)
 
QUOTIENT_API QString Quotient::prettyPrint (const QString &plainText)
 
QUOTIENT_API QString Quotient::cacheLocation (QStringView dirName)
 
QUOTIENT_API qreal Quotient::stringToHueF (const QString &s)
 
QUOTIENT_API QString Quotient::serverPart (const QString &mxId)
 
QUOTIENT_API QString Quotient::versionString ()
 
QUOTIENT_API int Quotient::majorVersion ()
 
QUOTIENT_API int Quotient::minorVersion ()
 
QUOTIENT_API int Quotient::patchVersion ()
 
QDebug Quotient::formatJson (QDebug dbg)
 QDebug manipulator to setup the stream for JSON output. More...
 
QDebug Quotient::terse (QDebug dbg)
 Suppress full qualification of enums/QFlags when logging. More...
 
QDebug operator<< (QDebug dbg, std::invocable< QDebug > auto manipFn)
 A helper operator for QDebug manipulators, e.g. formatJson. More...
 
QDebug operator<< (QDebug dbg, QElapsedTimer et)
 
template<typename FnT >
auto Quotient::lift (FnT &&fn, auto &&... args)
 Lift an operation into dereferenceable types (std::optional or pointers) More...
 
template<typename T1 , typename T2 >
requires constexpr std::is_assignable_v< T1 &, const T2 & > bool Quotient::merge (T1 &lhs, const std::optional< T2 > &rhs)
 Merge the value from an optional. More...
 
template<typename StructT >
constexpr size_t Quotient::mergeStruct (StructT &lhs, const StructT &rhs, const auto... fields)
 Merge structure-like types. More...
 
QUOTIENT_API bool Quotient::isGuestUserId (const UserId &uId)
 

Variables

 Quotient::ForwardIt
 
constexpr qint64 Quotient::ProfilerMinNsecs
 

Macro Definition Documentation

◆ DECL_DEPRECATED_ENUMERATOR

#define DECL_DEPRECATED_ENUMERATOR (   Deprecated,
  Recommended 
)     Deprecated Q_DECL_ENUMERATOR_DEPRECATED_X("Use " #Recommended) = Recommended

Definition at line 20 of file util.h.

◆ QUO_ALARM

#define QUO_ALARM (   ...)    ::Quotient::alarmX((__VA_ARGS__) ? true : false, "Alarm: " #__VA_ARGS__)

Definition at line 85 of file util.h.

◆ QUO_ALARM_X

#define QUO_ALARM_X (   ...)    ::Quotient::alarmX(__VA_ARGS__)

A negative assertion facility that can be put in an if statement.

Unlike most other assertion functions or macros, this doesn't collapse to no-op in Release builds; rather, it sends a critical level message to the log and returns true so that you could immediately return or do some other damage control for Release builds. Also unlike most other assertion functions, AlarmCondition is the condition for failure, not for health; in other words, Message is sent to logs (and, in Debug configuration, the assertion fails) if AlarmCondition holds, not the other way around.

This macro is a trivial wrapper around alarmX(), provided for API uniformity with QUO_ALARM()

Definition at line 83 of file util.h.

◆ QUO_CHECK

#define QUO_CHECK (   ...)     !::Quotient::alarmX(!(__VA_ARGS__) ? true : false, "Failing expression: " #__VA_ARGS__)

Evaluate the boolean expression and, in Debug mode, assert it to be true.

Definition at line 88 of file util.h.

◆ QUO_CSTR

#define QUO_CSTR (   StringConvertible_)    std::data(::Quotient::_impl::toUtf8(StringConvertible_))

q(Utf8)Printable that can handle more than just QStrings

This macro accepts all kinds of string-like input, from const char* all the way to raw QStringBuilder constructs. It returns a const char* pointer to a UTF-8 string; if the original input was QChar/u16-based, it creates a temporary buffer to store the UTF-8 representation that is destroyed once the statement containing QUO_CSTR() is done (therefore, ALWAYS copy the result based on QUO_CSTR() contents if you need to store it).

Definition at line 61 of file util.h.

◆ SLICE

#define SLICE (   Object,
  ToType 
)    ToType{static_cast<const ToType&>(Object)}

Copy an object with slicing.

Unintended slicing is bad, which is why there's a C++ Core Guideline that basically says "don't slice, or if you do, make it explicit". Sonar and clang-tidy have warnings matching this guideline; unfortunately, those warnings trigger even when you have a dedicated method (as the guideline recommends) that makes a slicing copy.

This macro is meant for cases when slicing is intended: the static cast silences the static analysis warning, and the macro appearance itself makes it very clear that slicing is wanted here. It is made as a macro (not as a function template) to support the case of private inheritance in which a function template would not be able to cast to the private base (see Uri::toUrl() for an example of just that situation).

Definition at line 37 of file util.h.

Function Documentation

◆ operator<<() [1/2]

QDebug operator<< ( QDebug  dbg,
QElapsedTimer  et 
)
inline

Definition at line 384 of file util.h.

◆ operator<<() [2/2]

QDebug operator<< ( QDebug  dbg,
std::invocable< QDebug > auto  manipFn 
)
inline

A helper operator for QDebug manipulators, e.g. formatJson.

Parameters
dbgto output the json to
manipFna QDebug manipulator
Returns
a copy of dbg that has its mode altered by manipFn

Definition at line 379 of file util.h.