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