summaryrefslogtreecommitdiff
path: root/indra/llcommon/tempset.h
blob: 07e607a576d49fec163355a20a3f2717dba167b8 (plain)
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
/**
 * @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;
    VAR mOldValue;
};

#endif /* ! defined(LL_TEMPSET_H) */