smob.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* classes: h_files */
  2. #ifndef SCM_SMOB_H
  3. #define SCM_SMOB_H
  4. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004, 2006 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libguile/__scm.h"
  21. #include "libguile/print.h"
  22. /* This is the internal representation of a smob type */
  23. typedef struct scm_smob_descriptor
  24. {
  25. char const *name;
  26. size_t size;
  27. SCM (*mark) (SCM);
  28. size_t (*free) (SCM);
  29. int (*print) (SCM exp, SCM port, scm_print_state *pstate);
  30. SCM (*equalp) (SCM, SCM);
  31. SCM (*apply) ();
  32. SCM (*apply_0) (SCM);
  33. SCM (*apply_1) (SCM, SCM);
  34. SCM (*apply_2) (SCM, SCM, SCM);
  35. SCM (*apply_3) (SCM, SCM, SCM, SCM);
  36. int gsubr_type; /* Used in procprop.c */
  37. } scm_smob_descriptor;
  38. #define SCM_NEWSMOB(z, tc, data) \
  39. do { \
  40. z = scm_cell ((tc), (scm_t_bits) (data)); \
  41. } while (0)
  42. #define SCM_RETURN_NEWSMOB(tc, data) \
  43. do { SCM __SCM_smob_answer; \
  44. SCM_NEWSMOB (__SCM_smob_answer, (tc), (data)); \
  45. return __SCM_smob_answer; \
  46. } while (0)
  47. #define SCM_NEWSMOB2(z, tc, data1, data2) \
  48. do { \
  49. z = scm_double_cell ((tc), (scm_t_bits)(data1), (scm_t_bits)(data2), 0); \
  50. } while (0)
  51. #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \
  52. do { SCM __SCM_smob_answer; \
  53. SCM_NEWSMOB2 (__SCM_smob_answer, (tc), (data1), (data2)); \
  54. return __SCM_smob_answer; \
  55. } while (0)
  56. #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
  57. do { \
  58. z = scm_double_cell ((tc), (scm_t_bits)(data1), \
  59. (scm_t_bits)(data2), (scm_t_bits)(data3)); \
  60. } while (0)
  61. #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \
  62. do { SCM __SCM_smob_answer; \
  63. SCM_NEWSMOB3 (__SCM_smob_answer, (tc), (data1), (data2), (data3)); \
  64. return __SCM_smob_answer; \
  65. } while (0)
  66. #define SCM_SMOB_FLAGS(x) (SCM_CELL_WORD_0 (x) >> 16)
  67. #define SCM_SMOB_DATA(x) (SCM_CELL_WORD_1 (x))
  68. #define SCM_SMOB_DATA_2(x) (SCM_CELL_WORD_2 (x))
  69. #define SCM_SMOB_DATA_3(x) (SCM_CELL_WORD_3 (x))
  70. #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_CELL_WORD_1 ((x), (data)))
  71. #define SCM_SET_SMOB_DATA_2(x, data) (SCM_SET_CELL_WORD_2 ((x), (data)))
  72. #define SCM_SET_SMOB_DATA_3(x, data) (SCM_SET_CELL_WORD_3 ((x), (data)))
  73. #define SCM_SET_SMOB_FLAGS(x, data) (scm_i_set_smob_flags((x),(data)<<16))
  74. #define SCM_SMOB_OBJECT(x) (SCM_CELL_OBJECT_1 (x))
  75. #define SCM_SMOB_OBJECT_2(x) (SCM_CELL_OBJECT_2 (x))
  76. #define SCM_SMOB_OBJECT_3(x) (SCM_CELL_OBJECT_3 (x))
  77. #define SCM_SET_SMOB_OBJECT(x,obj) (SCM_SET_CELL_OBJECT_1 ((x), (obj)))
  78. #define SCM_SET_SMOB_OBJECT_2(x,obj) (SCM_SET_CELL_OBJECT_2 ((x), (obj)))
  79. #define SCM_SET_SMOB_OBJECT_3(x,obj) (SCM_SET_CELL_OBJECT_3 ((x), (obj)))
  80. #define SCM_SMOB_OBJECT_LOC(x) (SCM_CELL_OBJECT_LOC ((x), 1))
  81. #define SCM_SMOB_OBJECT_2_LOC(x) (SCM_CELL_OBJECT_LOC ((x), 2))
  82. #define SCM_SMOB_OBJECT_3_LOC(x) (SCM_CELL_OBJECT_LOC ((x), 3))
  83. #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
  84. #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
  85. /* SCM_SMOBNAME can be 0 if name is missing */
  86. #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
  87. #define SCM_SMOB_PREDICATE(tag, obj) SCM_TYP16_PREDICATE (tag, obj)
  88. #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
  89. #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
  90. #define SCM_SMOB_APPLY_0(x) (SCM_SMOB_DESCRIPTOR (x).apply_0 (x))
  91. #define SCM_SMOB_APPLY_1(x, a1) (SCM_SMOB_DESCRIPTOR (x).apply_1 (x, (a1)))
  92. #define SCM_SMOB_APPLY_2(x, a1, a2) (SCM_SMOB_DESCRIPTOR (x).apply_2 (x, (a1), (a2)))
  93. #define SCM_SMOB_APPLY_3(x, a1, a2, rst) (SCM_SMOB_DESCRIPTOR (x).apply_3 (x, (a1), (a2), (rst)))
  94. SCM_API long scm_numsmob;
  95. SCM_API scm_smob_descriptor scm_smobs[];
  96. SCM_API void scm_i_set_smob_flags (SCM x, scm_t_bits data);
  97. SCM_API SCM scm_mark0 (SCM ptr);
  98. SCM_API SCM scm_markcdr (SCM ptr);
  99. SCM_API size_t scm_free0 (SCM ptr);
  100. SCM_API size_t scm_smob_free (SCM obj);
  101. SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate);
  102. /* The following set of functions is the standard way to create new
  103. * SMOB types.
  104. *
  105. * Create a type tag using `scm_make_smob_type', accept default values
  106. * for mark, free, print and/or equalp functions, or set your own
  107. * values using `scm_set_smob_xxx'.
  108. */
  109. SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size);
  110. SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM));
  111. SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM));
  112. SCM_API void scm_set_smob_print (scm_t_bits tc,
  113. int (*print) (SCM, SCM, scm_print_state*));
  114. SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
  115. SCM_API void scm_set_smob_apply (scm_t_bits tc,
  116. SCM (*apply) (),
  117. unsigned int req,
  118. unsigned int opt,
  119. unsigned int rst);
  120. SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val);
  121. /* Function for creating smobs */
  122. SCM_API SCM scm_make_smob (scm_t_bits tc);
  123. SCM_API void scm_smob_prehistory (void);
  124. #endif /* SCM_SMOB_H */
  125. /*
  126. Local Variables:
  127. c-file-style: "gnu"
  128. End:
  129. */