Qx v0.6
Qt Extensions Library
Loading...
Searching...
No Matches
qx-helpers.h
Go to the documentation of this file.
1#ifndef QX_HELPERS_H
2#define QX_HELPERS_H
3
4// Standard Library Includes
5#include <type_traits>
6#include <utility>
7
8// Qt Includes
9#include <QMetaObject>
10#include <QObject>
11
12// Inner-component Includes
15
16namespace Qx
17{
18
20{
21 Q_DISABLE_COPY(ScopedConnection);
22//-Instance Variables------------------------------------------------------------------------------------------
23private:
24 QMetaObject::Connection mConnection;
25
26//-Constructor-------------------------------------------------------------------------------------------------
27public:
28 inline ScopedConnection(const QMetaObject::Connection& connection) : mConnection(connection) {}
29 inline ScopedConnection(ScopedConnection&& other) = default;
30
31//-Destructor-------------------------------------------------------------------------------------------------
32public:
33 inline ~ScopedConnection() { if(mConnection) QObject::disconnect(mConnection); }
34
35//-Operators--------------------------------------------------------------------------------------------------
36public:
37 inline ScopedConnection& operator=(ScopedConnection&& other) = default;
38 inline operator bool() const { return static_cast<bool>(mConnection); }
39};
40
41//Namespace Functions-----------------------------------------------------------------------------------------
42template<typename PointerToMemberFunction, typename Functor>
43ScopedConnection scopedConnect(const QObject* sender, PointerToMemberFunction signal, Functor&& functor)
44{
45 return QObject::connect(sender, std::forward<PointerToMemberFunction>(signal), std::forward<Functor>(functor));
46}
47
48//TODO: Might want to move this to a container tools specific header (along with some other todos related to them)
49template<typename T>
50using container_arrow_result = std::conditional_t<defines_member_ptr<T>, T&,
51 std::conditional_t<std::is_class_v<target_type<T>>, target_type<T>*, void>>;
52
53template<typename T>
54concept arrowable_container_type = defines_member_ptr<T> || std::is_class_v<target_type<T>>;
55
56template<arrowable_container_type T>
58{
59 if constexpr(defines_member_ptr<T> || std::is_pointer_v<T>)
60 return data;
61 else
62 return &data;
63}
64
65}
66
67//Non-namespace Structs----------------------------------------------------------
68/* TODO: Figure out how to constrain this to only accept functors, issue is at least as of C++20
69 * there doesnt seem to be a way to check if a type has an arbitrary number of operator() overloads
70 * with an arbitrary number of arguments.
71 */
72template<typename... Functors>
73struct qxFuncAggregate : Functors... {
74 using Functors::operator()...;
75};
76
77/* Explicit deduction guide. Shouldn't be needed as of C++20, but some compilers are late to the party.
78 * We'd like to check for #if __cpp_deduction_guides < 201907 to see if the compiler has the feature,
79 * but it seems that GCC 10.x still won't compile without this even though it reports 201907
80 * implementation. This might have been an oversight, or have to due with P2082R1
81 * (Fixing CTAD for aggregates) which wasn't implemented in GCC until 11.x
82 */
84template<class... Ts>
85qxFuncAggregate(Ts...) -> qxFuncAggregate<Ts...>;
88//Non-namespace Functions----------------------------------------------------------
89template <typename T>
90const T qxAsConst(T&& t) { return std::move(t); }
91
92template <typename T>
93typename std::add_const<T>::type qxAsConst(T& t) { return qAsConst(t); }
94
95template <typename T>
96void qxDelete(T*& pointer)
97{
98 delete pointer;
99 pointer = nullptr;
100}
101
102#endif // QX_HELPERS_H
The ScopedConnection class disconnects a connection when it goes out of scope.
Definition qx-helpers.h:20
~ScopedConnection()
Definition qx-helpers.h:33
ScopedConnection(ScopedConnection &&other)=default
ScopedConnection(const QMetaObject::Connection &connection)
Definition qx-helpers.h:28
ScopedConnection & operator=(ScopedConnection &&other)=default
Definition qx-helpers.h:54
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-abstracterror.cpp:13
container_arrow_result< T > container_arrow_operator(T &data)
Definition qx-helpers.h:57
std::conditional_t< defines_member_ptr< T >, T &, std::conditional_t< std::is_class_v< target_type< T > >, target_type< T > *, void > > container_arrow_result
Definition qx-helpers.h:50
std::remove_pointer_t< std::remove_reference_t< T > > target_type
Definition qx-typetraits.h:22
ScopedConnection scopedConnect(const QObject *sender, PointerToMemberFunction signal, Functor &&functor)
Definition qx-helpers.h:43
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...
void qxDelete(T *&pointer)
Definition qx-helpers.h:96
const T qxAsConst(T &&t)
Definition qx-helpers.h:90
The qx-typetraits header file provides a library of general purpose type-traits as an extension of th...
Definition qx-helpers.h:73