add base type of enum to reduce class size.
[platform/core/uifw/dali-core.git] / dali / public-api / signals / callback.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/signals/callback.h>
20
21 namespace Dali
22 {
23 // CallbackBase
24
25 CallbackBase::CallbackBase()
26 : mImpl(nullptr),
27   mFunction(nullptr)
28 {
29 }
30
31 CallbackBase::~CallbackBase()
32 {
33   Reset();
34 }
35
36 CallbackBase::CallbackBase(Function function)
37 : mImpl(nullptr),
38   mFunction(function)
39 {
40 }
41
42 CallbackBase::CallbackBase(void* object, MemberFunction function, Dispatcher dispatcher)
43 : mMemberFunction(function)
44 {
45   mImpl = new CallbackBase::Impl;
46   if(mImpl)
47   {
48     mImpl->mObjectPointer            = object;
49     mImpl->mMemberFunctionDispatcher = dispatcher;
50     mImpl->mDestructorDispatcher     = nullptr; // object is not owned
51   }
52 }
53
54 CallbackBase::CallbackBase(void* object, MemberFunction function, Dispatcher dispatcher, Destructor destructor)
55 : mMemberFunction(function)
56 {
57   mImpl = new CallbackBase::Impl;
58   if(mImpl)
59   {
60     mImpl->mObjectPointer            = object;
61     mImpl->mMemberFunctionDispatcher = dispatcher;
62     mImpl->mDestructorDispatcher     = destructor; // object is owned
63   }
64 }
65
66 void CallbackBase::Reset()
67 {
68   if(mImpl)
69   {
70     // if destructor function is set it means we own this object
71     if(mImpl->mObjectPointer &&
72        mImpl->mDestructorDispatcher)
73     {
74       // call the destructor dispatcher
75       (*mImpl->mDestructorDispatcher)(mImpl->mObjectPointer);
76     }
77
78     delete mImpl;
79     mImpl = nullptr;
80   }
81
82   mFunction = nullptr;
83 }
84
85 // CallbackBase::Impl
86
87 CallbackBase::Impl::Impl()
88 : mObjectPointer(nullptr),
89   mMemberFunctionDispatcher(nullptr),
90   mDestructorDispatcher(nullptr)
91 {
92 }
93
94 // Non-member equality operator
95
96 bool operator==(const CallbackBase& lhs, const CallbackBase& rhs)
97 {
98   if(lhs.mImpl)
99   {
100     // check it's the same member function / object
101     return (lhs.mFunction == rhs.mFunction) && (lhs.mImpl->mObjectPointer == rhs.mImpl->mObjectPointer);
102   }
103   else
104   {
105     // check if it the same C function or a static member function
106     return (lhs.mFunction == rhs.mFunction);
107   }
108 }
109
110 } // namespace Dali