]> git.donarmstrong.com Git - lilypond.git/blob - lily/lilypond-key.cc
* The grand 2005-2006 replace.
[lilypond.git] / lily / lilypond-key.cc
1 /*
2   lilypond-key.cc -- implement Lilypond_{grob, context}_key
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 "lilypond-key.hh"
10
11 Lilypond_grob_key::Lilypond_grob_key (Object_key const *context,
12                                       Moment start,
13                                       String name,
14                                       int disambiguation_count)
15 {
16   context_ = context;
17   creation_moment_ = start;
18   grob_name_ = name;
19   disambiguation_count_ = disambiguation_count;
20 }
21
22 void
23 Lilypond_grob_key::derived_mark () const
24 {
25   scm_gc_mark (context_->self_scm ());
26 }
27
28 // todo: reverse order of comparison for efficiency reasons.
29 int
30 Lilypond_grob_key::do_compare (Object_key const *key) const
31 {
32   Lilypond_grob_key const *other = dynamic_cast<Lilypond_grob_key const *> (key);
33   int c;
34
35   c = context_->compare (other->context_);
36   if (c)
37     return c;
38
39   c = Moment::compare (creation_moment_, other->creation_moment_);
40   if (c)
41     return c;
42
43   c = String::compare (grob_name_, other->grob_name_);
44   if (c)
45     return c;
46
47   c = sign (disambiguation_count_ - other->disambiguation_count_);
48   if (c)
49     return c;
50
51   return 0;
52 }
53
54 int
55 Lilypond_grob_key::get_type () const
56 {
57   return GROB_KEY;
58 }
59
60 SCM
61 Lilypond_grob_key::as_scheme () const
62 {
63   return scm_list_4 (context_ ? context_->self_scm () : SCM_BOOL_F,
64                      creation_moment_.smobbed_copy (),
65                      scm_makfrom0str (grob_name_.to_str0 ()),
66                      scm_from_int (disambiguation_count_));
67 }
68
69 Object_key *
70 Lilypond_grob_key::from_scheme (SCM a)
71 {
72   return new Lilypond_grob_key (unsmob_key (scm_car (a)),
73                                 *unsmob_moment (scm_cadr (a)),
74                                 ly_scm2string (scm_caddr (a)),
75                                 scm_to_int (scm_list_ref (a, scm_from_int (3))));
76 }
77
78 /****************************************************************/
79
80 void
81 Lilypond_context_key::derived_mark () const
82 {
83   if (parent_context_)
84     scm_gc_mark (parent_context_->self_scm ());
85 }
86
87 Lilypond_context_key::Lilypond_context_key (Object_key const *parent,
88                                             Moment start,
89                                             String type,
90                                             String id,
91                                             int count)
92 {
93   disambiguation_count_ = count;
94   parent_context_ = parent;
95   start_moment_ = start;
96   context_name_ = type;
97   id_ = id;
98 }
99
100 int
101 Lilypond_context_key::do_compare (Object_key const *key) const
102 {
103   Lilypond_context_key const *other
104     = dynamic_cast<Lilypond_context_key const *> (key);
105
106   int c;
107   if (parent_context_ && other->parent_context_)
108     {
109       c = parent_context_->compare (other->parent_context_);
110       if (c)
111         return c;
112     }
113   else if (parent_context_)
114     return -1;
115   else if (other->parent_context_)
116     return 1;
117
118   c = Moment::compare (start_moment_, other->start_moment_);
119   if (c)
120     return c;
121
122   c = String::compare (context_name_, other->context_name_);
123   if (c)
124     return c;
125
126   c = String::compare (id_, other->id_);
127   if (c)
128     return c;
129
130   c = sign (disambiguation_count_ - other->disambiguation_count_);
131   if (c)
132     return c;
133
134   return 0;
135 }
136
137 int
138 Lilypond_context_key::get_type () const
139 {
140   return CONTEXT_KEY;
141 }
142
143 SCM
144 Lilypond_context_key::as_scheme () const
145 {
146   return scm_list_5 (parent_context_ ? parent_context_->self_scm () : SCM_BOOL_F,
147                      start_moment_.smobbed_copy (),
148                      scm_makfrom0str (context_name_.to_str0 ()),
149                      scm_makfrom0str (id_.to_str0 ()),
150                      scm_from_int (disambiguation_count_));
151 }
152
153 Object_key *
154 Lilypond_context_key::from_scheme (SCM a)
155 {
156   return new Lilypond_context_key (unsmob_key (scm_car (a)),
157                                    *unsmob_moment (scm_cadr (a)),
158                                    ly_scm2string (scm_list_ref (a, scm_from_int (2))),
159                                    ly_scm2string (scm_list_ref (a, scm_from_int (3))),
160                                    scm_to_int (scm_list_ref (a, scm_from_int (4))));
161 }
162
163 /****************************************************************/
164
165 int
166 Lilypond_general_key::get_type () const
167 {
168   return GENERAL_KEY;
169 }
170
171 void
172 Lilypond_general_key::derived_mark () const
173 {
174   if (parent_)
175     scm_gc_mark (parent_->self_scm ());
176 }
177
178 Lilypond_general_key::Lilypond_general_key (Object_key const *parent,
179                                             String name,
180                                             int count)
181 {
182   parent_ = parent;
183   name_ = name;
184   disambiguation_count_ = count;
185 }
186
187 int
188 Lilypond_general_key::do_compare (Object_key const *key)const
189 {
190   Lilypond_general_key const *other
191     = dynamic_cast<Lilypond_general_key const *> (key);
192
193   if (parent_ && other->parent_)
194     parent_->compare (other->parent_);
195   else if (parent_)
196     return -1;
197   else if (other->parent_)
198     return 1;
199
200   int c = String::compare (name_, other->name_);
201   if (c)
202     return c;
203
204   c = sign (disambiguation_count_ - other->disambiguation_count_);
205   if (c)
206     return c;
207
208   return 0;
209 }
210
211 SCM
212 Lilypond_general_key::as_scheme () const
213 {
214   return scm_list_3 (parent_ ? parent_->self_scm () : SCM_BOOL_F,
215                      scm_makfrom0str (name_.to_str0 ()),
216                      scm_from_int (disambiguation_count_));
217 }
218
219 Object_key *
220 Lilypond_general_key::from_scheme (SCM a)
221 {
222   return new Lilypond_general_key (unsmob_key (scm_car (a)),
223                                    ly_scm2string (scm_list_ref (a, scm_from_int (1))),
224                                    scm_to_int (scm_list_ref (a, scm_from_int (2))));
225 }