]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
(IMPLEMENT_SMOBS): don't return SCM
[lilypond.git] / lily / include / ly-smobs.icc
1 /*   
2   ly-smobs.icc -- implement smob glue. 
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c)  1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7  */
8
9 #ifndef LY_SMOBS_ICC
10 #define LY_SMOBS_ICC
11
12 #include "smobs.hh"
13
14
15 #define IMPLEMENT_TYPE_P(CL, FUNCNAME)\
16 SCM CL ## _type_p_proc;\
17 void init_type_ ## CL ()\
18 {\
19   SCM subr = scm_c_define_gsubr (FUNCNAME, 1, 0, 0, (Scheme_function_unknown) CL::smob_p);\
20   CL ## _type_p_proc = subr;\
21   ly_add_function_documentation (subr, FUNCNAME, "(SCM x)", "Is @var{x} a @code{" #CL "} object?");\
22   scm_c_export (FUNCNAME, NULL);\
23 }\
24 ADD_SCM_INIT_FUNC (init_type_ ## CL, init_type_ ## CL)
25
26 #ifndef SCM_CELL_TYPE
27 #define SCM_CELL_TYPE(X) SCM_CAR (X)
28 #endif
29
30 #ifndef SCM_CELL_WORD_1
31 #define SCM_CELL_WORD_1(X) SCM_CDR (X)
32 #endif
33
34
35
36 #define IMPLEMENT_BASE_SMOBS(CL)                                \
37 scm_t_bits CL::smob_tag_;                                       \
38 SCM                                                             \
39 CL::smob_p (SCM s)                                              \
40 {                                                               \
41   if (SCM_NIMP (s) && SCM_CELL_TYPE (s) == smob_tag_)           \
42     return SCM_BOOL_T;                                          \
43   else                                                          \
44     return SCM_BOOL_F;                                          \
45                                                                 \
46 }                                                               \
47 void                                                            \
48 CL::init_smobs ()                                               \
49 {                                                               \
50   smob_tag_ = scm_make_smob_type (#CL, 0);                      \
51   scm_set_smob_mark (smob_tag_, CL::mark_smob);                 \
52   scm_set_smob_free (smob_tag_, CL::free_smob);                 \
53   scm_set_smob_print (smob_tag_, CL::print_smob);               \
54   scm_set_smob_equalp (smob_tag_, CL::equal_p);                 \
55 }                                                               \
56 size_t                                                          \
57 CL::free_smob (SCM ses)                                         \
58 {                                                               \
59   CL * s = (CL*) SCM_CDR (ses);                                 \
60   delete s;                                                     \
61   scm_gc_unregister_collectable_memory (s, sizeof (CL), #CL " smob");  \
62   return SMOB_FREE_RETURN_VAL(CL);\
63 }\
64 ADD_SCM_INIT_FUNC (CL, CL::init_smobs)
65
66
67 #define IMPLEMENT_SIMPLE_SMOBS(CL)                              \
68 IMPLEMENT_BASE_SMOBS(CL);\
69 SCM CL::smobbed_copy () const                                   \
70 {                                                               \
71   CL * ptr  = new CL (*this);\
72   SCM s;                                                        \
73   s = scm_cons (SCM_PACK (CL::smob_tag_), SCM_PACK (ptr));      \
74   scm_gc_register_collectable_memory ((CL*)this, sizeof (CL), #CL " smob"); \
75                                                                 \
76   return s;                                                     \
77 }                                                               \
78
79
80 #define IMPLEMENT_SMOBS(CL)                                                     \
81 IMPLEMENT_BASE_SMOBS (CL)                                                       \
82 void                                                                            \
83 CL::smobify_self ()                                                             \
84 {                                                                               \
85   SCM s = unprotected_smobify_self ();\
86   scm_gc_protect_object (s);\
87 }\
88 SCM                                                                             \
89 CL::unprotected_smobify_self ()                                                         \
90 {                                                                               \
91   /*                                                                            \
92     This is local. We don't assign to self_scm_ directly, to assure             \
93     that S isn't GC-ed from under us.                                           \
94                                                                                 \
95     We don't use smobbed_self () to ensure that mark_smob () doesn't have to    \
96     deal half-initialized objects: scm_done_malloc ( ) might trigger GC.                \
97     the warning in smobs.hh is just to be doubleplus goodly sure                \
98    */                                                                           \
99   SCM s;\
100   SCM_NEWSMOB (s, CL::smob_tag_, this);\
101   self_scm_ = s;                                                                \
102  scm_gc_register_collectable_memory (this, sizeof (CL), #CL " smob");                                                   \
103   return s;                                                                     \
104 }
105
106 #define IMPLEMENT_DEFAULT_EQUAL_P(CL)           \
107 SCM                                             \
108 CL::equal_p (SCM a , SCM b)                     \
109 {                                               \
110   return a == b ? SCM_BOOL_T : SCM_BOOL_F;      \
111 }
112
113
114 #endif /* LY_SMOBS_ICC */
115
116