LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
slotclosure.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <functional>
12#include <QObject>
13#include "sllconfig.h"
14
15namespace LC
16{
17namespace Util
18{
21 class UTIL_SLL_API SlotClosureBase : public QObject
22 {
23 Q_OBJECT
24 public:
25 using QObject::QObject;
26
27 virtual ~SlotClosureBase () = default;
28 public Q_SLOTS:
31 virtual void run () = 0;
32 };
33
78 template<typename FireDestrPolicy>
80 , public FireDestrPolicy
81 {
82 public:
83 using FunType_t = std::function<typename FireDestrPolicy::Signature_t>;
84 private:
85 FunType_t Func_;
86 public:
105 SlotClosure (const FunType_t& func, QObject *parent)
106 : SlotClosureBase { parent }
107 , Func_ { func }
108 {
109 }
110
120 SlotClosure (const FunType_t& func,
121 QObject *sender,
122 const char *signal,
123 QObject *parent)
124 : SlotClosureBase { parent }
125 , Func_ { func }
126 {
127 connect (sender,
128 signal,
129 this,
130 SLOT (run ()));
131 }
132
143 SlotClosure (const FunType_t& func,
144 QObject *sender,
145 const std::initializer_list<const char*>& signalsList,
146 QObject *parent)
147 : SlotClosureBase { parent }
148 , Func_ { func }
149 {
150 for (const auto signal : signalsList)
151 connect (sender,
152 signal,
153 this,
154 SLOT (run ()));
155 }
156
159 void run () override
160 {
161 FireDestrPolicy::Invoke (Func_, this);
162 FireDestrPolicy::Fired (this);
163 }
164 };
165
167 {
168 protected:
169 using Signature_t = void ();
170
171 void Invoke (const std::function<Signature_t>& f, SlotClosureBase*)
172 {
173 f ();
174 }
175
176 virtual ~BasicDeletePolicy () = default;
177 };
178
182 {
183 protected:
184 static void Fired (SlotClosureBase *base)
185 {
186 base->deleteLater ();
187 }
188 };
189
193 {
194 protected:
195 static void Fired (SlotClosureBase*)
196 {
197 }
198 };
199
207 {
208 public:
211 enum class Delete
212 {
215 No,
216
219 Yes
220 };
221 protected:
223
224 using Signature_t = Delete ();
225
226 static void Invoke (const std::function<Signature_t>& f, SlotClosureBase *base)
227 {
228 if (f () == Delete::Yes)
229 base->deleteLater ();
230 }
231
232 static void Fired (SlotClosureBase*)
233 {
234 }
235 };
236}
237}
virtual ~BasicDeletePolicy()=default
void Invoke(const std::function< Signature_t > &f, SlotClosureBase *)
Delegates the SlotClosure deletion decision to the signal handler.
Delete
Whether the SlotClosure shall be deleted.
@ Yes
Delete SlotClosure after this invocation.
@ No
Do not delete SlotClosure after this invocation.
static void Invoke(const std::function< Signature_t > &f, SlotClosureBase *base)
static void Fired(SlotClosureBase *)
Deletes a SlotClosure object after its signal has fired.
static void Fired(SlotClosureBase *base)
Does not delete a SlotClosure object.
static void Fired(SlotClosureBase *)
Base class for SlotClosure.
Definition slotclosure.h:22
virtual void run()=0
Triggers the function.
virtual ~SlotClosureBase()=default
Executes a given functor upon a signal (or a list of signals).
Definition slotclosure.h:81
std::function< typename FireDestrPolicy::Signature_t > FunType_t
Definition slotclosure.h:83
void run() override
Triggers the function and invokes the destroy policy.
SlotClosure(const FunType_t &func, QObject *sender, const std::initializer_list< const char * > &signalsList, QObject *parent)
Constructs a SlotClosure running a given func with the given parent as a QObject on the given signals...
SlotClosure(const FunType_t &func, QObject *sender, const char *signal, QObject *parent)
Constructs a SlotClosure running a given func with the given parent as a QObject on the given signal.
SlotClosure(const FunType_t &func, QObject *parent)
Constructs a SlotClosure running a given func with the given parent as a QObject.
Definition constants.h:15
#define UTIL_SLL_API
Definition sllconfig.h:16