]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/translator-group.cc
release: 1.3.93
[lilypond.git] / lily / translator-group.cc
index c824164d5053eeae495f8ce68d4e377d7b97f5e7..24416817d00941fb8a32113d78cbf357e636f99a 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
 #include "music-output-def.hh"
 #include "translator-group.hh"
 #include "translator.hh"
 #include "debug.hh"
-#include "pcursor.hh"
+#include "moment.hh"
+#include "scm-hash.hh"
+#include "killing-cons.tcc"
+#include "translator-def.hh"
 
 Translator_group::Translator_group (Translator_group const&s)
   : Translator(s)
 {
-  consists_str_arr_ = s.consists_str_arr_;
-  accepts_str_arr_ = s.accepts_str_arr_;
   iterator_count_ =0;
+  
+  Scheme_hash_table * tab =  new Scheme_hash_table (*s.properties_dict ());
+  properties_scm_ = tab->self_scm ();
+  scm_unprotect_object (tab->self_scm( ));
+}
+
+Scheme_hash_table*
+Translator_group::properties_dict () const
+{
+  return Scheme_hash_table::unsmob (properties_scm_);
 }
 
 Translator_group::~Translator_group ()
 {
-  assert (removable_b());
+  
+  //assert (removable_b());
 }
 
 
 Translator_group::Translator_group()
 {
   iterator_count_  = 0;
+  Scheme_hash_table *tab = new Scheme_hash_table ;
+  properties_scm_ = tab->self_scm ();
+
+  scm_unprotect_object (tab->self_scm ());
 }
 
 void
 Translator_group::check_removal()
 {
-  for (int i =0; i < group_l_arr ().size();) 
+  SCM next = SCM_EOL; 
+  for (SCM p = trans_group_list_; gh_pair_p (p); p = next)
     {
-      group_l_arr ()[i]->check_removal();
-      if (group_l_arr ()[i]->removable_b())
-       terminate_translator (group_l_arr ()[i]);
-      else 
-       i++;
+      next = gh_cdr (p);
+
+      Translator_group *trg =  dynamic_cast<Translator_group*> (unsmob_translator (gh_car (p)));
+
+      trg->check_removal ();
+      if (trg->removable_b())
+       terminate_translator (trg);
     }
 }
 
 
+SCM
+Translator_group::add_translator (SCM list, Translator *t)
+{
+  list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL));
+  t->daddy_trans_l_ = this;
+  t->output_def_l_ = output_def_l_;
+  t->add_processing ();
 
-IMPLEMENT_IS_TYPE_B1(Translator_group, Translator);
-
+  return list;
+}
 void
-Translator_group::add (Translator *trans_p)
+Translator_group::add_group_translator (Translator *t)
 {
-  trans_p_list_.bottom().add (trans_p);
-  trans_p->daddy_trans_l_ = this;
-  trans_p->output_def_l_ = output_def_l_;
-  trans_p->add_processing ();
+  trans_group_list_ = add_translator (trans_group_list_,t);
 }
 
+
+
 bool
 Translator_group::removable_b() const
 {
-  return !(iterator_count_ || group_l_arr ().size());
+  return trans_group_list_ == SCM_EOL && ! iterator_count_;
 }
 
-Translator_group * 
+Translator_group *
 Translator_group::find_existing_translator_l (String n, String id)
 {
-  if (is_alias_b (n) && (id_str_ == id || !id)) 
+  if (is_alias_b (n) && (id_str_ == id || id.empty_b ()))
     return this;
+
   Translator_group* r = 0;
-  for (int i =0; !r && i < group_l_arr ().size(); i++) 
+  for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = gh_cdr (p))
     {
-      r = group_l_arr ()[i]->find_existing_translator_l (n,id);
+      Translator *  t = unsmob_translator (gh_car (p));
+      
+      r = dynamic_cast<Translator_group*> (t)->find_existing_translator_l (n, id);
     }
-  
+
   return r;
 }
 
