[dali_1.9.20] Merge branch 'devel/master'
[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()
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   virtual void ObjectDestroyed( BaseObject& object )
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 bool WeakHandleBase::operator==( const WeakHandleBase& rhs ) const
113 {
114   return this->mImpl->mObject == rhs.mImpl->mObject;
115 }
116
117 bool WeakHandleBase::operator!=( const WeakHandleBase& rhs ) const
118 {
119   return !( *this == rhs );
120 }
121
122 BaseHandle WeakHandleBase::GetBaseHandle() const
123 {
124   return BaseHandle( mImpl->mObject );
125 }
126
127 void WeakHandleBase::Reset()
128 {
129   mImpl->Reset();
130 }
131
132
133 } // Dali