]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/fluid.hh
Issue 4421/3: Add Fluid class for caching fluid values
[lilypond.git] / lily / include / fluid.hh
1 #ifndef FLUID_HH
2 #define FLUID_HH
3
4 /*
5   This file is part of LilyPond, the GNU music typesetter.
6
7   Copyright (C) 2015 David Kastrup <dak@gnu.org>
8
9   LilyPond is free software: you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation, either version 3 of the License, or
12   (at your option) any later version.
13
14   LilyPond is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "lily-guile.hh"
24
25 // Fluid is a wrapper class for cached storage of GUILE fluids.
26 // You use it like
27 //
28 //   Fluid parser (ly_lily_module_constant ("%parser"));
29 //
30 // and when you first access `parser' as an SCM value, its value is
31 // fetched from the respective fluid (in this case `%parser') and
32 // cached for future accesses.  Be sure to use ly_lily_module_constant
33 // for the argument in order to have the SCM for the fluid itself only
34 // looked up once per run.
35 //
36 // Since fluids act as implicit function parameters, it can only be
37 // meaningfully employed for variables of automatic
38 // (function/block-local) duration.
39 //
40 // Once you create a fluid cache, it should be passed around by
41 // reference in order to keep the performance impact low.
42
43 class Fluid
44 {
45   SCM const fluid_;             // the fluid itself
46   SCM value_;                   // its cached value, SCM_UNDEFINED if unset
47
48   Fluid ();                     // No accessible default constructor
49   Fluid (const Fluid &);        // Don't copy
50   // Caching fluids only makes sense if we really treat them as
51   // function parameters, namely only modify them synchronized to
52   // function calls, like when using scm_with_fluid.  So no assignment
53   // operator or any other interface to scm_fluid_set_x.
54   Fluid & operator= (const Fluid &);
55 public:
56   Fluid (SCM fluid) : fluid_ (fluid), value_ (SCM_UNDEFINED)
57   {
58   }
59   operator SCM ()
60   {
61     if (SCM_UNBNDP (value_))
62       value_ = scm_fluid_ref (fluid_);
63     return value_;
64   }
65 };
66
67 #endif