]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/context.cc
* scm/lily-library.scm (ordered-cons): new function.
[lilypond.git] / lily / context.cc
index 852303f25de174c52f74eb8b97e51927559ca750..2445ea12f38a96c33a71dc17782c776914af4300 100644 (file)
@@ -8,17 +8,17 @@
 
 #include "context.hh"
 
-#include "program-option.hh"
 #include "context-def.hh"
+#include "international.hh"
 #include "ly-smobs.icc"
 #include "main.hh"
 #include "output-def.hh"
+#include "profile.hh"
+#include "program-option.hh"
 #include "scm-hash.hh"
 #include "score-context.hh"
 #include "translator-group.hh"
 #include "warn.hh"
-#include "lilypond-key.hh"
-#include "profile.hh"
 
 bool
 Context::is_removable () const
@@ -45,7 +45,8 @@ Context::check_removal ()
     }
 }
 
-Context::Context (Context const &)
+Context::Context (Context const &src)
+  : key_manager_ (src.key_manager_)
 {
   assert (false);
 }
@@ -84,8 +85,8 @@ Context::add_context (Context *t)
 
 
 Context::Context (Object_key const *key)
+  : key_manager_ (key)
 {
-  key_ = key;
   daddy_context_ = 0;
   init_ = false;
   aliases_ = SCM_EOL;
@@ -105,38 +106,42 @@ Context::Context (Object_key const *key)
     UGH UGH
     const correctness.
   */
-  if (key_)
-    ((Object_key *)key)->unprotect ();
+  key_manager_.unprotect();
 }
 
 /* TODO:  this shares code with find_create_context ().  */
 Context *
-Context::create_unique_context (SCM n, SCM operations)
+Context::create_unique_context (SCM name, string id, SCM operations)
 {
   /*
     Don't create multiple score contexts.
   */
   if (dynamic_cast<Global_context *> (this)
       && dynamic_cast<Global_context *> (this)->get_score_context ())
-    return get_score_context ()->create_unique_context (n, operations);
+    return get_score_context ()->create_unique_context (name, id, operations);
 
   /*
     TODO: use accepts_list_.
   */
-  Link_array<Context_def> path
-    = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
+  vector<Context_def*> path
+    = unsmob_context_def (definition_)->path_to_acceptable_context (name, get_output_def ());
 
   if (path.size ())
     {
       Context *current = this;
 
       // start at 1.  The first one (index 0) will be us.
-      for (int i = 0; i < path.size (); i++)
+      for (vsize i = 0; i < path.size (); i++)
        {
-         SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
-
+         SCM ops = SCM_EOL;
+         string id_str = "\\new";
+         if (i == path.size () - 1)
+           {
+             ops = operations;
+             id_str = id;
+           }
          current = current->create_context (path[i],
-                                            "\\new",
+                                            id_str,
                                             ops);
        }
 
@@ -149,18 +154,18 @@ Context::create_unique_context (SCM n, SCM operations)
   */
   Context *ret = 0;
   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
-    ret = daddy_context_->create_unique_context (n, operations);
+    ret = daddy_context_->create_unique_context (name, id, operations);
   else
     {
       warning (_f ("can't find or create new `%s'",
-                  ly_symbol2string (n).c_str ()));
+                  ly_symbol2string (name).c_str ()));
       ret = 0;
     }
   return ret;
 }
 
 Context *
