31 #ifndef LEDGER_CORE_PROGRESSNOTIFIER_H 32 #define LEDGER_CORE_PROGRESSNOTIFIER_H 34 #include <api/ExecutionContext.hpp> 36 #include <utils/Option.hpp> 37 #include <utils/Exception.hpp> 39 #include <async/Future.hpp> 40 #include <async/Promise.hpp> 46 using ProgressHandler = std::function<void (const std::string&, double)>;
57 void setProgress(
const std::string& step,
double progress) {
58 std::lock_guard<std::mutex> lock(_mutex);
60 for (
auto& handler : _handlers) {
61 auto callback = handler.second;
63 callback(step, std::max(progress, 1.0));
69 void success(
const T& result) {
70 setProgress(_lastStep, 1.0);
71 std::lock_guard<std::mutex> lock(_mutex);
72 _promise.success(result);
75 void failure(
const Exception& exception) {
76 std::lock_guard<std::mutex> lock(_mutex);
77 _promise.failure(exception);
80 void onProgress(
const std::shared_ptr<api::ExecutionContext>& context,
const ProgressHandler& handler)
const {
81 std::lock_guard<std::mutex> lock(_mutex);
82 _handlers.push_back(std::make_pair(context, handler));
86 return _promise.getFuture();
89 mutable std::list<std::pair<std::shared_ptr<api::ExecutionContext>, ProgressHandler>> _handlers;
91 std::string _lastStep;
98 #endif //LEDGER_CORE_PROGRESSNOTIFIER_H Definition: Deffered.hpp:49
Definition: Deffered.hpp:52
Definition: Account.cpp:8
Definition: ProgressNotifier.h:49
Definition: Exception.hpp:45