Merge "Revert "Initialize random number generator"" into devel/master
[platform/core/uifw/dali-core.git] / dali / devel-api / object / weak-handle.h
1 #ifndef __DALI_WEAK_HANDLE_H__
2 #define __DALI_WEAK_HANDLE_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/handle.h>
23 #include <dali/public-api/actors/custom-actor.h>
24
25 namespace Dali
26 {
27
28 /**
29  * @brief Base class to store a weak pointer to an internal Dali object. The handle to the object
30  * can be accessed if the object exists, and such access is not reference counted. When the object
31  * is deleted, the weak pointer will be set to NULL, and any further attmpt to access to a deleted
32  * object will return an empty handle.
33  *
34  */
35 class DALI_IMPORT_API WeakHandleBase
36 {
37
38 public:
39
40   /**
41    * @brief Default constructor which provides an uninitialized Dali::WeakHandleBase.
42    */
43   WeakHandleBase();
44
45   /**
46    * @brief This constructor creates a weak handle of the Dali object.
47    *
48    * @param [in] handle A reference to the handle of the Dali object.
49    */
50   WeakHandleBase( Handle& handle );
51
52   /**
53    * @brief Destructor to free resources.
54    */
55   ~WeakHandleBase();
56
57   /**
58    * @brief Copy constructor.
59    *
60    * @param [in] handle A reference to the copied WeakHandleBase
61    */
62   WeakHandleBase(const WeakHandleBase& handle);
63
64   /**
65    * @brief Assignment operator.
66    *
67    * It makes this WeakHandleBase point to the same internal Dali object as the copied WeakHandleBase
68    * @param [in] rhs  A reference to the copied WeakHandleBase
69    * @return A reference to this WeakHandleBase
70    */
71   WeakHandleBase& operator=( const WeakHandleBase& rhs );
72
73   /**
74    * @brief Equality operator overload.
75    *
76    * @param [in] rhs A reference to the compared WeakHandleBase.
77    * @return true if the handle points to the same Dali resource, or if both are uninitialized.
78    */
79   bool operator==(const WeakHandleBase& rhs) const;
80
81   /**
82    * @brief Inequality operator overload.
83    *
84    * @param [in] rhs A reference to the compared WeakHandleBase.
85    * @return true if the handle points to the different Dali resources.
86    */
87   bool operator!=(const WeakHandleBase& rhs) const;
88
89   /**
90    * @brief Gets the handle to the Dali object.
91    *
92    * @return The handle of the Dali object pointed by this WeakHandleBase or an empty handle if the Dali object doesn't exist.
93    */
94   Handle GetBaseHandle() const;
95
96   /**
97    * @brief Resets this weak handle to not point to any Dali object
98    */
99   void Reset();
100
101
102 protected:
103
104   struct Impl;
105   Impl* mImpl;
106 };
107
108
109 /**
110  * @brief Weak handle for the given type of Dali object.
111  */
112 template < class T >
113 class WeakHandle : public WeakHandleBase
114 {
115 public:
116
117   /**
118    * @copydoc Dali::WeakHandleBase::WeakHandleBase()
119    */
120   WeakHandle()
121   : WeakHandleBase()
122   {
123   }
124
125   /**
126    * @copydoc Dali::WeakHandleBase::WeakHandleBase(Handle&)
127    */
128   WeakHandle( T& handle )
129   : WeakHandleBase( handle )
130   {
131   }
132
133   /**
134    * @copydoc Dali::WeakHandleBase::~WeakHandleBase()
135    */
136   ~WeakHandle() {}
137
138   /**
139    * @copydoc Dali::WeakHandleBase::WeakHandleBase(const WeakHandleBase&)
140    */
141   WeakHandle(const WeakHandle& handle)
142   : WeakHandleBase( handle )
143   {
144   }
145
146   /**
147    * @copydoc Dali::WeakHandleBase::operator=()
148    */
149   WeakHandle& operator=( const WeakHandle& rhs )
150   {
151     WeakHandleBase::operator=(rhs);
152     return *this;
153   }
154
155   /**
156    * @copydoc Dali::WeakHandleBase::operator==()
157    */
158   bool operator==(const WeakHandle& rhs) const
159   {
160     return WeakHandleBase::operator==(rhs);
161   }
162
163   /**
164    * @copydoc Dali::WeakHandleBase::operator!=()
165    */
166   bool operator!=(const WeakHandle& rhs) const
167   {
168     return WeakHandleBase::operator!=(rhs);
169   }
170
171   /**
172    * @copydoc Dali::WeakHandleBase::GetBaseHandle()
173    */
174   T GetHandle() const
175   {
176     Handle handle( GetBaseHandle() );
177     if( handle )
178     {
179       return DownCast< T >( handle );
180     }
181     else
182     {
183       return T();
184     }
185   }
186 };
187
188 } // namespace Dali
189
190 #endif // __DALI_WEAK_HANDLE_H__