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>
35 #include <dpl/event/event_support.h>
36 #include <dpl/event/thread_event_dispatcher.h>
37 #include <dpl/event/main_event_dispatcher.h>
38 #include <dpl/generic_event.h>
39 #include <dpl/foreach.h>
40 #include <dpl/recursive_mutex.h>
41 #include <dpl/mutex.h>
42 #include <dpl/noncopyable.h>
43 #include <dpl/log/log.h>
44 #include <dpl/assert.h>
45 #include <dpl/apply.h>
49 * - Created ICDelegate can be passed freely to other threads.
50 * - ICDelegate can be called just once. All following calls will be
52 * - When ICDelegateSupport is destroyed, all its ICDelegates
53 * are invalidated and safetly removed.
54 * - ICDelegate can be invalidated by call to disable method on it.
55 * - To use ICDelegate you have to do two steps:
57 * 1. Class that will be used to create delegate have to derive from templated
58 * class ICDelegateSupport<T>
60 * 2. Create instance of ICDelegate by calling
61 * makeICDelegate and passing to it pointer to method
63 * class A : ICDelegateSupport<A>
65 * void methodA(int) {}
66 * void createDelegate() {
67 * ICDelegate<int> dlg;
68 * dlg = makeICDelegate(&A::MethodA);
75 template <typename ... ArgTypesList>
79 // This Type defines whether ICDelegate should be destroyed after the call, or
80 // could be reused later.
87 namespace ICDPrivate {
88 // Base for all ICDSharedDatas. Needed for auto disabling and deleting of
90 // If ICDSharedData is disabled, delegate won't be called anymore.
91 class ICDSharedDataBase
94 typedef std::shared_ptr<ICDSharedDataBase> ICDSharedDataBasePtr;
95 typedef std::list<ICDSharedDataBasePtr> ICDSharedDataBaseList;
97 class ScopedLock : DPL::Noncopyable
100 explicit ScopedLock(ICDSharedDataBasePtr helperBase) :
101 m_scopedLock(&helperBase->m_mutex)
105 DPL::RecursiveMutex::ScopedLock m_scopedLock;
108 ICDSharedDataBase() : m_disabled(false)
110 virtual ~ICDSharedDataBase()
113 bool isDisabled() const
117 virtual void disable()
122 void setIterator(ICDSharedDataBaseList::iterator pos)
127 ICDSharedDataBaseList::iterator getIterator() const
134 DPL::RecursiveMutex m_mutex;
135 ICDSharedDataBaseList::iterator m_position;
138 // Pure Event to remove ICDSharedData.
139 class DeleteICDSharedDataBaseEventCall : public DPL::Event::AbstractEventCall
142 DeleteICDSharedDataBaseEventCall(
143 ICDSharedDataBase::ICDSharedDataBasePtr helperBase) :
144 m_helperBase(helperBase)
148 m_helperBase.reset();
152 ICDSharedDataBase::ICDSharedDataBasePtr m_helperBase;
155 class ICDelegateSupportInterface
158 virtual ~ICDelegateSupportInterface()
160 virtual void unregisterICDSharedData(
161 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper) = 0;
162 virtual void registerICDSharedData(
163 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper) = 0;
166 template <typename ... ArgTypesList>
167 friend class DPL::Event::ICDelegate;
171 template<typename ThisType, typename ... ArgTypesList>
172 std::function<void (ArgTypesList ...)>
173 makeDelegate(ThisType* This,
174 void (ThisType::*Func)(ArgTypesList ...))
176 return DPL::Bind(Func, This);
179 // ICDelegate class represents delegate that can be called from
180 // any context (thread). The actual calling context (thread) is allways the same
181 // as the context in which it was created.
182 template <typename ... ArgTypesList>
188 ICDelegate(ICDPrivate::ICDelegateSupportInterface* base,
189 std::function<void (ArgTypesList ...)> outerDelegate,
192 ICDSharedData* hlp = new ICDSharedData(base, outerDelegate, reuse);
196 // Calling operator will pass all args passed to it to correct context and
197 // will call apropriate method that was registered with.
198 void operator()(ArgTypesList ... args)
201 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
202 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(m_helper));
203 m_helper->CallDelegate(args ...);
206 //Disable delegate (it won't be called anymore)
210 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
211 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(m_helper));
216 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr
217 getRelatedICDSharedData() const
219 return std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(m_helper);
223 template<typename ThisType>
224 friend class ICDelegateSupport;
226 typedef std::shared_ptr<ICDSharedData> ICDSharedDataPtr;
230 PrivateEvent(ICDSharedDataPtr a_helper,
231 ArgTypesList ... arguments) :
233 args(std::make_tuple(arguments ...))
236 ICDSharedDataPtr helper;
237 std::tuple<ArgTypesList ...> args;
240 typedef std::function<void (const PrivateEvent&)>
241 ICDSharedDataDelegateType;
242 class ICDSharedData : private DPL::Event::EventSupport<PrivateEvent>,
243 public std::enable_shared_from_this<ICDSharedData>,
244 public ICDPrivate::ICDSharedDataBase
248 ICDPrivate::ICDelegateSupportInterface *base,
249 std::function<void (ArgTypesList ...)> outerDelegate,
252 m_outerDelegate(outerDelegate),
256 // lock is not needed: this object is not shared at that moment
258 DPL::Event::makeDelegate(this,
259 &ICDSharedData::delegateForwarder);
260 EventSupport<PrivateEvent>::AddListener(m_subDelegate);
263 void CallDelegate(ArgTypesList ... args)
265 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
266 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(
267 this->shared_from_this()));
269 EmitEvent(PrivateEvent(this->shared_from_this(),
274 virtual void disable()
276 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr ptr(
277 std::static_pointer_cast<ICDSharedDataBase>(
278 this->shared_from_this()));
279 ICDPrivate::ICDSharedDataBase::ScopedLock lock(ptr);
281 ICDPrivate::ICDSharedDataBase::disable();
282 EventSupport<PrivateEvent>::RemoveListener(m_subDelegate);
283 m_base->unregisterICDSharedData(ptr);
288 friend class std::shared_ptr<ICDSharedData>;
289 ICDSharedDataDelegateType m_subDelegate;
290 ICDPrivate::ICDelegateSupportInterface* m_base;
291 std::function<void (ArgTypesList ...)> m_outerDelegate;
294 void delegateForwarder(const PrivateEvent& event)
296 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr ptr(
297 std::static_pointer_cast<ICDSharedDataBase>(event.helper));
298 ICDPrivate::ICDSharedDataBase::ScopedLock lock(ptr);
300 Assert(m_outerDelegate);
301 if (ptr->isDisabled()) {
302 LogPedantic("ICDSharedData has been disabled - call is ignored");
304 DPL::Apply(m_outerDelegate, event.args);
306 if (m_reuse == ICD::Reuse::Yes) {
311 deleteICDSharedDataBase(ptr);
316 // Schedules helper removal.
317 static void deleteICDSharedDataBase(
318 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper)
320 using namespace ICDPrivate;
321 ICDSharedDataBase::ScopedLock lock(helper);
322 DeleteICDSharedDataBaseEventCall* event =
323 new DeleteICDSharedDataBaseEventCall(helper);
324 if (DPL::Thread::GetCurrentThread() == NULL) {
325 DPL::Event::GetMainEventDispatcherInstance().AddEventCall(event);
327 DPL::Event::ThreadEventDispatcher dispatcher;
328 dispatcher.SetThread(DPL::Thread::GetCurrentThread());
329 dispatcher.AddEventCall(event);
333 ICDSharedDataPtr m_helper;
336 template <typename ThisType>
337 class ICDelegateSupport : public ICDPrivate::ICDelegateSupportInterface
340 template<typename ... ArgTypesList>
341 ICDelegate<ArgTypesList ...> makeICDelegate(
342 void (ThisType::*Func)(ArgTypesList ...),
343 ICD::Reuse reuse = ICD::Reuse::No)
345 ThisType* This = static_cast<ThisType*>(this);
346 ICDelegate<ArgTypesList ...> icdelegate(
348 makeDelegate(This, Func),
350 this->registerICDSharedData(icdelegate.getRelatedICDSharedData());
359 ICDPrivate::ICDSharedDataBase::ICDSharedDataBaseList list =
361 FOREACH(helper, list) {
362 ICDPrivate::ICDSharedDataBase::ScopedLock lock(
363 std::static_pointer_cast<ICDPrivate::ICDSharedDataBase>(*helper));
364 (*helper)->disable();
366 m_ICDSharedDatas.clear();
370 virtual void unregisterICDSharedData(
371 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper)
373 m_ICDSharedDatas.erase(helper->getIterator());
376 virtual void registerICDSharedData(
377 ICDPrivate::ICDSharedDataBase::ICDSharedDataBasePtr helper)
380 m_ICDSharedDatas.insert(m_ICDSharedDatas.begin(),
385 ICDPrivate::ICDSharedDataBase::ICDSharedDataBaseList m_ICDSharedDatas;
390 #endif // __GXX_EXPERIMENTAL_CXX0X__
392 #endif //DPL_INTER_CONTEXT_DELEGATE_H_