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