Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / inc / FUi_SharedPtr.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #ifndef _FUI_INTERNAL_SHARED_PTR_H_
19 #define _FUI_INTERNAL_SHARED_PTR_H_
20
21 #include <new>
22
23 namespace Tizen { namespace Ui
24 {
25
26 template<typename BaseType>
27 class _SharedPtr
28 {
29 protected:
30         BaseType* _pInstance;
31         unsigned int* _useCount;
32
33 public:
34         _SharedPtr(void)
35                 : _pInstance(0)
36                 , _useCount(0)
37         {
38         }
39
40         explicit _SharedPtr(BaseType* pInstance)
41                 : _pInstance(pInstance)
42                 , _useCount(new (std::nothrow) unsigned int (1))
43         {
44         }
45
46         _SharedPtr(const _SharedPtr& refInstance)
47                 : _pInstance(0)
48                 , _useCount(0)
49         {
50                 _pInstance = refInstance._pInstance;
51                 _useCount = refInstance._useCount;
52
53                 if (_useCount)
54                         ++(*_useCount);
55         }
56
57         virtual ~_SharedPtr(void)
58         {
59                 _Release();
60         }
61
62         _SharedPtr& operator =(const _SharedPtr& refInstance)
63         {
64                 if (_pInstance == refInstance._pInstance)
65                 {
66                         return *this;
67                 }
68
69                 _Release();
70                 {
71                         _pInstance = refInstance._pInstance;
72                         _useCount = refInstance._useCount;
73                         if (_useCount)
74                         {
75                                 ++(*_useCount);
76                         }
77                 }
78
79                 return *this;
80         }
81
82         inline BaseType& operator *(void) const
83         {
84                 // assert(_pInstance);
85                 return *_pInstance;
86         }
87
88         inline BaseType* operator ->(void) const
89         {
90                 return _pInstance;
91         }
92
93         inline BaseType* Get(void) const
94         {
95                 return _pInstance;
96         }
97
98         void Bind(BaseType* rep)
99         {
100                 if (!_pInstance && !_useCount)
101                 {
102                         _useCount = new (std::nothrow) unsigned int(1);
103                         _pInstance = rep;
104                 }
105         }
106
107         inline bool Unique(void) const
108         {
109                 return (_useCount) ? (*_useCount == 1) : true;
110         }
111
112         inline unsigned int UseCount() const
113         {
114                 return (_useCount) ? *_useCount : 0;
115         }
116
117         inline unsigned int* UseCountPointer(void) const
118         {
119                 return _useCount;
120         }
121
122         inline bool IsNull(void) const
123         {
124                 return (_pInstance == 0);
125         }
126
127         inline void SetNull(void)
128         {
129                 if (_pInstance)
130                 {
131                         _Release();
132                         _pInstance = 0;
133                         _useCount = 0;
134                 }
135         }
136
137 protected:
138         inline void _Release(void)
139         {
140                 if (_useCount)
141                 {
142                         if (--(*_useCount) == 0)
143                         {
144                                 _Destroy();
145                         }
146                 }
147         }
148
149         virtual void _Destroy(void)
150         {
151                 delete _pInstance;
152                 delete _useCount;
153         }
154 };
155
156 template<typename BaseType1, typename BaseType2> inline bool
157 operator ==(_SharedPtr <BaseType1> const& a, _SharedPtr <BaseType2> const& b)
158 {
159         return a.Get() == b.Get();
160 }
161
162 template<typename BaseType1, typename BaseType2> inline bool
163 operator !=(_SharedPtr <BaseType1> const& a, _SharedPtr <BaseType2> const& b)
164 {
165         return a.Get() != b.Get();
166 }
167
168 }} // Tizen::Ui
169
170
171 #endif // _FUI_INTERNAL_SHARED_PTR_H_