Default progress is now infinite. Finite is moved to new class

This commit is contained in:
2026-05-29 17:15:38 +03:00
parent 89c5629176
commit c38665fd0c
7 changed files with 126 additions and 54 deletions

View File

@@ -1,66 +1,39 @@
#include "Progress.h" #include "ProgressFinite.h"
#include <sstream> #include <sstream>
using std::ostream; using std::ostream;
using std::string; using std::string;
using std::stringstream; using std::stringstream;
Progress::Progress(const string& message, size_t total, size_t current) Progress::Progress(const string& message, size_t current)
: m_current(current), : m_message(message),
m_total(total), m_current(current)
m_message(message) {}
{
if (m_current > m_total)
{
m_current = m_total;
}
}
void Progress::set_total(size_t total)
{
m_total = total;
if (m_current > m_total)
{
m_current = m_total;
}
}
void Progress::set_progress(size_t current)
{
m_current = current;
if (m_current > m_total)
{
m_current = m_total;
}
}
void Progress::set_message(const std::string& message) void Progress::set_message(const std::string& message)
{ {
m_message = message; m_message = message;
} }
void Progress::set_progress(size_t current)
{
m_current = current;
}
void Progress::increment_progress(size_t inc) void Progress::increment_progress(size_t inc)
{ {
m_current += inc; m_current += inc;
if (m_current > m_total)
{
m_current = m_total;
}
} }
string Progress::print_progress() string Progress::print_progress() const
{ {
stringstream out; stringstream out;
out << m_current << "/" << m_total << " - " << m_message; out << m_current << " - " << m_message;
return out.str(); return out.str();
} }
ostream& operator<<(ostream& out, const Progress& progress) ostream& operator<<(ostream& out, const Progress& progress)
{ {
out << progress.m_current << "/" << progress.m_total; out << progress.print_progress();
out << " - " << progress.m_message;
return out; return out;
} }

View File

@@ -7,23 +7,20 @@
class Progress class Progress
{ {
protected: protected:
size_t m_current;
size_t m_total;
std::string m_message; std::string m_message;
size_t m_current;
public: public:
Progress(const std::string& message, size_t total, size_t current = 0); Progress(const std::string& message, size_t current = 0);
virtual ~Progress() = default;
void set_total(size_t total);
void set_progress(size_t current);
void set_message(const std::string& message); void set_message(const std::string& message);
void increment_progress(size_t inc = 1); virtual void set_progress(size_t current);
virtual void increment_progress(size_t inc = 1);
std::string print_progress(); virtual std::string print_progress() const;
friend std::ostream& operator<<(std::ostream& os, const Progress& progress);
}; };
std::ostream& operator<<(std::ostream& out, const Progress& progress);
#endif // PROGRESS_H_ #endif // PROGRESS_H_

53
ProgressFinite.cpp Normal file
View File

@@ -0,0 +1,53 @@
#include "ProgressFinite.h"
#include <sstream>
using std::ostream;
using std::string;
using std::stringstream;
ProgressFinite::ProgressFinite(const string& message, size_t total, size_t current)
: Progress(message, current),
m_total(total)
{
if (m_current > m_total)
{
m_current = m_total;
}
}
void ProgressFinite::set_total(size_t total)
{
m_total = total;
if (m_current > m_total)
{
m_current = m_total;
}
}
void ProgressFinite::set_progress(size_t current)
{
m_current = current;
if (m_current > m_total)
{
m_current = m_total;
}
}
void ProgressFinite::increment_progress(size_t inc)
{
m_current += inc;
if (m_current > m_total)
{
m_current = m_total;
}
}
string ProgressFinite::print_progress() const
{
stringstream out;
out << m_current << "/" << m_total << " - " << m_message;
return out.str();
}

22
ProgressFinite.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef PROGRESS_FINITE_H_
#define PROGRESS_FINITE_H_
#include "Progress.h"
#include <ostream>
#include <string>
class ProgressFinite : public Progress
{
protected:
size_t m_total;
public:
ProgressFinite(const std::string& message, size_t total, size_t current = 0);
virtual void set_total(size_t total);
virtual void set_progress(size_t current) override;
virtual void increment_progress(size_t inc = 1);
virtual std::string print_progress() const override;
};
#endif // PROGRESS_FINITE_H_

26
ProgressInfinite.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef PROGRESS_INFINITE_H_
#define PROGRESS_INFINITE_H_
#include "Progress.h"
class ProgressInfinite : public Progress
{
protected:
size_t m_current;
std::string m_message;
public:
Progress(const std::string& message, size_t current = 0);
virtual void set_progress(size_t current);
void set_message(const std::string& message);
virtual void increment_progress(size_t inc = 1);
virtual std::string print_progress();
friend std::ostream& operator<<(std::ostream& os, const Progress& progress);
};
#endif // PROGRESS_INFINITE_H_

View File

@@ -1,4 +1,4 @@
all: all:
g++ main.cpp ../Progress.cpp ../StatusBarManager.cpp -o example g++ main.cpp ../Progress.cpp ../ProgressFinite.cpp ../StatusBarManager.cpp -o example
.PHONY: all .PHONY: all

View File

@@ -8,6 +8,7 @@
#include <unistd.h> #include <unistd.h>
#include "../Progress.h" #include "../Progress.h"
#include "../ProgressFinite.h"
#include "../StatusBarManager.h" #include "../StatusBarManager.h"
using namespace std; using namespace std;
@@ -28,8 +29,8 @@ void INT_handler(int sig)
int main() int main()
{ {
signal(SIGINT, INT_handler); signal(SIGINT, INT_handler);
Progress p("Normal", 10); Progress p("Normal");
Progress p2("Test", 10); ProgressFinite p2("Test", 10);
StatusBarManager manager; StatusBarManager manager;
g_manager = &manager; g_manager = &manager;
manager.add_progress_message(&p); manager.add_progress_message(&p);