Clean up the code to build successfully on macOS
[platform/core/uifw/dali-core.git] / dali / public-api / signals / slot-delegate.h
1 #ifndef DALI_SLOT_DELEGATE_H
2 #define DALI_SLOT_DELEGATE_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/common/dali-common.h>
23 #include <dali/public-api/signals/connection-tracker.h>
24
25 namespace Dali
26 {
27 /**
28  * @addtogroup dali_core_signals
29  * @{
30  */
31
32 /**
33  * @brief SlotDelegates can be used to connect member functions to signals, without inheriting from SlotDelegateInterface.
34  *
35  * Note that the object providing the member function is expected to own the SlotDelegate; therefore when the object
36  * is destroyed, the SlotDelegate destructor will automatically disconnect.
37  *
38  * @code
39  *
40  * class Example // This does not inherit from SlotDelegateInterface!
41  * {
42  * public:
43  *
44  *   Example()
45  *   : mSlotDelegate( this )
46  *   {
47  *   }
48  *
49  *   ~Example()
50  *   {
51  *     // mSlotDelegate disconnects automatically here
52  *   }
53  *
54  *   void Animate()
55  *   {
56  *     Animation animation = Animation::New( 1.0f );
57  *     animation.FinishedSignal().Connect( mSlotDelegate, &Example::OnAnimationFinished );
58  *     animation.Play(); // fire & forget
59  *   }
60  *
61  *   void OnAnimationFinished( Animation& animation )
62  *   {
63  *     std::cout << "Animation Finished!" << std::endl;
64  *   }
65  *
66  * private:
67  *
68  *  SlotDelegate<Example> mSlotDelegate;
69  *
70  * };
71  *
72  * @endcode
73  * @SINCE_1_0.0
74  */
75 template<typename Slot>
76 class SlotDelegate
77 {
78 public:
79   /**
80    * @brief Constructor.
81    *
82    * @SINCE_1_0.0
83    * @param[in] slot The object with a callback
84    */
85   SlotDelegate(Slot* slot)
86   : mSlot(slot)
87   {
88   }
89
90   /**
91    * @brief Non-virtual destructor.
92    *
93    * @SINCE_1_0.0
94    */
95   ~SlotDelegate()
96   {
97   }
98
99   /**
100    * @brief Disconnects all signals from this object.
101    *
102    * @SINCE_1_0.0
103    */
104   void DisconnectAll()
105   {
106     mConnectionTracker.DisconnectAll();
107   }
108
109   /**
110    * @copydoc ConnectionTracker::GetConnectionCount
111    */
112   std::size_t GetConnectionCount() const
113   {
114     return mConnectionTracker.GetConnectionCount();
115   }
116
117   /**
118    * @brief Retrieves the slot object.
119    *
120    * @SINCE_1_0.0
121    * @return The object with a callback
122    */
123   Slot* GetSlot()
124   {
125     return mSlot;
126   }
127
128   /**
129    * @brief Retrieves the connection tracker component.
130    *
131    * @SINCE_1_0.0
132    * @return The connection tracker component
133    */
134   ConnectionTracker* GetConnectionTracker()
135   {
136     return &mConnectionTracker;
137   }
138
139 private:
140   SlotDelegate(const SlotDelegate&) = delete;            ///< Deleted copy constructor. @SINCE_1_0.0
141   SlotDelegate(SlotDelegate&&)      = delete;            ///< Deleted move constructor. @SINCE_1_9.25
142   SlotDelegate& operator=(const SlotDelegate&) = delete; ///< Deleted copy assignment operator. @SINCE_1_0.0
143   SlotDelegate& operator=(SlotDelegate&&) = delete;      ///< Deleted move assignment operator. @SINCE_1_9.25
144
145 private:
146   Slot* mSlot; ///< The slot object
147
148   // Use composition instead of inheritance (virtual methods don't mix well with templates)
149   ConnectionTracker mConnectionTracker; ///< A connection tracker
150 };
151
152 /**
153  * @}
154  */
155 } // namespace Dali
156
157 #endif // DALI_SLOT_DELEGATE_H