2e247585aecce24a9fbb6296b21abec7eed24d90
[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 protected:
97
98   struct Impl;
99   Impl* mImpl;
100 };
101
102 /**
103  * @brief Type CustomActors support
104  */
105 template <typename Type>
106 struct CustomActors
107 {
108   /**
109    * This flag tells Dali if a class is derived from CustomActor.
110    */
111   enum { IS_CUSTOM_ACTOR = __is_base_of(Dali::CustomActor, Type) };
112 };
113
114 template <typename Type>
115 struct TypeCustomActors : public CustomActors< Type >
116 {
117 };
118
119 template < bool CustomActorType >
120 class InternalTypeName
121 {
122 public: // Typedefs
123
124   typedef Dali::Internal::CustomActor InternalObjectType;
125 };
126
127 template <>
128 class InternalTypeName< false >
129 {
130 public: // Typedefs
131
132   typedef Dali::Internal::Actor InternalObjectType;
133 };
134
135 /**
136  * @brief Weak handle for the given type of Dali object.
137  */
138 template < class T >
139 class WeakHandle : public WeakHandleBase
140 {
141 public: // Typedefs
142
143   typedef typename InternalTypeName< TypeCustomActors<T>::IS_CUSTOM_ACTOR >::InternalObjectType InternalObjectType;
144
145 public:
146
147   /**
148    * @copydoc Dali::WeakHandleBase::WeakHandleBase()
149    */
150   WeakHandle()
151   : WeakHandleBase()
152   {
153   }
154
155   /**
156    * @copydoc Dali::WeakHandleBase::WeakHandleBase(Handle&)
157    */
158   WeakHandle( T& handle )
159   : WeakHandleBase( handle )
160   {
161   }
162
163   /**
164    * @copydoc Dali::WeakHandleBase::~WeakHandleBase()
165    */
166   ~WeakHandle() {}
167
168   /**
169    * @copydoc Dali::WeakHandleBase::WeakHandleBase(const WeakHandleBase&)
170    */
171   WeakHandle(const WeakHandle& handle)
172   : WeakHandleBase( handle )
173   {
174   }
175
176   /**
177    * @copydoc Dali::WeakHandleBase::operator=()
178    */
179   WeakHandle& operator=( const WeakHandle& rhs )
180   {
181     WeakHandleBase::operator=(rhs);
182     return *this;
183   }
184
185   /**
186    * @copydoc Dali::WeakHandleBase::operator==()
187    */
188   bool operator==(const WeakHandle& rhs) const
189   {
190     return WeakHandleBase::operator==(rhs);
191   }
192
193   /**
194    * @copydoc Dali::WeakHandleBase::operator!=()
195    */
196   bool operator!=(const WeakHandle& rhs) const
197   {
198     return WeakHandleBase::operator!=(rhs);
199   }
200
201   /**
202    * @copydoc Dali::WeakHandleBase::GetHandle()
203    */
204   T GetHandle()
205   {
206     Handle handle( GetBaseHandle() );
207     if( handle )
208     {
209       return T( reinterpret_cast< InternalObjectType* >( handle.GetObjectPtr() ) );
210     }
211     else
212     {
213       return T();
214     }
215   }
216 };
217
218 } // namespace Dali
219
220 #endif // __DALI_WEAK_HANDLE_H__