|
// temp.h : header file for CTemp class
// Created by: Alvaro Mendez - 10/20/1999
#ifndef __TEMP_H__
#define __TEMP_H__
///////////////////////////////////////////////////////////////////////////////
// Variable-value saver class
// Class CTemp has a template constructor which allows you to save a
// variable's current value (on construction) so that it can be restored
// at a later time (on destruction).
//
// Sample usage: CTemp v = variable_whose_value_you_are_saving;
//
class CTemp
{
public:
// Base class for template class below.
class SaverBase
{
public:
virtual ~SaverBase() { }
virtual void Restore() = 0;
};
// Template class which saves the value in construction
// and restores it on destruction.
template <class T>
class Saver : public SaverBase
{
public:
Saver(T& valueToSave) :
m_ref(valueToSave),
m_val(valueToSave)
{ }
virtual ~Saver()
{
Restore();
}
virtual void Restore()
{
m_ref = m_val;
}
private:
T& m_ref;
T m_val;
};
public:
template <class T>
CTemp(T& valueToSave) :
m_pVar(new Saver<T>(valueToSave))
{ }
~CTemp()
{
Restore();
delete m_pVar;
}
void Restore()
{
m_pVar->Restore();
}
private:
// Prevent accidental self copying
CTemp(const CTemp&);
CTemp& operator=(const CTemp&);
protected:
SaverBase* m_pVar;
};
#endif // __TEMP_H__
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.