]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-selector.cc
6bfc0c9e844c6effc91f9343510e0d84048772ca
[lilypond.git] / lily / context-selector.cc
1 /*
2   context-selector.cc -- implement Context selection.
3
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "context-selector.hh"
10 #include "context.hh"
11 #include "scm-hash.hh"
12
13 Scheme_hash_table *Context_selector::contexts_ = 0;
14
15 void
16 Context_selector::register_context (Context *context)
17 {
18   if (!contexts_)
19     contexts_ = new Scheme_hash_table ();
20   int count = 0;
21   if (Context *first = retrieve_context (identify_context (context, 0)))
22     {
23       count = robust_scm2int (first->get_property ("max"), 0);
24       count++;
25       SCM s = scm_int2num (count);
26       first->set_property ("max", s);
27       context->set_property ("count", s);
28   }
29   /* FIXME: must alway set count, for get_property () not to segfault.  */
30   context->set_property ("count", scm_int2num (count));
31   contexts_->set (identify_context (context, count), context->self_scm ());
32 }
33
34 SCM
35 Context_selector::identify_context (Context *context, int count)
36 {
37   /* TODO: start time, parent-context-at-start */
38   return ly_symbol2scm ((context->context_name ()
39                          + ","
40                          + context->id_string ()
41                          + ","
42                          + to_string (count)).to_str0 ());
43 }
44
45 SCM
46 Context_selector::identify_context (Context *context)
47 {
48   return
49     identify_context (context,
50                       robust_scm2int (context->get_property ("count"), 0));
51 }
52
53 Context *
54 Context_selector::retrieve_context (SCM key)
55 {
56   return unsmob_context (contexts_->get (key));
57 }