]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/smobs.tcc
Issue 4360: Reorganize smob initialization to make it more reliable
[lilypond.git] / lily / include / smobs.tcc
1 /* -*- C++ -*-
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 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 SMOBS_TCC
21 #define SMOBS_TCC
22
23 // Contains generic template definitions.  With GCC, it is just
24 // included from smobs.hh, but other template expansion systems might
25 // make it feasible to compile this only a single time.
26
27 #include "lily-guile-macros.hh"
28 #include "smobs.hh"
29 #include <typeinfo>
30
31 template <class Super>
32 SCM
33 Smob_base<Super>::mark_trampoline (SCM arg)
34 {
35   Super *ptr = Super::unsmob (arg);
36   if (ptr)
37     return ptr->mark_smob ();
38   return SCM_UNDEFINED;
39 }
40
41 template <class Super>
42 int
43 Smob_base<Super>::print_trampoline (SCM arg, SCM port, scm_print_state *p)
44 {
45   Super *ptr = Super::unsmob (arg);
46   if (ptr)
47     return ptr->print_smob (port, p);
48   return 0;
49 }
50
51 template <class Super>
52 SCM
53 Smob_base<Super>::register_ptr (Super *p)
54 {
55   // Don't use SCM_RETURN_NEWSMOB since that would require us to
56   // first register the memory and then create the smob.  That would
57   // announce the memory as being GC-controlled before even
58   // allocating the controlling smob.
59   SCM s = SCM_UNDEFINED;
60   SCM_NEWSMOB (s, smob_tag (), p);
61   scm_gc_register_collectable_memory (p, sizeof (*p), smob_name_.c_str ());
62   return s;
63 }
64
65 // Defaults, should not actually get called
66 template <class Super>
67 SCM
68 Smob_base<Super>::mark_smob ()
69 {
70   return SCM_UNSPECIFIED;
71 }
72
73 template <class Super>
74 size_t
75 Smob_base<Super>::free_smob (SCM)
76 {
77   return 0;
78 }
79
80 template <class Super>
81 SCM
82 Smob_base<Super>::equal_p (SCM, SCM)
83 {
84   return SCM_BOOL_F;
85 }
86
87 // Default, will often get called
88
89 template <class Super>
90 int
91 Smob_base<Super>::print_smob (SCM p, scm_print_state *)
92 {
93   scm_puts ("#<", p);
94   scm_puts (smob_name_.c_str (), p);
95   scm_puts (">", p);
96   return 1;
97 }
98
99 template <class Super>
100 Super *
101 Smob_base<Super>::unregister_ptr (SCM obj)
102 {
103   Super *p = Super::unchecked_unsmob (obj);
104   scm_gc_unregister_collectable_memory (p, sizeof (*p), smob_name_.c_str ());
105   SCM_SET_SMOB_DATA (obj, static_cast<Super *> (0));
106   return p;
107 }
108
109 template <class Super>
110 string Smob_base<Super>::smob_name_;
111
112 template <class Super>
113 scm_t_bits Smob_base<Super>::init_id ()
114 {
115   smob_name_ = typeid (Super).name ();
116   // Primitive demangling, suitable for GCC, should be harmless
117   // elsewhere.  The worst that can happen is that we get material
118   // unsuitable for Texinfo documentation.  If that proves to be an
119   // issue, we need some smarter strategy.
120   smob_name_ = smob_name_.substr (smob_name_.find_first_not_of ("0123456789"));
121   scm_t_bits smob_tag = scm_make_smob_type (smob_name_.c_str (), 0);
122   // The following have trivial private default definitions not
123   // referring to any aspect of the Super class apart from its name.
124   // They should be overridden (or rather masked) at Super level: that
125   // way they can refer to Super-private data.
126   // While that's not a consideration for type_p_name_, it's easier
127   // doing it like the rest.
128
129   if (&Super::free_smob != &Smob_base<Super>::free_smob)
130     scm_set_smob_free (smob_tag, Super::free_smob);
131   // Old GCC versions get their type lattice for pointers-to-members
132   // tangled up to a degree where we need to typecast _both_ covariant
133   // types in order to be able to compare them.  The other comparisons
134   // are for static member functions and thus are ordinary function
135   // pointers which work without those contortions.
136   if (static_cast<SCM (Super::*)()>(&Super::mark_smob) !=
137       static_cast<SCM (Super::*)()>(&Smob_base<Super>::mark_smob))
138     scm_set_smob_mark (smob_tag, Super::mark_trampoline);
139   scm_set_smob_print (smob_tag, Super::print_trampoline);
140   if (&Super::equal_p != &Smob_base<Super>::equal_p)
141     scm_set_smob_equalp (smob_tag, Super::equal_p);
142   if (Super::type_p_name_ != 0)
143     {
144       SCM subr = scm_c_define_gsubr (Super::type_p_name_, 1, 0, 0,
145                                      (scm_t_subr) smob_p);
146       string fundoc = string("Is @var{x} a @code{") + smob_name_
147         + "} object?";
148       ly_add_function_documentation (subr, Super::type_p_name_, "(SCM x)",
149                                      fundoc);
150       scm_c_export (Super::type_p_name_, NULL);
151     }
152   ly_add_type_predicate ((void *) is_smob, smob_name_.c_str ());
153   if (Super::smob_proc_signature_ >= 0)
154     scm_set_smob_apply (smob_tag,
155                         (scm_t_subr)Super::smob_proc,
156                         Super::smob_proc_signature_ >> 8,
157                         (Super::smob_proc_signature_ >> 4)&0xf,
158                         Super::smob_proc_signature_ & 0xf);
159   return smob_tag;
160 }
161 #endif