Update change log and spec for wrt-plugins-tizen_0.2.73
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / MsgServiceHandleMgr.h
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 /**
19  *
20  *
21  * @file       CallbackMgr.h
22  * @author     Pawel Misiak (p.misiak@samsung.com)
23  * @version    0.1
24  * @brief
25  */
26
27 #ifndef MSGSERVICEHANDLEMGR_H
28 #define MSGSERVICEHANDLEMGR_H
29
30 #include <dpl/singleton.h>
31 #include <dpl/log/log.h>
32 #include <dpl/shared_ptr.h>
33 #include <dpl/thread.h>
34 #include <Commons/Exception.h>
35
36 extern "C" {
37 #include <msg.h>
38 }
39
40 namespace TizenApis {
41 namespace Platform {
42 namespace Messaging {
43
44 /*
45  * Class used for msg service handle manager
46  */
47 class MsgServiceHandleMgr
48 {
49   public:
50     MsgServiceHandleMgr() : m_msgHandle(NULL)
51     {
52         LogInfo("enter");
53         // open one handle per all messaging
54         if (MSG_SUCCESS != msg_open_msg_handle(&m_msgHandle)) {
55             LogError("Unable to open handle");
56             Throw(WrtDeviceApis::Commons::PlatformException);
57         }
58     }
59
60   public:
61     ~MsgServiceHandleMgr()
62     {
63         LogInfo("enter");
64         if (!!m_msgHandle) {
65             msg_close_msg_handle(&m_msgHandle);
66         }
67     }
68
69     msg_handle_t getHandle() const
70     {
71         if (!m_msgHandle) {
72             LogError("Messaging handle not initialized");
73             Throw(WrtDeviceApis::Commons::PlatformException);
74         }
75         return m_msgHandle;
76     }
77
78   private:
79     msg_handle_t m_msgHandle;
80 };
81
82 typedef DPL::SharedPtr<MsgServiceHandleMgr> MsgServiceHandleMgrPtr;
83 typedef DPL::ThreadLocalVariable<MsgServiceHandleMgrPtr> TlvMsgServiceHandleMgr;
84
85 class MsgServiceHandle
86 {
87   protected:
88     MsgServiceHandle()
89     {
90     }
91
92   public:
93     ~MsgServiceHandle()
94     {
95     }
96
97     msg_handle_t getHandle()
98     {
99         if (m_tlvHandleMgr.IsNull()) {
100             m_tlvHandleMgr = MsgServiceHandleMgrPtr(new MsgServiceHandleMgr());
101         }
102         return (*m_tlvHandleMgr)->getHandle();
103     }
104
105   private:
106     TlvMsgServiceHandleMgr m_tlvHandleMgr;
107 };
108
109 typedef DPL::Singleton<MsgServiceHandle> MsgServiceHandleSingleton;
110
111 #define MsgGetCommonHandle() MsgServiceHandleSingleton::Instance().getHandle()
112 }
113 }
114 }
115 #endif /* MSGSERVICEHANDLEMGR_H */
116