2176c3198791d5e52dd3cdd82ac6fef1e7c575eb
[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 <stddef.h>
24 #include <dpl/task_list.h>
25 #include <dpl/assert.h>
26
27 namespace DPL
28 {
29 TaskList::TaskList()
30     : m_switched(false),
31       m_running(false)
32 {
33     m_currentTask = m_tasks.end();
34 }
35
36 TaskList::~TaskList()
37 {
38     for (Tasks::iterator i = m_tasks.begin(); i != m_tasks.end(); ++i)
39         delete *i;
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(m_currentTask != m_tasks.end() && "Task list is empty or all tasks done");
54
55     m_switched = false;
56
57     bool result = (*m_currentTask)->NextStep();
58
59     if (result || m_switched)
60         return true;
61
62     return ++m_currentTask != m_tasks.end();
63 }
64
65 bool TaskList::Abort()
66 {
67     m_tasks.erase(m_currentTask,m_tasks.end());
68     m_tasks.reverse();
69     for (Tasks::iterator i = m_tasks.begin(); i != m_tasks.end();)
70     {
71         //If given task does not have any "abortSteps", remove it from the list
72         if(!(*i)->Abort())
73         {
74             delete *i;
75             i=m_tasks.erase(i);
76             continue;
77         }
78         ++i;
79     }
80
81     if(m_tasks.empty())
82         return false;
83
84     m_currentTask=m_tasks.begin();
85
86     return true;
87 }
88
89 void TaskList::SwitchToTask(Task *task)
90 {
91     Tasks::iterator i = std::find(m_tasks.begin(), m_tasks.end(), task);
92     Assert(i != m_tasks.end());
93     m_currentTask = i;
94     m_switched = true;
95 }
96
97 size_t TaskList::GetTaskCount() const
98 {
99     return static_cast<size_t>(m_tasks.size());
100 }
101
102 size_t TaskList::GetStepCount() const
103 {
104     size_t count = 0;
105
106     for (Tasks::const_iterator i = m_tasks.begin(); i != m_tasks.end(); ++i)
107         count += (*i)->GetStepCount();
108
109     return count;
110 }
111
112 } // namespace DPL