]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
0bcb9d2e8dbabde1ac3bc00c3b0a4e2f89ebbc95
[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
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 long CL::smob_tag_;                                             \
35 void                                                            \
36 CL::init_smobs ()                                               \
37 {                                                               \
38   smob_tag_ = scm_make_smob_type_mfpe (                         \
39      #CL, 0, CL::mark_smob, CL::free_smob, CL::print_smob, 0);  \
40 }                                                               \
41 SCM CL::smobbed_self () const                                   \
42 {                                                               \
43   SCM s;                                                        \
44   s = gh_cons (SCM_PACK(CL::smob_tag_), SCM_PACK(this));        \
45   scm_done_malloc(sizeof(CL));                                  \
46                                                                 \
47   return s;                                                     \
48 }                                                               \
49 CL *                                                            \
50 CL::unsmob (SCM s)                                              \
51 {                                                               \
52   if (SCM_NIMP(s) && SCM_CELL_TYPE(s) == smob_tag_)             \
53     return (CL*) SCM_CELL_WORD_1(s);                            \
54   else                                                          \
55     return 0;                                                   \
56 }                                                               \
57 scm_sizet                                                       \
58 CL::free_smob (SCM ses)                                         \
59 {                                                               \
60   CL * s = (CL*) SCM_CDR(ses);                                  \
61    delete s;                                                    \
62   return sizeof (CL);                                           \
63 }                                                               \
64 ADD_SCM_INIT_FUNC(CL, CL::init_smobs)
65
66 #define IMPLEMENT_SMOBS(CL)                                                     \
67 IMPLEMENT_SIMPLE_SMOBS(CL)                                                      \
68 SCM                                                                             \
69 CL::smobify_self ()                                                             \
70 {                                                                               \
71   SCM s =   unprotected_smobify_self ();\
72   scm_protect_object (s);\
73   return s;\
74 }\
75 SCM                                                                             \
76 CL::unprotected_smobify_self ()                                                         \
77 {                                                                               \
78   /*                                                                            \
79     This is local. We don't assign to self_scm_ directly, to assure             \
80     that S isn't GC-ed from under us.                                           \
81                                                                                 \
82     We don't use smobbed_self () to ensure that mark_smob () doesn't have to    \
83     deal half-initialized objects: scm_done_malloc( ) might trigger GC.         \
84     the warning in smobs.hh is just to be doubleplus goodly sure                \
85    */                                                                           \
86   SCM s;                                                                                \
87   SCM_NEWCELL(s);                                                               \
88   SCM_SETCAR(s,CL::smob_tag_);                                                  \
89   SCM_SETCDR (s, SCM_PACK(this));                                               \
90   self_scm_ = s;                                                                \
91  scm_done_malloc(sizeof(CL));                                                   \
92   return s;                                                                     \
93 }
94
95 #define IMPLEMENT_DEFAULT_EQUAL_P(CL)           \
96 SCM                                             \
97 CL::equal_p (SCM a , SCM b)                     \
98 {                                               \
99   return a == b ? SCM_BOOL_T : SCM_BOOL_F;      \
100 }
101
102
103 #endif /* LY_SMOBS_ICC */
104
105