dbd619f91dae9eaa77e88ec8e97f07c944d7e888
[platform/core/uifw/dali-core.git] / dali / devel-api / common / scoped-pointer.h
1 #ifndef __DALI_SCOPED_POINTER_H__
2 #define __DALI_SCOPED_POINTER_H__
3 /*
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 namespace Dali
21 {
22
23 /**
24  * @brief Deletes the object pointed-to when it goes out of scope.
25  *
26  * A simple template class to call delete on an owned pointer when it goes
27  * out of scope, whether that be by ordinary return or stack unwind for
28  * exception throw.
29  */
30 template<typename Owned>
31 class ScopedPointer
32 {
33 public:
34
35   /**
36    * @brief Construct a ScopedPointer guarding a new Owned*.
37    */
38   ScopedPointer( Owned * const owned ) :
39     mOwned( owned )
40   {}
41
42    /**
43     * @brief Destroy the ScopedPointer and clean up its Owned*.
44     */
45   ~ScopedPointer()
46   {
47     if( mOwned != 0 )
48     {
49       delete mOwned;
50       mOwned = 0;
51     }
52   }
53
54   /**
55    * @brief Getter for the underlying pointer.
56    * @return The Owned* guarded by this object.
57    */
58   Owned* Get() const
59   {
60     return mOwned;
61   }
62
63   /**
64    * @brief Give up ownership of the object guarded by this pointer.
65    * @return The Owned* previously guarded by this object.
66    */
67   Owned* Release()
68   {
69     Owned* const owned = mOwned;
70     mOwned = 0;
71     return owned;
72   }
73
74   /**
75    * @brief Dereference this pointer.
76    */
77   Owned& operator*() const
78   {
79     return *mOwned;
80   }
81
82   /**
83    * @brief Allow member access through arrow notation.
84    */
85   Owned * operator->() const
86   {
87     return mOwned;
88   }
89
90 private:
91
92   // Non-copyable:
93   ScopedPointer( const ScopedPointer& rhs );
94   ScopedPointer& operator = ( const ScopedPointer& rhs );
95
96   Owned* mOwned;
97 };
98
99 } /* namespace Dali */
100
101 #endif /* __DALI_SCOPED_POINTER_H__ */