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