]> git.donarmstrong.com Git - lilypond.git/blob - lily/unpure-pure-container.cc
Issue 4365: non-member unsmob<T> replaces T::unsmob and T::is_smob
[lilypond.git] / lily / unpure-pure-container.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2011--2015 Mike Solomon <mike@mikesolomon.org>
5
6
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.
11
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.
16
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/>.
19 */
20 #include "unpure-pure-container.hh"
21
22 // Reroutes a call to the contained function after dropping last two
23 // arguments.  Used for applying an "unpure" function in a "pure"
24 // context.
25 class Unpure_pure_call : public Smob1<Unpure_pure_call>
26 {
27 public:
28   LY_DECLARE_SMOB_PROC (&Unpure_pure_call::call, 2, 0, 1)
29   SCM call (SCM arg1, SCM arg2, SCM rest)
30   {
31     return scm_apply_0 (scm1 (),
32                         scm_call_2 (ly_lily_module_constant ("drop-right"),
33                                     scm_cons2 (arg1, arg2, rest),
34                                     scm_from_int (2)));
35   }
36 };
37
38 SCM
39 Unpure_pure_container::pure_part () const
40 {
41   return SCM_UNBNDP (scm2 ())
42     ? Unpure_pure_call::make_smob (scm1 ())
43     : scm2 ();
44 }
45
46 const char Unpure_pure_container::type_p_name_[] = "ly:unpure-pure-container?";
47
48 LY_DEFINE (ly_make_unpure_pure_container, "ly:make-unpure-pure-container",
49            1, 1, 0, (SCM unpure, SCM pure),
50            "Make an unpure-pure container.  @var{unpure} should be an unpure"
51            " expression, and @var{pure} should be a pure expression.  If @var{pure}"
52            " is omitted, the value of @var{unpure} will be used twice,"
53            " except that a callback is given two extra arguments"
54            " that are ignored for the sake of pure calculations.")
55 {
56   return Unpure_pure_container::make_smob (unpure, pure);
57 }
58
59 LY_DEFINE (ly_unpure_pure_container_unpure_part, "ly:unpure-pure-container-unpure-part",
60            1, 0, 0, (SCM pc),
61            "Return the unpure part of @var{pc}.")
62 {
63   LY_ASSERT_SMOB (Unpure_pure_container, pc, 1);
64   return unsmob<Unpure_pure_container> (pc)->unpure_part ();
65 }
66
67 LY_DEFINE (ly_unpure_pure_container_pure_part, "ly:unpure-pure-container-pure-part",
68            1, 0, 0, (SCM pc),
69            "Return the pure part of @var{pc}.")
70 {
71   LY_ASSERT_SMOB (Unpure_pure_container, pc, 1);
72   return unsmob<Unpure_pure_container> (pc)->pure_part ();
73 }
74
75 int
76 Unpure_pure_container::print_smob (SCM port, scm_print_state *)
77 {
78   scm_puts ("#<unpure-pure-container ", port);
79   scm_display (unpure_part (), port);
80   if (!is_unchanging ())
81     {
82       scm_puts (" ", port);
83       scm_display (pure_part (), port);
84     }
85   scm_puts (" >", port);
86   return 1;
87 }