LeechCraft  0.6.70-15082-g543737046d
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  * 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 
15 namespace LC
16 {
17 namespace 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>
79  class SlotClosure : public SlotClosureBase
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 
166  class BasicDeletePolicy
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 
192  class NoDeletePolicy : public BasicDeletePolicy
193  {
194  protected:
195  static void Fired (SlotClosureBase*)
196  {
197  }
198  };
199 
206  class ChoiceDeletePolicy
207  {
208  public:
211  enum class Delete
212  {
215  No,
216 
219  Yes
220  };
221  protected:
222  virtual ~ChoiceDeletePolicy () {}
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 }
sllconfig.h
UTIL_SLL_API
#define UTIL_SLL_API
Definition: sllconfig.h:16
LC::Util::ChoiceDeletePolicy::Delete::Yes
@ Yes
Delete SlotClosure after this invocation.
LC::Util::NoDeletePolicy::Fired
static void Fired(SlotClosureBase *)
Definition: slotclosure.h:207
LC::Util::BasicDeletePolicy::~BasicDeletePolicy
virtual ~BasicDeletePolicy()=default
LC::Util::ChoiceDeletePolicy::Delete
Delete
Whether the SlotClosure shall be deleted.
Definition: slotclosure.h:223
LC::Util::ChoiceDeletePolicy::Invoke
static void Invoke(const std::function< Signature_t > &f, SlotClosureBase *base)
Definition: slotclosure.h:238
LC::Util::BasicDeletePolicy::Signature_t
void() Signature_t
Definition: slotclosure.h:181
LC::Util::SlotClosureBase
Base class for SlotClosure.
Definition: slotclosure.h:33
LC::Util::DeleteLaterPolicy::Fired
static void Fired(SlotClosureBase *base)
Definition: slotclosure.h:196
LC::Util::SlotClosure::FunType_t
std::function< typename FireDestrPolicy::Signature_t > FunType_t
Definition: slotclosure.h:95
LC::Util::DeleteLaterPolicy
Deletes a SlotClosure object after its signal has fired.
Definition: slotclosure.h:193
LC::Util::SlotClosure
Executes a given functor upon a signal (or a list of signals).
Definition: slotclosure.h:91
LC::Util::BasicDeletePolicy::Invoke
void Invoke(const std::function< Signature_t > &f, SlotClosureBase *)
Definition: slotclosure.h:183
LC::Util::SlotClosure::SlotClosure
SlotClosure(const FunType_t &func, QObject *parent)
Constructs a SlotClosure running a given func with the given parent as a QObject.
Definition: slotclosure.h:117
LC::Util::BasicDeletePolicy
Definition: slotclosure.h:178
LC::Util::ChoiceDeletePolicy::Delete::No
@ No
Do not delete SlotClosure after this invocation.
LC
Definition: constants.h:14
LC::Util::ChoiceDeletePolicy::Fired
static void Fired(SlotClosureBase *)
Definition: slotclosure.h:244
LC::Util::oral::sph::f
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:952
LC::Util::ChoiceDeletePolicy::Signature_t
Delete() Signature_t
Definition: slotclosure.h:236
LC::Util::ChoiceDeletePolicy::~ChoiceDeletePolicy
virtual ~ChoiceDeletePolicy()
Definition: slotclosure.h:234
LC::Util::SlotClosure::run
void run() override
Triggers the function and invokes the destroy policy.
Definition: slotclosure.h:171