]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/ly-smobs.icc
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / lily / include / ly-smobs.icc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef LY_SMOBS_ICC
21 #define LY_SMOBS_ICC
22
23 #include "smobs.hh"
24
25 #define IMPLEMENT_TYPE_P(CL, FUNCNAME)                                  \
26   SCM CL ## _type_p_proc;                                               \
27   void init_type_ ## CL ()                                              \
28   {                                                                     \
29     SCM subr = scm_c_define_gsubr (FUNCNAME, 1, 0, 0,                   \
30                                    (Scheme_function_unknown) CL::smob_p); \
31     CL ## _type_p_proc = subr;                                          \
32     ly_add_function_documentation (subr, FUNCNAME, "(SCM x)",           \
33                                    "Is @var{x} a @code{" #CL "} object?"); \
34     scm_c_export (FUNCNAME, NULL);                                      \
35   }                                                                     \
36   ADD_SCM_INIT_FUNC (init_type_ ## CL, init_type_ ## CL)
37
38 #define IMPLEMENT_BASE_SMOBS(CL)                                        \
39   void \
40   CL ## _type_adder ()                  \
41   {\
42     ly_add_type_predicate ((void*) &CL::unsmob, #CL);   \
43   }\
44   ADD_SCM_INIT_FUNC(CL ## _type_adder_ctor, \
45                     CL ## _type_adder);\
46   const char *CL::smob_name_ = #CL;                                     \
47   scm_t_bits CL::smob_tag_;                                             \
48   SCM                                                                   \
49   CL::smob_p (SCM s)                                                    \
50   {                                                                     \
51     if (SCM_NIMP (s) && SCM_CELL_TYPE (s) == smob_tag_)                 \
52       return SCM_BOOL_T;                                                \
53     else                                                                \
54       return SCM_BOOL_F;                                                \
55                                                                         \
56   }                                                                     \
57                                                                         \
58   void                                                                  \
59   CL::init_smobs ()                                                     \
60   {                                                                     \
61     smob_tag_ = scm_make_smob_type (#CL, 0);                            \
62     scm_set_smob_mark (smob_tag_, CL::mark_smob);                       \
63     scm_set_smob_free (smob_tag_, CL::free_smob);                       \
64     scm_set_smob_print (smob_tag_, CL::print_smob);                     \
65     scm_set_smob_equalp (smob_tag_, CL::equal_p);                       \
66   }                                                                     \
67                                                                         \
68   size_t                                                                \
69   CL::free_smob (SCM ses)                                               \
70   {                                                                     \
71     CL *s = (CL *) SCM_CELL_WORD_1 (ses);                               \
72     delete s;                                                           \
73     /*    scm_gc_unregister_collectable_memory (s, sizeof (CL), #CL " smob"); */ \
74     return SMOB_FREE_RETURN_VAL (CL);                                   \
75   }                                                                     \
76                                                                         \
77   ADD_SCM_INIT_FUNC (CL, CL::init_smobs)
78
79 #define IMPLEMENT_SIMPLE_SMOBS(CL)                                      \
80   IMPLEMENT_BASE_SMOBS (CL);                                            \
81   SCM CL::smobbed_copy () const                                         \
82   {                                                                     \
83     CL *ptr = new CL (*this);                                           \
84     SCM s;                                                              \
85     s = scm_cons (SCM_PACK (CL::smob_tag_), SCM_PACK (ptr));            \
86     scm_gc_register_collectable_memory ((CL *)this, sizeof (CL), #CL " smob"); \
87                                                                         \
88     return s;                                                           \
89   }
90
91 #define IMPLEMENT_SMOBS(CL)                                             \
92   IMPLEMENT_BASE_SMOBS (CL)                                             \
93     void                                                                \
94   CL::smobify_self ()                                                   \
95   {                                                                     \
96     protection_cons_ = SCM_EOL;                                         \
97     self_scm_ = unprotected_smobify_self ();                            \
98     protect ();                                                         \
99   }                                                                     \
100   void                                                                  \
101   CL::protect ()                                                        \
102   {                                                                     \
103     protect_smob (self_scm_, &protection_cons_);                        \
104   }                                                                     \
105   SCM                                                                   \
106   CL::unprotect ()                                                      \
107   {                                                                     \
108     unprotect_smob (self_scm_, &protection_cons_);                      \
109     return self_scm_;                                                   \
110   }                                                                     \
111   SCM                                                                   \
112   CL::unprotected_smobify_self ()                                       \
113   {                                                                     \
114     /*                                                                  \
115       This is local. We don't assign to self_scm_ directly, to assure   \
116       that S isn't GC-ed from under us.                                 \
117                                                                         \
118       We don't use smobbed_self () to ensure that mark_smob () doesn't  \
119       have to deal half-initialized objects: scm_done_malloc ( ) might  \
120       trigger GC.the warning in smobs.hh is just to be doubleplus       \
121       goodly sure                                                       \
122     */                                                                  \
123     SCM s;                                                              \
124     SCM_NEWSMOB (s, CL::smob_tag_, this);                               \
125     self_scm_ = s;                                                      \
126     scm_gc_register_collectable_memory (this, sizeof (CL), #CL " smob"); \
127     return s;                                                           \
128   }
129
130 #define IMPLEMENT_DEFAULT_EQUAL_P(CL)           \
131   SCM                                           \
132   CL::equal_p (SCM a, SCM b)                    \
133   {                                             \
134     return a == b ? SCM_BOOL_T : SCM_BOOL_F;    \
135   }
136
137 #endif /* LY_SMOBS_ICC */
138