-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add progressbar class, part of solve of #81
- Loading branch information
Showing
3 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright Copyright 2016-17 Pierre Marijon | ||
This file is part of fastQT. | ||
Foobar is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Foobar is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Foobar. If not, see <http://www.gnu.org/licenses/>. | ||
@author : Pierre Marijon <pierre@marijon.fr> | ||
Freely inspired by GATB-Core Progress class https://github.com/GATB/gatb-core | ||
*/ | ||
|
||
#include "progressbar.h" | ||
|
||
ProgressBar::ProgressBar(unsigned int ntasks, const std::string& message, std::ostream& output) : message(message), os(output) | ||
{ | ||
reset(ntasks); | ||
} | ||
|
||
void ProgressBar::reset(unsigned int ntasks) | ||
{ | ||
this->todo = ntasks; | ||
this->done = 0; | ||
this->partial = 0; | ||
this->subdiv = 100; | ||
this->steps = static_cast<double>(ntasks > 0 ? ntasks : 1) / static_cast<double>(subdiv); | ||
} | ||
|
||
void ProgressBar::init() | ||
{ | ||
os << message; | ||
postInit(); | ||
} | ||
|
||
void ProgressBar::finish() | ||
{ | ||
/** We set the total done. */ | ||
set (todo); | ||
|
||
/** We forward the remaining of the finish to postFinish method. */ | ||
postFinish (); | ||
|
||
/** We reset all progression variables. */ | ||
todo = 0; | ||
done = 0; | ||
partial = 0; | ||
} | ||
|
||
void ProgressBar::inc (unsigned int ntasks_done) | ||
{ | ||
done += ntasks_done; | ||
partial += ntasks_done; | ||
|
||
while (partial >= steps) | ||
{ | ||
update(); | ||
partial -= steps; | ||
} | ||
} | ||
|
||
void ProgressBar::set (unsigned int ntasks_done) | ||
{ | ||
if (ntasks_done > done) | ||
{ | ||
inc (ntasks_done-done); | ||
} | ||
} | ||
|
||
void ProgressBar::postInit () | ||
{ | ||
os << "["; | ||
os.flush(); | ||
} | ||
|
||
void ProgressBar::postFinish () | ||
{ | ||
os << "]" << std::endl; | ||
os.flush(); | ||
} | ||
|
||
void ProgressBar::update() | ||
{ | ||
os << "-"; | ||
os.flush(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright Copyright 2016-17 Pierre Marijon | ||
This file is part of FastQt. | ||
Foobar is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Foobar is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Foobar. If not, see <http://www.gnu.org/licenses/>. | ||
@author : Pierre Marijon <pierre@marijon.fr> | ||
Freely inspired by GATB-Core Progress class https://github.com/GATB/gatb-core | ||
*/ | ||
|
||
#ifndef PROGRESSBAR_H | ||
#define PROGRESSBAR_H | ||
|
||
#include <iostream> | ||
|
||
class ProgressBar | ||
{ | ||
public: | ||
ProgressBar(unsigned int ntask, const std::string& message, std::ostream& os=std::cerr); | ||
|
||
virtual ~ProgressBar() {} | ||
|
||
void init(); | ||
|
||
void finish(); | ||
|
||
void inc(unsigned int ntask_done); | ||
|
||
void set(unsigned int ntask_done); | ||
|
||
void reset(unsigned int ntask); | ||
|
||
protected: | ||
|
||
protected: | ||
|
||
virtual void update (); | ||
virtual void postInit (); | ||
virtual void postFinish (); | ||
|
||
std::string message; | ||
u_int64_t done; | ||
u_int64_t todo; | ||
int subdiv ; // progress printed every 1/subdiv of total to do | ||
double partial; | ||
double steps ; //steps = _todo/subidv | ||
std::ostream& os; | ||
char buffer[512]; | ||
}; | ||
|
||
#endif // PROGRESSBAR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters