Fixed get instance for setting client.
[platform/framework/native/appfw.git] / src / app / FApp_RequestManagerT.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FApp_RequestManagerT.cpp
19  * @brief       This is the implementationfor the _RequestManagerT class.
20  */
21
22 #include <limits.h>
23
24 #include <FBaseRt_LibraryImpl.h>
25
26 #include "FApp_RequestManagerT.h"
27 #include "FApp_LaunchInfo.h"
28 #include "FApp_AppArg.h"
29
30 using namespace Tizen::Base::Runtime;
31
32
33 namespace Tizen { namespace App
34 {
35
36 _ResultInfo::~_ResultInfo(void)
37 {
38         _AppArg* pArg = &arg;
39         delete pArg;
40 }
41
42 _InProcessInfo::~_InProcessInfo(void)
43 {
44         delete pLib;
45 }
46
47 _LaunchInfo::~_LaunchInfo(void)
48 {
49         delete pArg;
50 }
51
52
53 template<typename T>
54 _RequestManagerT<T>::_RequestManagerT(void)
55 : __reqId(-1)
56 {
57         __requestList.Construct();
58 }
59
60 template<typename T>
61 _RequestManagerT<T>::~_RequestManagerT(void)
62 {
63         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
64         if (pIter == null)
65         {
66                 return;
67         }
68
69         while (pIter->MoveNext() == E_SUCCESS)
70         {
71                 T* pInfo = null;
72                 result r = pIter->GetValue(pInfo);
73
74                 if (IsFailed(r))
75                 {
76                         continue;
77                 }
78
79                 delete pInfo;
80         }
81
82         delete pIter;
83 }
84
85 template<typename T> int
86 _RequestManagerT<T>::InsertItem(T* pItem)
87 {
88         SysTryReturn(NID_APP, pItem != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
89
90 #if 0
91         // [INFO] temporary disable due to OBS build system
92         __sync_add_and_fetch(&__reqId, 1);
93
94         // [TODO] awkward implementation after overflow
95         // overflow detection
96         __sync_bool_compare_and_swap(&__reqId, INT_MAX, 1);
97 #else
98         __reqId++;
99         if (__reqId >= INT_MAX)
100         {
101                 __reqId = 1;
102         }
103 #endif
104         SysLog(NID_APP, "Current request ID : %d.", __reqId);
105
106         pItem->reqId = __reqId;
107         __requestList.Add(__reqId, pItem);
108
109         return __reqId;
110 }
111
112 template<typename T> void
113 _RequestManagerT<T>::RemoveItem(int reqId)
114 {
115         T* pItem = FindItem(reqId);
116         if (pItem)
117         {
118                 __requestList.Remove(reqId);
119                 delete pItem;
120         }
121 }
122
123 template<typename T> void
124 _RequestManagerT<T>::RemoveItem(T* pItem)
125 {
126         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
127         if (pItem == null || pIter == null)
128         {
129                 return;
130         }
131
132         while (pIter->MoveNext() == E_SUCCESS)
133         {
134                 T* pInfo = null;
135                 result r = pIter->GetValue(pInfo);
136
137                 if (pInfo == pItem)
138                 {
139                         int id = -1;
140                         r = pIter->GetKey(id);
141                         if (r == E_SUCCESS)
142                         {
143                                 __requestList.Remove(id);
144                         }
145
146                         delete pItem;
147                         delete pIter;
148
149                         return;
150                 }
151         }
152
153         delete pIter;
154 }
155
156 template<typename T> T*
157 _RequestManagerT<T>::FindItem(int reqId) const
158 {
159         T* pItem = null;
160         result r = __requestList.GetValue(reqId, pItem);
161
162         return (r == E_SUCCESS) ? pItem : null;
163 }
164
165
166 // specialization
167 template<>
168 _RequestManagerT<_ResultInfo>::~_RequestManagerT(void)
169 {
170         // clear 1st request
171         _ResultInfo* pItem = FindItem(0);
172         if (pItem)
173         {
174                 __requestList.Remove(0);
175                 delete pItem;
176         }
177
178
179         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
180         if (pIter == null)
181         {
182                 return;
183         }
184
185         while (pIter->MoveNext() == E_SUCCESS)
186         {
187                 _ResultInfo* pInfo = null;
188                 result r = pIter->GetValue(pInfo);
189
190                 if (IsFailed(r))
191                 {
192                         continue;
193                 }
194
195                 delete pInfo;
196         }
197
198         delete pIter;
199 }
200
201
202 template<> void
203 _RequestManagerT<_ResultInfo>::RemoveItem(int reqId)
204 {
205         // 1st request is launch request itself and do not remove it ever.
206         if (reqId == 0)
207         {
208                 return;
209         }
210
211         _ResultInfo* pItem = FindItem(reqId);
212         if (pItem)
213         {
214                 __requestList.Remove(reqId);
215                 delete pItem;
216         }
217 }
218
219
220 // explicit template initialization
221 template class _RequestManagerT<_LaunchInfo>;
222 template class _RequestManagerT<_InProcessInfo>;
223 template class _RequestManagerT<_ResultInfo>;
224
225 } } // Tizen::App
226