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