2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "protected-scm.hh"
22 // We store the data in the car of a cons cell: it is faster to keep
23 // only one object protected during the life time of Protected_scm
26 Protected_scm::Protected_scm ()
27 : object_ (SCM_UNDEFINED)
31 Protected_scm::Protected_scm (SCM s)
34 // Only allow immediate objects at construction time. Protected_scm
35 // is intended for variables of static duration, and creating
36 // non-immediate objects when GUILE is not yet up is a bad idea.
40 SCM Protected_scm::list_ = SCM_EOL;
41 SCM Protected_scm::last_ = SCM_EOL;
44 Protected_scm::protectify (SCM s)
47 if (SCM_CONSP (last_))
48 SCM_SETCDR (last_, s);
50 list_ = scm_permanent_object (s);
55 Protected_scm::operator = (SCM s)
57 if (SCM_CONSP (object_))
58 SCM_SETCAR (object_, s);
68 Protected_scm::operator = (Protected_scm const &s)
70 return *this = (SCM) s;
73 Protected_scm::operator SCM const & () const
75 if (SCM_CONSP (object_))
76 return *SCM_CARLOC (object_);
80 Protected_scm::operator SCM & ()
82 // The reference may be used to overwrite an immediate value with a
83 // non-immediate one, so we _have_ to create full protection.
84 if (!SCM_CONSP (object_))
87 return *SCM_CARLOC (object_);