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