Created
February 28, 2011 12:38
-
-
Save hiroshi/847260 to your computer and use it in GitHub Desktop.
AutoRef.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _AutoRef_h_ | |
#define _AutoRef_h_ | |
/* Example | |
#include "AutoRef.h" | |
void AutoRefRelease(CGImageRef ref) { CGImageRelease(ref); } | |
void someFunction(void) { | |
AutoRef<CGImage> image = CGImageCreateCopy(src); | |
// do something... | |
// you don't need to call | |
// CGImageRelease(image); | |
} | |
*/ | |
template<typename T> void AutoRefRelease(T *ref); | |
template<typename T> class AutoRef { | |
public: | |
AutoRef(T *ref = NULL) : m_ref(ref) {} | |
~AutoRef() { | |
if (m_ref) AutoRefRelease(m_ref); | |
m_ref = NULL; | |
} | |
operator T*() const { return m_ref; } | |
AutoRef& operator=(T *ref) { | |
if (m_ref) AutoRefRelease(m_ref); | |
m_ref = ref; | |
return *this; | |
} | |
private: | |
T *m_ref; | |
}; | |
#endif //_AutoRef_h_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment