VariableGuard.h

Go to the documentation of this file.
00001 //**************************************************************************/
00002 // Copyright (c) 1998-2007 Autodesk, Inc.
00003 // All rights reserved.
00004 // 
00005 // These coded instructions, statements, and computer programs contain
00006 // unpublished proprietary information written by Autodesk, Inc., and are
00007 // protected by Federal copyright law. They may not be disclosed to third
00008 // parties or copied or duplicated in any form, in whole or in part, without
00009 // the prior written consent of Autodesk, Inc.
00010 //**************************************************************************/
00011 // AUTHOR: Nicolas Desjardins
00012 // DATE: 2007-05-24
00013 //***************************************************************************/
00014 
00015 #pragma once
00016 
00017 #include "noncopyable.h"
00018 
00019 namespace MaxSDK
00020 {
00021 
00039     template<typename T> class VariableGuard : MaxSDK::Util::Noncopyable
00040     {
00041     public:
00042 
00047         VariableGuard(T& variable) :
00048                 mVariable(variable),
00049                     mOriginalValue(variable)
00050                 { }
00051 
00058                 VariableGuard(T& variable, T value) :
00059                 mVariable(variable),
00060                     mOriginalValue(variable)
00061                 {
00062                     SetCurrentValue(value);
00063                 }
00064 
00067                 ~VariableGuard()
00068                 {
00069                     mVariable = mOriginalValue;
00070                 }
00071 
00074                 T GetCurrentValue() const
00075                 {
00076                     return mVariable;
00077                 }
00078 
00085                 void SetCurrentValue(T value)
00086                 {
00087                     mVariable = value;
00088                 }
00089 
00090     private:
00091         T& mVariable;
00092         T mOriginalValue;
00093     };
00094 
00095 }
00096