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