00001 /* 00002 * ProgressBar.cpp 00003 * 00004 * Created on: 14.01.2011 00005 * Author: Thomas Wiemann 00006 */ 00007 00008 #include "Progress.hpp" 00009 00010 #include <sstream> 00011 #include <iostream> 00012 00013 using std::stringstream; 00014 using std::cout; 00015 using std::endl; 00016 using std::flush; 00017 00018 namespace lssr 00019 { 00020 00021 ProgressBar::ProgressBar(int max_val, string prefix) 00022 { 00023 m_prefix = prefix; 00024 m_maxVal = max_val; 00025 m_currentVal = 0; 00026 m_stepSize = max_val / 100; 00027 m_percent = 0; 00028 } 00029 00030 void ProgressBar::operator++() 00031 { 00032 boost::mutex::scoped_lock lock(m_mutex); 00033 m_currentVal++; 00034 if(m_currentVal >= m_stepSize) 00035 { 00036 m_currentVal = 0; 00037 print_bar(); 00038 } 00039 } 00040 00041 void ProgressBar::print_bar() 00042 { 00043 m_percent += 1; 00044 cout << "\r" << m_prefix << " " << m_percent << "%" << flush; 00045 } 00046 00047 00048 00049 ProgressCounter::ProgressCounter(int stepVal, string prefix) 00050 { 00051 m_prefix = prefix; 00052 m_stepVal = stepVal; 00053 m_currentVal = 0; 00054 } 00055 00056 void ProgressCounter::operator++() 00057 { 00058 boost::mutex::scoped_lock lock(m_mutex); 00059 m_currentVal++; 00060 if(m_currentVal % m_stepVal == 0) 00061 { 00062 print_progress(); 00063 } 00064 } 00065 00066 void ProgressCounter::print_progress() 00067 { 00068 cout << "\r" << m_prefix << " " << m_currentVal << flush; 00069 } 00070 00071 } // namespace lssr 00072