use modern construct 'override' in the derive class.
[platform/core/uifw/dali-core.git] / dali / public-api / object / weak-handle.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/object/weak-handle.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/common/base-object-impl.h>
23
24 namespace Dali
25 {
26
27 struct WeakHandleBase::Impl : public BaseObject::Impl::Observer
28 {
29   // Construction
30   Impl()
31   : mObject( nullptr )
32   {
33   }
34
35   // Construction
36   Impl( BaseHandle& handle )
37   : mObject( nullptr )
38   {
39     if( handle )
40     {
41       mObject = static_cast<Dali::BaseObject*>( handle.GetObjectPtr() );
42       if( mObject )
43       {
44         BaseObject::Impl::Get( *mObject ).AddObserver( *this );
45       }
46     }
47   }
48
49   // Destruction
50   ~Impl() override
51   {
52     Reset();
53   }
54
55   void Reset()
56   {
57     if( mObject )
58     {
59       BaseObject::Impl::Get( *mObject ).RemoveObserver( *this );
60       mObject = nullptr;
61     }
62   }
63
64   /**
65    * From BaseObject::Impl::Observer
66    */
67   void ObjectDestroyed( BaseObject& object ) override
68   {
69     mObject = nullptr;
70   }
71
72   // Data
73   Dali::BaseObject* mObject;
74 };
75
76 WeakHandleBase::WeakHandleBase()
77 : mImpl( new Impl() )
78 {
79 }
80
81 WeakHandleBase::WeakHandleBase( BaseHandle& handle )
82 : mImpl( new Impl( handle ) )
83 {
84 }
85
86 WeakHandleBase::~WeakHandleBase()
87 {
88   delete mImpl;
89   mImpl = nullptr;
90 }
91
92 WeakHandleBase::WeakHandleBase(const WeakHandleBase& handle)
93 : mImpl( nullptr )
94 {
95   BaseHandle object = handle.GetBaseHandle();
96   mImpl = new Impl(object);
97 }
98
99 WeakHandleBase& WeakHandleBase::operator=( const WeakHandleBase& rhs )
100 {
101   if( this != &rhs )
102   {
103     delete mImpl;
104
105     BaseHandle handle = rhs.GetBaseHandle();
106     mImpl = new Impl(handle);
107   }
108
109   return *this;
110 }
111
112 WeakHandleBase::WeakHandleBase( WeakHandleBase&& rhs )
113 : mImpl( rhs.mImpl )
114 {
115   rhs.mImpl = nullptr;
116 }
117
118 WeakHandleBase& WeakHandleBase::operator=( WeakHandleBase&& rhs )
119 {
120   if (this != &rhs)
121   {
122     delete mImpl;
123
124     mImpl = rhs.mImpl;
125     rhs.mImpl = nullptr;
126   }
127
128   return *this;
129 }
130
131 bool WeakHandleBase::operator==( const WeakHandleBase& rhs ) const
132 {
133   return this->mImpl->mObject == rhs.mImpl->mObject;
134 }
135
136 bool WeakHandleBase::operator!=( const WeakHandleBase& rhs ) const
137 {
138   return !( *this == rhs );
139 }
140
141 BaseHandle WeakHandleBase::GetBaseHandle() const
142 {
143   return mImpl ? BaseHandle( mImpl->mObject ) : BaseHandle();
144 }
145
146 void WeakHandleBase::Reset()
147 {
148   mImpl->Reset();
149 }
150
151
152 } // Dali