summaryrefslogtreecommitdiff
path: root/platform/darwin/src/CFHandle.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/darwin/src/CFHandle.hpp')
-rw-r--r--platform/darwin/src/CFHandle.hpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/platform/darwin/src/CFHandle.hpp b/platform/darwin/src/CFHandle.hpp
new file mode 100644
index 0000000000..edcc9aafdf
--- /dev/null
+++ b/platform/darwin/src/CFHandle.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+/*
+ CFHandle is a minimal wrapper designed to hold and release CoreFoundation-style handles
+ It is non-transferrable: wrap it in something like a unique_ptr if you need to pass it around,
+ or just use unique_ptr with a custom deleter.
+ CFHandle has no special treatment for null handles -- be careful not to let it hold a null
+ handle if the behavior of the Releaser isn't defined for null.
+
+ ex:
+ using CFDataHandle = CFHandle<CFDataRef, CFTypeRef, CFRelease>;
+
+ CFDataHandle data(CFDataCreateWithBytesNoCopy(
+ kCFAllocatorDefault, reinterpret_cast<const unsigned char*>(source.data()), source.size(),
+ kCFAllocatorNull));
+*/
+
+namespace {
+
+template <typename HandleType, typename ReleaserArgumentType, void (*Releaser)(ReleaserArgumentType)>
+struct CFHandle {
+ CFHandle(HandleType handle_): handle(handle_) {}
+ ~CFHandle() { Releaser(handle); }
+ HandleType operator*() { return handle; }
+ operator bool() { return handle; }
+private:
+ HandleType handle;
+};
+
+} // namespace
+