LeechCraft  0.6.70-6645-gcd10d7e
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 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_);
183 
184  this->Fired ();
185  }
186  };
187 
189  {
190  protected:
191  using Signature_t = void ();
192 
193  void Invoke (const std::function<Signature_t>& f)
194  {
195  f ();
196  }
197 
198  virtual ~BasicDeletePolicy () = default;
199  };
200 
204  {
205  protected:
206  void Fired ()
207  {
208  dynamic_cast<SlotClosureBase*> (this)->deleteLater ();
209  }
210  };
211 
215  {
216  protected:
217  void Fired ()
218  {
219  }
220  };
221 
223  {
224  public:
225  enum class Delete
226  {
227  No,
228  Yes
229  };
230  protected:
231  virtual ~ChoiceDeletePolicy () {}
232 
233  using Signature_t = Delete ();
234 
235  void Invoke (const std::function<Signature_t>& f)
236  {
237  if (f () == Delete::Yes)
238  dynamic_cast<SlotClosureBase*> (this)->deleteLater ();
239  }
240 
241  void Fired ()
242  {
243  }
244  };
245 }
246 }
Deletes a SlotClosure object after its signal has fired.
Definition: slotclosure.h:203
#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
void Invoke(const std::function< Signature_t > &f)
Definition: slotclosure.h:193
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
Executes a given functor upon a signal (or a list of signals).
Definition: slotclosure.h:100
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
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
auto Invoke(F &&f, Args &&...args) -> decltype(std::forward< F >(f)(std::forward< Args >(args)...))
Definition: oldcppkludges.h:39
void Invoke(const std::function< Signature_t > &f)
Definition: slotclosure.h:235
Does not delete a SlotClosure object.
Definition: slotclosure.h:214