Created
April 19, 2014 09:51
-
-
Save superwills/11079606 to your computer and use it in GitHub Desktop.
CCFSet and CCFArray template wrappers
This file contains hidden or 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
template <typename T> | |
struct CCFArray | |
{ | |
CFArrayRef array; | |
CCFArray() | |
{ | |
array = CFArrayCreate( kCFAllocatorDefault, 0, 0, 0 ); | |
} | |
CCFArray(CFArrayRef cf):array(cf) | |
{ | |
} | |
int size() | |
{ | |
return (int)CFArrayGetCount( array ); | |
} | |
T operator[]( int i ) | |
{ | |
return (T)CFArrayGetValueAtIndex( array, i ); | |
} | |
~CCFArray() | |
{ | |
CFRelease( array ); | |
} | |
}; | |
template <typename T> | |
struct CCFSet | |
{ | |
CFSetRef set; | |
CCFSet() | |
{ | |
set = CFSetCreate( kCFAllocatorDefault, 0, 0, 0 ); | |
} | |
CCFSet(CFSetRef cf):set(cf) | |
{ | |
} | |
int size() | |
{ | |
return CFSetGetCount( set ); | |
} | |
vector<T> toVector() | |
{ | |
vector<T> v; | |
v.resize( CFSetGetCount(set) ); | |
CFSetGetValues(set, (const void**)&v[0]); | |
return v; | |
} | |
~CCFSet() | |
{ | |
CFRelease( set ); | |
} | |
}; | |
// Set usage example, before: Copying a CFSet to a std::vector for iteration: | |
vector<IOHIDDeviceRef> devices; | |
if( CFSetRef cfDeviceSet = IOHIDManagerCopyDevices(hid) ) | |
{ | |
devices.resize( CFSetGetCount(cfDeviceSet) ); | |
CFSetGetValues(cfDeviceSet, (const void**)&devices[0]); | |
CFRelease(cfDeviceSet); | |
} | |
// After, with CCFSet: (note automatic memory management) | |
vector<IOHIDDeviceRef> devices; | |
CCFSet<IOHIDDeviceRef> devSet = IOHIDManagerCopyDevices(hid); | |
if( devSet.set ) | |
{ | |
devices = devSet.toVector(); | |
} | |
// CCFArray: iteration, before: | |
CFArrayRef elts = IOHIDDeviceCopyMatchingElements(devices[i], NULL, kIOHIDOptionsTypeNone); | |
if( elts ) | |
{ | |
// iterate over all the elements | |
CFIndex numElts = CFArrayGetCount( elts ); | |
for( int i = 0; i < numElts; i++ ) | |
{ | |
IOHIDElementRef elt = (IOHIDElementRef)CFArrayGetValueAtIndex( elts, i ); | |
if( elt ) | |
HIDDumpElementInfo(elt); | |
} | |
CFRelease( elts ); | |
} | |
// After | |
CCFArray<IOHIDElementRef> elts = IOHIDDeviceCopyMatchingElements(devices[i], NULL, kIOHIDOptionsTypeNone); | |
if( elts.array ) | |
{ | |
// iterate over all the elements | |
for( int i = 0; i < elts.size(); i++ ) | |
{ | |
IOHIDElementRef elt = elts[i]; | |
if( elt ) | |
HIDDumpElementInfo(elt); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment