ProgressManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/progress/ProgressManager.cpp
22 
23  \brief A singleton class used to manage tasks progresses and their viewers.
24 */
25 
26 // TerraLib
27 #include "AbstractProgressViewer.h"
28 #include "ProgressManager.h"
29 #include "TaskProgress.h"
30 
32 {
33  assert(apv);
34 
35  int id = generateViewerId();
36 
37  m_viewers.insert(std::map<int, AbstractProgressViewer*>::value_type(id, apv));
38 
39  //// add current tasks into the new viewer
40  //std::map<int, TaskProgress*>::iterator it = m_tasks.begin();
41 
42  //while(it != m_tasks.end())
43  //{
44  // apv->addTask(it->second, it->first);
45  // ++it;
46  //}
47 
48  return id;
49 }
50 
52 {
53  std::map<int, AbstractProgressViewer*>::iterator it = m_viewers.find(viewerId);
54 
55  if(it != m_viewers.end())
56  {
57  m_viewers.erase(it);
58  }
59 }
60 
62 {
63  LockWrite l(this);
64 
65  int id = generateTaskId();
66 
67  m_tasks.insert(std::map<int, TaskProgress*>::value_type(id, tp));
68 
69  // add task in viewers
70  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
71 
72  while(itV != m_viewers.end())
73  {
74  if(!m_suspendViewers)
75  itV->second->addTask(tp, id);
76 
77  ++itV;
78  }
79 
80  return id;
81 }
82 
84 {
85  LockWrite l(this);
86 
87  std::map<int, TaskProgress*>::iterator it = m_tasks.find(taskId);
88 
89  if(it != m_tasks.end())
90  {
91  // remove task from viewers
92  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
93 
94  while(itV != m_viewers.end())
95  {
96  if(!m_suspendViewers)
97  itV->second->removeTask(taskId);
98 
99  ++itV;
100  }
101 
102  m_tasks.erase(it);
103  }
104 }
105 
107 {
108  if(m_suspendViewers)
109  return;
110 
111  std::map<int, TaskProgress*>::iterator it = m_tasks.find(taskId);
112 
113  if(it != m_tasks.end())
114  {
115  // remove task from viewers
116  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
117 
118  while(itV != m_viewers.end())
119  {
120  itV->second->cancelTask(taskId);
121 
122  ++itV;
123  }
124  }
125 }
126 
128 {
129  if(m_suspendViewers)
130  return;
131 
132  LockWrite l(this);
133 
134  std::vector<TaskProgress*> cancelled;
135  std::map<int, TaskProgress*>::iterator it = m_tasks.begin();
136 
137  while(it != m_tasks.end())
138  {
139  if(it->second->getType() == type)
140  cancelled.push_back(it->second);
141 
142  ++it;
143  }
144 
145  for(std::size_t i = 0; i < cancelled.size(); ++i)
146  cancelled[i]->cancel();
147 }
148 
150 {
151  if(m_suspendViewers)
152  return;
153 
154  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
155 
156  while(itV != m_viewers.end())
157  {
158  itV->second->setTotalValues(taskId);
159 
160  ++itV;
161  }
162 }
163 
165 {
166  if(m_suspendViewers)
167  return;
168 
169  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
170 
171  while(itV != m_viewers.end())
172  {
173  itV->second->updateValue(taskId);
174 
175  ++itV;
176  }
177 }
178 
180 {
181  if(m_suspendViewers)
182  return;
183 
184  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
185 
186  while(itV != m_viewers.end())
187  {
188  itV->second->updateMessage(taskId);
189 
190  ++itV;
191  }
192 }
193 
195 {
196  m_viewers.clear();
197  m_tasks.clear();
198 }
199 
201  : m_taskCounter(0),
202  m_viewerCounter(0),
203  m_suspendViewers(false)
204 {
205 }
206 
208 {
209  clearAll();
210 }
211 
213 {
214  return m_viewerCounter++;
215 }
216 
218 {
219  return m_taskCounter++;
220 }
221 
This class can be used to inform the progress of a task.
A singleton class used to manage tasks progresses and their viewers.
int addTask(TaskProgress *tp)
Used in TaskProgress constructor, register this task generating a task id.
int m_taskCounter
Counter used to generate a task id.
std::map< int, TaskProgress * > m_tasks
Container with tasks.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
void cancelTask(int taskId)
Inform all viewers that a task was canceled.
int m_viewerCounter
Counter used to generate a viewer id.
int generateTaskId()
Used to generate a new task id (use internal counter).
A class that defines the interface of an abstract progress viewer.
void removeViewer(int viewerId)
Dettach a progress viewer (AbstractProgressViewer destructor calls this method).
A class that defines the interface of an abstract progress viewer.
void setTotalValues(int taskId)
Inform all viewers that a task set the total values.
void cancelTasks(unsigned int type)
Cancels the task with the given task type and inform all viewers that a task was canceled.
void removeTask(int taskId)
Used in TaskProgress destructor, remove task from singleton.
std::map< int, AbstractProgressViewer * > m_viewers
Container with viewers.
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
ProgressManager()
Default constructor.
void updateMessage(int taskId)
Inform all viewers that a task set the message.
void clearAll()
Removes references for Viewers and tasks.
int generateViewerId()
Used to generate a new viewer id (use internal counter).
void updateValue(int taskId)
Inform all viewers that a task set the current step.