From 8125d51a0f804b77197142c986aacbdb8dce7752 Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 6 Aug 2022 16:55:09 +0200 Subject: [PATCH] Use std::unique_ptr to implement ScopedLoad This gives correct implementation of move constructor and assignment operators. --- .../loadinglistener/loadinglistener.hpp | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/components/loadinglistener/loadinglistener.hpp b/components/loadinglistener/loadinglistener.hpp index 14a1b96f9a..e61f3837f1 100644 --- a/components/loadinglistener/loadinglistener.hpp +++ b/components/loadinglistener/loadinglistener.hpp @@ -2,6 +2,7 @@ #define COMPONENTS_LOADINGLISTENER_H #include +#include namespace Loading { @@ -33,12 +34,26 @@ namespace Loading virtual ~Listener() = default; }; + struct LoadingOff + { + void operator()(Listener* listener) const + { + if (listener != nullptr) + listener->loadingOff(); + } + }; + /// @brief Used for stopping a loading sequence when the object goes out of scope struct ScopedLoad { - ScopedLoad(Listener* l) : mListener(l) { mListener->loadingOn(); } - ~ScopedLoad() { mListener->loadingOff(); } - Listener* mListener; + std::unique_ptr mListener; + + explicit ScopedLoad(Listener* listener) + : mListener(listener) + { + if (mListener != nullptr) + mListener->loadingOn(); + } }; }