]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-array.cc
Issue 4360: Reorganize smob initialization to make it more reliable
[lilypond.git] / lily / grob-array.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "grob-array.hh"
21 #include "item.hh"
22 #include "spanner.hh"
23
24 ADD_SMOB_INIT (Grob_array);
25
26 Item *
27 Grob_array::item (vsize i)
28 {
29   return dynamic_cast<Item *> (grobs_.at (i));
30 }
31
32 Spanner *
33 Grob_array::spanner (vsize i)
34 {
35   return dynamic_cast<Spanner *> (grobs_.at (i));
36 }
37
38 Grob_array::Grob_array ()
39 {
40   ordered_ = true;
41 }
42
43 vector<Grob *> &
44 Grob_array::array_reference ()
45 {
46   return grobs_;
47 }
48
49 vector<Grob *> const &
50 Grob_array::array () const
51 {
52   return grobs_;
53 }
54
55 SCM
56 Grob_array::mark_smob ()
57 {
58 #if 0  /* see System::derived_mark () const */
59   for (vsize i = 0; i < grobs_.size (); i++)
60     scm_gc_mark (grobs_[i]->self_scm ());
61 #endif
62   return SCM_UNDEFINED;
63 }
64
65 int
66 Grob_array::print_smob (SCM port, scm_print_state *)
67 {
68   scm_puts ("#<Grob_array", port);
69   for (vsize i = 0; i < size (); i++)
70     {
71       scm_display (grob (i)->self_scm (), port);
72       scm_puts (" ", port);
73     }
74   scm_puts (">", port);
75   return 1;
76 }
77
78 SCM
79 Grob_array::make_array ()
80 {
81   Grob_array ga;
82   return ga.smobbed_copy ();
83 }
84
85 void
86 Grob_array::clear ()
87 {
88   grobs_.clear ();
89 }
90
91 void
92 Grob_array::remove_duplicates ()
93 {
94   assert (!ordered_);
95
96   uniquify (grobs_);
97 }
98
99 bool
100 Grob_array::empty () const
101 {
102   return grobs_.empty ();
103 }
104
105 void
106 Grob_array::set_array (vector<Grob *> const &src)
107 {
108   grobs_ = src;
109 }
110
111 const char Grob_array::type_p_name_[] = "ly:grob-array?";
112
113
114 SCM
115 grob_list_to_grob_array (SCM lst)
116 {
117   SCM arr_scm = Grob_array::make_array ();
118   Grob_array *ga = Grob_array::unsmob (arr_scm);
119   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
120     ga->add (Grob::unsmob (scm_car (s)));
121   return arr_scm;
122 }
123
124 SCM
125 grob_array_to_list (Grob_array *array)
126 {
127   SCM list = SCM_EOL;
128   SCM *tail = &list;
129
130   for (vsize i = 0; i < array->size (); i++)
131     {
132       *tail = scm_cons (array->grob (i)->self_scm (), SCM_EOL);
133       tail = SCM_CDRLOC (*tail);
134     }
135   return list;
136 }