ScopedTemporaryValue.h Source File
Back to the index.
src
include
ScopedTemporaryValue.h
Go to the documentation of this file.
1
#ifndef SCOPEDTEMPORARYVALUE_H
2
#define SCOPEDTEMPORARYVALUE_H
3
4
/*
5
* Copyright (C) 2009-2010 Anders Gavare. All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
*
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
* 3. The name of the author may not be used to endorse or promote products
16
* derived from this software without specific prior written permission.
17
*
18
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
* SUCH DAMAGE.
29
*/
30
31
/**
32
* \brief Scoped temporary variable template.
33
*
34
* Usage:<pre>
35
* SomeType var = A;
36
* {
37
* ScopedTemporaryValue<SomeType> holder(var);
38
* var = B;
39
* ...
40
* }
41
* // var will here be reset to A.
42
* </pre>
43
* or
44
* <pre>
45
* SomeType var = A;
46
* {
47
* ScopedTemporaryValue<SomeType> holder(var, B);
48
* // Here, var is B.
49
* ...
50
* }
51
* // var will here be reset to A.
52
* </pre>
53
*
54
* Implementation note: This is just a schoolbook-style implementation
55
* of a class which holds a variable, and then restores the original value
56
* when going out of scope.
57
*/
58
template
<
class
T>
59
class
ScopedTemporaryValue
60
{
61
private
:
62
// Prevent construction without reference.
63
ScopedTemporaryValue
();
64
65
public
:
66
/**
67
* \brief Constructor, which reads the old value from T, but
68
* does not change it.
69
*
70
* @param var The variable.
71
*/
72
ScopedTemporaryValue
(T& var)
73
: m_var(var)
74
{
75
m_origValue = m_var;
76
}
77
78
/**
79
* \brief Constructor, which reads the old value from T, and sets
80
* it to a new (temporary) value.
81
*
82
* @param var The variable.
83
* @param newValue The new (temporary) value.
84
*/
85
ScopedTemporaryValue
(T& var, T newValue)
86
: m_var(var)
87
{
88
m_origValue = m_var;
89
m_var = newValue;
90
}
91
92
/**
93
* \brief Destructor, which restores the original value.
94
*/
95
~ScopedTemporaryValue
()
96
{
97
m_var = m_origValue;
98
}
99
100
private
:
101
T& m_var;
102
T m_origValue;
103
};
104
105
106
#endif // SCOPEDTEMPORARYVALUE_H
ScopedTemporaryValue::ScopedTemporaryValue
ScopedTemporaryValue(T &var, T newValue)
Constructor, which reads the old value from T, and sets it to a new (temporary) value.
Definition:
ScopedTemporaryValue.h:85
ScopedTemporaryValue::ScopedTemporaryValue
ScopedTemporaryValue(T &var)
Constructor, which reads the old value from T, but does not change it.
Definition:
ScopedTemporaryValue.h:72
ScopedTemporaryValue
Scoped temporary variable template.
Definition:
ScopedTemporaryValue.h:59
ScopedTemporaryValue::~ScopedTemporaryValue
~ScopedTemporaryValue()
Destructor, which restores the original value.
Definition:
ScopedTemporaryValue.h:95
Generated on Tue Mar 24 2020 14:04:48 for GXemul by
1.8.17