]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/grob-property.cc
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / lily / grob-property.cc
index dfeee301dacffee50e6afaacf6622b9d0315d908..c4dce927b43f86f3265348779ede3ef846f6b1b9 100644 (file)
 /*
   Implement storage and manipulation of grob properties.
- */
+*/
 
-#include <string.h>
-#include <math.h>
+#include <cstring>
 
 #include "main.hh"
 #include "input-smob.hh"
-
-#include "group-interface.hh"
+#include "pointer-group-interface.hh"
 #include "misc.hh"
 #include "paper-score.hh"
-#include "paper-def.hh"
-#include "grob.hh"
-#include "debug.hh"
+#include "output-def.hh"
 #include "spanner.hh"
 #include "item.hh"
 #include "misc.hh"
 #include "item.hh"
+#include "program-option.hh"
+#include "profile.hh"
+#include "simple-closure.hh"
 
-/*
-  HASHING_FOR_MUTABLE_PROPS:
-
-  
-  plain, -O0 compile
-  
-user   0m12.400s
-
-sz == 13, -O0 compile
-  
- xdvi trip
-
-user   0m13.780s
-  
-sz == 5
-
-
-user   0m13.000s
+SCM
+Grob::get_property_alist_chain (SCM def) const
+{
+  return scm_list_n (mutable_property_alist_,
+                    immutable_property_alist_,
+                    def,
+                    SCM_UNDEFINED);
+}
 
-sz == 3
 
+extern void check_interfaces_for_property (Grob const *me, SCM sym);
 
-user   0m13.080s
+void
+Grob::internal_set_property (SCM sym, SCM v)
+{
+#ifndef NDEBUG
+  SCM grob_p = ly_lily_module_constant ("ly:grob?");
+  SCM grob_list_p = ly_lily_module_constant ("grob-list?");
+  SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
 
-Hashing doesn't improve the result of grob property lookup, at least
-not with naive hashing. It is possible that the overhead of the
-scm_hash* functions take too much time. One way to solve this is by
-using vector accesses directly, and precompute the hashvalues, similar
-to CACHE_SYMBOLS. That option could only cause slowdowns if the hash
-tables produces weird cache-line trashing.
+  if (type == grob_p
+      || type == grob_list_p
+      || (unsmob_grob (v) && ly_symbol2scm ("cause") != sym))
+    {
+      scm_display (scm_list_2 (sym, type), scm_current_output_port ());
+      assert (0);
+    }
+#endif
 
-Second option: we could index immutable props in a hash tab as
-well. This only takes space, since they are immutable no updates are
-needed.  This does take a lot of space, since we must duplicate the
-alists (but not the entries).
+  /* Perhaps we simply do the assq_set, but what the heck. */
+  if (!is_live ())
+    return;
 
-*/
+  if (do_internal_type_checking_global)
+    {
+      if (!ly_is_procedure (v)
+         && !is_simple_closure (v)
+         && v != ly_symbol2scm ("calculation-in-progress") 
+         && !type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
+       abort ();
+      check_interfaces_for_property (this, sym);
+    }
 
-// #define HASHING_FOR_MUTABLE_PROPS
+  mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, v);
+}
 
-/*
-  Remove the value associated with KEY, and return it. The result is
-  that a next call will yield SCM_EOL (and not the underlying
-  `basic' property.
-*/
+//#define PROFILE_PROPERTY_ACCESSES
 SCM
-Grob::remove_grob_property (const char* key)
+Grob::get_property_data (SCM sym) const
 {
-  SCM val = get_grob_property (key);
-  if (val != SCM_EOL)
-    set_grob_property (key, SCM_EOL);
-  return val;
-}
+#ifndef NDEBUG
+  if (profile_property_accesses)
+    note_property_access (&grob_property_lookup_table, sym);
+#endif
+  
+  SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
+  if (handle != SCM_BOOL_F)
+    return scm_cdr (handle);
+
+  handle = scm_sloppy_assq (sym, immutable_property_alist_);
 
+  if (do_internal_type_checking_global && scm_is_pair (handle))
+    {
+      SCM val = scm_cdr (handle);
+      if (!ly_is_procedure (val)
+         && !is_simple_closure (val)
+         && !type_check_assignment (sym, val, 
+                                 ly_symbol2scm ("backend-type?")))
+       abort ();
+
+      check_interfaces_for_property (this, sym);
+    }
+  
+  return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
+}
 
 SCM
-Grob::get_property_alist_chain (SCM def) const
+Grob::internal_get_property (SCM sym) const
 {
-#ifndef HASHING_FOR_MUTABLE_PROPS
-  return  scm_list_n (mutable_property_alist_,
-                     immutable_property_alist_,
-                     def,
-                     SCM_UNDEFINED);
-#else
-  SCM chain = gh_list (immutable_property_alist_, def, SCM_UNDEFINED);
-  SCM * velts = SCM_VELTS (mutable_property_alist_);
-  int l = SCM_VECTOR_LENGTH(mutable_property_alist_);
-  for (int i = 0; i < l; i++)
+  SCM val = get_property_data (sym);
+  if (ly_is_procedure (val)
+      || is_simple_closure (val))
     {
-      if (gh_pair_p (velts[i]))
-       chain = gh_cons ( velts[i], chain);
+      val = ((Grob*)this)->try_callback (sym, val);
     }
-
-  return chain;
-#endif
+  
+  return val;
 }
 
+#ifndef NDEBUG
+#include "protected-scm.hh"
 
+Protected_scm grob_property_callback_stack = SCM_EOL;
+bool debug_property_callbacks = 0;
+#endif
 
