]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/smobs.tcc
Issue 4082/1: Reimplement Smobs via templates rather than preprocessor
[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>::register_ptr (Super *p)
34 {
35   // Don't use SCM_RETURN_NEWSMOB since that would require us to
36   // first register the memory and then create the smob.  That would
37   // announce the memory as being GC-controlled before even
38   // allocating the controlling smob.
39   SCM s = SCM_UNDEFINED;
40   SCM_NEWSMOB (s, smob_tag (), p);
41   scm_gc_register_collectable_memory (p, sizeof (*p), smob_name_.c_str ());
42   return s;
43 }
44
45 template <class Super>
46 int
47 Smob_base<Super>::print_smob (SCM, SCM p, scm_print_state *)
48 {
49   scm_puts ("#<", p);
50   scm_puts (smob_name_.c_str (), p);
51   scm_puts (">", p);
52   return 1;
53 }
54
55 template <class Super>
56 Super *
57 Smob_base<Super>::unregister_ptr (SCM obj)
58 {
59   Super *p = Super::unchecked_unsmob (obj);
60   scm_gc_unregister_collectable_memory (p, sizeof (*p), smob_name_.c_str ());
61   return p;
62 }
63
64 template <class Super>
65 scm_t_bits Smob_base<Super>::smob_tag_ = 0;
66
67 template <class Super>
68 Scm_init Smob_base<Super>::scm_init_ = init;
69
70 template <class Super>
71 string Smob_base<Super>::smob_name_;
72
73 template <class Super>
74 void Smob_base<Super>::init ()
75 {
76   smob_name_ = typeid (Super).name ();
77   // Primitive demangling, suitable for GCC, should be harmless
78   // elsewhere.  The worst that can happen is that we get material
79   // unsuitable for Texinfo documentation.  If that proves to be an
80   // issue, we need some smarter strategy.
81   smob_name_ = smob_name_.substr (smob_name_.find_first_not_of ("0123456789"));
82   assert(!smob_tag_);
83   smob_tag_ = scm_make_smob_type (smob_name_.c_str (), 0);
84   // The following have trivial private default definitions not
85   // referring to any aspect of the Super class apart from its name.
86   // They should be overridden (or rather masked) at Super level: that
87   // way they can refer to Super-private data.
88   // While that's not a consideration for type_p_name_, it's easier
89   // doing it like the rest.
90
91   if (Super::free_smob != 0)
92     scm_set_smob_free (smob_tag_, Super::free_smob);
93   if (Super::mark_smob != 0)
94     scm_set_smob_mark (smob_tag_, Super::mark_smob);
95   if (Super::print_smob != 0)
96     scm_set_smob_print (smob_tag_, Super::print_smob);
97   if (Super::equal_p != 0)
98     scm_set_smob_equalp (smob_tag_, Super::equal_p);
99   if (Super::type_p_name_ != 0)
100     {
101       SCM subr = scm_c_define_gsubr (Super::type_p_name_, 1, 0, 0,
102                                      (scm_t_subr) smob_p);
103       string fundoc = string("Is @var{x} a @code{") + smob_name_
104         + "} object?";
105       ly_add_function_documentation (subr, Super::type_p_name_, "(SCM x)",
106                                      fundoc);
107       scm_c_export (Super::type_p_name_, NULL);
108     }
109   ly_add_type_predicate ((void *) unsmob, smob_name_.c_str ());
110 }
111
112 #endif