Limo
A simple mod manager
Loading...
Searching...
No Matches
progressnode.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <functional>
9#include <memory>
10#include <optional>
11#include <vector>
12
13
22{
23public:
30 ProgressNode(int id, const std::vector<float>& weights, std::optional<ProgressNode*> parent);
37 ProgressNode(std::function<void(float)> progress_callback,
38 const std::vector<float>& weights = {});
39
45 void advance(uint64_t num_steps = 1);
50 int totalSteps() const;
55 void setTotalSteps(uint64_t total_steps);
60 int id() const;
65 void addChildren(const std::vector<float>& weights);
71 ProgressNode& child(int id);
77 void setProgressCallback(std::function<void(float)> progress_callback);
82 float updateStepSize() const;
87 void setUpdateStepSize(float step_size);
92 float getProgress() const;
93
94private:
96 int id_;
98 uint64_t cur_step_ = 0;
100 uint64_t total_steps_;
102 float progress_ = 0.0f;
104 float prev_progress_ = 0.0f;
106 float update_step_size_ = 0.01f;
108 std::optional<ProgressNode*> parent_;
110 std::vector<float> weights_;
112 std::vector<ProgressNode> children_;
113
118 std::function<void(float)> set_progress_ = [](float f) {};
123 void updateProgress();
130 void propagateProgress();
131};
ProgressNode & child(int id)
Returns a reference to the child with the given id.
Definition progressnode.cpp:63
float update_step_size_
minimal progress interval after which set_progress_ is called.
Definition progressnode.h:106
float updateStepSize() const
Returns the minimal progress interval after which the progress callback is called.
Definition progressnode.cpp:74
void propagateProgress()
Informs this nodes parent of a change in progress.
Definition progressnode.cpp:97
float prev_progress_
Progress at the time of the last call to set_progress_.
Definition progressnode.h:104
std::vector< float > weights_
Weights of children.
Definition progressnode.h:110
void setTotalSteps(uint64_t total_steps)
Sets the total number of steps in this task.
Definition progressnode.cpp:37
void setUpdateStepSize(float step_size)
Sets the minimal progress interval after which the progress callback is called.
Definition progressnode.cpp:79
void addChildren(const std::vector< float > &weights)
Adds new child nodes with given weights to this node.
Definition progressnode.cpp:49
float progress_
Current progress in this task.
Definition progressnode.h:102
float getProgress() const
Returns the current progress.
Definition progressnode.cpp:84
void updateProgress()
Sets the current progress of this node to the weighted sum of the current progresses of its children.
Definition progressnode.cpp:89
std::function< void(float)> set_progress_
Callback function used by the root node to inform about changes in the task progress.
Definition progressnode.h:118
uint64_t total_steps_
Number of total steps in this task. Only used for leaf nodes.
Definition progressnode.h:100
void advance(uint64_t num_steps=1)
Advances the current progress of this node by the given amount of steps. This must be a leaf node.
Definition progressnode.cpp:20
uint64_t cur_step_
Current step in this task. Only used for leaf nodes.
Definition progressnode.h:98
int id_
This nodes id.
Definition progressnode.h:96
int totalSteps() const
Returns the total number of steps in this task.
Definition progressnode.cpp:32
void setProgressCallback(std::function< void(float)> progress_callback)
Sets a callback function used by the root node to inform about changes in the task progress.
Definition progressnode.cpp:68
ProgressNode(int id, const std::vector< float > &weights, std::optional< ProgressNode * > parent)
Constructor.
Definition progressnode.cpp:6
std::vector< ProgressNode > children_
Children representing sub-tasks of this task.
Definition progressnode.h:112
int id() const
Returns the id of this node.
Definition progressnode.cpp:44
std::optional< ProgressNode * > parent_
The parent of this, if this is not the root.
Definition progressnode.h:108