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_EMITTER_H_
21 #define WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_EMITTER_H_
24 #include <dpl/noncopyable.h>
25 #include <dpl/shared_ptr.h>
26 #include <dpl/mutex.h>
27 #include <Commons/EventListener.h>
28 #include <Commons/ListenerEvent.h>
30 namespace WrtDeviceApis {
32 template<class EmitterClass>
36 * Creates listener events in the abstraction layer in response to some
37 * asynchronous action.
38 * To enable passing events from abstract layer to layer that uses it, first
39 * proper event emitter should be registered in abstract layer by the layer
40 * that uses it. Then when some action happens in abstract layer event should be
41 * created and passed to this emitter's emit() function. Then emitter passes
42 * this event to proper listener.
43 * Template parameter should be class that derives from @see ListenerEvent.
45 template<class EventClass>
46 class ListenerEventEmitter : private DPL::Noncopyable
49 typedef EventClass EventType;
50 typedef DPL::SharedPtr<EventType> EventPtrType;
51 typedef ListenerEventEmitter<EventType> Type;
52 typedef typename ListenerEvent<EventType>::PrivateDataType
54 typedef typename ListenerEvent<EventType>::PrivateDataTypePtr
55 EventPrivateDataTypePtr;
56 typedef EventListener<EventType> ListenerType;
57 typedef uintptr_t IdType;
60 * Empty (NULL) value of emitter's Id.
62 static const IdType emptyId;
65 ListenerEventEmitter() : m_listener(NULL)
69 virtual ~ListenerEventEmitter()
73 * Sets event's private data.
74 * Event's private data object has to implement @see IEventPrivateData
76 * @param data Private data.
77 * @remarks Practically private dat should be only set at object creation
79 * not chaged during this object lifetime.
81 virtual void setEventPrivateData(const EventPrivateDataTypePtr& data)
83 DPL::Mutex::ScopedLock lock(&m_mtx);
88 * Gets event's private data.
89 * @return Private data.
91 virtual EventPrivateDataTypePtr getEventPrivateData()
93 DPL::Mutex::ScopedLock lock(&m_mtx);
99 * Object set as listener has to implement @see EventListener interface.
100 * @param listener Listener object.
101 * @remarks Doesn't take ownership over this object.
102 * It's suggested to use singletons to have one listener for all
103 * events (no dynamic allocation overhead).
105 virtual void setListener(ListenerType* listener)
107 DPL::Mutex::ScopedLock lock(&m_mtx);
108 m_listener = listener;
113 * @param event Event to emit.
115 virtual void emit(const EventPtrType& event)
117 DPL::Mutex::ScopedLock lock(&m_mtx);
118 EventPtrType copy(new EventType(*event.Get()));
120 copy->setPrivateData(m_privateData);
121 m_listener->postAnswer(copy);
127 * @return Event's id.
128 * @remarks Id is implemented as value of `this` pointer.
130 virtual IdType getId()
132 return reinterpret_cast<IdType>(this);
136 DPL::Mutex m_mtx; ///< Mutex for thread-safety.
137 ListenerType* m_listener; ///< Event listener object.
138 EventPrivateDataTypePtr m_privateData; ///< Private data.
141 template<class EventClass>
142 const typename ListenerEventEmitter<EventClass>::IdType ListenerEventEmitter<
143 EventClass>::emptyId = 0;
145 } // WrtDeviceApisCommon
147 #endif // WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_EMITTER_H_