refactor SignalConnection class.
[platform/core/uifw/dali-core.git] / dali / public-api / signals / signal-slot-connections.h
1 #ifndef DALI_SIGNAL_SLOT_CONNECTIONS_H
2 #define DALI_SIGNAL_SLOT_CONNECTIONS_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/signals/callback.h>
23 #include <dali/public-api/signals/signal-slot-observers.h>
24
25 namespace Dali
26 {
27 /**
28  * @addtogroup dali_core_signals
29  * @{
30  */
31
32
33 /**
34  * @brief Slot connection is the connection information held by a connection tracker.
35  *
36  * A slot can have zero to many connection objects, depending
37  * on how many signals it is connected to.
38  *
39  * A connection contains:
40  * - Callback (slot)
41  * - SlotObserver -interface provided by the signal
42  *
43  * It holds a pointer to the callback, but does not own it.
44  * @SINCE_1_0.0
45  */
46 class DALI_CORE_API SlotConnection
47 {
48 public:
49   /**
50    * @brief Constructor.
51    *
52    * @SINCE_1_0.0
53    * @param[in] slotObserver The slot observer
54    * @param[in] callback A callback object (not owned)
55    */
56   SlotConnection(SlotObserver* slotObserver, CallbackBase* callback);
57
58   /**
59    * @brief Non-virtual destructor, not intended as a base class.
60    * @SINCE_1_0.0
61    */
62   ~SlotConnection();
63
64   /**
65    * @brief Retrieves the callback.
66    *
67    * @SINCE_1_0.0
68    * @return A pointer to the callback
69    */
70   CallbackBase* GetCallback();
71
72   /**
73    * @brief Retrieves the slot observer.
74    *
75    * @SINCE_1_0.0
76    * @return A pointer to the slot observer
77    */
78   SlotObserver* GetSlotObserver();
79
80 private:
81   SlotConnection(const SlotConnection&) = delete;            ///< Deleted copy constructor. @SINCE_1_0.0
82   SlotConnection(SlotConnection&&)      = delete;            ///< Deleted move constructor. @SINCE_1_9.25
83   SlotConnection& operator=(const SlotConnection&) = delete; ///< Deleted copy assignment operator. @SINCE_1_0.0
84   SlotConnection& operator=(SlotConnection&&) = delete;      ///< Deleted move assignment operator. @SINCE_1_9.25
85
86 private:
87   SlotObserver* mSlotObserver; ///< a pointer to the slot observer (not owned)
88   CallbackBase* mCallback;     ///< The callback. This is not owned, the corresponding SignalConnection has ownership.
89 };
90
91 /**
92  * @brief SignalConnection is the connection information held by the signal.
93  *
94  * A signal can have zero to many connections, depending on how
95  * many slots are connected to this signal.
96  *
97  * A connection contains:
98  * - Callback (slot)
99  * - SignalObserver - interface provided by a slot owning object.
100  *
101  * It takes ownership of the callback, and will delete it when
102  * the connection is destroyed.
103  * @SINCE_1_0.0
104  */
105 class DALI_CORE_API SignalConnection
106 {
107 public:
108   /**
109    * @brief Constructor.
110    *
111    * @SINCE_1_0.0
112    * @param[in] callback The callback which should be a C function
113    */
114   SignalConnection(CallbackBase* callback) noexcept
115   : mCallback(callback)
116   {
117   }
118
119   /**
120    * @brief Constructor.
121    *
122    * @SINCE_1_0.0
123    * @param[in] signalObserver The signal observer
124    * @param[in] callback Ownership of this callback object is taken
125    */
126   SignalConnection(SignalObserver* signalObserver, CallbackBase* callback) noexcept
127   : mSignalObserver(signalObserver),
128     mCallback(callback)
129   {
130   }
131
132   /**
133    * @brief Non-virtual destructor, not intended as a base class.
134    * @SINCE_1_0.0
135    */
136   ~SignalConnection() noexcept
137   {
138     // signal connections have ownership of the callback.
139     delete mCallback;
140   }
141
142   /**
143    * @brief Disconnects the signal from the slot.
144    *
145    * @SINCE_1_0.0
146    * @param[in] slotObserver The signal disconnecting from the slot
147    */
148   void Disconnect(SlotObserver* slotObserver) noexcept;
149
150   /**
151    * @brief Retrieves the callback.
152    *
153    * @SINCE_1_0.0
154    * @return A pointer to the callback
155    */
156   CallbackBase* GetCallback() const noexcept
157   {
158     return mCallback;
159   }
160
161   SignalConnection(const SignalConnection&) = delete;            ///< Deleted copy constructor. @SINCE_1_0.0
162   SignalConnection& operator=(const SignalConnection&) = delete; ///< Deleted copy assignment operator. @SINCE_1_0.0
163
164   /**
165    * @brief Move constructor.
166    *
167    * A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.
168    * @SINCE_1_9.38
169    * @param[in] connection The property value to move from
170    */
171   SignalConnection(SignalConnection&& connection) noexcept
172   : mSignalObserver(connection.mSignalObserver),
173     mCallback(connection.mCallback)
174   {
175     connection.mSignalObserver = nullptr;
176     connection.mCallback       = nullptr;
177   }
178
179   /**
180    * @brief Move assignment operator.
181    *
182    * @SINCE_1_9.38
183    * @param[in] connection The connection to move from
184    * @return a reference to this
185    */
186   SignalConnection& operator=(SignalConnection&& connection) noexcept
187   {
188     if(this != &connection)
189     {
190       // release the callback
191       delete mCallback;
192
193       mSignalObserver = connection.mSignalObserver;
194       mCallback       = connection.mCallback;
195
196       connection.mSignalObserver = nullptr;
197       connection.mCallback       = nullptr;
198     }
199     return *this;
200   }
201
202   explicit operator bool() const noexcept
203   {
204     return mCallback ? true : false;
205   }
206
207 private:
208   SignalObserver* mSignalObserver{nullptr}; ///< a pointer to the signal observer (not owned)
209   CallbackBase*   mCallback{nullptr};       ///< The callback, has ownership.
210 };
211
212 /**
213  * @}
214  */
215 } // namespace Dali
216
217 #endif // DALI_SIGNAL_SLOT_CONNECTIONS_H