summaryrefslogtreecommitdiff
path: root/indra/llcommon/tempset.h
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2024-06-12 16:42:39 -0400
committerNat Goodspeed <nat@lindenlab.com>2024-06-12 16:42:39 -0400
commitffdcf33364ebfdc1829cb7c7eea6ae4c908d12f1 (patch)
treeae2728619e2a6420d744f1daa36e1ed6929ef351 /indra/llcommon/tempset.h
parent77ac5e44a6b57e2b33dc3026c7a3bf2aa73d2df3 (diff)
Extract TempSet from llcallbacklist.cpp into its own tempset.h.
Diffstat (limited to 'indra/llcommon/tempset.h')
-rwxr-xr-xindra/llcommon/tempset.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/indra/llcommon/tempset.h b/indra/llcommon/tempset.h
new file mode 100755
index 0000000000..e1496bd5fc
--- /dev/null
+++ b/indra/llcommon/tempset.h
@@ -0,0 +1,41 @@
+/**
+ * @file tempset.h
+ * @author Nat Goodspeed
+ * @date 2024-06-12
+ * @brief Temporarily override a variable for scope duration, then restore
+ *
+ * $LicenseInfo:firstyear=2024&license=viewerlgpl$
+ * Copyright (c) 2024, Linden Research, Inc.
+ * $/LicenseInfo$
+ */
+
+#if ! defined(LL_TEMPSET_H)
+#define LL_TEMPSET_H
+
+// RAII class to set specified variable to specified value
+// only for the duration of containing scope
+template <typename VAR, typename VALUE>
+class TempSet
+{
+public:
+ TempSet(VAR& var, const VALUE& value):
+ mVar(var),
+ mOldValue(mVar)
+ {
+ mVar = value;
+ }
+
+ TempSet(const TempSet&) = delete;
+ TempSet& operator=(const TempSet&) = delete;
+
+ ~TempSet()
+ {
+ mVar = mOldValue;
+ }
+
+private:
+ VAR& mVar;
+ VALUE mOldValue;
+};
+
+#endif /* ! defined(LL_TEMPSET_H) */