-/*
-  This special add_thing routine is slightly more efficient than
-
-    set_prop (name,cons (thing, get_prop (name)))
+SCM
+Grob::try_callback (SCM sym, SCM proc)
+{      
+  SCM marker = ly_symbol2scm ("calculation-in-progress");
+  /*
+    need to put a value in SYM to ensure that we don't get a
+    cyclic call chain.
+  */
+  mutable_property_alist_
+    = scm_assq_set_x (mutable_property_alist_, sym, marker);
 
-  since it can reuse the handle returned by scm_assq().
-*/
-void
-Grob::add_to_list_property (SCM sym, SCM thing) 
-{
-  SCM handle
-#ifndef HASHING_FOR_MUTABLE_PROPS
-    = scm_sloppy_assq (sym, mutable_property_alist_)
-#else
-    = scm_hashq_get_handle (mutable_property_alist_, sym);
+#ifndef NDEBUG
+  if (debug_property_callbacks)
+    grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
 #endif
-    ;
-  if (handle != SCM_BOOL_F)
+
+  SCM value = SCM_EOL;
+  if (ly_is_procedure (proc))
+    value = scm_call_1 (proc, self_scm ());
+  else if (is_simple_closure (proc))
     {
-      gh_set_cdr_x (handle, gh_cons (thing, gh_cdr (handle)));
+      value = evaluate_with_simple_closure (self_scm (),
+                                           simple_closure_expression (proc));
     }
-  else
-    {
-      /*
-       There is no mutable prop yet, so create an entry, and put it in front of the
-       mutable prop list.
-      */
-      handle = scm_sloppy_assq (sym, immutable_property_alist_);
-      SCM tail = (handle != SCM_BOOL_F) ? gh_cdr(handle) : SCM_EOL;
-      SCM val = gh_cons (thing, tail);
-#ifndef HASHING_FOR_MUTABLE_PROPS
-      mutable_property_alist_ = gh_cons (gh_cons (sym, val),
-                                        mutable_property_alist_);
-#else
-      scm_hashq_set_x (mutable_property_alist_, sym, val);
+#ifndef NDEBUG
+  if (debug_property_callbacks)
+    grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
 #endif
+         
+  /*
+    If the function returns SCM_UNSPECIFIED, we assume the
+    property has been set with an explicit set_property()
+    call.
+  */
+  if (value == SCM_UNSPECIFIED)
+    {
+      value = internal_get_property (sym);
+      if (value == marker)
+       mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
     }
+  else
+    internal_set_property (sym, value);
+         
+  return value;
 }
 
-
-extern void check_interfaces_for_property (Grob const *me, SCM sym);
-
 void
-Grob::internal_set_grob_property (SCM s, SCM v)
+Grob::internal_set_object (SCM s, SCM v)
 {
-  assert (live());
-
-#ifndef NDEBUG
-  if (internal_type_checking_global_b)
-    {
-      assert (type_check_assignment (s, v, ly_symbol2scm ("backend-type?")));
-      check_interfaces_for_property(this, s);
-    }
-#endif
+  /* Perhaps we simply do the assq_set, but what the heck. */
+  if (!is_live ())
+    return;
 
-#ifndef HASHING_FOR_MUTABLE_PROPS
-  mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
-#else
-  scm_hashq_set_x (mutable_property_alist_, s, v);
-#endif
+  object_alist_ = scm_assq_set_x (object_alist_, s, v);
 }
 
+void
+Grob::del_property (SCM sym)
+{
+  mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
+}
 
 SCM
-Grob::internal_get_grob_property (SCM sym) const
+Grob::internal_get_object (SCM sym) const
 {
-#ifndef HASHING_FOR_MUTABLE_PROPS
-  SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
-  if (s != SCM_BOOL_F)
-    return ly_cdr (s);
-#else
-  if (mutable_property_alist_ == SCM_EOL)
-    return SCM_EOL;
-  
-  SCM s = scm_hashq_ref (mutable_property_alist_, sym, SCM_EOL);
-  if (s!=SCM_EOL)
-    return s;
+#ifdef PROFILE_PROPERTY_ACCESSES
+  note_property_access (&grob_property_lookup_table, sym);
 #endif
 
-  s = scm_sloppy_assq (sym, immutable_property_alist_);
-  
-#ifndef NDEBUG
-  if (internal_type_checking_global_b && gh_pair_p (s))
-    {
-      assert (type_check_assignment (sym, gh_cdr (s), ly_symbol2scm ("backend-type?")));
-      check_interfaces_for_property(this, sym);
-    }
-#endif
+  SCM s = scm_sloppy_assq (sym, object_alist_);
 
-  return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s); 
+  return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
 }
 
-void
-Grob::substitute_mutable_properties (SCM crit, SCM orig)
+bool
+Grob::is_live () const
 {
-  set_break_subsititution(crit);
-#ifndef HASHING_FOR_MUTABLE_PROPS
-  mutable_property_alist_ = substitute_mutable_property_alist (orig);
-#else
-  if (orig == SCM_EOL)
-    {
-      mutable_property_alist_ = SCM_EOL;
-      return ;
-    }
-  
-  SCM * src_elts = SCM_VELTS (orig);
-  SCM * dest_elts = SCM_VELTS (mutable_property_alist_);  
-  unsigned int l = SCM_VECTOR_LENGTH(mutable_property_alist_);
-  assert (l == SCM_VECTOR_LENGTH(orig));
-  for (unsigned int i = 0; i < l; i++)
-    {
-      dest_elts[i] = substitute_mutable_property_alist (src_elts[i]);
-    }
-#endif
+  return immutable_property_alist_ != SCM_EOL;
 }
 
 
 bool
-Grob::live () const
+Grob::internal_has_interface (SCM k)
 {
-  return immutable_property_alist_ != SCM_EOL;
+  return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
 }