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