Merge "Remove dummy code for Device/Power/Runtime." into tizen_2.2
[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 "FApp_RequestManagerT.h"
25 #include "FApp_LaunchInfo.h"
26 #include "FApp_AppArg.h"
27
28
29 namespace Tizen { namespace App
30 {
31
32 _ResultInfo::~_ResultInfo(void)
33 {
34         _AppArg* pArg = &arg;
35         delete pArg;
36 }
37
38
39 template<typename T>
40 _RequestManagerT<T>::_RequestManagerT(void)
41 : __reqId(-1)
42 {
43         __requestList.Construct();
44 }
45
46 template<typename T>
47 _RequestManagerT<T>::~_RequestManagerT(void)
48 {
49         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
50         if (pIter == null)
51         {
52                 return;
53         }
54
55         while (pIter->MoveNext() == E_SUCCESS)
56         {
57                 T* pInfo = null;
58                 result r = pIter->GetValue(pInfo);
59
60                 if (IsFailed(r))
61                 {
62                         continue;
63                 }
64
65                 delete pInfo;
66         }
67
68         delete pIter;
69 }
70
71 template<typename T> int
72 _RequestManagerT<T>::InsertItem(T* pItem)
73 {
74         SysTryReturn(NID_APP, pItem != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
75
76 #if 0
77         // [INFO] temporary disable due to OBS build system
78         __sync_add_and_fetch(&__reqId, 1);
79
80         // [TODO] awkward implementation after overflow
81         // overflow detection
82         __sync_bool_compare_and_swap(&__reqId, INT_MAX, 1);
83 #else
84         __reqId++;
85         if (__reqId >= INT_MAX)
86         {
87                 __reqId = 1;
88         }
89 #endif
90         SysLog(NID_APP, "Current request ID : %d.", __reqId);
91
92         pItem->reqId = __reqId;
93         __requestList.Add(__reqId, pItem);
94
95         return __reqId;
96 }
97
98 template<typename T> void
99 _RequestManagerT<T>::RemoveItem(int reqId)
100 {
101         T* pItem = FindItem(reqId);
102         if (pItem)
103         {
104                 __requestList.Remove(reqId);
105                 delete pItem;
106         }
107 }
108
109 template<typename T> void
110 _RequestManagerT<T>::RemoveItem(T* pItem)
111 {
112         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
113         if (pItem == null || pIter == null)
114         {
115                 return;
116         }
117
118         while (pIter->MoveNext() == E_SUCCESS)
119         {
120                 T* pInfo = null;
121                 result r = pIter->GetValue(pInfo);
122
123                 if (pInfo == pItem)
124                 {
125                         int id = -1;
126                         r = pIter->GetKey(id);
127                         if (r == E_SUCCESS)
128                         {
129                                 __requestList.Remove(id);
130                         }
131
132                         delete pItem;
133                         delete pIter;
134
135                         return;
136                 }
137         }
138
139         delete pIter;
140 }
141
142 template<typename T> T*
143 _RequestManagerT<T>::FindItem(int reqId) const
144 {
145         T* pItem = null;
146         result r = __requestList.GetValue(reqId, pItem);
147
148         return (r == E_SUCCESS) ? pItem : null;
149 }
150
151
152 // specialization
153 template<>
154 _RequestManagerT<_ResultInfo>::~_RequestManagerT(void)
155 {
156         // clear 1st request
157         _ResultInfo* pItem = FindItem(0);
158         if (pItem)
159         {
160                 __requestList.Remove(0);
161                 delete pItem;
162         }
163
164
165         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
166         if (pIter == null)
167         {
168                 return;
169         }
170
171         while (pIter->MoveNext() == E_SUCCESS)
172         {
173                 _ResultInfo* pInfo = null;
174                 result r = pIter->GetValue(pInfo);
175
176                 if (IsFailed(r))
177                 {
178                         continue;
179                 }
180
181                 delete pInfo;
182         }
183
184         delete pIter;
185 }
186
187
188 template<> void
189 _RequestManagerT<_ResultInfo>::RemoveItem(int reqId)
190 {
191         // 1st request is launch request itself and do not remove it ever.
192         if (reqId == 0)
193         {
194                 return;
195         }
196
197         _ResultInfo* pItem = FindItem(reqId);
198         if (pItem)
199         {
200                 __requestList.Remove(reqId);
201                 delete pItem;
202         }
203 }
204
205
206 // explicit template initialization
207 template class _RequestManagerT<_LaunchInfo>;
208 template class _RequestManagerT<_InProcessInfo>;
209 template class _RequestManagerT<_ResultInfo>;
210
211 } } // Tizen::App
212