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