Small RAII-like cleanup class in C++/CLI
-
In native code, we often use helper classes to save a reference to a member, set its value and reset it in destruction. standard stuff. Unfortunately, I haven't found a good way in a ref class to do the same and I'm hoping for some tips here. Since I cannot hold a reference to a member of a managed object as a member of another object, I tried using a tracking reference. Unfortunately, that results in an similar error:
error C3160: 'System::Boolean ^%': a data member of a managed class cannot have this type
note: an interior reference can never be allocated on the gc heapAn example I hope to get working...
ref class Foo { bool m_guard; . . . }
ref class PushBool sealed {
bool ^% m_tref;
public:
PushBool(bool ^% b, bool newVal) : m_tref(b), m_oldval(b) { b = newVal; }
~PushBool() { m_tref = m_oldval; }
};usage...
void Foo::work() {
if (m_guard) return;
PushBool no_reentry(m_guard, true);
...
// Resets m_flag to original value here.
}Do any of the C++/CLI devs here have advice on handling this? I can probably concoct a way to make it work but was hoping for a reusable utility class. John