Skip to content

Commit

Permalink
Upgrade multi-bar management mechanism
Browse files Browse the repository at this point in the history
Multi-bar position management mechanism upgraded from a simple counter to bar-registry to allow dynamic ordering
  • Loading branch information
moshiba committed Jan 18, 2020
1 parent 31b542d commit 5ab3bde
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
27 changes: 22 additions & 5 deletions pbar/pbar.cpp
Expand Up @@ -9,14 +9,22 @@
#include "pbar.hpp"

#include <aesc.hpp>
#include <algorithm>
#include <iomanip>
#include <iostream>

#include "unistd.h"

namespace pbar {

int ProgressBar::nbars = 0;
std::mutex ProgressBar::class_mutex;
std::vector<ProgressBar*> ProgressBar::bar_registry;

void ProgressBar::update_positions() {
for (unsigned int i = 0; i != ProgressBar::bar_registry.size(); ++i) {
ProgressBar::bar_registry[i]->position = i;
}
}

namespace window_width {
// window_width
Expand Down Expand Up @@ -60,7 +68,14 @@ ProgressBar::ProgressBar(const std::string& description, const long long& total,
} else {
this->window_width = new window_width::dynamic_window_width();
}
ProgressBar::nbars += 1;
{
std::lock_guard<std::mutex> guard(ProgressBar::class_mutex);
ProgressBar::bar_registry.emplace(
ProgressBar::bar_registry.begin() + position, this);

This comment has been minimized.

Copy link
@moshiba

moshiba Apr 6, 2020

Author Owner

grants preferred position by storing the pointer in that particular position in the registry,
the position appointed stored in data member position will soon be overwritten as seen in line 23 to line 27
https://github.com/HsuanTingLu/pbar/blob/5ab3bdef40ab6bc65d3d4861380ee5bb363d0bed/pbar/pbar.cpp#L23-L27

ProgressBar::update_positions();
}
this->display();
}
Expand All @@ -69,7 +84,7 @@ ProgressBar::ProgressBar(const std::string& description, const long long& total,
: ProgressBar(description, total, leave, -1,
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::milliseconds(10)),
"", 0, ProgressBar::nbars) {}
"", 0, ProgressBar::bar_registry.size()) {}
ProgressBar::~ProgressBar() { this->close(); }
Expand Down Expand Up @@ -236,8 +251,10 @@ void ProgressBar::close() {
this->disable = true;
// Remove pbar from record
ProgressBar::nbars -= 1;
// TODO: implement internal set instead of simple counting to maintain
ProgressBar::bar_registry.erase(std::find(ProgressBar::bar_registry.begin(),
ProgressBar::bar_registry.end(),
this));
// TODO: WIP: implement internal set instead of simple counting to maintain
// multi-bar order
delete window_width;
Expand Down
18 changes: 11 additions & 7 deletions pbar/pbar.hpp
Expand Up @@ -17,6 +17,7 @@
#include <ratio>
#include <sstream>
#include <string>
#include <vector>

namespace pbar {

Expand Down Expand Up @@ -53,27 +54,29 @@ class dynamic_window_width final : public window_width {
class ProgressBar {
/* TODO: Not copyable, but movable
*/
private:
static int nbars; // bar count
private: // class-wise stuff
static std::mutex class_mutex;
static std::vector<ProgressBar*> bar_registry;
static void update_positions();

private: // initial values, used in reset()
long long initial_value;

private:
private: // internal data
std::string description;
long long total;
const bool leave;
std::chrono::nanoseconds min_interval_time;
long min_interval_iter;
std::string bar_format;
std::atomic<long long> n;
const int position;
int position;
std::atomic<long long> last_update_n;
std::chrono::system_clock::time_point last_update_time;
bool disable;
std::mutex pbar_mutex;

private:
private: // internal functions
inline float percentage();
int __digits(long long number);
window_width::window_width* window_width; // Functor pointer
Expand All @@ -92,8 +95,9 @@ class ProgressBar {
const std::string& bar_format,
const long long& initial_value, const int position);
explicit ProgressBar(const std::string& description, const long long& total,
const bool leave = (ProgressBar::nbars ? false
: true));
const bool leave = (ProgressBar::bar_registry.empty()
? true
: false));
~ProgressBar();
void update(const int n = 1);
void close();
Expand Down

1 comment on commit 5ab3bde

@moshiba
Copy link
Owner Author

@moshiba moshiba commented on 5ab3bde Jan 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solves issue #17

Please sign in to comment.