Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / 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 <stddef.h>
24 #include <dpl/task_list.h>
25 #include <dpl/assert.h>
26
27 namespace DPL {
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
42 void TaskList::AddTask(Task *task)
43 {
44     Assert(!m_running && "AddTask is not allowed after calling NextStep");
45     m_tasks.push_back(task);
46     m_currentTask = m_tasks.begin();
47 }
48
49 bool TaskList::NextStep()
50 {
51     m_running = true;
52
53     Assert(
54         m_currentTask != m_tasks.end() &&
55         "Task list is empty or all tasks done");
56
57     m_switched = false;
58
59     bool result = (*m_currentTask)->NextStep();
60
61     if (result || m_switched) {
62         return true;
63     }
64
65     return ++m_currentTask != m_tasks.end();
66 }
67
68 bool TaskList::Abort()
69 {
70     m_tasks.erase(m_currentTask, m_tasks.end());
71     m_tasks.reverse();
72     for (Tasks::iterator i = m_tasks.begin(); i != m_tasks.end();) {
73         //If given task does not have any "abortSteps", remove it from the list
74         if (!(*i)->Abort()) {
75             delete *i;
76             i = m_tasks.erase(i);
77             continue;
78         }
79         ++i;
80     }
81
82     if (m_tasks.empty()) {
83         return false;
84     }
85
86     m_currentTask = m_tasks.begin();
87
88     return true;
89 }
90
91 void TaskList::SwitchToTask(Task *task)
92 {
93     Tasks::iterator i = std::find(m_tasks.begin(), m_tasks.end(), task);
94     Assert(i != m_tasks.end());
95     m_currentTask = i;
96     m_switched = true;
97 }
98
99 size_t TaskList::GetTaskCount() const
100 {
101     return static_cast<size_t>(m_tasks.size());
102 }
103
104 size_t TaskList::GetStepCount() const
105 {
106     size_t count = 0;
107
108     for (Tasks::const_iterator i = m_tasks.begin(); i != m_tasks.end(); ++i) {
109         count += (*i)->GetStepCount();
110     }
111
112     return count;
113 }
114 } // namespace DPL