Dali 3D User Interface Engine
owner-pointer.h
Go to the documentation of this file.
1 #ifndef __DALI_INTERNAL_OWNER_POINTER_H__
2 #define __DALI_INTERNAL_OWNER_POINTER_H__
3 
4 /*
5  * Copyright (c) 2014 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 // EXTERNAL INCLUDES
22 #include <cstddef> // NULL
23 
24 // INTERNAL INCLUDES
26 
27 namespace Dali
28 {
29 
30 namespace Internal
31 {
32 
33 template < typename T >
35 {
36 public:
41  {
42  mObject = NULL;
43  }
44 
49  OwnerPointer( T* object )
50  {
51  mObject = object;
52  }
53 
59  {
60  Init( other );
61  }
62 
68  {
69  if( this != &other ) // no self-assignment
70  {
71  Reset();
72  Init( other );
73  }
74 
75  // return self
76  return *this;
77  }
78 
84  OwnerPointer& operator=( T* pointer )
85  {
86  if( mObject != pointer )
87  {
88  Reset();
89  mObject = pointer;
90  }
91 
92  return *this;
93  }
94 
99  {
100  Reset();
101  }
102 
108  {
109  DALI_ASSERT_DEBUG( mObject != NULL );
110 
111  return *mObject;
112  }
113 
118  T& operator*() const
119  {
120  DALI_ASSERT_DEBUG( mObject != NULL );
121 
122  // Pointer semantics: A const pointer does not mean const data.
123  return const_cast< T& >( *mObject );
124  }
125 
131  {
132  return mObject;
133  }
134 
139  T* operator->() const
140  {
141  // Pointer semantics: A const pointer does not mean const data.
142  return const_cast< T* >( mObject );
143  }
144 
149  bool operator==( const T* pointer )
150  {
151  return ( mObject == pointer );
152  }
153 
157  void Reset()
158  {
159  if ( mObject != NULL )
160  {
161  delete mObject;
162  mObject = NULL;
163  }
164  }
165 
170  T* Release()
171  {
172  T* tmp = mObject;
173  mObject = NULL;
174  return tmp;
175  }
176 
181  const T* Get() const
182  {
183  return mObject;
184  }
185 
186  // Handle comparisons - This is a variation of the safe bool idiom
187 
191  typedef void (OwnerPointer::*BooleanType)() const;
192 
197  operator BooleanType() const
198  {
199  return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
200  }
201 
202 private:
203 
208 
209 private:
210 
216  void Init( OwnerPointer& ownerPointer )
217  {
218  mObject = ownerPointer.mObject;
219  ownerPointer.mObject = NULL;
220  }
221 
222  // data
223  T* mObject;
224 };
225 
226 } // namespace Internal
227 
228 } // namespace Dali
229 
230 #endif //__DALI_INTERNAL_OWNER_POINTER_H__
Dali Docs Home
Read more about Dali