/tags/rel-1-3-18/SWIG/Source/DOH/void.c

# · C · 97 lines · 53 code · 12 blank · 32 comment · 1 complexity · 2b1dddc8e3c4f65fa10345aa136ca7d8 MD5 · raw file

  1. /* -----------------------------------------------------------------------------
  2. * void.c
  3. *
  4. * Implements a "void" object that is really just a DOH container around
  5. * an arbitrary C object represented as a void *.
  6. *
  7. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  8. *
  9. * Copyright (C) 1999-2000. The University of Chicago
  10. * See the file LICENSE for information on usage and redistribution.
  11. * ----------------------------------------------------------------------------- */
  12. char cvsroot_void_c[] = "$Header$";
  13. #include "dohint.h"
  14. typedef struct {
  15. void *ptr;
  16. void (*del)(void *);
  17. } VoidObj;
  18. /* -----------------------------------------------------------------------------
  19. * Void_delete()
  20. *
  21. * Delete a void object. Invokes the destructor supplied at the time of creation.
  22. * ----------------------------------------------------------------------------- */
  23. static void
  24. Void_delete(DOH *vo) {
  25. VoidObj *v = (VoidObj *) ObjData(vo);
  26. if (v->del)
  27. (*v->del)(v->ptr);
  28. DohFree(v);
  29. }
  30. /* -----------------------------------------------------------------------------
  31. * Void_copy()
  32. *
  33. * Copies a void object. This is only a shallow copy. The object destruction
  34. * function is not copied in order to avoid potential double-free problems.
  35. * ----------------------------------------------------------------------------- */
  36. static DOH *
  37. Void_copy(DOH *vo) {
  38. VoidObj *v = (VoidObj *) ObjData(vo);
  39. return NewVoid(v->ptr,0);
  40. }
  41. /* -----------------------------------------------------------------------------
  42. * Void_data()
  43. *
  44. * Returns the void * stored in the object.
  45. * ----------------------------------------------------------------------------- */
  46. static void *
  47. Void_data(DOH *vo) {
  48. VoidObj *v = (VoidObj *) ObjData(vo);
  49. return v->ptr;
  50. }
  51. static DohObjInfo DohVoidType = {
  52. "VoidObj", /* objname */
  53. Void_delete, /* doh_del */
  54. Void_copy, /* doh_copy */
  55. 0, /* doh_clear */
  56. 0, /* doh_str */
  57. Void_data, /* doh_data */
  58. 0, /* doh_dump */
  59. 0, /* doh_len */
  60. 0, /* doh_hash */
  61. 0, /* doh_cmp */
  62. 0, /* doh_setfile */
  63. 0, /* doh_getfile */
  64. 0, /* doh_setline */
  65. 0, /* doh_getline */
  66. 0, /* doh_mapping */
  67. 0, /* doh_sequence */
  68. 0, /* doh_file */
  69. 0, /* doh_string */
  70. 0, /* doh_reserved */
  71. 0, /* clientdata */
  72. };
  73. /* -----------------------------------------------------------------------------
  74. * NewVoid()
  75. *
  76. * Creates a new Void object given a void * and an optional destructor function.
  77. * ----------------------------------------------------------------------------- */
  78. DOH *
  79. DohNewVoid(void *obj, void (*del)(void *)) {
  80. VoidObj *v;
  81. v = (VoidObj *) DohMalloc(sizeof(VoidObj));
  82. v->ptr = obj;
  83. v->del = del;
  84. return DohObjMalloc(&DohVoidType,v);
  85. }