]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/fluid.hh
Imported Upstream version 2.19.45
[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 (Lily::f_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', cf
32 // lily/lily-imports.cc) and cached for future accesses.
33 //
34 // Since fluids act as implicit function parameters, it can only be
35 // meaningfully employed for variables of automatic
36 // (function/block-local) duration.
37 //
38 // Once you create a fluid cache, it should be passed around by
39 // reference in order to keep the performance impact low.
40
41 class Fluid
42 {
43   SCM const fluid_;             // the fluid itself
44   SCM value_;                   // its cached value, SCM_UNDEFINED if unset
45
46   Fluid ();                     // No accessible default constructor
47   Fluid (const Fluid &);        // Don't copy
48   // Caching fluids only makes sense if we really treat them as
49   // function parameters, namely only modify them synchronized to
50   // function calls, like when using scm_with_fluid.  So no assignment
51   // operator or any other interface to scm_fluid_set_x.
52   Fluid & operator= (const Fluid &);
53 public:
54   Fluid (SCM fluid) : fluid_ (fluid), value_ (SCM_UNDEFINED)
55   {
56   }
57   operator SCM ()
58   {
59     if (SCM_UNBNDP (value_))
60       value_ = scm_fluid_ref (fluid_);
61     return value_;
62   }
63 };
64
65 #endif