2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2011--2014 Mike Solomon <mike@mikesolomon.org>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "unpure-pure-container.hh"
24 // Reroutes a call to the contained function after dropping last two
25 // arguments. Used for applying an "unpure" function in a "pure"
27 class Unpure_pure_call : public Smob1<Unpure_pure_call>
30 LY_DECLARE_SMOB_PROC (2, 0, 1, (SCM self, SCM arg1, SCM arg2, SCM rest))
32 return scm_apply_0 (Unpure_pure_call::unsmob (self)->scm1 (),
33 scm_call_2 (ly_lily_module_constant ("drop-right"),
34 scm_cons2 (arg1, arg2, rest),
40 Unpure_pure_container::pure_part () const
42 return SCM_UNBNDP (scm2 ())
43 ? Unpure_pure_call::make_smob (scm1 ())
47 const char Unpure_pure_container::type_p_name_[] = "ly:unpure-pure-container?";
49 LY_DEFINE (ly_make_unpure_pure_container, "ly:make-unpure-pure-container",
50 1, 1, 0, (SCM unpure, SCM pure),
51 "Make an unpure-pure container. @var{unpure} should be an unpure"
52 " expression, and @var{pure} should be a pure expression. If @var{pure}"
53 " is omitted, the value of @var{unpure} will be used twice,"
54 " except that a callback is given two extra arguments"
55 " that are ignored for the sake of pure calculations.")
57 return Unpure_pure_container::make_smob (unpure, pure);
60 LY_DEFINE (ly_unpure_pure_container_unpure_part, "ly:unpure-pure-container-unpure-part",
62 "Return the unpure part of @var{pc}.")
64 LY_ASSERT_TYPE (Unpure_pure_container::unsmob, pc, 1);
65 return Unpure_pure_container::unsmob (pc)->unpure_part ();
68 LY_DEFINE (ly_unpure_pure_container_pure_part, "ly:unpure-pure-container-pure-part",
70 "Return the pure part of @var{pc}.")
72 LY_ASSERT_TYPE (Unpure_pure_container::unsmob, pc, 1);
73 return Unpure_pure_container::unsmob (pc)->pure_part ();
77 Unpure_pure_container::print_smob (SCM s, SCM port, scm_print_state *)
79 Unpure_pure_container *p = Unpure_pure_container::unsmob (s);
80 scm_puts ("#<unpure-pure-container ", port);
81 scm_display (p->unpure_part (), port);
82 if (!p->is_unchanging ())
85 scm_display (p->pure_part (), port);
87 scm_puts (" >", port);