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 * This file contains the definition of the NestedLoopManager class.
19 * @file nested_loop.cpp
20 * @author Lukasz Marek (l.marek@samsung.com)
22 * @brief This file contains the definition of the NestedLoopManager class.
25 #include <dpl/event/nested_loop.h>
28 #include <dpl/log/log.h>
29 #include <dpl/assert.h>
30 #include <dpl/singleton_impl.h>
32 IMPLEMENT_SINGLETON(DPL::Event::NestedLoopManager)
39 NestedLoopManager::NestedLoopManager() : m_eventGuard(false),
45 NestedLoopManager::~NestedLoopManager()
49 void* NestedLoopManager::begin(LoopHandle loopHandle)
51 LoopInformation info(loopHandle);
52 m_runningLoops.push_back(info);
53 LogPedantic("Nested loop begin. Nested loop level: " << getLevel());
55 ecore_main_loop_begin();
57 Assert(m_runningLoops.size() && "No loop on the stack");
59 info = m_runningLoops.back();
60 m_runningLoops.pop_back();
62 Assert(info.loopHandle == loopHandle && "You exit from wrong loop");
63 Assert(info.exitFlag == true && "Exit flag not set");
65 LogPedantic("Nested loop quit. Nested loop level: " << getLevel());
67 if (!m_runningLoops.empty() && m_runningLoops.back().exitFlag &&
73 return info.userParam;
76 void NestedLoopManager::exit(LoopHandle loopHandle,
79 RunningLoopsListIterator iterator = std::find_if(
80 m_runningLoops.begin(),
82 RunningLoopsHandlePredicate(loopHandle));
84 Assert(iterator != m_runningLoops.end() && "Unknown loopHandle");
85 Assert(iterator->exitFlag == false && "You cannot close a loop twice.");
87 iterator->exitFlag = true;
88 iterator->userParam = userParam;
90 if (m_runningLoops.back().exitFlag && !m_eventGuard) {
97 size_t NestedLoopManager::getLevel() const
99 return m_runningLoops.size();
102 LoopHandle NestedLoopManager::getNewHandle()
107 void NestedLoopManager::OnEventReceived(const LoopExitEvent& event)
110 Assert(!m_runningLoops.empty());
111 m_eventGuard = false; // no event in queue
112 if (m_runningLoops.back().exitFlag) {
113 //exit loop when last started one is readu to finish
114 //this will post event if next loop is ready to exit
115 ecore_main_loop_quit();