1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#ifndef COMPONENTS_LOADINGLISTENER_ASYNCLISTENER_H
#define COMPONENTS_LOADINGLISTENER_ASYNCLISTENER_H
#include <mutex>
#include <optional>
#include <string>
#include "loadinglistener.hpp"
namespace Loading
{
class AsyncListener : public Listener
{
public:
AsyncListener(Listener& baseListener)
: mBaseListener(baseListener)
{
}
void setLabel(const std::string& label, bool important) override
{
std::lock_guard<std::mutex> guard(mMutex);
mLabelUpdate = label;
mImportantLabel = important;
}
void setProgressRange(size_t range) override
{
std::lock_guard<std::mutex> guard(mMutex);
mRangeUpdate = range;
}
void setProgress(size_t value) override
{
std::lock_guard<std::mutex> guard(mMutex);
mProgressUpdate = value;
}
void increaseProgress(size_t increase) override
{ /* not implemented */
}
void update()
{
std::lock_guard<std::mutex> guard(mMutex);
if (mLabelUpdate)
mBaseListener.setLabel(*mLabelUpdate, mImportantLabel);
if (mRangeUpdate)
mBaseListener.setProgressRange(*mRangeUpdate);
if (mProgressUpdate)
mBaseListener.setProgress(*mProgressUpdate);
mLabelUpdate = std::nullopt;
mRangeUpdate = std::nullopt;
mProgressUpdate = std::nullopt;
}
private:
Listener& mBaseListener;
std::mutex mMutex;
std::optional<std::string> mLabelUpdate;
bool mImportantLabel = false;
std::optional<size_t> mRangeUpdate;
std::optional<size_t> mProgressUpdate;
};
}
#endif // COMPONENTS_LOADINGLISTENER_ASYNCLISTENER_H
|