Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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  * Manages listener events emitters.
34  * Template parameter should be class that derives from @see ListenerEvent.
35  */
36 template<class EmitterClass>
37 class Emitters
38 {
39   public:
40     typedef EmitterClass EmitterType;
41     typedef DPL::SharedPtr<EmitterType>           EmitterPtrType;
42     typedef typename EmitterType::IdType EmitterIdType;
43     typedef typename EmitterType::EventType EventType;
44     typedef typename EmitterType::EventPtrType EventPtrType;
45     typedef std::auto_ptr<DPL::Mutex::ScopedLock> LockType;
46
47   public:
48     ~Emitters()
49     {
50         DPL::Mutex::ScopedLock lock(&m_mtx);
51         m_emitters.clear();
52     }
53
54     /**
55      * Attaches emitter.
56      * @param emitter Emitter.
57      * @remarks Thread-safe. Do not use it in the scope of getLock() result.
58      */
59     void attach(const EmitterPtrType& emitter)
60     {
61         DPL::Mutex::ScopedLock lock(&m_mtx);
62         m_emitters[emitter->getId()] = emitter;
63     }
64
65     /**
66      * Detaches emitter.
67      * @param id Id of an emitter.
68      * @return True when emitter was found and successfully detached,
69      *         false otherwise.
70      * @remarks Thread-safe. Do not use it in the scope of getLock() result.
71      */
72     bool detach(const EmitterIdType id)
73     {
74         DPL::Mutex::ScopedLock lock(&m_mtx);
75         return (m_emitters.erase(id) > 0);
76     }
77
78     /**
79      * Emits event through all emitters.
80      * @param event Event to emit.
81      * @remarks Thread-safe. Do not use it in the scope of getLock() result.
82      */
83     void emit(const EventPtrType& event)
84     {
85         DPL::Mutex::ScopedLock lock(&m_mtx);
86         for (Iterator it = m_emitters.begin(); it != m_emitters.end(); ++it) {
87             it->second->emit(event);
88         }
89     }
90
91     /**
92      * Emits event through those emitters that when passed to predicate result
93      * 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
135      * someone
136      * copies it). Do not use in the same scope as other API of this class.
137      * @return Lock object.
138      * @remarks Provided to allow locking emitters in scope of some client
139      *          code.
140      */
141     LockType getLock()
142     {
143         return LockType(new DPL::Mutex::ScopedLock(&m_mtx));
144     }
145
146   private:
147     typedef std::map<EmitterIdType, EmitterPtrType> Map;
148     typedef typename Map::iterator Iterator;
149     typedef typename Map::const_iterator ConstIterator;
150
151   private:
152     DPL::Mutex m_mtx; ///< Synchronizes operation on this object.
153     Map m_emitters; ///< Emitters container.
154 };
155 }
156 } // WrtDeviceApisCommon
157
158 #endif // WRTDEVICEAPIS_COMMONS_EMITTERS_H_