LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include <functional>
33 #include <QObject>
34 #include "sllconfig.h"
35 
36 namespace LeechCraft
37 {
38 namespace Util
39 {
42  class UTIL_SLL_API SlotClosureBase : public QObject
43  {
44  Q_OBJECT
45  public:
46  using QObject::QObject;
47 
48  virtual ~SlotClosureBase () = default;
49  public Q_SLOTS:
52  virtual void run () = 0;
53  };
54 
99  template<typename FireDestrPolicy>
101  , public FireDestrPolicy
102  {
103  public:
104  using FunType_t = std::function<typename FireDestrPolicy::Signature_t>;
105  private:
106  FunType_t Func_;
107  public:
126  SlotClosure (const FunType_t& func, QObject *parent)
127  : SlotClosureBase { parent }
128  , Func_ { func }
129  {
130  }
131 
141  SlotClosure (const FunType_t& func,
142  QObject *sender,
143  const char *signal,
144  QObject *parent)
145  : SlotClosureBase { parent }
146  , Func_ { func }
147  {
148  connect (sender,
149  signal,
150  this,
151  SLOT (run ()));
152  }
153 
164  SlotClosure (const FunType_t& func,
165  QObject *sender,
166  const std::initializer_list<const char*>& signalsList,
167  QObject *parent)
168  : SlotClosureBase { parent }
169  , Func_ { func }
170  {
171  for (const auto signal : signalsList)
172  connect (sender,
173  signal,
174  this,
175  SLOT (run ()));
176  }
177 
180  void run () override
181  {
182  FireDestrPolicy::Invoke (Func_, this);
183  FireDestrPolicy::Fired (this);
184  }
185  };
186 
188  {
189  protected:
190  using Signature_t = void ();
191 
192  void Invoke (const std::function<Signature_t>& f, SlotClosureBase*)
193  {
194  f ();
195  }
196 
197  virtual ~BasicDeletePolicy () = default;
198  };
199 
203  {
204  protected:
205  static void Fired (SlotClosureBase *base)
206  {
207  base->deleteLater ();
208  }
209  };
210 
214  {
215  protected:
216  static void Fired (SlotClosureBase*)
217  {
218  }
219  };
220 
228  {
229  public:
232  enum class Delete
233  {
236  No,
237 
240  Yes
241  };
242  protected:
243  virtual ~ChoiceDeletePolicy () {}
244 
245  using Signature_t = Delete ();
246 
247  static void Invoke (const std::function<Signature_t>& f, SlotClosureBase *base)
248  {
249  if (f () == Delete::Yes)
250  base->deleteLater ();
251  }
252 
253  static void Fired (SlotClosureBase*)
254  {
255  }
256  };
257 }
258 }
Deletes a SlotClosure object after its signal has fired.
Definition: slotclosure.h:202
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:931
Delete SlotClosure after this invocation.
#define UTIL_SLL_API
Definition: sllconfig.h:37
SlotClosure(const FunType_t &func, QObject *parent)
Constructs a SlotClosure running a given func with the given parent as a QObject. ...
Definition: slotclosure.h:126
Executes a given functor upon a signal (or a list of signals).
Definition: slotclosure.h:100
static void Fired(SlotClosureBase *)
Definition: slotclosure.h:253
Do not delete SlotClosure after this invocation.
void Invoke(const std::function< Signature_t > &f, SlotClosureBase *)
Definition: slotclosure.h:192
Delete
Whether the SlotClosure shall be deleted.
Definition: slotclosure.h:232
static void Invoke(const std::function< Signature_t > &f, SlotClosureBase *base)
Definition: slotclosure.h:247
static void Fired(SlotClosureBase *)
Definition: slotclosure.h:216
Base class for SlotClosure.
Definition: slotclosure.h:42
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...
Definition: slotclosure.h:141
Delegates the SlotClosure deletion decision to the signal handler.
Definition: slotclosure.h:227
void run() override
Triggers the function and invokes the destroy policy.
Definition: slotclosure.h:180
std::function< typename FireDestrPolicy::Signature_t > FunType_t
Definition: slotclosure.h:104
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...
Definition: slotclosure.h:164
static void Fired(SlotClosureBase *base)
Definition: slotclosure.h:205
Does not delete a SlotClosure object.
Definition: slotclosure.h:213