89e0ff8be6de937b1a97b4841a4b3cde5a285ebf
[platform/framework/web/wrt-commons.git] / modules / core / src / task_list.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    task_list.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @author  Radoslaw Wicik (r.wicik@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for task list
22  */
23 #include <dpl/task_list.h>
24 #include <dpl/assert.h>
25
26 namespace DPL
27 {
28 TaskList::TaskList()
29     : m_switched(false),
30       m_running(false)
31 {
32     m_currentTask = m_tasks.end();
33 }
34
35 TaskList::~TaskList()
36 {
37     for (Tasks::iterator i = m_tasks.begin(); i != m_tasks.end(); ++i)
38         delete *i;
39 }
40
41 void TaskList::AddTask(Task *task)
42 {
43     Assert(!m_running && "AddTask is not allowed after calling NextStep");
44     m_tasks.push_back(task);
45     m_currentTask = m_tasks.begin();
46 }
47
48 bool TaskList::NextStep()
49 {
50     m_running = true;
51
52     Assert(m_currentTask != m_tasks.end() && "Task list is empty or all tasks done");
53
54     m_switched = false;
55
56     bool result = (*m_currentTask)->NextStep();
57
58     if (result || m_switched)
59         return true;
60
61     return ++m_currentTask != m_tasks.end();
62 }
63
64 bool TaskList::Abort()
65 {
66     m_tasks.erase(m_currentTask,m_tasks.end());
67     m_tasks.reverse();
68     for (Tasks::iterator i = m_tasks.begin(); i != m_tasks.end();)
69     {
70         //If given task does not have any "abortSteps", remove it from the list
71         if(!(*i)->Abort())
72         {
73             delete *i;
74             i=m_tasks.erase(i);
75             continue;
76         }
77         ++i;
78     }
79
80     if(m_tasks.empty())
81         return false;
82
83     m_currentTask=m_tasks.begin();
84
85     return true;
86 }
87
88 void TaskList::SwitchToTask(Task *task)
89 {
90     Tasks::iterator i = std::find(m_tasks.begin(), m_tasks.end(), task);
91     Assert(i != m_tasks.end());
92     m_currentTask = i;
93     m_switched = true;
94 }
95
96 size_t TaskList::GetTaskCount() const
97 {
98     return static_cast<size_t>(m_tasks.size());
99 }
100
101 size_t TaskList::GetStepCount() const
102 {
103     size_t count = 0;
104
105     for (Tasks::const_iterator i = m_tasks.begin(); i != m_tasks.end(); ++i)
106         count += (*i)->GetStepCount();
107
108     return count;
109 }
110
111 } // namespace DPL