2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
20 #ifndef WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_H_
21 #define WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_H_
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>
29 namespace WrtDeviceApis {
32 template<class EventClass>
33 class ListenerEventEmitter;
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).
43 template<class Derived>
46 friend class ListenerEventEmitter<Derived>;
49 typedef IEventPrivateData PrivateDataType;
50 typedef DPL::SharedPtr<PrivateDataType> PrivateDataTypePtr;
53 virtual ~ListenerEvent()
59 * Gets exception code.
60 * @return Exception code.
62 Commons::ExceptionCodes::Enumeration getExceptionCode() const
64 DPL::Mutex::ScopedLock lock(m_mtx);
69 * Sets exception code.
70 * @param code Exception code.
71 * @throw EventWrongStateExeption When event has already been emitted.
73 void setExceptionCode(Commons::ExceptionCodes::Enumeration code)
75 DPL::Mutex::ScopedLock lock(m_mtx);
80 * Gets event's private data.
81 * @return Private data (use DPL cast to proper object).
83 const PrivateDataTypePtr& getPrivateData() const
85 DPL::Mutex::ScopedLock lock(m_mtx);
91 m_mtx(new DPL::Mutex()),
92 m_code(Commons::ExceptionCodes::None)
96 ListenerEvent(const ListenerEvent &ths) :
97 m_mtx(new DPL::Mutex()),
99 m_privateData(ths.m_privateData)
103 ListenerEvent& operator=(const ListenerEvent &other)
105 if (this != &other) {
106 m_mtx = new DPL::Mutex();
107 m_code = other.m_code;
108 m_privateData = other.m_privateData;
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.
119 void setPrivateData(const PrivateDataTypePtr& data)
121 DPL::Mutex::ScopedLock lock(m_mtx);
122 m_privateData = data;
126 mutable DPL::Mutex *m_mtx;
127 Commons::ExceptionCodes::Enumeration m_code; ///< Exception code.
128 PrivateDataTypePtr m_privateData; ///< Private data.
132 } // WrtDeviceApisCommon
134 #endif // WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_H_