-Context::find_create_context (SCM n, String id, SCM operations)
+Context::find_create_context (SCM n, string id, SCM operations)
 {
   /*
     Don't create multiple score contexts.
@@ -181,7 +186,7 @@ Context::find_create_context (SCM n, String id, SCM operations)
   /*
     TODO: use accepts_list_.
   */
-  Link_array<Context_def> path
+  vector<Context_def*> path
     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
 
   if (path.size ())
@@ -189,11 +194,11 @@ Context::find_create_context (SCM n, String id, SCM operations)
       Context *current = this;
 
       // start at 1.  The first one (index 0) will be us.
-      for (int i = 0; i < path.size (); i++)
+      for (vsize i = 0; i < path.size (); i++)
        {
          SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
 
-         String this_id = "";
+         string this_id = "";
          if (i == path.size () -1)
            this_id = id;
 
@@ -223,11 +228,11 @@ Context::find_create_context (SCM n, String id, SCM operations)
 
 Context *
 Context::create_context (Context_def *cdef,
-                        String id,
+                        string id,
                         SCM ops)
 {
-  String type = ly_symbol2string (cdef->get_context_name ());
-  Object_key const *key = get_context_key (type, id);
+  string type = ly_symbol2string (cdef->get_context_name ());
+  Object_key const *key = key_manager_.get_context_key (now_mom(), type, id);
   Context *new_context
     = cdef->instantiate (ops, key);
 
@@ -238,45 +243,6 @@ Context::create_context (Context_def *cdef,
   return new_context;
 }
 
-Object_key const *
-Context::get_context_key (String type, String id)
-{
-  if (!use_object_keys)
-    return 0;
-
-  String now_key = type + "@" + id;
-
-  int disambiguation_count = 0;
-  if (context_counts_.find (now_key) != context_counts_.end ())
-    disambiguation_count = context_counts_[now_key];
-
-  context_counts_[now_key] = disambiguation_count + 1;
-
-  return new Lilypond_context_key (key (),
-                                  now_mom (),
-                                  type, id,
-                                  disambiguation_count);
-}
-
-Object_key const *
-Context::get_grob_key (String name)
-{
-  if (!use_object_keys)
-    return 0;
-
-  int disambiguation_count = 0;
-  if (grob_counts_.find (name) != grob_counts_.end ())
-    disambiguation_count = grob_counts_[name];
-  grob_counts_[name] = disambiguation_count + 1;
-
-  Object_key *k = new Lilypond_grob_key (key (),
-                                        now_mom (),
-                                        name,
-                                        disambiguation_count);
-
-  return k;
-}
-
 /*
   Default child context as a SCM string, or something else if there is
   none.
@@ -303,7 +269,7 @@ Context::get_default_interpreter ()
       SCM nm = default_child_context_name ();
       SCM st = find_context_def (get_output_def (), nm);
 
-      String name = ly_symbol2string (nm);
+      string name = ly_symbol2string (nm);
       Context_def *t = unsmob_context_def (st);
       if (!t)
        {
@@ -414,7 +380,7 @@ Context::remove_context (Context *trans)
 */
 Context *
 find_context_below (Context *where,
-                   SCM type, String id)
+                   SCM type, string id)
 {
   if (where->is_alias (type))
     {
@@ -447,7 +413,7 @@ Context::context_name_symbol () const
   return td->get_context_name ();
 }
 
-String
+string
 Context::context_name () const
 {
   return ly_symbol2string (context_name_symbol ());
@@ -511,12 +477,23 @@ Context::print_smob (SCM s, SCM port, scm_print_state *)
   return 1;
 }
 
+Object_key const *
+Context::get_grob_key (string name)
+{
+  return key_manager_.get_grob_key (now_mom (), name);
+}
+
+Object_key const *
+Context::get_context_key (string name, string id)
+{
+  return key_manager_.get_context_key (now_mom (), name, id);
+}
+
 SCM
 Context::mark_smob (SCM sm)
 {
   Context *me = (Context *) SCM_CELL_WORD_1 (sm);
-  if (me->key_)
-    scm_gc_mark (me->key_->self_scm ());
+  me->key_manager_.gc_mark();
 
   scm_gc_mark (me->context_list_);
   scm_gc_mark (me->aliases_);
@@ -572,8 +549,7 @@ Context::clear_key_disambiguations ()
   if (!use_object_keys)
     return;
 
-  grob_counts_.clear ();
-  context_counts_.clear ();
+  key_manager_.clear ();
   for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
     unsmob_context (scm_car (s))->clear_key_disambiguations ();
 }