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