]> git.donarmstrong.com Git - lilypond.git/commitdiff
Issue 4421/3: Add Fluid class for caching fluid values
authorDavid Kastrup <dak@gnu.org>
Fri, 29 May 2015 14:28:50 +0000 (16:28 +0200)
committerDavid Kastrup <dak@gnu.org>
Tue, 9 Jun 2015 13:55:30 +0000 (15:55 +0200)
lily/include/fluid.hh [new file with mode: 0644]

diff --git a/lily/include/fluid.hh b/lily/include/fluid.hh
new file mode 100644 (file)
index 0000000..44e908e
--- /dev/null
@@ -0,0 +1,67 @@
+#ifndef FLUID_HH
+#define FLUID_HH
+
+/*
+  This file is part of LilyPond, the GNU music typesetter.
+
+  Copyright (C) 2015 David Kastrup <dak@gnu.org>
+
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "lily-guile.hh"
+
+// Fluid is a wrapper class for cached storage of GUILE fluids.
+// You use it like
+//
+//   Fluid parser (ly_lily_module_constant ("%parser"));
+//
+// and when you first access `parser' as an SCM value, its value is
+// fetched from the respective fluid (in this case `%parser') and
+// cached for future accesses.  Be sure to use ly_lily_module_constant
+// for the argument in order to have the SCM for the fluid itself only
+// looked up once per run.
+//
+// Since fluids act as implicit function parameters, it can only be
+// meaningfully employed for variables of automatic
+// (function/block-local) duration.
+//
+// Once you create a fluid cache, it should be passed around by
+// reference in order to keep the performance impact low.
+
+class Fluid
+{
+  SCM const fluid_;             // the fluid itself
+  SCM value_;                   // its cached value, SCM_UNDEFINED if unset
+
+  Fluid ();                     // No accessible default constructor
+  Fluid (const Fluid &);        // Don't copy
+  // Caching fluids only makes sense if we really treat them as
+  // function parameters, namely only modify them synchronized to
+  // function calls, like when using scm_with_fluid.  So no assignment
+  // operator or any other interface to scm_fluid_set_x.
+  Fluid & operator= (const Fluid &);
+public:
+  Fluid (SCM fluid) : fluid_ (fluid), value_ (SCM_UNDEFINED)
+  {
+  }
+  operator SCM ()
+  {
+    if (SCM_UNBNDP (value_))
+      value_ = scm_fluid_ref (fluid_);
+    return value_;
+  }
+};
+
+#endif