]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-selector.cc
* lily/lily-guile.cc (ly_to_string, ly_to_symbol): New function.
[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   store_context (identify_context (context, count), context);
32 }
33
34 SCM
35 Context_selector::identify_context (Context *context, int count)
36 {
37   /* TODO: start time, parent-context-at-start */
38   return scm_list_3 (scm_makfrom0str (context->context_name ().to_str0 ()),
39                      scm_makfrom0str (context->id_string ().to_str0 ()),
40                      scm_int2num (count));
41 }
42
43 SCM
44 Context_selector::identify_context (Context *context)
45 {
46   return
47     identify_context (context,
48                       robust_scm2int (context->get_property ("count"), 0));
49 }
50
51 void
52 Context_selector::store_context (SCM context_id, Context *context)
53 {
54   contexts_->set (ly_to_symbol (context_id), context->self_scm ());
55 }
56
57 Context *
58 Context_selector::retrieve_context (SCM context_id)
59 {
60   return unsmob_context (contexts_->get (ly_to_symbol (context_id)));
61 }