tizen 2.4 release
[framework/web/wrt-commons.git] / examples / timed_event / timed_event.cpp
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        timed_event.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of timed event example
21  */
22 #include <stddef.h>
23 #include <dpl/generic_event.h>
24 #include <dpl/application.h>
25 #include <dpl/controller.h>
26 #include <dpl/type_list.h>
27 #include <dpl/thread.h>
28 #include <dpl/log/wrt_log.h>
29 #include <string>
30
31 DECLARE_GENERIC_EVENT_0(FirstEvent)
32 DECLARE_GENERIC_EVENT_0(SecondEvent)
33
34 class ControllerInThread
35     : public DPL::Controller<DPL::TypeListDecl<FirstEvent,
36                                                SecondEvent>::Type>
37 {
38 protected:
39     virtual void OnEventReceived(const FirstEvent &event)
40     {
41         (void)event;
42         WrtLogD("First event occurred");
43     }
44
45     virtual void OnEventReceived(const SecondEvent &event)
46     {
47         (void)event;
48         WrtLogD("Second event occurred");
49     }
50 };
51
52 DECLARE_GENERIC_EVENT_0(QuitEvent)
53
54 class MyApplication
55     : public DPL::Application,
56       private DPL::Controller<DPL::TypeListDecl<QuitEvent>::Type>
57 {
58 private:
59     DPL::Thread m_thread;
60     ControllerInThread m_controllerInThread;
61
62     // Quit application event occurred
63     virtual void OnEventReceived(const QuitEvent &event)
64     {
65         (void)event;
66         Quit();
67     }
68
69 public:
70     MyApplication(int argc, char **argv)
71         : Application(argc, argv, "timed_event", false)
72     {
73         // Touch
74         Touch();
75         m_controllerInThread.Touch();
76
77         // Run thread
78         m_thread.Run();
79         m_controllerInThread.SwitchToThread(&m_thread);
80
81         // Emit thread timed events
82         m_controllerInThread.DPL::ControllerEventHandler<SecondEvent>::PostTimedEvent(SecondEvent(), 3);
83         m_controllerInThread.DPL::ControllerEventHandler<FirstEvent>::PostTimedEvent(FirstEvent(), 2);
84
85         // Emit framework timed quit event
86         DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 5);
87     }
88
89     virtual ~MyApplication()
90     {
91         m_controllerInThread.SwitchToThread(NULL);
92
93         // Quit thread
94         m_thread.Quit();
95     }
96 };
97
98 int main(int argc, char *argv[])
99 {
100     MyApplication app(argc, argv);
101     return app.Exec();
102 }