Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / Commons / ListenerEventEmitter.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_EMITTER_H_
21 #define WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_EMITTER_H_
22
23 #include <stdint.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>
29
30 namespace WrtDeviceApis {
31 namespace Commons {
32 template<class EmitterClass>
33 class Emitters;
34
35 /**
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.
44  */
45 template<class EventClass>
46 class ListenerEventEmitter : private DPL::Noncopyable
47 {
48   public:
49     typedef EventClass EventType;
50     typedef DPL::SharedPtr<EventType> EventPtrType;
51     typedef ListenerEventEmitter<EventType> Type;
52     typedef typename ListenerEvent<EventType>::PrivateDataType
53     EventPrivateDataType;
54     typedef typename ListenerEvent<EventType>::PrivateDataTypePtr
55     EventPrivateDataTypePtr;
56     typedef EventListener<EventType> ListenerType;
57     typedef uintptr_t IdType;
58
59     /**
60      * Empty (NULL) value of emitter's Id.
61      */
62     static const IdType emptyId;
63
64   public:
65     ListenerEventEmitter() : m_listener(NULL)
66     {}
67
68   public:
69     virtual ~ListenerEventEmitter()
70     {}
71
72     /**
73      * Sets event's private data.
74      * Event's private data object has to implement @see IEventPrivateData
75      * interface.
76      * @param data Private data.
77      * @remarks Practically private dat should be only set at object creation
78      * and
79      *          not chaged during this object lifetime.
80      */
81     virtual void setEventPrivateData(const EventPrivateDataTypePtr& data)
82     {
83         DPL::Mutex::ScopedLock lock(&m_mtx);
84         m_privateData = data;
85     }
86
87     /**
88      * Gets event's private data.
89      * @return Private data.
90      */
91     virtual EventPrivateDataTypePtr getEventPrivateData()
92     {
93         DPL::Mutex::ScopedLock lock(&m_mtx);
94         return m_privateData;
95     }
96
97     /**
98      * Sets listener.
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).
104      */
105     virtual void setListener(ListenerType* listener)
106     {
107         DPL::Mutex::ScopedLock lock(&m_mtx);
108         m_listener = listener;
109     }
110
111     /**
112      * Emits event.
113      * @param event Event to emit.
114      */
115     virtual void emit(const EventPtrType& event)
116     {
117         DPL::Mutex::ScopedLock lock(&m_mtx);
118         EventPtrType copy(new EventType(*event.Get()));
119         if (m_listener) {
120             copy->setPrivateData(m_privateData);
121             m_listener->postAnswer(copy);
122         }
123     }
124
125     /**
126      * Gets id.
127      * @return Event's id.
128      * @remarks Id is implemented as value of `this` pointer.
129      */
130     virtual IdType getId()
131     {
132         return reinterpret_cast<IdType>(this);
133     }
134
135   protected:
136     DPL::Mutex m_mtx; ///< Mutex for thread-safety.
137     ListenerType* m_listener; ///< Event listener object.
138     EventPrivateDataTypePtr m_privateData; ///< Private data.
139 };
140
141 template<class EventClass>
142 const typename ListenerEventEmitter<EventClass>::IdType ListenerEventEmitter<
143     EventClass>::emptyId = 0;
144 }
145 } // WrtDeviceApisCommon
146
147 #endif // WRTDEVICEAPIS_COMMONS_LISTENER_EVENT_EMITTER_H_