Guest User

Chris M Thomasson

a guest
Jun 7th, 2009
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. /* Interfaces
  2. ____________________________________________________________________*/
  3. #include <stddef.h>
  4.  
  5.  
  6. struct object_prv_vtable {
  7.   int (*fp_destroy) (void* const);
  8. };
  9.  
  10.  
  11. struct device_prv_vtable {
  12.   int (*fp_read) (void* const, void*, size_t);
  13.   int (*fp_write) (void* const, void const*, size_t);
  14. };
  15.  
  16.  
  17. struct device_vtable {
  18.   struct object_prv_vtable const object;
  19.   struct device_prv_vtable const device;
  20. };
  21.  
  22.  
  23. struct device {
  24.   struct device_vtable const* vtable;
  25. };
  26.  
  27.  
  28. #define object_destroy(mp_self) ( \
  29.   (mp_self)->vtable->object.fp_destroy((mp_self)) \
  30. )
  31.  
  32.  
  33. #define device_read(mp_self, mp_buf, mp_size) ( \
  34.   (mp_self)->vtable->device.fp_read((mp_self), (mp_buf), (mp_size)) \
  35. )
  36.  
  37.  
  38. #define device_write(mp_self, mp_buf, mp_size) ( \
  39.   (mp_self)->vtable->device.fp_write((mp_self), (mp_buf), (mp_size)) \
  40. )
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /* Sample Header (usb_drive.h)
  48. ____________________________________________________________________*/
  49. #if ! defined(USB_HEADER_H)
  50. #define USB_HEADER_H
  51.  
  52.  
  53. extern int usb_drive_create(struct device** const);
  54.  
  55.  
  56. #endif
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. /* Sample Impl (usb_drive.c)
  65. ____________________________________________________________________*/
  66. /* #include "usb_drive.c" */
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69.  
  70.  
  71. struct usb_drive {
  72.   struct device device;
  73.   /* whatever */
  74. };
  75.  
  76.  
  77. static int usb_drive_object_destroy(void* const);
  78. static int usb_drive_device_read(void* const, void*, size_t);
  79. static int usb_drive_device_write(void* const, void const*, size_t);
  80.  
  81.  
  82. static struct device_vtable const g_table = {
  83.   { /* object */
  84.     usb_drive_object_destroy
  85.   },
  86.  
  87.   { /* device */
  88.     usb_drive_device_read,
  89.     usb_drive_device_write
  90.   }
  91. };
  92.  
  93.  
  94. int usb_drive_create(
  95.  struct device** const pself
  96. ) {
  97.   struct usb_drive* const self = malloc(sizeof(*self));
  98.   if (self) {
  99.     self->device.vtable = &g_table;
  100.     *pself = &self->device;
  101.     return 0;
  102.   }
  103.   return -1;
  104. }
  105.  
  106.  
  107. int usb_drive_object_destroy(
  108.  void* const self_
  109. ) {
  110.   struct usb_drive* const self = self_;
  111.   printf("usb_drive_object_destroy(%p)\n", (void*)self);
  112.   free(self_);
  113.   return 0;
  114. }
  115.  
  116.  
  117. int usb_drive_device_read(
  118.  void* const self_,
  119.  void* buf,
  120.  size_t size
  121. ) {
  122.   struct usb_drive* const self = self_;
  123.   printf("usb_drive_device_read(%p, %p, %lu)\n",
  124.     (void*)self, buf, (unsigned long)size);
  125.   return 0;
  126. }
  127.  
  128.  
  129. int usb_drive_device_write(
  130.  void* const self_,
  131.  void const* buf,
  132.  size_t size
  133. ) {
  134.   struct usb_drive* const self = self_;
  135.   printf("usb_drive_device_write(%p, %p, %lu)\n",
  136.     (void*)self, buf, (unsigned long)size);
  137.   return 0;
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. /* Sample Application
  147. ____________________________________________________________________*/
  148. void read_write(
  149.  struct device* const self
  150. ) {
  151.   char buf[100];
  152.  
  153.   device_read(self, buf, 50);
  154.  
  155.   device_write(self, buf, 5);
  156. }
  157.  
  158.  
  159. int main(void) {
  160.   struct device* a_device;
  161.  
  162.   if (! usb_drive_create(&a_device)) {
  163.     read_write(a_device);
  164.    
  165.     object_destroy(a_device);
  166.   }
  167.  
  168.   return 0;
  169. }
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment