Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / event / include / dpl / event / generic_event_call.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  * @file        generic_event_call.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of generic event call
21  */
22 #ifndef DPL_GENERIC_EVENT_CALL_H
23 #define DPL_GENERIC_EVENT_CALL_H
24
25 #include <functional>
26
27 #include <dpl/event/abstract_event_call.h>
28 #include <dpl/event/event_listener.h>
29 #include <dpl/noncopyable.h>
30 #include <dpl/log/wrt_log.h>
31 #include <dpl/assert.h>
32
33 namespace DPL {
34 namespace Event {
35 template<typename EventType, typename SupportDataType>
36 class GenericEventCall :
37     public AbstractEventCall
38 {
39   public:
40     typedef EventListener<EventType> EventListenerType;
41     typedef std::function<void(const EventType &)> DelegateType;
42
43   protected:
44     SupportDataType m_supportData;
45     EventListenerType *m_eventListener;
46     DelegateType m_delegate;
47     EventType m_event;
48
49   public:
50     template<typename OtherEventType, typename OtherSupportType>
51     struct Rebind
52     {
53         typedef GenericEventCall<OtherEventType, OtherSupportType> Other;
54     };
55
56     GenericEventCall(SupportDataType supportData,
57                      EventListenerType *eventListener,
58                      DelegateType delegate,
59                      const EventType &event) :
60         m_supportData(supportData),
61         m_eventListener(eventListener),
62         m_delegate(delegate),
63         m_event(event)
64     {}
65
66     virtual ~GenericEventCall()
67     {
68         Assert(m_supportData == NULL &&
69                "Call method hasn't been called"
70                " (support data wasn't destroyed)");
71     }
72
73     virtual void Call()
74     {
75         WrtLogD("Calling generic event call");
76
77         m_supportData->CallAndDestroy(m_event, m_eventListener, m_delegate);
78
79         // Now m_supportData points to invalid object. Marking it as NULL.
80         m_supportData = NULL;
81
82         WrtLogD("Generic event called");
83     }
84
85     virtual void DisableEvent()
86     {
87         WrtLogD("Disabling this EventCall");
88         m_supportData->Reset();
89
90         // TODO: In the future, event should be completely removed
91         // from the event queue (not just marked "disabled")
92     }
93 };
94 }
95 } // namespace DPL
96
97 #endif // DPL_GENERIC_EVENT_CALL_H