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