J
Jef Driesen
I'm writing a library (to communicate with a number of devices over a
serial port) and have some questions about the design. I have now a
header and source file like this:
/* device.h */
typedef struct device device;
int device_open (device **dev, const char *name);
int device_close (device *dev);
int device_read (device *dev, void *data, unsigned int size);
int device_write (device *dev, const void *data, unsigned int size);
/* device.c */
struct device {
/* some data here */
};
int
device_open (device **dev, const char *name)
{
if (dev == NULL)
return -1;
struct device *out = malloc (sizeof (struct device));
if (out == NULL)
return -1;
/* Do some stuff here */
*dev = out;
return 0;
}
As you can see, I made the internal data structure opaque by means of
the typedef and pointers to an incomplete data structure. Now my
questions are:
1. Is there any advantage to change the typedef to
typedef struct device *device;
and thus hiding the fact that the "device" type is actually a pointer.
2. I want to add support for a second type of device, which happens to
be very similar to the first one. For instance the read/write functions
are different, but the open/close functions and the contents of the
struct itself are the same. How can I implement this without copying the
code to a new pair of header and source files?
I was thinking to move the shared code to a common header and source
file and make the device-specific functions call the common functions.
But what should I do with the typdefs below, because both structs would
be the same in my case. But I would like to hide that from the user of
the library.
/* common.h (for internal use only) */
struct device {
/* some data here */
};
int device_open (device **dev, const char *name);
/* device1.h (public) */
typedef struct device1 device1;
int device1_open (device1 **dev, const char *name);
....
/* device2.h (public) */
typedef struct device2 device2;
int device1_open (device1 **dev, const char *name);
....
serial port) and have some questions about the design. I have now a
header and source file like this:
/* device.h */
typedef struct device device;
int device_open (device **dev, const char *name);
int device_close (device *dev);
int device_read (device *dev, void *data, unsigned int size);
int device_write (device *dev, const void *data, unsigned int size);
/* device.c */
struct device {
/* some data here */
};
int
device_open (device **dev, const char *name)
{
if (dev == NULL)
return -1;
struct device *out = malloc (sizeof (struct device));
if (out == NULL)
return -1;
/* Do some stuff here */
*dev = out;
return 0;
}
As you can see, I made the internal data structure opaque by means of
the typedef and pointers to an incomplete data structure. Now my
questions are:
1. Is there any advantage to change the typedef to
typedef struct device *device;
and thus hiding the fact that the "device" type is actually a pointer.
2. I want to add support for a second type of device, which happens to
be very similar to the first one. For instance the read/write functions
are different, but the open/close functions and the contents of the
struct itself are the same. How can I implement this without copying the
code to a new pair of header and source files?
I was thinking to move the shared code to a common header and source
file and make the device-specific functions call the common functions.
But what should I do with the typdefs below, because both structs would
be the same in my case. But I would like to hide that from the user of
the library.
/* common.h (for internal use only) */
struct device {
/* some data here */
};
int device_open (device **dev, const char *name);
/* device1.h (public) */
typedef struct device1 device1;
int device1_open (device1 **dev, const char *name);
....
/* device2.h (public) */
typedef struct device2 device2;
int device1_open (device1 **dev, const char *name);
....