Call an IOUSBDeviceInterface function on an obj-c object instead of a C structure
-
20-06-2021 - |
Pregunta
Let's say I want to close an USB device. Here is a C structure representing the USB device:
struct __USBDevice {
uint16_t idProduct;
io_service_t usbService;
IOUSBDeviceInterface **deviceHandle;
IOUSBInterfaceInterface **interfaceHandle;
Boolean open;
};
typedef struct __USBDevice *USBDeviceRef;
Here is the code to close the device:
// device is a USBDeviceRef structure
// USBDeviceClose is a function member of IOUSBDeviceInterface C Pseudoclass
(*device->deviceHandle)->USBDeviceClose(device->deviceHandle);
Now imagine that the device properties are declared in an obj-c class
@interface Device : NSObject {
NSNumber idProduct
io_service_t usbService;
IOUSBDeviceInterface **deviceHandle;
IOUSBInterfaceInterface **interfaceHandle;
BOOL open;
}
@end
How would I do to call USBDeviceClose() ?
Solución
There are two ways. You can either model your class similar to a struct, and add @public
above your declarations (that way the syntax won't change) or you can add a Close
method to your interface which will do the same logic internally (but without the need to dereference device
of course).
Otros consejos
No need to be redundant. Ivars can be structs.
@interface Device : NSObject {
USBDeviceRef deviceRef;
}
@end
#implementation Device
- (void) close {
USBDeviceClose(deviceRef->deviceHandle);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow