]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
b82378dc1b1591a7f3c5b372b467b90b029493e6
[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   ly_add_function_documentation (FUNCNAME, "(SCM x)", "Check if @var{x} is a " #CL " object");\
21 }\
22 ADD_SCM_INIT_FUNC (init_type_p_ ## CL, init_type_p_ ## 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 #define IMPLEMENT_SIMPLE_SMOBS(CL)                              \
34 scm_t_bits CL::smob_tag_;                                       \
35 SCM                                                             \
36 CL::smob_p (SCM s)                                              \
37 {                                                               \
38   if (SCM_NIMP (s) && SCM_CELL_TYPE (s) == smob_tag_)           \
39     return SCM_BOOL_T;                                          \
40   else                                                          \
41     return SCM_BOOL_F;                                          \
42                                                                 \
43 }                                                               \
44 void                                                            \
45 CL::init_smobs ()                                               \
46 {                                                               \
47   smob_tag_ = scm_make_smob_type (#CL, 0);                      \
48   scm_set_smob_mark (smob_tag_, CL::mark_smob);                 \
49   scm_set_smob_free (smob_tag_, CL::free_smob);                 \
50   scm_set_smob_print (smob_tag_, CL::print_smob);               \
51   scm_set_smob_equalp (smob_tag_, CL::equal_p);                 \
52 }                                                               \
53 SCM CL::smobbed_self () const                                   \
54 {                                                               \
55   SCM s;                                                        \
56   s = gh_cons (SCM_PACK (CL::smob_tag_), SCM_PACK (this));      \
57   scm_done_malloc (sizeof (CL));                                \
58                                                                 \
59   return s;                                                     \
60 }                                                               \
61 size_t                                                          \
62 CL::free_smob (SCM ses)                                         \
63 {                                                               \
64   CL * s = (CL*) SCM_CDR (ses);                                 \
65    delete s;                                                    \
66   return sizeof (CL);                                           \
67 }                                                               \
68 ADD_SCM_INIT_FUNC (CL, CL::init_smobs)
69
70 #define IMPLEMENT_SMOBS(CL)                                                     \
71 IMPLEMENT_SIMPLE_SMOBS (CL)                                                     \
72 SCM                                                                             \
73 CL::smobify_self ()                                                             \
74 {                                                                               \
75   SCM s =   unprotected_smobify_self ();\
76   scm_gc_protect_object (s);\
77   return s;\
78 }\
79 SCM                                                                             \
80 CL::unprotected_smobify_self ()                                                         \
81 {                                                                               \
82   /*                                                                            \
83     This is local. We don't assign to self_scm_ directly, to assure             \
84     that S isn't GC-ed from under us.                                           \
85                                                                                 \
86     We don't use smobbed_self () to ensure that mark_smob () doesn't have to    \
87     deal half-initialized objects: scm_done_malloc ( ) might trigger GC.                \
88     the warning in smobs.hh is just to be doubleplus goodly sure                \
89    */                                                                           \
90   SCM s;                                                                                \
91   SCM_NEWCELL (s);                                                              \
92   SCM_SETCAR (s,CL::smob_tag_);                                                 \
93   SCM_SETCDR (s, SCM_PACK (this));                                              \
94   self_scm_ = s;                                                                \
95  scm_done_malloc (sizeof (CL));                                                 \
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