]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
release: 1.3.52
[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 #define IMPLEMENT_UNSMOB(CL, name)              \
14 CL *                                            \
15 unsmob_ ## name ( SCM s)                        \
16 {                                               \
17   if (SMOB_IS_TYPE_B(CL, s))                    \
18     return SMOB_TO_TYPE(CL, s);                 \
19   else                                          \
20     return 0;                                   \
21 }\
22 SCM smobify (CL *cl)\
23 {\
24   SCM s;                                                                \
25                                                                         \
26   SCM_NEWCELL(s);                                                       \
27   SCM_SETCAR(s,CL::smob_tag_);                                          \
28   void * me_p = cl;                                                     \
29   SCM_SETCDR(s,me_p);                                                   \
30 return s;\
31 }\
32
33
34 /*
35   should include equal_p ? 
36  */
37 #define IMPLEMENT_SMOBS(CL)\
38 long CL::smob_tag_;\
39 static scm_smobfuns CL ## _funs = {                                     \
40   CL::mark_smob, CL::free_smob,                                         \
41   CL::print_smob, 0,                                                    \
42 };                                                                      \
43 void                                                                    \
44 CL::init_smobs ()                                                       \
45 {                                                                       \
46   smob_tag_ = scm_newsmob (&CL ## _funs);                                       \
47 }                                                                       \
48                                                                         \
49                                                                          \
50 void                                                                     \
51 CL::unsmobify_self ()                                                    \
52 {                                                                        \
53   SCM s = self_scm_;                                                     \
54   scm_unprotect_object (s);                                              \
55                                                                          \
56   SCM_CAR(self_scm_) = SCM_EOL;  \
57   SCM_CDR(self_scm_) = SCM_EOL;                                          \
58   self_scm_ = SCM_EOL;                                                   \
59 \
60   scm_done_malloc ( - sizeof (CL));\
61 }                                                                        \
62 \
63 SCM                                                                     \
64 CL::smobify_self ()                                                     \
65 {                                                                       \
66   if (self_scm_ != SCM_EOL)                                             \
67     return self_scm_;                                                   \
68                                                                         \
69   /*                                                                    \
70     This is local. We don't assign to self_scm_ directly, to assure     \
71     that S isn't GC-ed from under us.                                   \
72    */                                                                   \
73   SCM s = smobify (this); \
74   self_scm_ = s;                                                        \
75   scm_protect_object (s);                                               \
76                                                                         \
77   scm_done_malloc(sizeof(CL));\
78   do_smobify_self();                                                    \
79   return s;                                     \
80 }                                               \
81 scm_sizet                                       \
82 CL::free_smob (SCM ses)                         \
83 {                                               \
84   CL * s = (CL*) SCM_CDR(ses);                  \
85   /* someone else did the deed already; this might be an automatic var.*/ \
86   if (s->self_scm_ != ses)\
87      return 0; \
88 \
89  /* no need to call scm_unprotect_object, since this call \
90     implies that the object is not protected. */ \
91   SCM_CAR(ses) = SCM_EOL;\
92   delete s;\
93   return sizeof (CL);\
94 } \
95 ADD_SCM_INIT_FUNC(CL, CL::init_smobs)\
96
97
98 #endif /* LY_SMOBS_ICC */
99