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_EMITTERS_H_
21 #define WRTDEVICEAPIS_COMMONS_EMITTERS_H_
26 #include <dpl/mutex.h>
27 #include <dpl/shared_ptr.h>
28 #include <Commons/ListenerEventEmitter.h>
30 namespace WrtDeviceApis {
33 * Manages listener events emitters.
34 * Template parameter should be class that derives from @see ListenerEvent.
36 template<class EmitterClass>
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;
50 DPL::Mutex::ScopedLock lock(&m_mtx);
56 * @param emitter Emitter.
57 * @remarks Thread-safe. Do not use it in the scope of getLock() result.
59 void attach(const EmitterPtrType& emitter)
61 DPL::Mutex::ScopedLock lock(&m_mtx);
62 m_emitters[emitter->getId()] = emitter;
67 * @param id Id of an emitter.
68 * @return True when emitter was found and successfully detached,
70 * @remarks Thread-safe. Do not use it in the scope of getLock() result.
72 bool detach(const EmitterIdType id)
74 DPL::Mutex::ScopedLock lock(&m_mtx);
75 return (m_emitters.erase(id) > 0);
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.
83 void emit(const EventPtrType& event)
85 DPL::Mutex::ScopedLock lock(&m_mtx);
86 for (Iterator it = m_emitters.begin(); it != m_emitters.end(); ++it) {
87 it->second->emit(event);
92 * Emits event through those emitters that when passed to predicate result
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.
99 template<typename Predicate>
100 void emitIf(const EventPtrType& event,
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);
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.
117 bool isAttached(const EmitterIdType id) const
119 return (m_emitters.count(id) > 0);
123 * Returns number of attached emitters.
124 * @return Number of emitters.
125 * @remarks Can be used within scope of getLock() call.
127 std::size_t size() const
129 return m_emitters.size();
134 * This lock will be automatically released when out of scope (unless
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
143 return LockType(new DPL::Mutex::ScopedLock(&m_mtx));
147 typedef std::map<EmitterIdType, EmitterPtrType> Map;
148 typedef typename Map::iterator Iterator;
149 typedef typename Map::const_iterator ConstIterator;
152 DPL::Mutex m_mtx; ///< Synchronizes operation on this object.
153 Map m_emitters; ///< Emitters container.
156 } // WrtDeviceApisCommon
158 #endif // WRTDEVICEAPIS_COMMONS_EMITTERS_H_