1 #ifndef __DALI_FUNCTOR_DELEGATE_H__
2 #define __DALI_FUNCTOR_DELEGATE_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/common/dali-common.h>
24 namespace Dali DALI_IMPORT_API
28 * @brief Dispatcher to call a functor
31 struct FunctorDispatcher
34 * @brief Call a function object.
36 * @param[in] objectPtr The object to call.
38 static void Dispatch( void* objectPtr )
40 // "downcast" the object and function type back to the correct ones
41 T* object = reinterpret_cast< T* >( objectPtr );
47 * @brief Dispatcher to delete an object
50 struct FunctorDestroyer
53 * @brief Dispatcher to delete an object
55 static void Delete( void* object )
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* >( object );
64 * @brief Used to connect a void() functor to a signal via BaseObject::SignalConnect()
71 * @brief Constructor which copies a function object.
73 * @param[in] object The object to copy.
74 * @return A pointer to the new function object
77 static FunctorDelegate* New( const T& object )
79 return new FunctorDelegate( reinterpret_cast< void* >( new T( object ) ), // copy the object
80 reinterpret_cast< FunctorDelegate::Dispatcher >( &FunctorDispatcher<T>::Dispatch ),
81 reinterpret_cast< FunctorDelegate::Destructor >( &FunctorDestroyer<T>::Delete ) );
85 * @brief Non-virtual destructor; not intended as a base class.
90 * @brief Function to call the function or member function dispatcher
97 * @brief Used to call the correct member function.
99 typedef void (*Dispatcher)( void* objectPtr );
102 * @brief Used to destroy mObjectPointer (NULL if not mObjectPointer is not owned).
104 typedef void(*Destructor)( void* objectPtr );
109 FunctorDelegate( const FunctorDelegate& rhs );
114 const FunctorDelegate& operator=( const FunctorDelegate& rhs );
117 * @brief Private constructor.
119 * @param[in] objectPtr A newly allocated object (takes ownership)
120 * @param dispatcher Used to call the actual object.
121 * @param destructor Used to delete the owned object.
123 FunctorDelegate( void* objectPtr, Dispatcher dispatcher, Destructor destructor );
125 public: // Data for deriving classes & Dispatchers
127 void* mObjectPointer; ///< Object whose member function will be called. Not owned if mDestructorDispatcher is NULL.
128 Dispatcher mMemberFunctionDispatcher; ///< Dispatcher for member functions
129 Destructor mDestructorDispatcher; ///< Destructor for owned objects. NULL if mDestructorDispatcher is not owned.
134 #endif // __DALI_FUNCTOR_DELEGATE_H__