]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* lily/grob.cc (Grob): idem. Plugs mem leaks.
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "context.hh"
11
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
22 bool
23 Context::is_removable () const
24 {
25   return context_list_ == SCM_EOL && ! iterator_count_ &&
26           !dynamic_cast<Score_context const*> (this);
27 }
28
29 void
30 Context::check_removal ()
31 {
32   for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
33     {
34       Context *trg = unsmob_context (scm_car (p));
35
36       trg->check_removal ();
37       if (trg->is_removable ())
38         {
39           recurse_over_translators (trg, &Translator::finalize, UP);
40           remove_context (trg);
41         }
42     }
43 }
44
45 Context::Context (Context const&)
46 {
47   assert (false);
48 }
49
50 Scheme_hash_table *
51 Context::properties_dict () const
52 {
53   return Scheme_hash_table::unsmob (properties_scm_);
54 }
55
56 void
57 Context::add_context (Context *t)
58 {
59   SCM ts = t->self_scm ();
60   context_list_ = ly_append2 (context_list_,
61                               scm_cons (ts, SCM_EOL));
62   
63   t->daddy_context_ = this;
64   if (!t->init_)
65     {
66       t->init_ = true;
67
68       scm_gc_unprotect_object (ts);
69       Context_def *td = unsmob_context_def (t->definition_);
70
71       /*
72         this can not move before add_context (), because \override
73         operations require that we are in the hierarchy.
74       */
75       td->apply_default_property_operations (t);
76
77       recurse_over_translators (t, &Translator::initialize, DOWN);
78     }
79 }
80
81 Object_key const*
82 Context::get_key () const
83 {
84   return key_;
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_ = SCM_EOL;
95   properties_scm_ = SCM_EOL;
96   accepts_list_ = SCM_EOL;
97   context_list_ = SCM_EOL;
98   definition_ = SCM_EOL;
99   
100   smobify_self ();
101   properties_scm_ = (new Scheme_hash_table)->self_scm ();
102   scm_gc_unprotect_object (properties_scm_);
103   scm_gc_unprotect_object (key_->self_scm ());
104 }
105
106
107
108 Context *
109 Context::find_create_context (SCM n, String id, SCM operations)
110 {
111   /*
112     Don't create multiple score contexts.
113    */
114   if (dynamic_cast<Global_context*> (this)
115       && dynamic_cast<Global_context*> (this)->get_score_context ())
116     return get_score_context ()->find_create_context (n, id, operations);
117
118   if (Context *existing = find_context_below (this, n, id))
119     return existing;
120
121   if (n == ly_symbol2scm ("Bottom"))
122     {
123       Context* tg = get_default_interpreter ();
124       tg->id_string_ = id;
125       return tg;
126     }
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           String this_id = "";
144           if (i == path.size () -1)
145             {
146               this_id = id;
147             }
148
149
150           current = current->create_context (path[i],
151                                              this_id,
152                                              ops); 
153         }
154
155       return current;
156     }
157
158   /*
159     Don't go up to Global_context, because global goes down to
160     Score_context
161    */
162   Context *ret = 0;
163   if (daddy_context_ && !dynamic_cast<Global_context*> (daddy_context_))
164     ret = daddy_context_->find_create_context (n, id, operations);
165   else
166     {
167       warning (_f ("Cannot find or create `%s' called `%s'",
168                    ly_symbol2string (n).to_str0 (), id));
169       ret =0;
170     }
171   return ret;
172 }
173
174
175 Context*
176 Context::create_context (Context_def * cdef,
177                          String id,
178                          SCM ops)
179 {
180   String type = ly_symbol2string (cdef->get_context_name());
181   Object_key const *key = get_context_key (type, id);
182   Context * new_group
183     = cdef->instantiate (ops, key);
184           
185   new_group->id_string_ = id;
186   add_context (new_group);
187   apply_property_operations (new_group, ops);
188
189   return new_group;
190 }
191
192 Object_key const*
193 Context::get_context_key (String type, String id)
194 {
195   String now_key = type + "@" + id;
196
197   int disambiguation_count = 0;
198   if (context_counts_.find (now_key) != context_counts_.end ())
199     {
200       disambiguation_count = context_counts_[now_key];
201     }
202
203   context_counts_[now_key] = disambiguation_count + 1;
204   
205   
206   return new Lilypond_context_key (get_key (),
207                                    now_mom(),
208                                    type, id,
209                                    disambiguation_count);
210 }
211
212 Object_key const*
213 Context::get_grob_key (String name) 
214 {
215   int disambiguation_count = 0;
216   if (grob_counts_.find (name) != grob_counts_.end ())
217     {
218       disambiguation_count = grob_counts_[name];
219     }
220   grob_counts_[name] = disambiguation_count + 1;
221
222   Object_key * k = new Lilypond_grob_key (get_key(),
223                                           now_mom(),
224                                           name,
225                                           disambiguation_count);
226
227   return k;                                       
228 }
229
230
231
232 /*
233   Default child context as a SCM string, or something else if there is
234   none.
235 */
236 SCM
237 Context::default_child_context_name () const
238 {
239   return scm_is_pair (accepts_list_)
240     ? scm_car (scm_last_pair (accepts_list_))
241     : SCM_EOL;
242 }
243
244
245 bool
246 Context::is_bottom_context () const
247 {
248   return !scm_is_symbol (default_child_context_name ());
249 }
250
251 Context*
252 Context::get_default_interpreter ()
253 {
254   if (!is_bottom_context ())
255     {
256       SCM nm = default_child_context_name ();
257       SCM st = find_context_def (get_output_def (), nm);
258
259       String name = ly_symbol2string (nm);
260       Context_def *t = unsmob_context_def (st);
261       if (!t)
262         {
263           warning (_f ("can't find or create: `%s'", name.to_str0 ()));
264           t = unsmob_context_def (this->definition_);
265         }
266
267       Context *tg = create_context (t, "", SCM_EOL);
268       if (!tg->is_bottom_context ())
269         return tg->get_default_interpreter ();
270       else
271         return tg;
272     }
273   return this;
274 }
275
276 /*
277   PROPERTIES
278  */
279 Context*
280 Context::where_defined (SCM sym) const
281 {
282   if (properties_dict ()->contains (sym))
283     {
284       return (Context*)this;
285     }
286
287   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
288 }
289
290 /*
291   return SCM_EOL when not found.
292 */
293 SCM
294 Context::internal_get_property (SCM sym) const
295 {
296   SCM val = SCM_EOL;
297   if (properties_dict ()->try_retrieve (sym, &val))
298     return val;
299
300   if (daddy_context_)
301     return daddy_context_->internal_get_property (sym);
302   
303   return val;
304 }
305
306 bool
307 Context::is_alias (SCM sym) const
308 {
309   if (sym == ly_symbol2scm ("Bottom")
310       && !scm_is_pair (accepts_list_))
311     return true;
312   if (sym == unsmob_context_def (definition_)->get_context_name ())
313     return true;
314   
315   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
316 }
317
318 void
319 Context::add_alias (SCM sym)
320 {
321   aliases_ = scm_cons (sym, aliases_);
322 }
323
324
325
326 void
327 Context::internal_set_property (SCM sym, SCM val)
328 {
329 #ifndef NDEBUG
330   if (internal_type_checking_global_b)
331     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
332 #endif
333   
334   properties_dict ()->set (sym, val);
335 }
336
337 /*
338   TODO: look up to check whether we have inherited var? 
339  */
340 void
341 Context::unset_property (SCM sym)
342 {
343   properties_dict ()->remove (sym);
344 }
345
346 /**
347    Remove a context from the hierarchy.
348  */
349 Context *
350 Context::remove_context (Context*trans)
351 {
352   assert (trans);
353
354   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
355   trans->daddy_context_ = 0;
356   return trans;
357 }
358
359 /*
360   ID == "" means accept any ID.
361  */
362 Context *
363 find_context_below (Context * where,
364                     SCM type, String id)
365 {
366   if (where->is_alias (type))
367     {
368       if (id == "" || where->id_string () == id)
369         return where;
370     }
371   
372   Context *found = 0;
373   for (SCM s = where->children_contexts ();
374        !found && scm_is_pair (s); s = scm_cdr (s))
375     {
376       Context *tr = unsmob_context (scm_car (s));
377
378       found = find_context_below (tr, type, id);
379     }
380
381   return found; 
382 }
383
384 SCM
385 Context::properties_as_alist () const
386 {
387   return properties_dict ()->to_alist ();
388 }
389
390 SCM
391 Context::context_name_symbol () const
392 {
393   Context_def *td = unsmob_context_def (definition_);
394   return td->get_context_name ();
395 }
396
397 String
398 Context::context_name () const
399 {
400   return ly_symbol2string (context_name_symbol ());
401 }
402
403 Score_context*
404 Context::get_score_context () const
405 {
406   if (Score_context *sc = dynamic_cast<Score_context*> ((Context*) this))
407     return sc;
408   else if (daddy_context_)
409     return daddy_context_->get_score_context ();
410   else
411     return 0;
412 }
413
414 Output_def *
415 Context::get_output_def () const
416 {
417   return daddy_context_ ? daddy_context_->get_output_def () : 0;
418 }
419
420 Context::~Context ()
421 {
422   
423 }
424
425 Moment
426 Context::now_mom () const
427 {
428   return daddy_context_->now_mom ();
429 }
430
431 int
432 Context::print_smob (SCM s, SCM port, scm_print_state *)
433 {
434   Context *sc = (Context *) SCM_CELL_WORD_1 (s);
435      
436   scm_puts ("#<", port);
437   scm_puts (classname (sc), port);
438   if (Context_def *d = unsmob_context_def (sc->definition_))
439     {
440       scm_puts (" ", port);
441       scm_display (d->get_context_name (), port);
442     }
443
444   if (Context *td=dynamic_cast<Context *> (sc))
445     {
446       scm_puts ("=", port);
447       scm_puts (td->id_string_.to_str0 (), port);
448     }
449   
450
451   scm_puts (" ", port);
452
453   scm_display (sc->context_list_, port);
454   scm_puts (" >", port);
455   
456   return 1;
457 }
458
459 SCM
460 Context::mark_smob (SCM sm)
461 {
462   Context *me = (Context*) SCM_CELL_WORD_1 (sm);
463   scm_gc_mark (me->key_->self_scm ());  
464   scm_gc_mark (me->context_list_);
465   scm_gc_mark (me->aliases_);
466   scm_gc_mark (me->definition_);  
467   scm_gc_mark (me->properties_scm_);  
468   scm_gc_mark (me->accepts_list_);
469   scm_gc_mark (me->implementation_);
470
471   return me->properties_scm_;
472 }
473
474 IMPLEMENT_SMOBS (Context);
475 IMPLEMENT_DEFAULT_EQUAL_P (Context);
476 IMPLEMENT_TYPE_P (Context,"ly:context?");
477
478 bool
479 Context::try_music (Music* m)
480 {
481   Translator*  t = implementation ();
482   if (!t)
483     return false;
484   
485   bool b = t->try_music (m);
486   if (!b && daddy_context_)
487     b = daddy_context_->try_music (m);
488
489   return b;
490 }
491
492
493 Global_context*
494 Context::get_global_context () const
495 {
496   if (dynamic_cast<Global_context *>((Context*) this))
497     return dynamic_cast<Global_context *> ((Context*) this);
498
499   if (daddy_context_)
500     return daddy_context_->get_global_context ();
501
502   programming_error ("No Global context!");
503   return 0;
504 }
505
506 Context*
507 Context::get_parent_context () const
508 {
509   return daddy_context_;
510 }
511
512 Translator_group*
513 Context::implementation () const
514 {
515   return dynamic_cast<Translator_group*> (unsmob_translator (implementation_));
516 }
517
518 void
519 Context::clear_key_disambiguations ()
520 {
521   grob_counts_.clear();
522   context_counts_.clear();
523   for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
524     {
525       unsmob_context (scm_car (s))->clear_key_disambiguations();
526     }
527 }