Revert " modify license, permission and remove ^M char"
[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  * @file        FUi_SharedPtr.h
19  * @brief       This is the header file for the _SharedPtr class.
20  *
21  * This file contains the declarations of the _SharedPtr class.
22  */
23
24 #ifndef _FUI_INTERNAL_SHARED_PTR_H_
25 #define _FUI_INTERNAL_SHARED_PTR_H_
26
27 #include <new>
28
29 namespace Tizen { namespace Ui
30 {
31
32 template<typename BaseType>
33 class _SharedPtr
34 {
35 protected:
36         BaseType* _pInstance;
37         unsigned int* _useCount;
38
39 public:
40         _SharedPtr(void)
41                 : _pInstance(0)
42                 , _useCount(0)
43         {
44         }
45
46         explicit _SharedPtr(BaseType* pInstance)
47                 : _pInstance(pInstance)
48                 , _useCount(new (std::nothrow) unsigned int (1))
49         {
50         }
51
52         _SharedPtr(const _SharedPtr& refInstance)
53                 : _pInstance(0)
54                 , _useCount(0)
55         {
56                 _pInstance = refInstance._pInstance;
57                 _useCount = refInstance._useCount;
58
59                 if (_useCount)
60                         ++(*_useCount);
61         }
62
63         virtual ~_SharedPtr(void)
64         {
65                 _Release();
66         }
67
68         _SharedPtr& operator =(const _SharedPtr& refInstance)
69         {
70                 if (_pInstance == refInstance._pInstance)
71                 {
72                         return *this;
73                 }
74
75                 _Release();
76                 {
77                         _pInstance = refInstance._pInstance;
78                         _useCount = refInstance._useCount;
79                         if (_useCount)
80                         {
81                                 ++(*_useCount);
82                         }
83                 }
84
85                 return *this;
86         }
87
88         inline BaseType& operator *(void) const
89         {
90                 // assert(_pInstance);
91                 return *_pInstance;
92         }
93
94         inline BaseType* operator ->(void) const
95         {
96                 return _pInstance;
97         }
98
99         inline BaseType* Get(void) const
100         {
101                 return _pInstance;
102         }
103
104         void Bind(BaseType* rep)
105         {
106                 if (!_pInstance && !_useCount)
107                 {
108                         _useCount = new (std::nothrow) unsigned int(1);
109                         _pInstance = rep;
110                 }
111         }
112
113         inline bool Unique(void) const
114         {
115                 return (_useCount) ? (*_useCount == 1) : true;
116         }
117
118         inline unsigned int UseCount() const
119         {
120                 return (_useCount) ? *_useCount : 0;
121         }
122
123         inline unsigned int* UseCountPointer(void) const
124         {
125                 return _useCount;
126         }
127
128         inline bool IsNull(void) const
129         {
130                 return (_pInstance == 0);
131         }
132
133         inline void SetNull(void)
134         {
135                 if (_pInstance)
136                 {
137                         _Release();
138                         _pInstance = 0;
139                         _useCount = 0;
140                 }
141         }
142
143 protected:
144         inline void _Release(void)
145         {
146                 if (_useCount)
147                 {
148                         if (--(*_useCount) == 0)
149                         {
150                                 _Destroy();
151                         }
152                 }
153         }
154
155         virtual void _Destroy(void)
156         {
157                 delete _pInstance;
158                 delete _useCount;
159         }
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 template<typename BaseType1, typename BaseType2> inline bool
169 operator !=(_SharedPtr <BaseType1> const& a, _SharedPtr <BaseType2> const& b)
170 {
171         return a.Get() != b.Get();
172 }
173
174 }} // Tizen::Ui
175
176
177 #endif // _FUI_INTERNAL_SHARED_PTR_H_