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