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