-Link_array<Translator_group>
-Translator_group::path_to_acceptable_translator (String type) const
-{
-  Link_array<Translator_group> retval; 
 
-  if (type_str_ == type)
-    {
-      retval.push (output_def_l ()->find_translator_l (type)->group_l ());
-    }
-  else for (int i=0; i < accepts_str_arr_.size (); i++)
-    {
-      Translator *t = output_def_l ()->find_translator_l (accepts_str_arr_[i]);
-      if (!t || !t->group_l ())
-       continue;
-      
-      Translator_group * g = t->group_l ();
-      
-      Link_array<Translator_group> result
-       = g->path_to_acceptable_translator (type);
-      if (result.size ())
-       {
-         retval.push (output_def_l ()->find_translator_l (type_str_)->group_l ());
-         retval.concat (result);
-         break;
-       }
-    }
 
-  return retval;
-}
 
 Translator_group*
 Translator_group::find_create_translator_l (String n, String id)
@@ -113,49 +114,57 @@ Translator_group::find_create_translator_l (String n, String id)
   Translator_group * existing = find_existing_translator_l (n,id);
   if (existing)
     return existing;
-  
-  Link_array<Translator_group> path = path_to_acceptable_translator (n);
-      
-  /* 
-        if path.size () == 1, then 
-        type_str_ == n, but not id == id_str_
-        */
-  if (path.size () > 1)
+
+  Link_array<Translator_def> path
+    = unsmob_translator_def (definition_)->path_to_acceptable_translator (ly_str02scm ((char*)n.ch_C()), output_def_l ());
+
+  if (path.size ())
     {
-      assert (path.size () > 1);
       Translator_group * current = this;
 
       // start at 1.  The first one (index 0) will be us.
-      for (int i=1; i < path.size (); i++) 
+      for (int i=0; i < path.size (); i++)
        {
-         Translator_group * new_group = path[i]->clone ()->group_l ();
-         current->add (new_group);
+         Translator_group * new_group = path[i]->instantiate (output_def_l_);
+
+         current->add_group_translator (new_group);
          current = new_group;
        }
       current->id_str_ = id;
       return current;
     }
-  
+
   Translator_group *ret = 0;
   if (daddy_trans_l_)
     ret = daddy_trans_l_->find_create_translator_l (n,id);
-  else 
+  else
     {
-      warning ("Can't find or create `" + n + "' called `" + id + "'\n");
+      warning (_f ("can't find or create `%s' called `%s'", n, id));
       ret =0;
     }
   return ret;
 }
 
-
 bool
-Translator_group::do_try_request (Request* req_l)
+Translator_group::try_music_on_nongroup_children (Music *m)
 {
   bool hebbes_b =false;
-  for (int i =0; !hebbes_b && i < nongroup_l_arr ().size() ; i++)
-    hebbes_b =nongroup_l_arr ()[i]->try_request (req_l);
+
+  
+  for (SCM p = simple_trans_list_; !hebbes_b && gh_pair_p (p); p = gh_cdr (p))
+    {
+      hebbes_b = unsmob_translator (gh_car (p))->try_music (m);
+    }
+  return hebbes_b;
+}
+
+bool
+Translator_group::do_try_music (Music* m)
+{
+  bool hebbes_b = try_music_on_nongroup_children (m);
+  
   if (!hebbes_b && daddy_trans_l_)
-    hebbes_b = daddy_trans_l_->try_request (req_l);
+    hebbes_b = daddy_trans_l_->try_music (m);
   return hebbes_b ;
 }
 
@@ -170,130 +179,201 @@ Translator_group::ancestor_l (int level)
 {
   if (!level || !daddy_trans_l_)
     return this;
-  
-  return daddy_trans_l_->ancestor_l (level-1);
-}
-
-Link_array<Translator_group>
-Translator_group::group_l_arr () const
-{
-  Link_array<Translator_group> groups;
-  for (PCursor<Translator*> i (trans_p_list_.top ()); i.ok (); i++)
-    {
-      if (i->group_l ())
-       groups.push (i->group_l ());
-    }
-  return groups;
-}
 
