tizen beta release
[framework/web/wrt-plugins-common.git] / src / Commons / ListenerEvent.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  * @author    Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
18  */
19
20 #ifndef WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_H_
21 #define WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_H_
22
23 #include <dpl/mutex.h>
24 #include <dpl/shared_ptr.h>
25 #include <dpl/noncopyable.h>
26 #include <Commons/Exception.h>
27 #include <Commons/IEvent.h>
28
29 namespace WrtDeviceApis {
30 namespace Commons {
31
32 template<class EventClass>
33 class ListenerEventEmitter;
34
35 /**
36  * This is base class for events that should act as signals between abstract
37  * layer and layer that uses it (e.g. JS layer in WRT plugins).
38  * Such event is created by specific event emitter from abstract layer
39  * and passed asynchronously to object that acts as event listener.
40  * Template parameter should be class of an event which derives from
41  * this class (co called CRTP pattern).
42  */
43 template<class Derived>
44 class ListenerEvent
45 {
46     friend class ListenerEventEmitter<Derived>;
47
48   public:
49     typedef IEventPrivateData PrivateDataType;
50     typedef DPL::SharedPtr<PrivateDataType> PrivateDataTypePtr;
51
52   public:
53     virtual ~ListenerEvent()
54     {
55         delete m_mtx;
56     }
57
58     /**
59      * Gets exception code.
60      * @return Exception code.
61      */
62     Commons::ExceptionCodes::Enumeration getExceptionCode() const
63     {
64         DPL::Mutex::ScopedLock lock(m_mtx);
65         return m_code;
66     }
67
68     /**
69      * Sets exception code.
70      * @param code Exception code.
71      * @throw EventWrongStateExeption When event has already been emitted.
72      */
73     void setExceptionCode(Commons::ExceptionCodes::Enumeration code)
74     {
75         DPL::Mutex::ScopedLock lock(m_mtx);
76         m_code = code;
77     }
78
79     /**
80      * Gets event's private data.
81      * @return Private data (use DPL cast to proper object).
82      */
83     const PrivateDataTypePtr& getPrivateData() const
84     {
85         DPL::Mutex::ScopedLock lock(m_mtx);
86         return m_privateData;
87     }
88
89   protected:
90     ListenerEvent() :
91         m_mtx(new DPL::Mutex()),
92         m_code(Commons::ExceptionCodes::None)
93     {
94     }
95
96     ListenerEvent(const ListenerEvent &ths) :
97         m_mtx(new DPL::Mutex()),
98         m_code(ths.m_code),
99         m_privateData(ths.m_privateData)
100     {
101     }
102
103     ListenerEvent& operator=(const ListenerEvent &other)
104     {
105         if (this != &other) {
106             m_mtx = new DPL::Mutex();
107             m_code = other.m_code;
108             m_privateData = other.m_privateData;
109         }
110         return *this;
111     }
112
113     /**
114      * Sets event's private data.
115      * Event's private data object has to implement @see IEventPrivateData interface.
116      * @param data Private data.
117      * @throw EventWrongStateExeption When event has already been emitted.
118      */
119     void setPrivateData(const PrivateDataTypePtr& data)
120     {
121         DPL::Mutex::ScopedLock lock(m_mtx);
122         m_privateData = data;
123     }
124
125   protected:
126     mutable DPL::Mutex *m_mtx;
127     Commons::ExceptionCodes::Enumeration m_code; ///< Exception code.
128     PrivateDataTypePtr m_privateData; ///< Private data.
129 };
130
131 }
132 } // WrtDeviceApisCommon
133
134 #endif // WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_H_