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 {
34 * Manages listener events emitters.
35 * Template parameter should be class that derives from @see ListenerEvent.
37 template<class EmitterClass>
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;
51 DPL::Mutex::ScopedLock lock(&m_mtx);
57 * @param emitter Emitter.
58 * @remarks Thread-safe. Do not use it in the scope of getLock() result.
60 void attach(const EmitterPtrType& emitter)
62 DPL::Mutex::ScopedLock lock(&m_mtx);
63 m_emitters[emitter->getId()] = emitter;
68 * @param id Id of an emitter.
69 * @return True when emitter was found and successfully detached,
71 * @remarks Thread-safe. Do not use it in the scope of getLock() result.
73 bool detach(const EmitterIdType id)
75 DPL::Mutex::ScopedLock lock(&m_mtx);
76 return (m_emitters.erase(id) > 0);
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.
84 void emit(const EventPtrType& event)
86 DPL::Mutex::ScopedLock lock(&m_mtx);
87 for (Iterator it = m_emitters.begin(); it != m_emitters.end(); ++it) {
88 it->second->emit(event);
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.
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 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
142 return LockType(new DPL::Mutex::ScopedLock(&m_mtx));
146 typedef std::map<EmitterIdType, EmitterPtrType> Map;
147 typedef typename Map::iterator Iterator;
148 typedef typename Map::const_iterator ConstIterator;
151 DPL::Mutex m_mtx; ///< Synchronizes operation on this object.
152 Map m_emitters; ///< Emitters container.
156 } // WrtDeviceApisCommon
158 #endif // WRTDEVICEAPIS_COMMONS_EMITTERS_H_