Marine systems simulation
CProgressBar.h
1#ifndef PROGRESS_BAR_H
2#define PROGRESS_BAR_H
3
41#include <CPrintDuringExec.h>
42#include <SimObject.h>
43#include <cmath>
44#include <iomanip>
45#include <iostream>
46#include <sstream>
47#include <string>
48
54{
55 public:
56 static int LENGTH_OF_PROGRESS_BAR;
57 static double PERCENTAGE_BIN_SIZE;
58
59 static std::string generateProgressBar(unsigned int percentage, double currentTime)
60 {
61 const int progress = static_cast<int>(std::ceil(percentage / (double)PERCENTAGE_BIN_SIZE));
62 std::ostringstream ss;
63 ss << std::fixed << std::setprecision(3) << "T:" << currentTime << " " << std::setw(3) << std::right << percentage << "% ";
64 std::string bar("[" + std::string(LENGTH_OF_PROGRESS_BAR - 2, ' ') + "]");
65
66 unsigned int numberOfSymbols = std::min(
67 std::max(0, progress - 1),
68 LENGTH_OF_PROGRESS_BAR - 2);
69
70 bar.replace(1, numberOfSymbols, std::string(numberOfSymbols, '#'));
71 ss << bar;
72 return ss.str();
73 }
74
81 ProgressBar(double totalTime, const std::string& initialMessage = "")
82 : mTotalTime(totalTime)
83 , mCurrentTime(0)
84 , mEnded(false)
85 , mOutStream(nullptr)
86 {
87 mCout = std::cout.rdbuf();
88 std::cout.rdbuf(mSs.rdbuf());
89 mOutStream.rdbuf(mCout);
90 mOutStream << initialMessage << "\n";
91 mLengthOfLastPrintedMessage = initialMessage.size();
92 mOutStream << generateProgressBar(0, 0) << "\r" << std::flush;
93 }
94
99 {
101 }
102
103 // Make the object non-copyable
104 ProgressBar(const ProgressBar& o) = delete;
105 ProgressBar& operator=(const ProgressBar& o) = delete;
106
113 {
114 if (!mEnded) {
115 mOutStream << std::string(2, '\n');
116 }
117 mEnded = true;
118 }
119
125 void printNewMessage(const std::string& message)
126 {
127 if (mEnded) {
128 throw std::runtime_error("Attempted to use progress bar after having terminated it");
129 }
130
131 mOutStream << "\r"
132 << std::left
133 << std::setw(LENGTH_OF_PROGRESS_BAR + 6)
134 << message << "\n";
135 mLengthOfLastPrintedMessage = message.size();
136 const unsigned int percentage = static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
137
138 mOutStream << generateProgressBar(percentage, mCurrentTime) << "\r" << std::flush;
139 }
140
141
148 /*void updateLastPrintedMessage(const std::string& message)
149 {
150 if (mEnded)
151 {
152 throw std::runtime_error("Attempted to use progress bar after having terminated it");
153 }
154
155 mOutStream << "\r\x1b[1A"
156 << std::left
157 << std::setw(mLengthOfLastPrintedMessage)
158 << message << "\n";
159 mLengthOfLastPrintedMessage = message.size();
160 }*/
161
162 void setTime(double currentTime)
163 {
164 if (mEnded) {
165 throw std::runtime_error(
166 "Attempted to use progress bar after having terminated it");
167 }
168 mCurrentTime = currentTime;
169
170 const unsigned int percentage = static_cast<unsigned int>(std::ceil(mCurrentTime / mTotalTime * 100));
171
172 if (mSs.str().length() > 0) {
173 printNewMessage("Message at time " + std::to_string(currentTime) + ": " + mSs.str());
174 mSs.clear();
175 }
176
177 mOutStream << generateProgressBar(percentage, mCurrentTime) << "\r" << std::flush;
178 }
179
180 private:
181 double mTotalTime;
182 double mCurrentTime;
183 bool mEnded;
184 size_t mLengthOfLastPrintedMessage;
185 std::stringstream mSs;
186 std::streambuf* mCout;
187 std::ostream mOutStream;
188};
189
190
191class CProgressBar : public SimObject
192{
193 public:
194 CProgressBar(const std::string& name, ISimObjectCreator* const creator);
195 void OdeFcn(const double t, const double* const x, double* const xdot, const bool isMajorTimeStep) { }
196 void PreOdeFcn(const double T, const double* const X, IStateUpdater* updater);
197 void FinalSetup(const double T, const double* const X, ISimObjectCreator* const creator);
198#ifdef FH_VISUALIZATION
199 virtual void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator)
200 { }
201 virtual void RenderUpdate(const double T, const double* const X) { }
202#endif
203 protected:
206 ProgressBar* m_actualProgress;
207 CPrintDuringExec* mPrint;
208};
209
210
211#endif /* PROGRESS_BAR_H */
Definition: CProgressBar.h:192
double m_end_time
Definition: CProgressBar.h:205
Definition: CProgressBar.h:54
ProgressBar(double totalTime, const std::string &initialMessage="")
Definition: CProgressBar.h:81
void printNewMessage(const std::string &message)
Definition: CProgressBar.h:125
~ProgressBar()
Definition: CProgressBar.h:98
void setTime(double currentTime)
Definition: CProgressBar.h:162
void endProgressBar()
Definition: CProgressBar.h:112