api-foreign-objects.texi 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2013, 2014
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Foreign Objects
  7. @section Foreign Objects
  8. @cindex foreign object
  9. This chapter contains reference information related to defining and
  10. working with foreign objects. @xref{Defining New Foreign Object Types},
  11. for a tutorial-like introduction to foreign objects.
  12. @deftp {C Type} scm_t_struct_finalize
  13. This function type returns @code{void} and takes one @code{SCM}
  14. argument.
  15. @end deftp
  16. @deftypefn {C Function} SCM scm_make_foreign_object_type (SCM name, SCM slots, scm_t_struct_finalize finalizer)
  17. Create a fresh foreign object type. @var{name} is a symbol naming the
  18. type. @var{slots} is a list of symbols, each one naming a field in the
  19. foreign object type. @var{finalizer} indicates the finalizer, and may
  20. be @code{NULL}.
  21. @end deftypefn
  22. @cindex finalizer
  23. @cindex finalization
  24. We recommend that finalizers be avoided if possible. @xref{Foreign
  25. Object Memory Management}. Finalizers must be async-safe and
  26. thread-safe. Again, @pxref{Foreign Object Memory Management}. If you
  27. are embedding Guile in an application that is not thread-safe, and you
  28. define foreign object types that need finalization, you might want to
  29. disable automatic finalization, and arrange to call
  30. @code{scm_manually_run_finalizers ()} yourself.
  31. @deftypefn {C Function} int scm_set_automatic_finalization_enabled (int enabled_p)
  32. Enable or disable automatic finalization. By default, Guile arranges to
  33. invoke object finalizers automatically, in a separate thread if
  34. possible. Passing a zero value for @var{enabled_p} will disable
  35. automatic finalization for Guile as a whole. If you disable automatic
  36. finalization, you will have to call @code{scm_run_finalizers ()}
  37. periodically.
  38. Unlike most other Guile functions, you can call
  39. @code{scm_set_automatic_finalization_enabled} before Guile has been
  40. initialized.
  41. Return the previous status of automatic finalization.
  42. @end deftypefn
  43. @deftypefn {C Function} int scm_run_finalizers (void)
  44. Invoke any pending finalizers. Returns the number of finalizers that
  45. were invoked. This function should be called when automatic
  46. finalization is disabled, though it may be called if it is enabled as
  47. well.
  48. @end deftypefn
  49. @deftypefn {C Function} void scm_assert_foreign_object_type (SCM type, SCM val)
  50. When @var{val} is a foreign object of the given @var{type}, do nothing.
  51. Otherwise, signal an error.
  52. @end deftypefn
  53. @deftypefn {C Function} SCM scm_make_foreign_object_0 (SCM type)
  54. @deftypefnx {C Function} SCM scm_make_foreign_object_1 (SCM type, void *val0)
  55. @deftypefnx {C Function} SCM scm_make_foreign_object_2 (SCM type, void *val0, void *val1)
  56. @deftypefnx {C Function} SCM scm_make_foreign_object_3 (SCM type, void *val0, void *val1, void *val2)
  57. @deftypefnx {C Function} SCM scm_make_foreign_object_n (SCM type, size_t n, void *vals[])
  58. Make a new foreign object of the type with type @var{type} and
  59. initialize the first @var{n} fields to the given values, as appropriate.
  60. The number of fields for objects of a given type is fixed when the type
  61. is created. It is an error to give more initializers than there are
  62. fields in the value. It is perfectly fine to give fewer initializers
  63. than needed; this is convenient when some fields are of non-pointer
  64. types, and would be easier to initialize with the setters described
  65. below.
  66. @end deftypefn
  67. @deftypefn {C Function} void* scm_foreign_object_ref (SCM obj, size_t n);
  68. @deftypefnx {C Function} scm_t_bits scm_foreign_object_unsigned_ref (SCM obj, size_t n);
  69. @deftypefnx {C Function} scm_t_signed_bits scm_foreign_object_signed_ref (SCM obj, size_t n);
  70. Return the value of the @var{n}th field of the foreign object @var{obj}.
  71. The backing store for the fields is as wide as a @code{scm_t_bits}
  72. value, which is at least as wide as a pointer. The different variants
  73. handle casting in a portable way.
  74. @end deftypefn
  75. @deftypefn {C Function} void scm_foreign_object_set_x (SCM obj, size_t n, void *val);
  76. @deftypefnx {C Function} void scm_foreign_object_unsigned_set_x (SCM obj, size_t n, scm_t_bits val);
  77. @deftypefnx {C Function} void scm_foreign_object_signed_set_x (SCM obj, size_t n, scm_t_signed_bits val);
  78. Set the value of the @var{n}th field of the foreign object @var{obj} to
  79. @var{val}, after portably converting to a @code{scm_t_bits} value, if
  80. needed.
  81. @end deftypefn
  82. One can also access foreign objects from Scheme. @xref{Foreign Objects
  83. and Scheme}, for some examples.
  84. @example
  85. (use-modules (system foreign-object))
  86. @end example
  87. @deffn {Scheme Procedure} make-foreign-object-type name slots [#:finalizer=#f] [#:supers='()]
  88. Make a new foreign object type. See the above documentation for
  89. @code{scm_make_foreign_object_type}; these functions are exactly
  90. equivalent, except for the way in which the finalizer gets attached to
  91. instances (an internal detail), and the fact that this function accepts
  92. an optional list of superclasses, which will be paseed to
  93. @code{make-class}.
  94. The resulting value is a GOOPS class. @xref{GOOPS}, for more on classes
  95. in Guile.
  96. @end deffn
  97. @deffn {Scheme Syntax} define-foreign-object-type name constructor (slot ...) [#:finalizer=#f]
  98. A convenience macro to define a type, using
  99. @code{make-foreign-object-type}, and bind it to @var{name}. A
  100. constructor will be bound to @var{constructor}, and getters will be
  101. bound to each of @var{slot...}.
  102. @end deffn
  103. @c Local Variables:
  104. @c TeX-master: "guile.texi"
  105. @c End: