Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / core / src / mutable_task_list.cpp
1 /*
2  * Copyright (c) 2013 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  Tomasz Iwanek (t.iwanek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for task list
21  */
22 #include <stddef.h>
23 #include <dpl/mutable_task_list.h>
24 #include <dpl/assert.h>
25
26 namespace DPL {
27 MutableTaskList::MutableTaskList() :
28     m_running(false)
29 {
30     m_currentTask = m_tasks.end();
31 }
32
33 MutableTaskList::~MutableTaskList()
34 {
35     for (Tasks::iterator i = m_tasks.begin(); i != m_tasks.end(); ++i) {
36         delete *i;
37     }
38 }
39
40 void MutableTaskList::AddTask(Task *task)
41 {
42     if(m_tasks.empty())
43     {
44         m_tasks.push_back(task);
45         m_currentTask = m_tasks.begin();
46     }
47     else
48     {
49         m_tasks.push_back(task);
50     }
51 }
52
53 bool MutableTaskList::NextStep()
54 {
55     m_running = true;
56
57     Assert(
58         m_currentTask != m_tasks.end() &&
59         "Task list is empty or all tasks done");
60
61     bool result = (*m_currentTask)->NextStep();
62
63     if (result) {
64         return true;
65     }
66
67     return ++m_currentTask != m_tasks.end();
68 }
69
70 bool MutableTaskList::Abort()
71 {
72     m_tasks.erase(m_currentTask, m_tasks.end());
73     m_tasks.reverse();
74     for (Tasks::iterator i = m_tasks.begin(); i != m_tasks.end();) {
75         //If given task does not have any "abortSteps", remove it from the list
76         if (!(*i)->Abort()) {
77             delete *i;
78             i = m_tasks.erase(i);
79             continue;
80         }
81         ++i;
82     }
83
84     if (m_tasks.empty()) {
85         return false;
86     }
87
88     m_currentTask = m_tasks.begin();
89
90     return true;
91 }
92
93 size_t MutableTaskList::GetStepCount() const
94 {
95     size_t count = 0;
96
97     for (Tasks::const_iterator i = m_tasks.begin(); i != m_tasks.end(); ++i) {
98         count += (*i)->GetStepCount();
99     }
100
101     return count;
102 }
103
104 } // namespace DPL