]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
af8035644c1fcbc762a47ab2a91d546c48a386c4
[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--2007 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   void \
29   CL ## _type_adder ()                  \
30   {\
31     ly_add_type_predicate ((void*) &CL::unsmob, #CL);   \
32   }\
33   ADD_SCM_INIT_FUNC(CL ## _type_adder_ctor, \
34                     CL ## _type_adder);\
35   const char *CL::smob_name_ = #CL;                                     \
36   scm_t_bits CL::smob_tag_;                                             \
37   SCM                                                                   \
38   CL::smob_p (SCM s)                                                    \
39   {                                                                     \
40     if (SCM_NIMP (s) && SCM_CELL_TYPE (s) == smob_tag_)                 \
41       return SCM_BOOL_T;                                                \
42     else                                                                \
43       return SCM_BOOL_F;                                                \
44                                                                         \
45   }                                                                     \
46                                                                         \
47   void                                                                  \
48   CL::init_smobs ()                                                     \
49   {                                                                     \
50     smob_tag_ = scm_make_smob_type (#CL, 0);                            \
51     scm_set_smob_mark (smob_tag_, CL::mark_smob);                       \
52     scm_set_smob_free (smob_tag_, CL::free_smob);                       \
53     scm_set_smob_print (smob_tag_, CL::print_smob);                     \
54     scm_set_smob_equalp (smob_tag_, CL::equal_p);                       \
55   }                                                                     \
56                                                                         \
57   size_t                                                                \
58   CL::free_smob (SCM ses)                                               \
59   {                                                                     \
60     CL *s = (CL *) SCM_CELL_WORD_1 (ses);                               \
61     delete s;                                                           \
62     /*    scm_gc_unregister_collectable_memory (s, sizeof (CL), #CL " smob"); */ \
63     return SMOB_FREE_RETURN_VAL (CL);                                   \
64   }                                                                     \
65                                                                         \
66   ADD_SCM_INIT_FUNC (CL, CL::init_smobs)
67
68 #define IMPLEMENT_SIMPLE_SMOBS(CL)                                      \
69   IMPLEMENT_BASE_SMOBS (CL);                                            \
70   SCM CL::smobbed_copy () const                                         \
71   {                                                                     \
72     CL *ptr = new CL (*this);                                           \
73     SCM s;                                                              \
74     s = scm_cons (SCM_PACK (CL::smob_tag_), SCM_PACK (ptr));            \
75     scm_gc_register_collectable_memory ((CL *)this, sizeof (CL), #CL " smob"); \
76                                                                         \
77     return s;                                                           \
78   }
79
80 #define IMPLEMENT_SMOBS(CL)                                             \
81   IMPLEMENT_BASE_SMOBS (CL)                                             \
82     void                                                                \
83   CL::smobify_self ()                                                   \
84   {                                                                     \
85     protection_cons_ = SCM_EOL;                                         \
86     self_scm_ = unprotected_smobify_self ();                            \
87     protect ();                                                         \
88   }                                                                     \
89   void                                                                  \
90   CL::protect ()                                                        \
91   {                                                                     \
92     protect_smob (self_scm_, &protection_cons_);                        \
93   }                                                                     \
94   SCM                                                                   \
95   CL::unprotect ()                                                      \
96   {                                                                     \
97     unprotect_smob (self_scm_, &protection_cons_);                      \
98     return self_scm_;                                                   \
99   }                                                                     \
100   SCM                                                                   \
101   CL::unprotected_smobify_self ()                                       \
102   {                                                                     \
103     /*                                                                  \
104       This is local. We don't assign to self_scm_ directly, to assure   \
105       that S isn't GC-ed from under us.                                 \
106                                                                         \
107       We don't use smobbed_self () to ensure that mark_smob () doesn't  \
108       have to deal half-initialized objects: scm_done_malloc ( ) might  \
109       trigger GC.the warning in smobs.hh is just to be doubleplus       \
110       goodly sure                                                       \
111     */                                                                  \
112     SCM s;                                                              \
113     SCM_NEWSMOB (s, CL::smob_tag_, this);                               \
114     self_scm_ = s;                                                      \
115     scm_gc_register_collectable_memory (this, sizeof (CL), #CL " smob"); \
116     return s;                                                           \
117   }
118
119 #define IMPLEMENT_DEFAULT_EQUAL_P(CL)           \
120   SCM                                           \
121   CL::equal_p (SCM a, SCM b)                    \
122   {                                             \
123     return a == b ? SCM_BOOL_T : SCM_BOOL_F;    \
124   }
125
126 #endif /* LY_SMOBS_ICC */
127