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