From a4e7cfdb5757dbee90741be87f194c80f8c17da4 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Fri, 21 Feb 2025 00:35:11 +0100 Subject: [PATCH] libutil: make PushActivity movable while not copy-assignable it was still copy-constructible, which can lead to some very unfortunate interactions with closure captures. we will also need to move current-activity handles in upcoming changes. Change-Id: I63cede83b9790820c6bd8784fb5d47e5246b7c4a --- lix/libutil/logging.hh | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lix/libutil/logging.hh b/lix/libutil/logging.hh index 08c7c382d..20588c828 100644 --- a/lix/libutil/logging.hh +++ b/lix/libutil/logging.hh @@ -214,11 +214,34 @@ struct Activity friend class Logger; }; -struct PushActivity +class PushActivity { - const ActivityId prevAct; - PushActivity(ActivityId act) : prevAct(getCurActivity()) { setCurActivity(act); } - ~PushActivity() { setCurActivity(prevAct); } + std::optional prevAct; + +public: + PushActivity(ActivityId act) : prevAct(getCurActivity()) + { + setCurActivity(act); + } + + PushActivity(PushActivity && other) + { + std::swap(prevAct, other.prevAct); + } + + PushActivity & operator=(PushActivity && other) + { + auto tmp(std::move(other)); + std::swap(prevAct, tmp.prevAct); + return *this; + } + + ~PushActivity() + { + if (prevAct) { + setCurActivity(*prevAct); + } + } }; extern Logger * logger;