7138c8a66396ee5445a95bf6ccc89d2f31ec80a5
[framework/web/wrt-commons.git] / modules / event / include / dpl / event / nested_loop.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  * This file contains the declaration of the NestedLoopManager class.
18  *
19  * @file    nested_loop.h
20  * @author  Lukasz Marek (l.marek@samsung.com)
21  * @version 0.1
22  * @brief   This file contains the declaration of the NestedLoopManager class.
23  */
24
25 #ifndef DPL_NESTED_LOOP_H_
26 #define DPL_NESTED_LOOP_H_
27
28 #include <vector>
29 #include <list>
30 #include <map>
31 #include <dpl/event/controller.h>
32 #include <dpl/singleton.h>
33
34 namespace DPL
35 {
36 namespace Event
37 {
38
39 class LoopExitEvent
40 {
41 };
42
43 typedef unsigned LoopHandle;
44 typedef std::list<LoopHandle> LoopHandleList;
45
46 const LoopHandle UNDEFINED_LOOP_HANDLE = 0;
47
48 class NestedLoopManager :
49     protected Controller<TypeListDecl<LoopExitEvent>::Type>
50 {
51   public:
52     void* begin(LoopHandle loopHandle);
53     void exit(LoopHandle loopHandle,
54             void *userParam = NULL);
55     size_t getLevel() const;
56     LoopHandle getNewHandle();
57
58   protected:
59     void OnEventReceived(const LoopExitEvent& event);
60
61   private:
62     struct LoopInformation
63     {
64         LoopInformation(LoopHandle handle) :
65             loopHandle(handle),
66             exitFlag(false),
67             userParam(NULL)
68         {
69         }
70
71         LoopHandle loopHandle;
72         bool exitFlag;
73         void *userParam;
74     };
75
76     class RunningLoopsHandlePredicate
77     {
78       public:
79         RunningLoopsHandlePredicate(LoopHandle handle) :
80             m_handle(handle)
81         {
82         }
83
84         bool operator()(const LoopInformation &loopInformation) const
85         {
86             return loopInformation.loopHandle == m_handle;
87         }
88
89       private:
90         LoopHandle m_handle;
91     };
92
93     NestedLoopManager();
94     ~NestedLoopManager();
95
96     typedef std::vector<LoopInformation> RunningLoopsList;
97     typedef RunningLoopsList::iterator RunningLoopsListIterator;
98
99     bool m_eventGuard; // only one event is allowed
100     LoopHandle m_handle;
101     RunningLoopsList m_runningLoops;
102
103     friend class Singleton<NestedLoopManager>;
104 };
105
106 typedef Singleton<NestedLoopManager> NestedLoopManagerSingleton;
107
108 }
109 } // namespace DPL
110
111 #endif // WRT_ENGINE_SRC_COMMON_NESTED_LOOP_H_