CLocalGuard
#ifndef LocalGuard_H
#define LocalGuard_H
template <class Type, class Construct, class Destruct>
class CLocalGuard
{
public:
CLocalGuard(Type *pObject)
: m_pObject(pObject)
{
if (m_pObject) {
Construct construct;
construct(m_pObject);
}
}
~CLocalGuard()
{
if (m_pObject)
{
Destruct destruct;
destruct(m_pObject);
}
}
private:
Type *m_pObject;
};
template <class Type>
struct CreateInt
{
void operator()(Type *&pValue)
{
pValue = new Type();
}
};
template <class Type>
struct DeleteInt
{
void operator()(Type *&pValue)
{
delete pValue;
pValue = 0;
}
};
int main()
{
int *pInt = 0;
CLocalGuard<int, CreateInt<int>, DeleteInt<int> > localGuard(pInt);
return 0;
}
#endif