[dali_2.3.28] Merge branch 'devel/master'
[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) 2021 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  * @brief Slot connection is the connection information held by a connection tracker.
34  *
35  * A slot can have zero to many connection objects, depending
36  * on how many signals it is connected to.
37  *
38  * A connection contains:
39  * - Callback (slot)
40  * - SlotObserver -interface provided by the signal
41  *
42  * It holds a pointer to the callback, but does not own it.
43  * @SINCE_1_0.0
44  */
45 class DALI_CORE_API SlotConnection
46 {
47 public:
48   /**
49    * @brief Constructor.
50    *
51    * @SINCE_1_0.0
52    * @param[in] slotObserver The slot observer
53    * @param[in] callback A callback object (not owned)
54    */
55   SlotConnection(SlotObserver* slotObserver, CallbackBase* callback);
56
57   /**
58    * @brief Retrieves the callback.
59    *
60    * @SINCE_1_0.0
61    * @return A pointer to the callback
62    */
63   CallbackBase* GetCallback() const
64   {
65     return mCallback;
66   }
67
68   /**
69    * @brief Retrieves the slot observer.
70    *
71    * @SINCE_1_0.0
72    * @return A pointer to the slot observer
73    */
74   SlotObserver* GetSlotObserver() const
75   {
76     return mSlotObserver;
77   }
78
79 private:
80   SlotObserver* mSlotObserver{nullptr}; ///< a pointer to the slot observer (not owned)
81   CallbackBase* mCallback{nullptr};     ///< The callback. This is not owned, the corresponding SignalConnection has ownership.
82 };
83
84 /**
85  * @brief SignalConnection is the connection information held by the signal.
86  *
87  * A signal can have zero to many connections, depending on how
88  * many slots are connected to this signal.
89  *
90  * A connection contains:
91  * - Callback (slot)
92  * - SignalObserver - interface provided by a slot owning object.
93  *
94  * It takes ownership of the callback, and will delete it when
95  * the connection is destroyed.
96  * @SINCE_1_0.0
97  */
98 class DALI_CORE_API SignalConnection
99 {
100 public:
101   /**
102    * @brief Constructor.
103    *
104    * @SINCE_1_0.0
105    * @param[in] callback The callback which should be a C function
106    */
107   SignalConnection(CallbackBase* callback) noexcept
108   : mCallback(callback)
109   {
110   }
111
112   /**
113    * @brief Constructor.
114    *
115    * @SINCE_1_0.0
116    * @param[in] signalObserver The signal observer
117    * @param[in] callback Ownership of this callback object is taken
118    */
119   SignalConnection(SignalObserver* signalObserver, CallbackBase* callback) noexcept
120   : mSignalObserver(signalObserver),
121     mCallback(callback)
122   {
123   }
124
125   /**
126    * @brief Non-virtual destructor, not intended as a base class.
127    * @SINCE_1_0.0
128    */
129   ~SignalConnection() noexcept
130   {
131     // signal connections have ownership of the callback.
132     delete mCallback;
133   }
134
135   /**
136    * @brief Disconnects the signal from the slot.
137    *
138    * @SINCE_1_0.0
139    * @param[in] slotObserver The signal disconnecting from the slot
140    */
141   void Disconnect(SlotObserver* slotObserver) noexcept;
142
143   /**
144    * @brief Retrieves the callback.
145    *
146    * @SINCE_1_0.0
147    * @return A pointer to the callback
148    */
149   CallbackBase* GetCallback() const noexcept
150   {
151     return mCallback;
152   }
153
154   SignalConnection(const SignalConnection&) = delete;            ///< Deleted copy constructor. @SINCE_1_0.0
155   SignalConnection& operator=(const SignalConnection&) = delete; ///< Deleted copy assignment operator. @SINCE_1_0.0
156
157   /**
158    * @brief Move constructor.
159    *
160    * A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.
161    * @SINCE_1_9.38
162    * @param[in] connection The property value to move from
163    */
164   SignalConnection(SignalConnection&& connection) noexcept
165   : mSignalObserver(connection.mSignalObserver),
166     mCallback(connection.mCallback)
167   {
168     connection.mSignalObserver = nullptr;
169     connection.mCallback       = nullptr;
170   }
171
172   /**
173    * @brief Move assignment operator.
174    *
175    * @SINCE_1_9.38
176    * @param[in] connection The connection to move from
177    * @return a reference to this
178    */
179   SignalConnection& operator=(SignalConnection&& connection) noexcept
180   {
181     if(this != &connection)
182     {
183       // release the callback
184       delete mCallback;
185
186       mSignalObserver = connection.mSignalObserver;
187       mCallback       = connection.mCallback;
188
189       connection.mSignalObserver = nullptr;
190       connection.mCallback       = nullptr;
191     }
192     return *this;
193   }
194
195   explicit operator bool() const noexcept
196   {
197     return mCallback ? true : false;
198   }
199
200 private:
201   SignalObserver* mSignalObserver{nullptr}; ///< a pointer to the signal observer (not owned)
202   CallbackBase*   mCallback{nullptr};       ///< The callback, has ownership.
203 };
204
205 /**
206  * @}
207  */
208 } // namespace Dali
209
210 #endif // DALI_SIGNAL_SLOT_CONNECTIONS_H