]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
release: 1.3.73
[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   if (SMOB_IS_TYPE_B(CL, s))                    \
21     return SMOB_TO_TYPE(CL, s);                 \
22   else                                          \
23     return 0;                                   \
24 }\
25 SCM smobify (CL *cl)\
26 {\
27   SCM s;                                                                \
28                                                                         \
29   SCM_NEWCELL(s);                                                       \
30   SCM_SETCAR(s,CL::smob_tag_);                                          \
31   SCM me_s = SCM_PACK(cl);                                                      \
32   SCM_SETCDR (s, me_s); \
33 return s;\
34 }\
35
36
37 /*
38   should include equal_p ? 
39  */
40 #define IMPLEMENT_SMOBS(CL)\
41 long CL::smob_tag_;\
42 void                                                                    \
43 CL::init_smobs ()                                                       \
44 {                                                                       \
45   smob_tag_ = scm_make_smob_type_mfpe ( \
46      #CL, 0, CL::mark_smob, CL::free_smob, CL::print_smob, 0);\
47 }\
48                                                                         \
49                                                                          \
50 void                                                                     \
51 CL::unsmobify_self ()                                                    \
52 {                                                                        \
53   SCM s = self_scm_;                                                     \
54   scm_unprotect_object (s);                                              \
55                                                                          \
56   SCM_SETCAR (self_scm_, SCM_EOL); \
57   SCM_SETCDR (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_SETCAR (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