-Link_array<Translator>
-Translator_group::nongroup_l_arr () const
-{
-  Link_array<Translator> groups;
-  for (PCursor<Translator*> i (trans_p_list_.top ()); i.ok (); i++)
-    {
-      if (!i->group_l ())
-       groups.push (i.ptr ());
-    }
-  return groups;
+  return daddy_trans_l_->ancestor_l (level-1);
 }
 
 void
 Translator_group::terminate_translator (Translator*r_l)
 {
-  DOUT << "Removing " << r_l->name() << " at " << now_moment () << "\n";
   r_l->removal_processing();
-  Translator * trans_p =remove_translator_p (r_l);
-  
-  delete trans_p;
+  /*
+    Return value ignored. GC does the rest.
+   */
+  remove_translator_p (r_l);
 }
 
+
+/**
+   Remove a translator from the hierarchy.
+ */
 Translator *
 Translator_group::remove_translator_p (Translator*trans_l)
 {
-  PCursor<Translator*> trans_cur (trans_p_list_.find (trans_l));
-  Translator * t =  trans_cur.remove_p();
-  /*
-    For elegant design, we would do this too.  Alas, it does not work yet..
-    
-    t-> removal_processing ();
-  */
-  t-> daddy_trans_l_ = 0;
-  return t;
-}
+  assert (trans_l);
 
+  trans_group_list_ = scm_delq_x (trans_l->self_scm (), trans_group_list_);
+  trans_l->daddy_trans_l_ = 0;
+  return trans_l;
+}
 
+#if 0
+/*
+  should not use, instead: use properties to communicate between engravers.
+ */
 Translator*
-Translator_group::get_simple_translator (char const *type) const
+Translator_group::get_simple_translator (String type) const
 {
-  for (int i=0; i < nongroup_l_arr ().size(); i++) 
+  for (SCM p = simple_trans_list_;  gh_pair_p (p); p =gh_cdr (p))
     {
-      if (nongroup_l_arr ()[i]->name() == type)
-       return nongroup_l_arr ()[i];
+      if (classname (unsmob_translator (gh_car (p))) == type)
+       return unsmob_translator (gh_car (p));
     }
   if (daddy_trans_l_)
     return daddy_trans_l_->get_simple_translator (type);
   return 0;
 }
-
+#endif 
 
 bool
 Translator_group::is_bottom_translator_b () const
 {
-  return !accepts_str_arr_.size ();
-}
-
+  return !gh_string_p (unsmob_translator_def (definition_)->default_child_context_name ());
 
+}
 
 Translator_group*
 Translator_group::get_default_interpreter()
 {
-  if (accepts_str_arr_.size())
+  if (!is_bottom_translator_b ())
     {
-      Translator*t = output_def_l ()->find_translator_l (accepts_str_arr_[0]);
-      Translator_group * g= t->clone ()->group_l ();
-      add (g);
-      
-      if (!g->is_bottom_translator_b ())
-       return g->get_default_interpreter ();
+      SCM nm = unsmob_translator_def (definition_)->default_child_context_name ();
+      SCM st = output_def_l ()->find_translator_l (nm);
+
+      Translator_def *t = unsmob_translator_def (st);
+      if (!t)
+       {
+         warning (_f ("can't find or create: `%s'", ly_scm2string (nm).ch_C()));
+         t = unsmob_translator_def (this->definition_);
+       }
+      Translator_group *tg = t->instantiate (output_def_l_);
+      add_group_translator (tg);
+
+      if (!tg->is_bottom_translator_b ())
+       return tg->get_default_interpreter ();
       else
-       return g;
+       return tg;
     }
   return this;
 }
 
+static void
+static_each (SCM list, Method_pointer method)
+{
+  for (SCM p = list; gh_pair_p (p); p = gh_cdr(p))
+    (unsmob_translator (gh_car (p))->*method) ();
+  
+}
+
 void
-Translator_group::each (Method_pointer method) const
+Translator_group::each (Method_pointer method) 
 {
-  for (PCursor<Translator*> i (trans_p_list_.top ()); i.ok (); i++)
-    (i.ptr()->*method) ();     
+  static_each (simple_trans_list_, method);
+  static_each (trans_group_list_, method);
 }
 
+
+
 void
