Merge "Revert "Initialize random number generator"" into devel/master
[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     Reset();
53   }
54
55   void Reset()
56   {
57     if( mObject )
58     {
59       mObject->RemoveObserver( *this );
60       mObject = NULL;
61     }
62   }
63
64   /**
65    * From Object::Observer
66    */
67   virtual void SceneObjectAdded( Internal::Object& object )
68   {
69   }
70
71   /**
72    * From Object::Observer
73    */
74   virtual void SceneObjectRemoved( Internal::Object& object )
75   {
76   }
77
78   /**
79    * From Object::Observer
80    */
81   virtual void ObjectDestroyed( Internal::Object& object )
82   {
83     mObject = NULL;
84   }
85
86   // Data
87   Internal::Object* mObject;
88 };
89
90 WeakHandleBase::WeakHandleBase()
91 : mImpl( new Impl() )
92 {
93 }
94
95 WeakHandleBase::WeakHandleBase( Handle& handle )
96 : mImpl( new Impl( handle ) )
97 {
98 }
99
100 WeakHandleBase::~WeakHandleBase()
101 {
102   delete mImpl;
103   mImpl = NULL;
104 }
105
106 WeakHandleBase::WeakHandleBase(const WeakHandleBase& handle)
107 : mImpl( NULL )
108 {
109   Handle object = handle.GetBaseHandle();
110   mImpl = new Impl(object);
111 }
112
113 WeakHandleBase& WeakHandleBase::operator=( const WeakHandleBase& rhs )
114 {
115   if( this != &rhs )
116   {
117     delete mImpl;
118
119     Handle handle = rhs.GetBaseHandle();
120     mImpl = new Impl(handle);
121   }
122
123   return *this;
124 }
125
126 bool WeakHandleBase::operator==( const WeakHandleBase& rhs ) const
127 {
128   return this->mImpl->mObject == rhs.mImpl->mObject;
129 }
130
131 bool WeakHandleBase::operator!=( const WeakHandleBase& rhs ) const
132 {
133   return !( *this == rhs );
134 }
135
136 Handle WeakHandleBase::GetBaseHandle() const
137 {
138   return Handle( mImpl->mObject );
139 }
140
141 void WeakHandleBase::Reset()
142 {
143   mImpl->Reset();
144 }
145
146
147 } // Dali