]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
* lily/global-context.cc (run_iterator_on_me): fix grace note
[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 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)", "Is @var{x} a @code{" #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_BASE_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 size_t                                                          \
55 CL::free_smob (SCM ses)                                         \
56 {                                                               \
57   CL * s = (CL*) SCM_CDR (ses);                                 \
58   delete s;                                                     \
59   scm_gc_unregister_collectable_memory (s, sizeof (CL), #CL " smob");  \
60   return SMOB_FREE_RETURN_VAL(CL);\
61 }\
62 ADD_SCM_INIT_FUNC (CL, CL::init_smobs)
63
64
65 #define IMPLEMENT_SIMPLE_SMOBS(CL)                              \
66 IMPLEMENT_BASE_SMOBS(CL);\
67 SCM CL::smobbed_copy () const                                   \
68 {                                                               \
69   CL * ptr  = new CL (*this);\
70   SCM s;                                                        \
71   s = gh_cons (SCM_PACK (CL::smob_tag_), SCM_PACK (ptr));       \
72   scm_gc_register_collectable_memory ((CL*)this, sizeof (CL), #CL " smob"); \
73                                                                 \
74   return s;                                                     \
75 }                                                               \
76
77
78 #define IMPLEMENT_SMOBS(CL)                                                     \
79 IMPLEMENT_BASE_SMOBS (CL)                                                       \
80 SCM                                                                             \
81 CL::smobify_self ()                                                             \
82 {                                                                               \
83   SCM s =   unprotected_smobify_self ();\
84   scm_gc_protect_object (s);\
85   return s;\
86 }\
87 SCM                                                                             \
88 CL::unprotected_smobify_self ()                                                         \
89 {                                                                               \
90   /*                                                                            \
91     This is local. We don't assign to self_scm_ directly, to assure             \
92     that S isn't GC-ed from under us.                                           \
93                                                                                 \
94     We don't use smobbed_self () to ensure that mark_smob () doesn't have to    \
95     deal half-initialized objects: scm_done_malloc ( ) might trigger GC.                \
96     the warning in smobs.hh is just to be doubleplus goodly sure                \
97    */                                                                           \
98   SCM s;\
99   SCM_NEWSMOB (s, CL::smob_tag_, this);\
100   self_scm_ = s;                                                                \
101  scm_gc_register_collectable_memory (this, sizeof (CL), #CL " smob");                                                   \
102   return s;                                                                     \
103 }
104
105 #define IMPLEMENT_DEFAULT_EQUAL_P(CL)           \
106 SCM                                             \
107 CL::equal_p (SCM a , SCM b)                     \
108 {                                               \
109   return a == b ? SCM_BOOL_T : SCM_BOOL_F;      \
110 }
111
112
113 #endif /* LY_SMOBS_ICC */
114
115