]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/protected-scm.cc
Issue 5167/6: Changes: show \markup xxx = ... \etc assignments
[lilypond.git] / lily / protected-scm.cc
index c0eb374bafc70c8e762774a5ada363735f46a505..3a16254187a05b0b7880a53ec9250c47071e8aee 100644 (file)
@@ -37,13 +37,18 @@ 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;
 }
 
 Protected_scm &
@@ -51,8 +56,10 @@ Protected_scm::operator = (SCM s)
 {
   if (SCM_CONSP (object_))
     SCM_SETCAR (object_, s);
+  else if (SCM_IMP (s))
+    object_ = s;
   else
-    object_ = SCM_NIMP (s) ? scm_permanent_object (scm_list_1 (s)) : s;
+    protectify (s);
 
   return *this;
 }
@@ -63,7 +70,27 @@ Protected_scm::operator = (Protected_scm const &s)
   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_;
+}
+
+Protected_scm::operator SCM & ()
 {
-  return SCM_CONSP (object_) ? SCM_CAR (object_) : 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_);
 }