use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / public-api / signals / callback.cpp
1 /*
2  * Copyright (c) 2015 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
22 namespace Dali
23 {
24
25 // CallbackBase
26
27 CallbackBase::CallbackBase()
28 : mImpl( nullptr ),
29   mFunction( nullptr )
30 {
31 }
32
33 CallbackBase::~CallbackBase()
34 {
35   Reset();
36 }
37
38 CallbackBase::CallbackBase( Function function )
39 : mImpl( nullptr ),
40   mFunction( function )
41 {
42 }
43
44 CallbackBase::CallbackBase( void* object, MemberFunction function, Dispatcher dispatcher )
45 : mMemberFunction( function )
46 {
47   mImpl = new CallbackBase::Impl;
48   if( mImpl )
49   {
50     mImpl->mObjectPointer = object;
51     mImpl->mMemberFunctionDispatcher = dispatcher;
52     mImpl->mDestructorDispatcher = nullptr; // object is not owned
53   }
54 }
55
56 CallbackBase::CallbackBase( void* object, MemberFunction function, Dispatcher dispatcher, Destructor destructor )
57 : mMemberFunction( function )
58 {
59   mImpl = new CallbackBase::Impl;
60   if( mImpl )
61   {
62     mImpl->mObjectPointer = object;
63     mImpl->mMemberFunctionDispatcher = dispatcher;
64     mImpl->mDestructorDispatcher = destructor; // object is owned
65   }
66 }
67
68 void CallbackBase::Reset()
69 {
70   if( mImpl )
71   {
72     // if destructor function is set it means we own this object
73     if ( mImpl->mObjectPointer &&
74          mImpl->mDestructorDispatcher )
75     {
76       // call the destructor dispatcher
77       (*mImpl->mDestructorDispatcher)( mImpl->mObjectPointer );
78     }
79
80     delete mImpl;
81     mImpl = nullptr;
82   }
83
84   mFunction = nullptr;
85 }
86
87 // CallbackBase::Impl
88
89 CallbackBase::Impl::Impl()
90 : mObjectPointer( nullptr ),
91   mMemberFunctionDispatcher( nullptr ),
92   mDestructorDispatcher( nullptr )
93 {
94 }
95
96 // Non-member equality operator
97
98 bool operator==( const CallbackBase& lhs, const CallbackBase& rhs )
99 {
100   if( lhs.mImpl )
101   {
102     // check it's the same member function / object
103     return ( lhs.mFunction == rhs.mFunction ) &&  ( lhs.mImpl->mObjectPointer == rhs.mImpl->mObjectPointer );
104   }
105   else
106   {
107     // check if it the same C function or a static member function
108     return ( lhs.mFunction == rhs.mFunction );
109   }
110 }
111
112 } // namespace DALI