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