2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2016 David Kastrup <dak@gnu.org>
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.
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.
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/>.
23 // A callback wrapper creates a Scheme-callable version of a fixed C++
24 // function. It is generally used for calling template-generated
25 // trampoline functions leading to calling a particular member
26 // function on a given Smob.
28 // The class itself is not templated in order not to explode the
29 // number of smob types: each class can support a particular call
32 // Check the GET_LISTENER call for a typical use case.
36 class Callback_wrapper : public Simple_smob<Callback_wrapper>
38 // We use an ordinary function pointer pointing to a trampoline
39 // function (templated on the callback in question) instead of
40 // storing a member function pointer to a common base class like
41 // Smob_core. The additional code for the trampolines is negligible
42 // and the performance implications of using member function
43 // pointers in connection with inheritance are somewhat opaque as
44 // this involves an adjustment of the this pointer from Smob_core to
45 // the scope containing the callback.
46 SCM (*trampoline_) (SCM, SCM);
47 Callback_wrapper (SCM (*trampoline) (SCM, SCM)) : trampoline_ (trampoline)
48 { } // Private constructor, use only in make_smob
50 static const char * const type_p_name_; // = 0
51 LY_DECLARE_SMOB_PROC (&Callback_wrapper::call, 2, 0, 0)
52 SCM call (SCM target, SCM arg)
54 return trampoline_ (target, arg);
56 // Callback wrappers are for an unchanging entity, so we do the Lisp
57 // creation just once on the first call of make_smob. So we only
58 // get a single Callback_wrapper instance for each differently
59 // templated make_smob call.
60 template <SCM (*trampoline) (SCM, SCM)>
61 static SCM make_smob ()
64 scm_permanent_object (Callback_wrapper (trampoline).smobbed_copy ());