b1onic
b1onic

Reputation: 239

Call an IOUSBDeviceInterface function on an obj-c object instead of a C structure

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() ?

Upvotes: 0

Views: 660

Answers (2)

NSResponder
NSResponder

Reputation: 16861

No need to be redundant. Ivars can be structs.

@interface Device : NSObject {

USBDeviceRef deviceRef;
}

@end

#implementation Device

- (void) close {
USBDeviceClose(deviceRef->deviceHandle);
}

Upvotes: 0

borrrden
borrrden

Reputation: 33421

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).

Upvotes: 1

Related Questions