]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
99c09fc0435f3e51792fd3966bc507c88235d81d
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #ifndef LY_SMOBS_ICC
10 #define LY_SMOBS_ICC
11
12 #include "smobs.hh"
13
14 #define IMPLEMENT_TYPE_P(CL, FUNCNAME)                                  \
15   SCM CL ## _type_p_proc;                                               \
16   void init_type_ ## CL ()                                              \
17   {                                                                     \
18     SCM subr = scm_c_define_gsubr (FUNCNAME, 1, 0, 0,                   \
19                                    (Scheme_function_unknown) CL::smob_p); \
20     CL ## _type_p_proc = subr;                                          \
21     ly_add_function_documentation (subr, FUNCNAME, "(SCM x)",           \
22                                    "Is @var{x} a @code{" #CL "} object?"); \
23     scm_c_export (FUNCNAME, NULL);                                      \
24   }                                                                     \
25   ADD_SCM_INIT_FUNC (init_type_ ## CL, init_type_ ## CL)
26
27 #define IMPLEMENT_BASE_SMOBS(CL)                                        \
28   scm_t_bits CL::smob_tag_;                                             \
29   SCM                                                                   \
30   CL::smob_p (SCM s)                                                    \
31   {                                                                     \
32     if (SCM_NIMP (s) && SCM_CELL_TYPE (s) == smob_tag_)                 \
33       return SCM_BOOL_T;                                                \
34     else                                                                \
35       return SCM_BOOL_F;                                                \
36                                                                         \
37   }                                                                     \
38                                                                         \
39   void                                                                  \
40   CL::init_smobs ()                                                     \
41   {                                                                     \
42     smob_tag_ = scm_make_smob_type (#CL, 0);                            \
43     scm_set_smob_mark (smob_tag_, CL::mark_smob);                       \
44     scm_set_smob_free (smob_tag_, CL::free_smob);                       \
45     scm_set_smob_print (smob_tag_, CL::print_smob);                     \
46     scm_set_smob_equalp (smob_tag_, CL::equal_p);                       \
47   }                                                                     \
48                                                                         \
49   size_t                                                                \
50   CL::free_smob (SCM ses)                                               \
51   {                                                                     \
52     CL *s = (CL *) SCM_CELL_WORD_1 (ses);                               \
53     delete s;                                                           \
54     /*    scm_gc_unregister_collectable_memory (s, sizeof (CL), #CL " smob"); */ \
55     return SMOB_FREE_RETURN_VAL (CL);                                   \
56   }                                                                     \
57                                                                         \
58   ADD_SCM_INIT_FUNC (CL, CL::init_smobs)
59
60 #define IMPLEMENT_SIMPLE_SMOBS(CL)                                      \
61   IMPLEMENT_BASE_SMOBS (CL);                                            \
62   SCM CL::smobbed_copy () const                                         \
63   {                                                                     \
64     CL *ptr = new CL (*this);                                           \
65     SCM s;                                                              \
66     s = scm_cons (SCM_PACK (CL::smob_tag_), SCM_PACK (ptr));            \
67     scm_gc_register_collectable_memory ((CL *)this, sizeof (CL), #CL " smob"); \
68                                                                         \
69     return s;                                                           \
70   }
71
72 #define IMPLEMENT_SMOBS(CL)                                             \
73   IMPLEMENT_BASE_SMOBS (CL)                                             \
74     void                                                                \
75   CL::smobify_self ()                                                   \
76   {                                                                     \
77     protection_cons_ = SCM_EOL;                                         \
78     self_scm_ = unprotected_smobify_self ();                            \
79     protect ();                                                         \
80   }                                                                     \
81   void                                                                  \
82   CL::protect ()                                                        \
83   {                                                                     \
84     protect_smob (self_scm_, &protection_cons_);                        \
85   }                                                                     \
86   SCM                                                                   \
87   CL::unprotect ()                                                      \
88   {                                                                     \
89     unprotect_smob (self_scm_, &protection_cons_);                      \
90     return self_scm_;                                                   \
91   }                                                                     \
92   SCM                                                                   \
93   CL::unprotected_smobify_self ()                                       \
94   {                                                                     \
95     /*                                                                  \
96       This is local. We don't assign to self_scm_ directly, to assure   \
97       that S isn't GC-ed from under us.                                 \
98                                                                         \
99                                                                         We don't use smobbed_self () to ensure that mark_smob () doesn't have to \
100                                                                         deal half-initialized objects: scm_done_malloc ( ) might trigger GC. \
101                                                                         the warning in smobs.hh is just to be doubleplus goodly sure \
102     */                                                                  \
103     SCM s;                                                              \
104     SCM_NEWSMOB (s, CL::smob_tag_, this);                               \
105     self_scm_ = s;                                                      \
106     scm_gc_register_collectable_memory (this, sizeof (CL), #CL " smob"); \
107     return s;                                                           \
108   }
109
110 #define IMPLEMENT_DEFAULT_EQUAL_P(CL)           \
111   SCM                                           \
112   CL::equal_p (SCM a, SCM b)                    \
113   {                                             \
114     return a == b ? SCM_BOOL_T : SCM_BOOL_F;    \
115   }
116
117 #endif /* LY_SMOBS_ICC */
118