Codechange: [OSX] Use std::unique_ptr with a custom deleter to simply memory management of Core Foundation types.

This commit is contained in:
Michael Lutz
2019-09-01 15:27:39 +02:00
parent 58122983fb
commit d5a9bd404a
5 changed files with 80 additions and 114 deletions

View File

@@ -40,4 +40,17 @@ bool IsMonospaceFont(CFStringRef name);
void MacOSSetThreadName(const char *name);
/** Deleter that calls CFRelease rather than deleting the pointer. */
template <typename T> struct CFDeleter {
void operator()(T *p)
{
if (p) ::CFRelease(p);
}
};
/** Specialisation of std::unique_ptr for CoreFoundation objects. */
template <typename T>
using CFAutoRelease = std::unique_ptr<typename std::remove_pointer<T>::type, CFDeleter<typename std::remove_pointer<T>::type>>;
#endif /* MACOS_H */