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