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 * @file inter_context_delegate.h
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
20 * @brief This file is the header file of inter context delegate
23 #ifndef DPL_INTER_CONTEXT_DELEGATE_H_
24 #define DPL_INTER_CONTEXT_DELEGATE_H_
26 #ifndef __GXX_EXPERIMENTAL_CXX0X__ // C++11 compatibility check
27 # include <bits/c++0x_warning.h>
30 #include <dpl/event/event_support.h>
31 #include <dpl/event/thread_event_dispatcher.h>
32 #include <dpl/event/main_event_dispatcher.h>
33 #include <dpl/fast_delegate.h>
34 #include <dpl/generic_event.h>
35 #include <dpl/foreach.h>
36 #include <dpl/recursive_mutex.h>
37 #include <dpl/mutex.h>
38 #include <dpl/noncopyable.h>
39 #include <dpl/log/log.h>
40 #include <dpl/assert.h>
41 #include <dpl/apply.h>
47 * - Created ICDelegate can be passed freely to other threads.
48 * - ICDelegate can be called just once. All following calls will be
50 * - When ICDelegateSupport is destroyed, all its ICDelegates
51 * are invalidated and safetly removed.
52 * - ICDelegate can be invalidated by call to disable method on it.
53 * - To use ICDelegate you have to do two steps:
55 * 1. Class that will be used to create delegate have to derive from templated
56 * class ICDelegateSupport<T>
58 * 2. Create instance of ICDelegate by calling
59 * makeICDelegate and passing to it pointer to method
61 * class A : ICDelegateSupport<A>
63 * void methodA(int) {}
64 * void createDelegate() {
65 * ICDelegate<int> dlg;
66 * dlg = makeICDelegate(&A::MethodA);
74 template <typename ... ArgTypesList>
78 // This Type defines whether ICDelegate should be destroyed after the call, or
79 // could be reused later.
80 enum class Reuse{ Yes, No };
83 namespace ICDPrivate {
84 // Base for all ICDSharedDatas. Needed for auto disabling and deleting of
86 // If ICDSharedData is disabled, delegate won't be called anymore.
87 class ICDSharedDataBase
90 typedef std::shared_ptr<ICDSharedDataBase> ICDSharedDataBasePtr;
91 typedef std::list<ICDSharedDataBasePtr> ICDSharedDataBaseList;
93 class ScopedLock : DPL::Noncopyable
96 explicit ScopedLock(ICDSharedDataBasePtr helperBase) :
97 m_scopedLock(&helperBase->m_mutex)
102 DPL::RecursiveMutex::ScopedLock m_scopedLock;
105 ICDSharedDataBase() : m_disabled(false)
108 virtual ~ICDSharedDataBase()
112 bool isDisabled() const
116 virtual void disable()
121 void setIterator(ICDSharedDataBaseList::iterator pos)
126 ICDSharedDataBaseList::iterator getIterator() const
133 DPL::RecursiveMutex m_mutex;
134 ICDSharedDataBaseList::iterator m_position;
137 // Pure Event to remove ICDSharedData.
138 class DeleteICDSharedDataBaseEventCall : public DPL::Event::AbstractEventCall
141 DeleteICDSharedDataBaseEventCall(
142 ICDSharedDataBase::ICDSharedDataBasePtr helperBase) :
143 m_helperBase(helperBase)
148 m_helperBase.reset();
152 ICDSharedDataBase::ICDSharedDataBasePtr m_helperBase;
156 class ICDelegateSupportInterface
159 virtual ~ICDelegateSupportInterface()
162 virtual void unregisterICDSharedData(
163 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper) = 0;
164 virtual void registerICDSharedData(
165 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper) = 0;
167 template <typename ... ArgTypesList>
168 friend class DPL::Event::ICDelegate;
172 // Better makeDelegate then DPL::MakeDelegate
173 template<typename ThisType, typename ... ArgTypesList>
174 FastDelegate<void (ArgTypesList ...)>
175 makeDelegate(ThisType* This,
176 void (ThisType::*Func)(ArgTypesList ...))
178 return FastDelegate<void (ArgTypesList ...)>(This, Func);
181 // ICDelegate class represents delegate that can be called from
182 // any context (thread). The actual calling context (thread) is allways the same
183 // as the context in which it was created.
184 template <typename ... ArgTypesList>
191 ICDelegate(ICDPrivate::ICDelegateSupportInterface* base,
192 DPL::FastDelegate<void (ArgTypesList ...)> outerDelegate,
195 ICDSharedData* hlp = new ICDSharedData(base, outerDelegate, reuse);
199 // Calling operator will pass all args passed to it to correct context and
200 // will call apropriate method that was registered with.
201 void operator()(ArgTypesList ... args)
204 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
205 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(m_helper));
206 m_helper->CallDelegate(args ...);
209 //Disable delegate (it won't be called anymore)
213 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
214 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(m_helper));
219 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr
220 getRelatedICDSharedData() const
222 return std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(m_helper);
226 template<typename ThisType>
227 friend class ICDelegateSupport;
229 typedef std::shared_ptr<ICDSharedData> ICDSharedDataPtr;
233 PrivateEvent(ICDSharedDataPtr a_helper,
234 ArgTypesList ... arguments) :
236 args(std::make_tuple(arguments ...))
240 ICDSharedDataPtr helper;
241 std::tuple<ArgTypesList ...> args;
244 typedef DPL::FastDelegate<void (const PrivateEvent&)>
245 ICDSharedDataDelegateType;
246 class ICDSharedData : private DPL::Event::EventSupport<PrivateEvent>,
247 public std::enable_shared_from_this<ICDSharedData>,
248 public ICDPrivate::ICDSharedDataBase
252 ICDPrivate::ICDelegateSupportInterface *base,
253 DPL::FastDelegate<void (ArgTypesList ...)> outerDelegate,
256 m_outerDelegate(outerDelegate),
260 // lock is not needed: this object is not shared at that moment
262 DPL::Event::makeDelegate(this,
263 &ICDSharedData::delegateForwarder);
264 EventSupport<PrivateEvent>::AddListener(m_subDelegate);
267 void CallDelegate(ArgTypesList ... args)
269 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
270 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(
271 this->shared_from_this()));
273 EmitEvent(PrivateEvent(this->shared_from_this(),
278 virtual void disable()
280 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr ptr(
281 std::static_pointer_cast<ICDSharedDataBase>(
282 this->shared_from_this()));
283 ICDPrivate::ICDSharedDataBase::ScopedLock lock(ptr);
285 ICDPrivate::ICDSharedDataBase::disable();
286 EventSupport<PrivateEvent>::RemoveListener(m_subDelegate);
287 m_base->unregisterICDSharedData(ptr);
292 friend class std::shared_ptr<ICDSharedData>;
293 ICDSharedDataDelegateType m_subDelegate;
294 ICDPrivate::ICDelegateSupportInterface* m_base;
295 DPL::FastDelegate<void (ArgTypesList ...)> m_outerDelegate;
298 void delegateForwarder(const PrivateEvent& event)
300 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr ptr(
301 std::static_pointer_cast<ICDSharedDataBase>(event.helper));
302 ICDPrivate::ICDSharedDataBase::ScopedLock lock(ptr);
304 Assert(!m_outerDelegate.empty());
305 if (ptr->isDisabled()) {
306 LogPedantic("ICDSharedData has been disabled - call is ignored");
308 DPL::Apply(m_outerDelegate, event.args);
310 if(m_reuse == ICD::Reuse::Yes)
314 deleteICDSharedDataBase(ptr);
319 // Schedules helper removal.
320 static void deleteICDSharedDataBase(ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper)
322 using namespace ICDPrivate;
323 ICDSharedDataBase::ScopedLock lock(helper);
324 DeleteICDSharedDataBaseEventCall* event =
325 new DeleteICDSharedDataBaseEventCall(helper);
326 if (DPL::Thread::GetCurrentThread() == NULL) {
327 DPL::Event::GetMainEventDispatcherInstance().AddEventCall(event);
329 DPL::Event::ThreadEventDispatcher dispatcher;
330 dispatcher.SetThread(DPL::Thread::GetCurrentThread());
331 dispatcher.AddEventCall(event);
335 ICDSharedDataPtr m_helper;
338 template <typename ThisType>
339 class ICDelegateSupport : public ICDPrivate::ICDelegateSupportInterface
342 template<typename ... ArgTypesList>
343 ICDelegate<ArgTypesList ...> makeICDelegate(
344 void (ThisType::*Func)(ArgTypesList ...),
345 ICD::Reuse reuse = ICD::Reuse::No)
347 ThisType* This = static_cast<ThisType*>(this);
348 ICDelegate<ArgTypesList ...> icdelegate(
350 makeDelegate(This, Func),
352 this->registerICDSharedData(icdelegate.getRelatedICDSharedData());
362 ICDPrivate::ICDSharedDataBase::ICDSharedDataBaseList list =
364 FOREACH(helper, list) {
365 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
366 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(*helper));
367 (*helper)->disable();
369 m_ICDSharedDatas.clear();
373 virtual void unregisterICDSharedData(
374 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper)
376 m_ICDSharedDatas.erase(helper->getIterator());
379 virtual void registerICDSharedData(
380 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper)
383 m_ICDSharedDatas.insert(m_ICDSharedDatas.begin(),
388 ICDPrivate::ICDSharedDataBase::ICDSharedDataBaseList m_ICDSharedDatas;
394 #endif // __GXX_EXPERIMENTAL_CXX0X__
396 #endif //DPL_INTER_CONTEXT_DELEGATE_H_