X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fprotected-scm.cc;h=a9fabdc4f2e6fa69803b4c2aaa2c3583039d2154;hb=c991eb5d49a1b36c81a890c4574f75d8834bb9a0;hp=225c64dc60fc220bf03910f25575a539a3ecbdfd;hpb=28d702692ec7fa5f4de6a60a7e165ec3305a5daf;p=lilypond.git diff --git a/lily/protected-scm.cc b/lily/protected-scm.cc index 225c64dc60..a9fabdc4f2 100644 --- a/lily/protected-scm.cc +++ b/lily/protected-scm.cc @@ -37,18 +37,20 @@ Protected_scm::Protected_scm (SCM s) assert (SCM_IMP (s)); } -// For static objects, this will be called at program exit. With the -// state of the memory system unknown, we refrain from any cleanup -// actions outside of the object memory itself. +SCM Protected_scm::list_ = SCM_EOL; +SCM Protected_scm::last_ = SCM_EOL; -Protected_scm::~Protected_scm () +void +Protected_scm::protectify (SCM s) { - object_ = SCM_UNDEFINED; + s = scm_list_1 (s); + if (SCM_CONSP (last_)) + SCM_SETCDR (last_, s); + else + list_ = scm_permanent_object (s); + last_ = object_ = s; } -SCM Protected_scm::list_ = SCM_EOL; -SCM Protected_scm::last_ = SCM_EOL; - Protected_scm & Protected_scm::operator = (SCM s) { @@ -57,14 +59,7 @@ Protected_scm::operator = (SCM s) else if (SCM_IMP (s)) object_ = s; else - { - s = scm_list_1 (s); - if (SCM_CONSP (last_)) - SCM_SETCDR (last_, s); - else - list_ = scm_permanent_object (s); - last_ = object_ = s; - } + protectify (s); return *this; } @@ -75,7 +70,19 @@ Protected_scm::operator = (Protected_scm const &s) return *this = (SCM) s; } -Protected_scm::operator SCM () const +Protected_scm::operator SCM const & () const { - return SCM_CONSP (object_) ? SCM_CAR (object_) : object_; + if (SCM_CONSP (object_)) + return *SCM_CARLOC (object_); + return object_; +} + +Protected_scm::operator SCM & () +{ + // 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_); }