summaryrefslogtreecommitdiff
path: root/platform/darwin/src/CFHandle.hpp
blob: edcc9aafdffef8c77d71df37570e16b6a420006e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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