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