858c1ce70efb272ddec1b9f797d67fc85d64f612
[platform/core/uifw/dali-core.git] / dali / public-api / signals / functor-delegate.h
1 #ifndef __DALI_FUNCTOR_DELEGATE_H__
2 #define __DALI_FUNCTOR_DELEGATE_H__
3
4 /*
5  * Copyright (c) 2018 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
24 namespace Dali
25 {
26 /**
27  * @addtogroup dali_core_signals
28  * @{
29  */
30
31 /**
32  * @brief Dispatcher to call a functor.
33  * @SINCE_1_0.0
34  */
35 template< typename T >
36 struct FunctorDispatcher
37 {
38   /**
39    * @brief Calls a function object.
40    *
41    * @SINCE_1_0.0
42    * @param[in] functorPtr The functor to call
43    */
44   static void Dispatch( void* functorPtr )
45   {
46     // "downcast" the functor type back to the correct one
47     T* functor = reinterpret_cast< T* >( functorPtr );
48     (*functor)();
49   }
50 };
51
52 /**
53  * @brief Dispatcher to delete a functor object.
54  * @SINCE_1_0.0
55  */
56 template< typename T >
57 struct FunctorDestroyer
58 {
59   /**
60    * @brief Dispatcher to delete an object.
61    * @SINCE_1_0.0
62    * @param[in] functorPtr A functor object to delete
63    */
64   static void Delete( void* functorPtr )
65   {
66     // FunctorDelegate owns the object but we're the only one who knows the real type so need
67     // to delete by "downcasting" from void* to the correct type
68     delete reinterpret_cast< T* >( functorPtr );
69   }
70 };
71
72 /**
73  * @brief Used to connect a void() functor to a signal via BaseObject::SignalConnect().
74  * @SINCE_1_0.0
75  */
76 class DALI_CORE_API FunctorDelegate
77 {
78 public:
79
80   /**
81    * @brief Constructor which copies a function object.
82    *
83    * @SINCE_1_0.0
84    * @param[in] functor The functor object to copy, either a class with operator() or a C function
85    * @return A pointer to the new function object
86    */
87   template< typename T >
88   static FunctorDelegate* New( const T& functor )
89   {
90     return new FunctorDelegate( reinterpret_cast< void* >( new T( functor ) ), // heap allocate the functor
91                                 reinterpret_cast< FunctorDelegate::Dispatcher >( &FunctorDispatcher<T>::Dispatch ),
92                                 reinterpret_cast< FunctorDelegate::Destructor >( &FunctorDestroyer<T>::Delete ) );
93   }
94
95   /**
96    * @brief Non-virtual destructor; not intended as a base class.
97    * @SINCE_1_0.0
98    */
99   ~FunctorDelegate();
100
101   /**
102    * @brief Function to call the function or member function dispatcher.
103    * @SINCE_1_0.0
104    */
105   void Execute();
106
107 private:
108
109   /**
110    * @brief Used to call the correct function.
111    * @SINCE_1_0.0
112    */
113   typedef void (*Dispatcher)( void* objectPtr );
114
115   /**
116    * @brief Used to destroy mObjectPointer.
117    * @SINCE_1_0.0
118    */
119   typedef void(*Destructor)( void* objectPtr );
120
121   /**
122    * @brief Not defined.
123    * @SINCE_1_0.0
124    */
125   FunctorDelegate( const FunctorDelegate& rhs );
126
127   /**
128    * @brief Not defined.
129    * @SINCE_1_0.0
130    */
131   const FunctorDelegate& operator=( const FunctorDelegate& rhs );
132
133   /**
134    * @brief Private constructor.
135    *
136    * @SINCE_1_0.0
137    * @param[in] functorPtr A newly allocated functor object (takes ownership)
138    * @param dispatcher Used to call the actual function
139    * @param destructor Used to delete the owned functor object
140    */
141   FunctorDelegate( void* functorPtr, Dispatcher dispatcher, Destructor destructor );
142
143 public: // Data for deriving classes & Dispatchers
144
145   void* mFunctorPointer;                ///< Functor that will be called
146   Dispatcher mMemberFunctionDispatcher; ///< Dispatcher for member functions
147   Destructor mDestructorDispatcher;     ///< Destructor for owned objects
148 };
149
150 /**
151  * @}
152  */
153 } // namespace DALI
154
155 #endif // __DALI_FUNCTOR_DELEGATE_H__