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