3737#include < vector>
3838
3939#include " distributed_slice.hh"
40+ #include " func_util.hh"
4041#include " lnav.console.hh"
4142#include " safe/safe.h"
4243
@@ -52,6 +53,67 @@ enum class progress_status_t : uint8_t {
5253 working,
5354};
5455
56+ namespace func {
57+
58+ enum class op_type {
59+ blocking,
60+ interactive,
61+ };
62+
63+ class scoped_cb {
64+ public:
65+ using callback_type = std::function<progress_result_t (op_type)>;
66+
67+ class guard {
68+ public:
69+ explicit guard (scoped_cb* owner) : g_owner(owner) {}
70+
71+ guard (const guard&) = delete ;
72+ guard& operator =(const guard&) = delete ;
73+
74+ guard (guard&& gu) noexcept : g_owner(std::exchange(gu.g_owner, nullptr ))
75+ {
76+ }
77+
78+ guard& operator =(guard&& gu) noexcept
79+ {
80+ this ->g_owner = std::exchange (gu.g_owner , nullptr );
81+ return *this ;
82+ }
83+
84+ ~guard ()
85+ {
86+ if (this ->g_owner != nullptr ) {
87+ this ->g_owner ->s_callback = {};
88+ }
89+ }
90+
91+ private:
92+ scoped_cb* g_owner;
93+ };
94+
95+ guard install (callback_type cb)
96+ {
97+ this ->s_callback = std::move (cb);
98+
99+ return guard{this };
100+ }
101+
102+ progress_result_t operator ()(op_type ot) const
103+ {
104+ if (s_callback) {
105+ return s_callback (ot);
106+ }
107+
108+ return progress_result_t ::ok;
109+ }
110+
111+ private:
112+ callback_type s_callback;
113+ };
114+
115+ } // namespace func
116+
55117struct task_progress {
56118 std::string tp_id;
57119 progress_status_t tp_status{progress_status_t ::idle};
@@ -73,7 +135,7 @@ struct progress_tracker {
73135
74136 static safe_task_container& get_tasks ();
75137
76- void wait_for_completion ();
138+ void wait_for_completion (func::scoped_cb* scoped_cb );
77139
78140 void notify_completion ();
79141
0 commit comments