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 {
31 template<class EventClass>
32 class ListenerEventEmitter;
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).
42 template<class Derived>
45 friend class ListenerEventEmitter<Derived>;
48 typedef IEventPrivateData PrivateDataType;
49 typedef DPL::SharedPtr<PrivateDataType> PrivateDataTypePtr;
52 virtual ~ListenerEvent()
58 * Gets exception code.
59 * @return Exception code.
61 Commons::ExceptionCodes::Enumeration getExceptionCode() const
63 DPL::Mutex::ScopedLock lock(m_mtx);
68 * Sets exception code.
69 * @param code Exception code.
70 * @throw EventWrongStateExeption When event has already been emitted.
72 void setExceptionCode(Commons::ExceptionCodes::Enumeration code)
74 DPL::Mutex::ScopedLock lock(m_mtx);
79 * Gets event's private data.
80 * @return Private data (use DPL cast to proper object).
82 const PrivateDataTypePtr& getPrivateData() const
84 DPL::Mutex::ScopedLock lock(m_mtx);
90 m_mtx(new DPL::Mutex()),
91 m_code(Commons::ExceptionCodes::None)
94 ListenerEvent(const ListenerEvent &ths) :
95 m_mtx(new DPL::Mutex()),
97 m_privateData(ths.m_privateData)
100 ListenerEvent& operator=(const ListenerEvent &other)
102 if (this != &other) {
103 m_mtx = new DPL::Mutex();
104 m_code = other.m_code;
105 m_privateData = other.m_privateData;
111 * Sets event's private data.
112 * Event's private data object has to implement @see IEventPrivateData
114 * @param data Private data.
115 * @throw EventWrongStateExeption When event has already been emitted.
117 void setPrivateData(const PrivateDataTypePtr& data)
119 DPL::Mutex::ScopedLock lock(m_mtx);
120 m_privateData = data;
124 mutable DPL::Mutex *m_mtx;
125 Commons::ExceptionCodes::Enumeration m_code; ///< Exception code.
126 PrivateDataTypePtr m_privateData; ///< Private data.
129 } // WrtDeviceApisCommon
131 #endif // WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_H_