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