]> git.donarmstrong.com Git - lilypond.git/blob - lily/lilypond-key.cc
6dc69be030f790aea8ab6c7aa0c912048304a86e
[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_context_key (unsmob_key (scm_car (a)),
167                                    *unsmob_moment (scm_cadr (a)),
168                                    ly_scm2string  (scm_list_ref (a, scm_from_int (2))),
169                                    ly_scm2string  (scm_list_ref (a, scm_from_int (3))),
170                                    scm_to_int  (scm_list_ref (a, scm_from_int (4))));
171 }
172
173
174 /****************************************************************/
175
176 int
177 Lilypond_general_key::get_type () const
178 {
179   return GENERAL_KEY;
180 }
181
182 void
183 Lilypond_general_key::derived_mark () const
184 {
185   if (parent_)
186     scm_gc_mark (parent_->self_scm ());
187 }
188
189 Lilypond_general_key::Lilypond_general_key (Object_key const*parent,
190                                             String name,
191                                             int count)
192 {
193   parent_ = parent;
194   name_ = name;
195   disambiguation_count_ = count; 
196 }
197
198
199 int
200 Lilypond_general_key::do_compare (Object_key const* key)const
201 {
202   Lilypond_general_key const* other
203     = dynamic_cast<Lilypond_general_key const*> (key);
204
205   if (parent_ && other->parent_)
206     parent_->compare (other->parent_);
207   else if (parent_)
208     return -1 ;
209   else if (other->parent_)
210     return 1;
211
212   int c = String::compare (name_, other->name_);
213   if (c)
214     return c;
215
216   c = sign (disambiguation_count_ - other->disambiguation_count_);
217   if (c)
218     return c;
219
220   return 0;  
221 }
222
223 SCM
224 Lilypond_general_key::as_scheme () const
225 {
226   return scm_list_3 (parent_ ? parent_->self_scm() : SCM_BOOL_F,
227                      scm_makfrom0str (name_.to_str0()),
228                      scm_from_int (disambiguation_count_)); 
229 }
230
231 Object_key *
232 Lilypond_general_key::from_scheme (SCM a) 
233 {
234   return new Lilypond_general_key (unsmob_key (scm_car (a)),
235                                    ly_scm2string  (scm_list_ref (a, scm_from_int (1))),
236                                    scm_to_int  (scm_list_ref (a, scm_from_int (2))));
237 }