X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fprotected-scm.cc;h=3a16254187a05b0b7880a53ec9250c47071e8aee;hb=97a0169312a260933246ab224e4f8b0969871dd5;hp=356bbf2121510dcd5fe7ea9dec6a6ad1305f3a27;hpb=8aad615ea7bb31f49a0c2afc21eea5ff5de20437;p=lilypond.git diff --git a/lily/protected-scm.cc b/lily/protected-scm.cc index 356bbf2121..3a16254187 100644 --- a/lily/protected-scm.cc +++ b/lily/protected-scm.cc @@ -1,61 +1,96 @@ -/* - protected-scm.cc -- implement Protected_scm - - source file of the GNU LilyPond music typesetter - - (c) 1998--1999 Han-Wen Nienhuys - - */ +/* + This file is part of LilyPond, the GNU music typesetter. + + Copyright (C) 1998--2015 Han-Wen Nienhuys + + 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 "protected-scm.hh" -extern "C" -{ -#include -}; +// We store the data in the car of a cons cell: it is faster to keep +// only one object protected during the life time of Protected_scm +// than several. Protected_scm::Protected_scm () + : object_ (SCM_UNDEFINED) { - object_ = 0; } Protected_scm::Protected_scm (SCM s) + : object_ (s) { - object_ = s ? scm_protect_object (s): 0; + // Only allow immediate objects at construction time. Protected_scm + // is intended for variables of static duration, and creating + // non-immediate objects when GUILE is not yet up is a bad idea. + assert (SCM_IMP (s)); } -Protected_scm::Protected_scm (Protected_scm const &s) +SCM Protected_scm::list_ = SCM_EOL; +SCM Protected_scm::last_ = SCM_EOL; + +void +Protected_scm::protectify (SCM s) { - object_ = s.object_ ? scm_protect_object (s.object_) : 0; + s = scm_list_1 (s); + if (SCM_CONSP (last_)) + SCM_SETCDR (last_, s); + else + list_ = scm_permanent_object (s); + last_ = object_ = s; } -Protected_scm & -Protected_scm::operator =(Protected_scm const &s) +Protected_scm & +Protected_scm::operator = (SCM s) { - if (this == &s) - return *this; - if (object_) - scm_unprotect_object(object_); + if (SCM_CONSP (object_)) + SCM_SETCAR (object_, s); + else if (SCM_IMP (s)) + object_ = s; + else + protectify (s); - object_ = (s.object_) ? scm_protect_object (s.object_): 0; return *this; } -Protected_scm::~Protected_scm () +Protected_scm & +Protected_scm::operator = (Protected_scm const &s) { - if (object_) - { - scm_unprotect_object (object_); - object_ =0L; // be nice to conservative GC - } + return *this = (SCM) s; } -Protected_scm::operator SCM () const +Protected_scm::operator SCM const & () const { + if (SCM_CONSP (object_)) + return *SCM_CARLOC (object_); return object_; } -SCM -Protected_scm::to_SCM () const +Protected_scm::operator SCM & () { - return object_; + // The reference may be used to overwrite an immediate value with a + // non-immediate one, so we _have_ to create full protection. + if (!SCM_CONSP (object_)) + protectify (object_); + + return *SCM_CARLOC (object_); +} + +bool +Protected_scm::is_bound () const +{ + if (SCM_CONSP (object_)) + return !SCM_UNBNDP (SCM_CAR (object_)); + return !SCM_UNBNDP (object_); }