libcamera  v0.0.0+100-debian/0_git20200629+e7aa92a-8-9-g77f5237c-dirty (2021-05-05T16:20:29+01:00)
Supporting cameras in Linux since 2019
signal.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2019, Google Inc.
4  *
5  * signal.h - Signal & slot implementation
6  */
7 #ifndef __LIBCAMERA_SIGNAL_H__
8 #define __LIBCAMERA_SIGNAL_H__
9 
10 #include <functional>
11 #include <list>
12 #include <type_traits>
13 #include <vector>
14 
15 #include <libcamera/bound_method.h>
16 #include <libcamera/object.h>
17 
18 namespace libcamera {
19 
20 class SignalBase
21 {
22 public:
23  void disconnect(Object *object);
24 
25 protected:
26  using SlotList = std::list<BoundMethodBase *>;
27 
28  void connect(BoundMethodBase *slot);
29  void disconnect(std::function<bool(SlotList::iterator &)> match);
30 
31  SlotList slots();
32 
33 private:
34  SlotList slots_;
35 };
36 
37 template<typename... Args>
38 class Signal : public SignalBase
39 {
40 public:
41  ~Signal()
42  {
43  disconnect();
44  }
45 
46 #ifndef __DOXYGEN__
47  template<typename T, typename R, typename std::enable_if_t<std::is_base_of<Object, T>::value> * = nullptr>
48  void connect(T *obj, R (T::*func)(Args...),
50  {
51  Object *object = static_cast<Object *>(obj);
52  SignalBase::connect(new BoundMethodMember<T, R, Args...>(obj, object, func, type));
53  }
54 
55  template<typename T, typename R, typename std::enable_if_t<!std::is_base_of<Object, T>::value> * = nullptr>
56 #else
57  template<typename T, typename R>
58 #endif
59  void connect(T *obj, R (T::*func)(Args...))
60  {
61  SignalBase::connect(new BoundMethodMember<T, R, Args...>(obj, nullptr, func));
62  }
63 
64  template<typename R>
65  void connect(R (*func)(Args...))
66  {
67  SignalBase::connect(new BoundMethodStatic<R, Args...>(func));
68  }
69 
70  void disconnect()
71  {
72  SignalBase::disconnect([]([[maybe_unused]] SlotList::iterator &iter) {
73  return true;
74  });
75  }
76 
77  template<typename T>
78  void disconnect(T *obj)
79  {
80  SignalBase::disconnect([obj](SlotList::iterator &iter) {
81  return (*iter)->match(obj);
82  });
83  }
84 
85  template<typename T, typename R>
86  void disconnect(T *obj, R (T::*func)(Args...))
87  {
88  SignalBase::disconnect([obj, func](SlotList::iterator &iter) {
89  BoundMethodArgs<R, Args...> *slot =
90  static_cast<BoundMethodArgs<R, Args...> *>(*iter);
91 
92  if (!slot->match(obj))
93  return false;
94 
95  /*
96  * If the object matches the slot, the slot is
97  * guaranteed to be a member slot, so we can safely
98  * cast it to BoundMethodMember<T, Args...> to match
99  * func.
100  */
101  return static_cast<BoundMethodMember<T, R, Args...> *>(slot)->match(func);
102  });
103  }
104 
105  template<typename R>
106  void disconnect(R (*func)(Args...))
107  {
108  SignalBase::disconnect([func](SlotList::iterator &iter) {
109  BoundMethodArgs<R, Args...> *slot =
110  static_cast<BoundMethodArgs<R, Args...> *>(*iter);
111 
112  if (!slot->match(nullptr))
113  return false;
114 
115  return static_cast<BoundMethodStatic<R, Args...> *>(slot)->match(func);
116  });
117  }
118 
119  void emit(Args... args)
120  {
121  /*
122  * Make a copy of the slots list as the slot could call the
123  * disconnect operation, invalidating the iterator.
124  */
125  for (BoundMethodBase *slot : slots())
126  static_cast<BoundMethodArgs<void, Args...> *>(slot)->activate(args...);
127  }
128 };
129 
130 } /* namespace libcamera */
131 
132 #endif /* __LIBCAMERA_SIGNAL_H__ */
void emit(Args... args)
Emit the signal and call all connected slots.
Definition: signal.h:119
ConnectionType
Connection type for asynchronous communication.
Definition: bound_method.h:19
void disconnect()
Disconnect the signal from all slots.
Definition: signal.h:70
void connect(T *obj, R(T::*func)(Args...))
Connect the signal to a member function slot.
Definition: signal.h:59
void disconnect(T *obj)
Disconnect the signal from all slots of the object.
Definition: signal.h:78
Top-level libcamera namespace.
Definition: bound_method.h:15
void disconnect(R(*func)(Args...))
Disconnect the signal from the slot static function func.
Definition: signal.h:106
void disconnect(T *obj, R(T::*func)(Args...))
Disconnect the signal from the object slot member function func.
Definition: signal.h:86
Base object to support automatic signal disconnection.
Method bind and invocation.
If the sender and the receiver live in the same thread, ConnectionTypeDirect is used. Otherwise ConnectionTypeQueued is used.
Definition: bound_method.h:20
void connect(R(*func)(Args...))
Connect the signal to a static function slot.
Definition: signal.h:65