From c38665fd0c4bde9cee7f88a19fda895544d152b4 Mon Sep 17 00:00:00 2001 From: nedko Date: Fri, 29 May 2026 17:15:38 +0300 Subject: [PATCH] Default progress is now infinite. Finite is moved to new class --- Progress.cpp | 53 ++++++++++++---------------------------------- Progress.h | 19 +++++++---------- ProgressFinite.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ ProgressFinite.h | 22 +++++++++++++++++++ ProgressInfinite.h | 26 +++++++++++++++++++++++ example/Makefile | 2 +- example/main.cpp | 5 +++-- 7 files changed, 126 insertions(+), 54 deletions(-) create mode 100644 ProgressFinite.cpp create mode 100644 ProgressFinite.h create mode 100644 ProgressInfinite.h diff --git a/Progress.cpp b/Progress.cpp index f83a6ca..945c34b 100644 --- a/Progress.cpp +++ b/Progress.cpp @@ -1,66 +1,39 @@ -#include "Progress.h" +#include "ProgressFinite.h" #include using std::ostream; using std::string; using std::stringstream; -Progress::Progress(const string& message, size_t total, size_t current) -: m_current(current), -m_total(total), -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; - } -} +Progress::Progress(const string& message, size_t current) +: m_message(message), +m_current(current) +{} void Progress::set_message(const std::string& message) { m_message = message; } +void Progress::set_progress(size_t current) +{ + m_current = current; +} + void Progress::increment_progress(size_t inc) { m_current += inc; - - if (m_current > m_total) - { - m_current = m_total; - } } -string Progress::print_progress() +string Progress::print_progress() const { stringstream out; - out << m_current << "/" << m_total << " - " << m_message; + out << m_current << " - " << m_message; return out.str(); } ostream& operator<<(ostream& out, const Progress& progress) { - out << progress.m_current << "/" << progress.m_total; - out << " - " << progress.m_message; + out << progress.print_progress(); return out; } diff --git a/Progress.h b/Progress.h index 7ebb065..32e6315 100644 --- a/Progress.h +++ b/Progress.h @@ -7,23 +7,20 @@ class Progress { protected: - size_t m_current; - size_t m_total; - std::string m_message; + size_t m_current; 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 increment_progress(size_t inc = 1); - - std::string print_progress(); - - friend std::ostream& operator<<(std::ostream& os, const Progress& progress); + virtual void set_progress(size_t current); + virtual void increment_progress(size_t inc = 1); + virtual std::string print_progress() const; }; +std::ostream& operator<<(std::ostream& out, const Progress& progress); + #endif // PROGRESS_H_ diff --git a/ProgressFinite.cpp b/ProgressFinite.cpp new file mode 100644 index 0000000..d9a0c63 --- /dev/null +++ b/ProgressFinite.cpp @@ -0,0 +1,53 @@ +#include "ProgressFinite.h" +#include + +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(); +} diff --git a/ProgressFinite.h b/ProgressFinite.h new file mode 100644 index 0000000..72415bf --- /dev/null +++ b/ProgressFinite.h @@ -0,0 +1,22 @@ +#ifndef PROGRESS_FINITE_H_ +#define PROGRESS_FINITE_H_ + +#include "Progress.h" + +#include +#include + +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_ diff --git a/ProgressInfinite.h b/ProgressInfinite.h new file mode 100644 index 0000000..7d37312 --- /dev/null +++ b/ProgressInfinite.h @@ -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_ diff --git a/example/Makefile b/example/Makefile index 3ece99d..4a8d759 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,4 +1,4 @@ all: - g++ main.cpp ../Progress.cpp ../StatusBarManager.cpp -o example + g++ main.cpp ../Progress.cpp ../ProgressFinite.cpp ../StatusBarManager.cpp -o example .PHONY: all diff --git a/example/main.cpp b/example/main.cpp index fce41ef..2e6490a 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -8,6 +8,7 @@ #include #include "../Progress.h" +#include "../ProgressFinite.h" #include "../StatusBarManager.h" using namespace std; @@ -28,8 +29,8 @@ void INT_handler(int sig) int main() { signal(SIGINT, INT_handler); - Progress p("Normal", 10); - Progress p2("Test", 10); + Progress p("Normal"); + ProgressFinite p2("Test", 10); StatusBarManager manager; g_manager = &manager; manager.add_progress_message(&p);