From c8d3b77715bf284fd6aca074d4fa1fef136c7f0d Mon Sep 17 00:00:00 2001 From: David Kastrup Date: Fri, 29 May 2015 16:28:50 +0200 Subject: [PATCH] Issue 4421/3: Add Fluid class for caching fluid values --- lily/include/fluid.hh | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lily/include/fluid.hh diff --git a/lily/include/fluid.hh b/lily/include/fluid.hh new file mode 100644 index 0000000000..44e908e667 --- /dev/null +++ b/lily/include/fluid.hh @@ -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 + + 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 . +*/ + +#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 -- 2.39.2