Merge "Modify Scanner class" into tizen_2.1
[platform/framework/native/appfw.git] / src / app / FApp_DataControlManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 /**
19  * @file        FApp_DataControlManager.cpp
20  * @brief       This is the implementation for the _DataControlManager class.
21  */
22
23 #include <new>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include <typeinfo>
27 #include <glib.h>
28
29 #include <FBaseInteger.h>
30 #include <FBaseSysLog.h>
31
32 #include "FApp_DataControlManager.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36
37 namespace Tizen { namespace App
38 {
39
40 _DataControlManager* _DataControlManager::__pDataControlManagerInstance = null;
41
42 _DataControlManager::_DataControlManager(void)
43         : __pDataControlRequestList(null)
44         , __uniqueId(-1)
45 {
46         __pDataControlRequestList = new (std::nothrow) HashMap(SingleObjectDeleter);
47         SysTryReturnVoidResult(NID_APP, __pDataControlRequestList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
48
49         __pDataControlRequestList->Construct();
50 }
51
52 _DataControlManager::~_DataControlManager(void)
53 {
54         delete __pDataControlRequestList;
55 }
56
57 void
58 _DataControlManager::InitSingleton(void)
59 {
60         _DataControlManager* pInst = new (std::nothrow) _DataControlManager();
61         SysTryReturnVoidResult(NID_APP, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
62
63         __pDataControlManagerInstance = pInst;
64                 
65         std::atexit(DestroySingleton);
66         return;
67 }
68
69 void
70 _DataControlManager::DestroySingleton(void)
71 {
72         delete __pDataControlManagerInstance;
73 }
74
75 _DataControlManager*
76 _DataControlManager::GetInstance(void)
77 {
78         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
79         
80         if (__pDataControlManagerInstance == null)
81         {
82                 ClearLastResult();
83                 pthread_once(&onceBlock, InitSingleton);
84                 result r = GetLastResult();
85                 if (IsFailed(r))
86                 {
87                         onceBlock = PTHREAD_ONCE_INIT;
88                         SysPropagate(NID_APP, r);
89                 }
90         }
91
92         return __pDataControlManagerInstance;
93 }
94
95 result
96 _DataControlManager::AddRequestInfo(Integer* pReqId, _DataControlRequestInfo* pReqInfo)
97 {
98         //SysLog(NID_APP, "DataControl request list count: %d", __pDataControlRequestList->GetCount());
99         return __pDataControlRequestList->Add(pReqId, pReqInfo);
100 }
101
102 _DataControlRequestInfo*
103 _DataControlManager::GetRequestInfo(Integer& reqId) 
104 {
105         result r = E_SUCCESS;
106
107         Object* pObj = __pDataControlRequestList->GetValue(reqId);
108         SysTryReturn(NID_APP, pObj != null, null, GetLastResult(), "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
109
110         _DataControlRequestInfo* pReqInfo = dynamic_cast< _DataControlRequestInfo* >(pObj);
111         SysTryReturn(NID_APP, pReqInfo != null, null, r, "[E_SYSTEM] invalid request info");
112
113         return pReqInfo;
114 }
115
116 void
117 _DataControlManager::RemoveRequestInfo(Integer& reqId)
118 {
119         __pDataControlRequestList->Remove(reqId);
120 }
121
122 int
123 _DataControlManager::GetUniqueId(void)
124 {
125         //++__uniqueId;
126         //__sync_fetch_and_add(&__uniqueId, 1);
127         g_atomic_int_inc(&__uniqueId);
128         return __uniqueId;
129 }
130
131 }} // Tizen::App
132