torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TemporaryAssignment.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the TemporaryAssignment class.
18 
19 #ifndef TORC_GENERIC_VERILOG_TEMPORARYASSIGNMENT_HPP
20 #define TORC_GENERIC_VERILOG_TEMPORARYASSIGNMENT_HPP
21 
22 namespace torc {
23 namespace generic {
24 
25  /// \brief Template class that stores the current value of a variable and restores that value
26  /// when this object goes out of scope.
27  /// \note This class inspired a StackOverflow question, and there may be more general ways to
28  /// solve the problem.
29  /// \see http://stackoverflow.com/questions/13383683/reverting-temporary-assignment-in-c
30  template <typename TYPE> class TemporaryAssignment {
31  protected:
32  // members
33  /// \brief Reference to the variable.
34  TYPE& mVariable;
35  /// \brief Original value of the variable
37  public:
38  // constructors
39  /// \brief Public constructor that does not assign a new value.
40  /// \param inVariable The variable to save and restore.
41  TemporaryAssignment(TYPE& inVariable)
42  : mVariable(inVariable), mOriginalValue(inVariable) {
43  }
44  /// \brief Public constructor that assigns a new value
45  /// \param inVariable The variable to save and restore.
46  /// \param inValue The new value to use.
47  TemporaryAssignment(TYPE& inVariable, TYPE inValue)
48  : mVariable(inVariable), mOriginalValue(inVariable) {
49  mVariable = inValue;
50  }
51  /// \brief Public destructor to restore the original value.
54  }
55  };
56 
57 } // namespace generic
58 } // namespace torc
59 
60 #endif // TORC_GENERIC_VERILOG_TEMPORARYASSIGNMENT_HPP
TYPE mOriginalValue
Original value of the variable.
TemporaryAssignment(TYPE &inVariable, TYPE inValue)
Public constructor that assigns a new value.
~TemporaryAssignment(void)
Public destructor to restore the original value.
TemporaryAssignment(TYPE &inVariable)
Public constructor that does not assign a new value.
Template class that stores the current value of a variable and restores that value when this object g...
TYPE & mVariable
Reference to the variable.