tizen beta release
[platform/framework/web/wrt-plugins-common.git] / src / Commons / Emitters.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_EMITTERS_H_
21 #define WRTDEVICEAPIS_COMMONS_EMITTERS_H_
22
23 #include <cstddef>
24 #include <map>
25 #include <memory>
26 #include <dpl/mutex.h>
27 #include <dpl/shared_ptr.h>
28 #include <Commons/ListenerEventEmitter.h>
29
30 namespace WrtDeviceApis {
31 namespace Commons {
32
33 /**
34  * Manages listener events emitters.
35  * Template parameter should be class that derives from @see ListenerEvent.
36  */
37 template<class EmitterClass>
38 class Emitters
39 {
40   public:
41     typedef EmitterClass EmitterType;
42     typedef DPL::SharedPtr<EmitterType>           EmitterPtrType;
43     typedef typename EmitterType::IdType EmitterIdType;
44     typedef typename EmitterType::EventType EventType;
45     typedef typename EmitterType::EventPtrType EventPtrType;
46     typedef std::auto_ptr<DPL::Mutex::ScopedLock> LockType;
47
48   public:
49     ~Emitters()
50     {
51         DPL::Mutex::ScopedLock lock(&m_mtx);
52         m_emitters.clear();
53     }
54
55     /**
56      * Attaches emitter.
57      * @param emitter Emitter.
58      * @remarks Thread-safe. Do not use it in the scope of getLock() result.
59      */
60     void attach(const EmitterPtrType& emitter)
61     {
62         DPL::Mutex::ScopedLock lock(&m_mtx);
63         m_emitters[emitter->getId()] = emitter;
64     }
65
66     /**
67      * Detaches emitter.
68      * @param id Id of an emitter.
69      * @return True when emitter was found and successfully detached,
70      *         false otherwise.
71      * @remarks Thread-safe. Do not use it in the scope of getLock() result.
72      */
73     bool detach(const EmitterIdType id)
74     {
75         DPL::Mutex::ScopedLock lock(&m_mtx);
76         return (m_emitters.erase(id) > 0);
77     }
78
79     /**
80      * Emits event through all emitters.
81      * @param event Event to emit.
82      * @remarks Thread-safe. Do not use it in the scope of getLock() result.
83      */
84     void emit(const EventPtrType& event)
85     {
86         DPL::Mutex::ScopedLock lock(&m_mtx);
87         for (Iterator it = m_emitters.begin(); it != m_emitters.end(); ++it) {
88             it->second->emit(event);
89         }
90     }
91
92     /**
93      * Emits event through those emitters that when passed to predicate result in
94      * returning true by it.
95      * @param event Event to emit.
96      * @param pred Predicate - a callable object (function, functor) that takes
97      *             an argument of type EmitterPtrType and returns boolean value.
98      */
99     template<typename Predicate>
100     void emitIf(const EventPtrType& event,
101             Predicate pred)
102     {
103         DPL::Mutex::ScopedLock lock(&m_mtx);
104         for (Iterator it = m_emitters.begin(); it != m_emitters.end(); ++it) {
105             if (bool(pred(it->second))) {
106                 it->second->emit(event);
107             }
108         }
109     }
110
111     /**
112      * Checks whether emitter of supplied id is attached.
113      * @param emitter Emitter.
114      * @return True when emitter has been found, false otherwise.
115      * @remarks Can be used within scope of getLock() call.
116      */
117     bool isAttached(const EmitterIdType id) const
118     {
119         return (m_emitters.count(id) > 0);
120     }
121
122     /**
123      * Returns number of attached emitters.
124      * @return Number of emitters.
125      * @remarks Can be used within scope of getLock() call.
126      */
127     std::size_t size() const
128     {
129         return m_emitters.size();
130     }
131
132     /**
133      * Lock this object.
134      * This lock will be automatically released when out of scope (unless someone
135      * copies it). Do not use in the same scope as other API of this class.
136      * @return Lock object.
137      * @remarks Provided to allow locking emitters in scope of some client
138      *          code.
139      */
140     LockType getLock()
141     {
142         return LockType(new DPL::Mutex::ScopedLock(&m_mtx));
143     }
144
145   private:
146     typedef std::map<EmitterIdType, EmitterPtrType> Map;
147     typedef typename Map::iterator Iterator;
148     typedef typename Map::const_iterator ConstIterator;
149
150   private:
151     DPL::Mutex m_mtx; ///< Synchronizes operation on this object.
152     Map m_emitters; ///< Emitters container.
153 };
154
155 }
156 } // WrtDeviceApisCommon
157
158 #endif // WRTDEVICEAPIS_COMMONS_EMITTERS_H_