]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/smobs.tcc
Issue 4152: Eliminate "will never be null" warnings in smobs.tcc
[lilypond.git] / lily / include / smobs.tcc
1 /* -*- C++ -*-
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--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 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   return (Super::unsmob (arg))->mark_smob ();
36 }
37
38 template <class Super>
39 SCM
40 Smob_base<Super>::register_ptr (Super *p)
41 {
42   // Don't use SCM_RETURN_NEWSMOB since that would require us to
43   // first register the memory and then create the smob.  That would
44   // announce the memory as being GC-controlled before even
45   // allocating the controlling smob.
46   SCM s = SCM_UNDEFINED;
47   SCM_NEWSMOB (s, smob_tag (), p);
48   scm_gc_register_collectable_memory (p, sizeof (*p), smob_name_.c_str ());
49   return s;
50 }
51
52 // Defaults, should not actually get called
53 template <class Super>
54 SCM
55 Smob_base<Super>::mark_smob ()
56 {
57   return SCM_UNSPECIFIED;
58 }
59
60 template <class Super>
61 size_t
62 Smob_base<Super>::free_smob (SCM)
63 {
64   return 0;
65 }
66
67 template <class Super>
68 SCM
69 Smob_base<Super>::equal_p (SCM, SCM)
70 {
71   return SCM_BOOL_F;
72 }
73
74 // Default, will often get called
75
76 template <class Super>
77 int
78 Smob_base<Super>::print_smob (SCM, SCM p, scm_print_state *)
79 {
80   scm_puts ("#<", p);
81   scm_puts (smob_name_.c_str (), p);
82   scm_puts (">", p);
83   return 1;
84 }
85
86 template <class Super>
87 Super *
88 Smob_base<Super>::unregister_ptr (SCM obj)
89 {
90   Super *p = Super::unchecked_unsmob (obj);
91   scm_gc_unregister_collectable_memory (p, sizeof (*p), smob_name_.c_str ());
92   return p;
93 }
94
95 template <class Super>
96 scm_t_bits Smob_base<Super>::smob_tag_ = 0;
97
98 template <class Super>
99 Scm_init Smob_base<Super>::scm_init_ = init;
100
101 template <class Super>
102 string Smob_base<Super>::smob_name_;
103
104 template <class Super>
105 void Smob_base<Super>::init ()
106 {
107   smob_name_ = typeid (Super).name ();
108   // Primitive demangling, suitable for GCC, should be harmless
109   // elsewhere.  The worst that can happen is that we get material
110   // unsuitable for Texinfo documentation.  If that proves to be an
111   // issue, we need some smarter strategy.
112   smob_name_ = smob_name_.substr (smob_name_.find_first_not_of ("0123456789"));
113   assert(!smob_tag_);
114   smob_tag_ = scm_make_smob_type (smob_name_.c_str (), 0);
115   // The following have trivial private default definitions not
116   // referring to any aspect of the Super class apart from its name.
117   // They should be overridden (or rather masked) at Super level: that
118   // way they can refer to Super-private data.
119   // While that's not a consideration for type_p_name_, it's easier
120   // doing it like the rest.
121
122   if (&Super::free_smob != &Smob_base<Super>::free_smob)
123     scm_set_smob_free (smob_tag_, Super::free_smob);
124   if (&Super::mark_smob != &Smob_base<Super>::mark_smob)
125     scm_set_smob_mark (smob_tag_, Super::mark_trampoline);
126   scm_set_smob_print (smob_tag_, Super::print_smob);
127   if (&Super::equal_p != &Smob_base<Super>::equal_p)
128     scm_set_smob_equalp (smob_tag_, Super::equal_p);
129   if (Super::type_p_name_ != 0)
130     {
131       SCM subr = scm_c_define_gsubr (Super::type_p_name_, 1, 0, 0,
132                                      (scm_t_subr) smob_p);
133       string fundoc = string("Is @var{x} a @code{") + smob_name_
134         + "} object?";
135       ly_add_function_documentation (subr, Super::type_p_name_, "(SCM x)",
136                                      fundoc);
137       scm_c_export (Super::type_p_name_, NULL);
138     }
139   ly_add_type_predicate ((void *) is_smob, smob_name_.c_str ());
140   if (Super::smob_proc_signature_ >= 0)
141     scm_set_smob_apply (smob_tag_,
142                         (scm_t_subr)Super::smob_proc,
143                         Super::smob_proc_signature_ >> 8,
144                         (Super::smob_proc_signature_ >> 4)&0xf,
145                         Super::smob_proc_signature_ & 0xf);
146 }
147 #endif