-Translator_group::do_print() const
+Translator_group::do_add_processing ()
 {
-#ifndef NPRINT
-  if (!check_debug)
-    return ;
-  if (status == ORPHAN)
+  unsmob_translator_def (definition_)->apply_property_operations (this);
+  for (SCM s = simple_trans_list_; gh_pair_p (s) ; s = gh_cdr (s))
     {
-      DOUT << "consists of: ";
-      for (int i=0; i < consists_str_arr_.size (); i++)
-       DOUT << consists_str_arr_[i] << ", ";
-      DOUT << "\naccepts: ";
-      for (int i=0; i < accepts_str_arr_.size (); i++)
-       DOUT << accepts_str_arr_[i] << ", ";
+      Translator * t = unsmob_translator (gh_car (s));
+      t->add_processing ();
     }
-  else
+}
+
+/*
+  PROPERTIES
+ */
+Translator_group*
+Translator_group::where_defined (SCM sym) const
+{
+  if (properties_dict ()->elem_b (sym))
     {
-      if (id_str_)
-       DOUT << "ID: " << id_str_ ;
-      DOUT << " iterators: " << iterator_count_<< "\n";
+      return (Translator_group*)this;
     }
-  each (&Translator::print);
-#endif
+
+  return (daddy_trans_l_) ? daddy_trans_l_->where_defined (sym) : 0;
+}
+
+SCM
+Translator_group::get_property (SCM sym) const
+{
+  SCM val =SCM_UNDEFINED;
+  if (properties_dict ()->try_retrieve (sym, &val))
+    return val;
+
+  if (daddy_trans_l_)
+    return daddy_trans_l_->get_property (sym);
+  
+  return val;
+}
+
+void
+Translator_group::set_property (String id, SCM val)
+{
+  set_property (ly_symbol2scm (id.ch_C()), val);
 }
 
+void
+Translator_group::set_property (SCM sym, SCM val)
+{
+  properties_dict ()->set (sym, val);
+}
+
+/*
+  Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
+  entry from a translator property list by name of PROP
+*/
+void
+Translator_group::execute_single_pushpop_property (SCM prop, SCM eltprop, SCM val)
+{
+  if (gh_symbol_p(prop))
+    {
+      if (val != SCM_UNDEFINED)
+       {
+         SCM prev = get_property (prop);
+
+         /*
+           we don't tack onto SCM_UNDEFINED, because it creates
+           errors down the line, if we do scm_assoc().
+          */
+         if (gh_pair_p (prev) || prev == SCM_EOL)
+           {
+                 prev = gh_cons (gh_cons (eltprop, val), prev);
+                 set_property (prop, prev);
+           }
+       }
+      else
+       {
+         SCM prev = get_property (prop);
+
+         SCM newprops= SCM_EOL ;
+         while (gh_pair_p (prev) && gh_caar (prev) != eltprop)
+           {
+             newprops = gh_cons (gh_car (prev), newprops);
+             prev = gh_cdr (prev);
+           }
+         
+         if (gh_pair_p (prev))
+           {
+             newprops = scm_reverse_x (newprops, gh_cdr (prev));
+             set_property (prop, newprops);
+           }
+       }
+    }
+}
+
+
+
+
+
+/*
+  STUBS
+*/
 void
 Translator_group::do_pre_move_processing ()
 {
@@ -307,9 +387,9 @@ Translator_group::do_post_move_processing ()
 }
 
 void
-Translator_group::do_process_requests ()
+Translator_group::do_process_music ()
 {
-  each (&Translator::process_requests);
+  each (&Translator::process_music);
 }
 
 void
@@ -323,16 +403,3 @@ Translator_group::do_removal_processing ()
 {
   each (&Translator::removal_processing);
 }
-
-void
-Translator_group::do_add_processing ()
-{
-   for (int i=0; i < consists_str_arr_.size(); i++) 
-    {
-      Translator * t = output_def_l ()->find_translator_l (consists_str_arr_[i]);
-      if (!t)
-       warning ("Could not find `" +consists_str_arr_[i]+ "'");
-      else
-       add (t->clone ());
-    }
-}