Add type parameter for performance
[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
27
28 namespace Tizen { namespace App
29 {
30
31 template<typename T>
32 _RequestManagerT<T>::_RequestManagerT(void)
33 : __reqId(-1)
34 {
35         __requestList.Construct();
36 }
37
38 template<typename T>
39 _RequestManagerT<T>::~_RequestManagerT(void)
40 {
41         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
42         if (pIter == null)
43         {
44                 return;
45         }
46
47         while (pIter->MoveNext() == E_SUCCESS)
48         {
49                 T* pInfo = null;
50                 result r = pIter->GetValue(pInfo);
51
52                 if (IsFailed(r))
53                 {
54                         continue;
55                 }
56
57                 delete pInfo;
58         }
59
60         delete pIter;
61 }
62
63 template<typename T> int
64 _RequestManagerT<T>::InsertItem(T* pItem)
65 {
66         SysTryReturn(NID_APP, pItem != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
67
68 #if 0
69         // [INFO] temporary disable due to OBS build system
70         __sync_add_and_fetch(&__reqId, 1);
71
72         // [TODO] awkward implementation after overflow
73         // overflow detection
74         __sync_bool_compare_and_swap(&__reqId, INT_MAX, 1);
75 #else
76         __reqId++;
77         if (__reqId >= INT_MAX)
78         {
79                 __reqId = 1;
80         }
81 #endif
82         SysLog(NID_APP, "Current request ID : %d.", __reqId);
83
84         pItem->reqId = __reqId;
85         __requestList.Add(__reqId, pItem);
86
87         return __reqId;
88 }
89
90 template<typename T> void
91 _RequestManagerT<T>::RemoveItem(int reqId)
92 {
93         T* pItem = FindItem(reqId);
94         if (pItem)
95         {
96                 __requestList.Remove(reqId);
97                 delete pItem;
98         }
99 }
100
101 template<typename T> void
102 _RequestManagerT<T>::RemoveItem(T* pItem)
103 {
104         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
105         if (pItem == null || pIter == null)
106         {
107                 return;
108         }
109
110         while (pIter->MoveNext() == E_SUCCESS)
111         {
112                 T* pInfo = null;
113                 result r = pIter->GetValue(pInfo);
114
115                 if (pInfo == pItem)
116                 {
117                         int id = -1;
118                         r = pIter->GetKey(id);
119                         if (r == E_SUCCESS)
120                         {
121                                 __requestList.Remove(id);
122                         }
123
124                         delete pItem;
125                         delete pIter;
126
127                         return;
128                 }
129         }
130
131         delete pIter;
132 }
133
134 template<typename T> T*
135 _RequestManagerT<T>::FindItem(int reqId) const
136 {
137         T* pItem = null;
138         result r = __requestList.GetValue(reqId, pItem);
139
140         return (r == E_SUCCESS) ? pItem : null;
141 }
142
143
144 // partial specialization
145 template<> void
146 _RequestManagerT<_ResultInfo>::RemoveItem(int reqId)
147 {
148         // 1st request is launch request itself and do not remove it ever.
149         if (reqId == 0)
150         {
151                 return;
152         }
153
154         _ResultInfo* pItem = FindItem(reqId);
155         if (pItem)
156         {
157                 __requestList.Remove(reqId);
158                 delete pItem;
159         }
160 }
161
162
163 // explicit template initialization
164 template class _RequestManagerT<_LaunchInfo>;
165 template class _RequestManagerT<_InProcessInfo>;
166 template class _RequestManagerT<_ResultInfo>;
167
168 } } // Tizen::App
169