1 #ifndef DALI_FUNCTOR_DELEGATE_H
2 #define DALI_FUNCTOR_DELEGATE_H
5 * Copyright (c) 2020 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>
27 * @addtogroup dali_core_signals
32 * @brief Dispatcher to call a functor.
36 struct FunctorDispatcher
39 * @brief Calls a function object.
42 * @param[in] functorPtr The functor to call
44 static void Dispatch(void* functorPtr)
46 // "downcast" the functor type back to the correct one
47 T* functor = reinterpret_cast<T*>(functorPtr);
53 * @brief Dispatcher to delete a functor object.
57 struct FunctorDestroyer
60 * @brief Dispatcher to delete an object.
62 * @param[in] functorPtr A functor object to delete
64 static void Delete(void* functorPtr)
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);
73 * @brief Used to connect a void() functor to a signal via BaseObject::SignalConnect().
76 class DALI_CORE_API FunctorDelegate
80 * @brief Constructor which copies a function object.
83 * @param[in] functor The functor object to copy, either a class with operator() or a C function
84 * @return A pointer to the new function object
87 static FunctorDelegate* New(const T& functor)
89 return new FunctorDelegate(reinterpret_cast<void*>(new T(functor)), // heap allocate the functor
90 reinterpret_cast<FunctorDelegate::Dispatcher>(&FunctorDispatcher<T>::Dispatch),
91 reinterpret_cast<FunctorDelegate::Destructor>(&FunctorDestroyer<T>::Delete));
95 * @brief Non-virtual destructor; not intended as a base class.
101 * @brief Function to call the function or member function dispatcher.
108 * @brief Used to call the correct function.
111 using Dispatcher = void (*)(void*);
114 * @brief Used to destroy mObjectPointer.
117 using Destructor = void (*)(void*);
120 * @brief Not defined.
123 FunctorDelegate(const FunctorDelegate& rhs);
126 * @brief Not defined.
129 const FunctorDelegate& operator=(const FunctorDelegate& rhs);
132 * @brief Private constructor.
135 * @param[in] functorPtr A newly allocated functor object (takes ownership)
136 * @param dispatcher Used to call the actual function
137 * @param destructor Used to delete the owned functor object
139 FunctorDelegate(void* functorPtr, Dispatcher dispatcher, Destructor destructor);
142 // Data for deriving classes & Dispatchers
144 void* mFunctorPointer; ///< Functor that will be called
145 Dispatcher mMemberFunctionDispatcher; ///< Dispatcher for member functions
146 Destructor mDestructorDispatcher; ///< Destructor for owned objects
154 #endif // DALI_FUNCTOR_DELEGATE_H