]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* flower/include/pqueue.hh: Derive from std::vector.
[lilypond.git] / lily / context.cc
1 /*
2   context.cc -- implement Context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10
11 #include "context-def.hh"
12 #include "international.hh"
13 #include "lilypond-key.hh"
14 #include "ly-smobs.icc"
15 #include "main.hh"
16 #include "output-def.hh"
17 #include "profile.hh"
18 #include "program-option.hh"
19 #include "scm-hash.hh"
20 #include "score-context.hh"
21 #include "translator-group.hh"
22 #include "warn.hh"
23
24 bool
25 Context::is_removable () const
26 {
27   return context_list_ == SCM_EOL && ! iterator_count_
28     && !dynamic_cast<Score_context const *> (this);
29 }
30
31 void
32 Context::check_removal ()
33 {
34   for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
35     {
36       Context *trg = unsmob_context (scm_car (p));
37
38       trg->check_removal ();
39       if (trg->is_removable ())
40         {
41           recurse_over_translators (trg, &Translator::finalize,
42                                     &Translator_group::finalize,
43                                     UP);
44           remove_context (trg);
45         }
46     }
47 }
48
49 Context::Context (Context const &)
50 {
51   assert (false);
52 }
53
54 Scheme_hash_table *
55 Context::properties_dict () const
56 {
57   return Scheme_hash_table::unsmob (properties_scm_);
58 }
59
60 void
61 Context::add_context (Context *t)
62 {
63   SCM ts = t->self_scm ();
64   context_list_ = ly_append2 (context_list_,
65                               scm_cons (ts, SCM_EOL));
66
67   t->daddy_context_ = this;
68   if (!t->init_)
69     {
70       t->init_ = true;
71
72       t->unprotect ();
73       Context_def *td = unsmob_context_def (t->definition_);
74
75       /* This cannot move before add_context (), because \override
76          operations require that we are in the hierarchy.  */
77       td->apply_default_property_operations (t);
78
79       recurse_over_translators (t,
80                                 &Translator::initialize,
81                                 &Translator_group::initialize,
82                                 DOWN);
83     }
84 }
85
86
87 Context::Context (Object_key const *key)
88 {
89   key_ = key;
90   daddy_context_ = 0;
91   init_ = false;
92   aliases_ = SCM_EOL;
93   iterator_count_ = 0;
94   implementation_ = 0;
95   properties_scm_ = SCM_EOL;
96   accepts_list_ = SCM_EOL;
97   context_list_ = SCM_EOL;
98   definition_ = SCM_EOL;
99
100   smobify_self ();
101
102   Scheme_hash_table *tab = new Scheme_hash_table;
103   properties_scm_ = tab->unprotect ();
104
105   /*
106     UGH UGH
107     const correctness.
108   */
109   if (key_)
110     ((Object_key *)key)->unprotect ();
111 }
112
113 /* TODO:  this shares code with find_create_context ().  */
114 Context *
115 Context::create_unique_context (SCM n, SCM operations)
116 {
117   /*
118     Don't create multiple score contexts.
119   */
120   if (dynamic_cast<Global_context *> (this)
121       && dynamic_cast<Global_context *> (this)->get_score_context ())
122     return get_score_context ()->create_unique_context (n, operations);
123
124   /*
125     TODO: use accepts_list_.
126   */
127   Link_array__Context_def_ path
128     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
129
130   if (path.size ())
131     {
132       Context *current = this;
133
134       // start at 1.  The first one (index 0) will be us.
135       for (vsize i = 0; i < path.size (); i++)
136         {
137           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
138
139           current = current->create_context (path[i],
140                                              "\\new",
141                                              ops);
142         }
143
144       return current;
145     }
146
147   /*
148     Don't go up to Global_context, because global goes down to
149     Score_context
150   */
151   Context *ret = 0;
152   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
153     ret = daddy_context_->create_unique_context (n, operations);
154   else
155     {
156       warning (_f ("can't find or create new `%s'",
157                    ly_symbol2string (n).c_str ()));
158       ret = 0;
159     }
160   return ret;
161 }
162
163 Context *
164 Context::find_create_context (SCM n, std::string id, SCM operations)
165 {
166   /*
167     Don't create multiple score contexts.
168   */
169   if (dynamic_cast<Global_context *> (this)
170       && dynamic_cast<Global_context *> (this)->get_score_context ())
171     return get_score_context ()->find_create_context (n, id, operations);
172
173   if (Context *existing = find_context_below (this, n, id))
174     return existing;
175
176   if (n == ly_symbol2scm ("Bottom"))
177     {
178       Context *tg = get_default_interpreter ();
179       return tg;
180     }
181
182   /*
183     TODO: use accepts_list_.
184   */
185   Link_array__Context_def_ path
186     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
187
188   if (path.size ())
189     {
190       Context *current = this;
191
192       // start at 1.  The first one (index 0) will be us.
193       for (vsize i = 0; i < path.size (); i++)
194         {
195           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
196
197           std::string this_id = "";
198           if (i == path.size () -1)
199             this_id = id;
200
201           current = current->create_context (path[i],
202                                              this_id,
203                                              ops);
204         }
205
206       return current;
207     }
208
209   /*
210     Don't go up to Global_context, because global goes down to
211     Score_context
212   */
213   Context *ret = 0;
214   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
215     ret = daddy_context_->find_create_context (n, id, operations);
216   else
217     {
218       warning (_f ("can't find or create `%s' called `%s'",
219                    ly_symbol2string (n).c_str (), id));
220       ret = 0;
221     }
222   return ret;
223 }
224
225 Context *
226 Context::create_context (Context_def *cdef,
227                          std::string id,
228                          SCM ops)
229 {
230   std::string type = ly_symbol2string (cdef->get_context_name ());
231   Object_key const *key = get_context_key (type, id);
232   Context *new_context
233     = cdef->instantiate (ops, key);
234
235   new_context->id_string_ = id;
236   add_context (new_context);
237   apply_property_operations (new_context, ops);
238
239   return new_context;
240 }
241
242 Object_key const *
243 Context::get_context_key (std::string type, std::string id)
244 {
245   if (!use_object_keys)
246     return 0;
247
248   std::string now_key = type + "@" + id;
249
250   int disambiguation_count = 0;
251   if (context_counts_.find (now_key) != context_counts_.end ())
252     disambiguation_count = context_counts_[now_key];
253
254   context_counts_[now_key] = disambiguation_count + 1;
255
256   return new Lilypond_context_key (key (),
257                                    now_mom (),
258                                    type, id,
259                                    disambiguation_count);
260 }
261
262 Object_key const *
263 Context::get_grob_key (std::string name)
264 {
265   if (!use_object_keys)
266     return 0;
267
268   return create_grob_key (name);
269 }
270
271 /*
272   We want to have a key for some objects anyway, so we can invent a
273   unique identifier for each (book,score) tuple.
274 */
275 Object_key const *
276 Context::create_grob_key (std::string name)
277 {
278   int disambiguation_count = 0;
279   if (grob_counts_.find (name) != grob_counts_.end ())
280     disambiguation_count = grob_counts_[name];
281   grob_counts_[name] = disambiguation_count + 1;
282
283   Object_key *k = new Lilypond_grob_key (key (),
284                                          now_mom (),
285                                          name,
286                                          disambiguation_count);
287
288   return k;
289 }
290
291 /*
292   Default child context as a SCM string, or something else if there is
293   none.
294 */
295 SCM
296 Context::default_child_context_name () const
297 {
298   return scm_is_pair (accepts_list_)
299     ? scm_car (accepts_list_)
300     : SCM_EOL;
301 }
302
303 bool
304 Context::is_bottom_context () const
305 {
306   return !scm_is_symbol (default_child_context_name ());
307 }
308
309 Context *
310 Context::get_default_interpreter ()
311 {
312   if (!is_bottom_context ())
313     {
314       SCM nm = default_child_context_name ();
315       SCM st = find_context_def (get_output_def (), nm);
316
317       std::string name = ly_symbol2string (nm);
318       Context_def *t = unsmob_context_def (st);
319       if (!t)
320         {
321           warning (_f ("can't find or create: `%s'", name.c_str ()));
322           t = unsmob_context_def (this->definition_);
323         }
324
325       Context *tg = create_context (t, "", SCM_EOL);
326       if (!tg->is_bottom_context ())
327         return tg->get_default_interpreter ();
328       else
329         return tg;
330     }
331   return this;
332 }
333
334 /*
335   PROPERTIES
336 */
337 Context *
338 Context::where_defined (SCM sym, SCM *value) const
339 {
340 #ifndef NDEBUG
341   if (profile_property_accesses)
342     note_property_access (&context_property_lookup_table, sym);
343 #endif
344
345   if (properties_dict ()->try_retrieve (sym, value))
346     return (Context *)this;
347
348   return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
349 }
350
351 /*
352   return SCM_EOL when not found.
353 */
354 SCM
355 Context::internal_get_property (SCM sym) const
356 {
357 #ifndef NDEBUG
358   if (profile_property_accesses)
359     note_property_access (&context_property_lookup_table, sym);
360 #endif
361
362   SCM val = SCM_EOL;
363   if (properties_dict ()->try_retrieve (sym, &val))
364     return val;
365
366   if (daddy_context_)
367     return daddy_context_->internal_get_property (sym);
368
369   return val;
370 }
371
372 bool
373 Context::is_alias (SCM sym) const
374 {
375   if (sym == ly_symbol2scm ("Bottom")
376       && !scm_is_pair (accepts_list_))
377     return true;
378   if (sym == unsmob_context_def (definition_)->get_context_name ())
379     return true;
380
381   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
382 }
383
384 void
385 Context::add_alias (SCM sym)
386 {
387   aliases_ = scm_cons (sym, aliases_);
388 }
389
390 void
391 Context::internal_set_property (SCM sym, SCM val)
392 {
393 #ifndef NDEBUG
394   if (do_internal_type_checking_global)
395     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
396 #endif
397
398   properties_dict ()->set (sym, val);
399 }
400
401 /*
402   TODO: look up to check whether we have inherited var?
403 */
404 void
405 Context::unset_property (SCM sym)
406 {
407   properties_dict ()->remove (sym);
408 }
409
410 /**
411    Remove a context from the hierarchy.
412 */
413 Context *
414 Context::remove_context (Context *trans)
415 {
416   assert (trans);
417
418   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
419   trans->daddy_context_ = 0;
420   return trans;
421 }
422
423 /*
424   ID == "" means accept any ID.
425 */
426 Context *
427 find_context_below (Context *where,
428                     SCM type, std::string id)
429 {
430   if (where->is_alias (type))
431     {
432       if (id == "" || where->id_string () == id)
433         return where;
434     }
435
436   Context *found = 0;
437   for (SCM s = where->children_contexts ();
438        !found && scm_is_pair (s); s = scm_cdr (s))
439     {
440       Context *tr = unsmob_context (scm_car (s));
441
442       found = find_context_below (tr, type, id);
443     }
444
445   return found;
446 }
447
448 SCM
449 Context::properties_as_alist () const
450 {
451   return properties_dict ()->to_alist ();
452 }
453
454 SCM
455 Context::context_name_symbol () const
456 {
457   Context_def *td = unsmob_context_def (definition_);
458   return td->get_context_name ();
459 }
460
461 std::string
462 Context::context_name () const
463 {
464   return ly_symbol2string (context_name_symbol ());
465 }
466
467 Score_context *
468 Context::get_score_context () const
469 {
470   if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
471     return sc;
472   else if (daddy_context_)
473     return daddy_context_->get_score_context ();
474   else
475     return 0;
476 }
477
478 Output_def *
479 Context::get_output_def () const
480 {
481   return daddy_context_ ? daddy_context_->get_output_def () : 0;
482 }
483
484 Context::~Context ()
485 {
486 }
487
488 Moment
489 Context::now_mom () const
490 {
491   Context const *p = this;
492   while (p->daddy_context_)
493     p = p->daddy_context_;
494
495   return p->now_mom ();
496 }
497
498 int
499 Context::print_smob (SCM s, SCM port, scm_print_state *)
500 {
501   Context *sc = (Context *) SCM_CELL_WORD_1 (s);
502
503   scm_puts ("#<", port);
504   scm_puts (sc->class_name (), port);
505   if (Context_def *d = unsmob_context_def (sc->definition_))
506     {
507       scm_puts (" ", port);
508       scm_display (d->get_context_name (), port);
509     }
510
511   if (!sc->id_string_.empty ())
512     {
513       scm_puts ("=", port);
514       scm_puts (sc->id_string_.c_str (), port);
515     }
516
517   scm_puts (" ", port);
518
519   scm_display (sc->context_list_, port);
520   scm_puts (" >", port);
521
522   return 1;
523 }
524
525 SCM
526 Context::mark_smob (SCM sm)
527 {
528   Context *me = (Context *) SCM_CELL_WORD_1 (sm);
529   if (me->key_)
530     scm_gc_mark (me->key_->self_scm ());
531
532   scm_gc_mark (me->context_list_);
533   scm_gc_mark (me->aliases_);
534   scm_gc_mark (me->definition_);
535   scm_gc_mark (me->properties_scm_);
536   scm_gc_mark (me->accepts_list_);
537   if (me->implementation_)
538     scm_gc_mark (me->implementation_->self_scm ());
539
540   return me->properties_scm_;
541 }
542
543 IMPLEMENT_SMOBS (Context);
544 IMPLEMENT_DEFAULT_EQUAL_P (Context);
545 IMPLEMENT_TYPE_P (Context, "ly:context?");
546
547 bool
548 Context::try_music (Music *m)
549 {
550   Translator_group *t = implementation ();
551   if (!t)
552     return false;
553
554   bool b = t->try_music (m);
555   if (!b && daddy_context_)
556     b = daddy_context_->try_music (m);
557
558   return b;
559 }
560
561 Global_context *
562 Context::get_global_context () const
563 {
564   if (dynamic_cast<Global_context *> ((Context *) this))
565     return dynamic_cast<Global_context *> ((Context *) this);
566
567   if (daddy_context_)
568     return daddy_context_->get_global_context ();
569
570   programming_error ("no Global context");
571   return 0;
572 }
573
574 Context *
575 Context::get_parent_context () const
576 {
577   return daddy_context_;
578 }
579
580 void
581 Context::clear_key_disambiguations ()
582 {
583   if (!use_object_keys)
584     return;
585
586   grob_counts_.clear ();
587   context_counts_.clear ();
588   for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
589     unsmob_context (scm_car (s))->clear_key_disambiguations ();
590 }
591
592 /*
593   Ugh. Where to put this?
594 */
595 Rational
596 measure_length (Context const *context)
597 {
598   SCM l = context->get_property ("measureLength");
599   Rational length (1);
600   if (unsmob_moment (l))
601     length = unsmob_moment (l)->main_part_;
602   return length;
603 }
604
605 Moment
606 measure_position (Context const *context)
607 {
608   SCM sm = context->get_property ("measurePosition");
609
610   Moment m = 0;
611   if (unsmob_moment (sm))
612     {
613       m = *unsmob_moment (sm);
614
615       if (m.main_part_ < Rational (0))
616         {
617           Rational length (measure_length (context));
618           while (m.main_part_ < Rational (0))
619             m.main_part_ += length;
620         }
621     }
622
623   return m;
624 }
625
626
627 void
628 set_context_property_on_children (Context *trans, SCM sym, SCM val)
629 {
630   trans->internal_set_property (sym, ly_deep_copy (val));
631   for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
632     {
633       Context *trg = unsmob_context (scm_car (p));
634       set_context_property_on_children (trg, sym, ly_deep_copy (val));
635     }
636 }