]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/context.hh
Issue 4997/7: Use Preinit for Global_context, protect output definition
[lilypond.git] / lily / include / context.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--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 #ifndef CONTEXT_HH
21 #define CONTEXT_HH
22
23 #include "duration.hh"
24 #include "lily-proto.hh"
25 #include "listener.hh"
26 #include "moment.hh"
27 #include "scm-hash.hh"
28 #include "std-vector.hh"
29 #include "virtual-methods.hh"
30
31 class Context : public Smob<Context>
32 {
33 public:
34   SCM mark_smob () const;
35   int print_smob (SCM, scm_print_state *) const;
36   static const char * const type_p_name_;
37   virtual ~Context ();
38 private:
39   Scheme_hash_table *properties_dict () const;
40   Context (Context const &src); // Do not define!  Not copyable!
41
42   DECLARE_CLASSNAME (Context);
43   void terminate ();
44
45 private:
46   friend class Context_handle;
47   /* how many Context_handles point to this Context */
48   int client_count_;
49
50   /* Used internally by create_context */
51   Stream_event *infant_event_;
52
53 protected:
54   virtual void derived_mark () const;
55   Context *daddy_context_;
56   /* The used Context_def */
57   SCM definition_;
58   /* Additions to the Context_def, given by \with */
59   SCM definition_mods_;
60
61   SCM properties_scm_;
62   SCM context_list_;
63   SCM accepts_list_;
64   SCM default_child_;
65   SCM aliases_;
66   Translator_group *implementation_;
67   string id_string_;
68
69   /* Events reported in the context is sent to this dispatcher. */
70   Dispatcher *event_source_;
71
72   /* Events reported to this context or recursively in any of its
73      children, are sent to this dispatcher. */
74   Dispatcher *events_below_;
75
76   // Translator_group is allowed to set implementation_.
77   friend class Translator_group;
78   // Context_def::instantiate initialises some protected members.
79   friend class Context_def;
80   // UGH! initialises implementation_
81   friend SCM ly_make_global_translator (SCM);
82
83   void set_property_from_event (SCM);
84   void unset_property_from_event (SCM);
85
86 public:
87   string id_string () const { return id_string_; }
88   SCM children_contexts () const { return context_list_; }
89   SCM default_child_context_name () const;
90
91   Dispatcher *event_source () const { return event_source_; }
92   Dispatcher *events_below () const { return events_below_; }
93   void internal_send_stream_event (SCM type, Input *origin, SCM props[]);
94
95   SCM get_definition () const { return definition_; }
96   SCM get_definition_mods () const { return definition_mods_; }
97
98   Translator_group *implementation () const { return implementation_; }
99   Context *get_parent_context () const;
100   Context ();
101
102   /* properties:  */
103   SCM internal_get_property (SCM name_sym) const;
104   SCM properties_as_alist () const;
105   Context *where_defined (SCM name_sym, SCM *value) const;
106   bool here_defined (SCM name_sym, SCM *value) const;
107   void unset_property (SCM var_sym);
108
109   void instrumented_set_property (SCM, SCM, const char *, int, const char *);
110   void internal_set_property (SCM var_sym, SCM value);
111
112   Context *create_context (Context_def *, const string&, SCM);
113   void create_context_from_event (SCM);
114   void acknowledge_infant (SCM);
115   void remove_context (SCM);
116   void change_parent (SCM);
117   void disconnect_from_parent ();
118   void check_removal ();
119   string context_name () const;
120   SCM context_name_symbol () const;
121   Global_context *get_global_context () const;
122
123   virtual Context *get_score_context () const;
124   virtual Output_def *get_output_def () const;
125   virtual Moment now_mom () const;
126   virtual Context *get_default_interpreter (const string &context_id = "");
127
128   bool is_alias (SCM) const;
129   void add_alias (SCM);
130   void add_context (Context *trans);
131   bool is_bottom_context () const;
132   bool is_removable () const;
133
134   Context *find_create_context (SCM context_name,
135                                 const string &id, SCM ops);
136   Context *create_unique_context (SCM context_name, const string &context_id,
137                                   SCM ops);
138   vector<Context_def *> path_to_acceptable_context (SCM alias) const;
139 };
140
141 /*
142   Context arg?
143 */
144
145 void apply_property_operations (Context *tg, SCM pre_init_ops);
146 void execute_pushpop_property (Context *trg, SCM prop, SCM eltprop, SCM val);
147
148 // Search for a context of the given type starting from the given context and
149 // moving toward the root of the tree.  If the starting context matches, it is
150 // returned.
151 Context *find_context_above (Context *where, SCM type_sym);
152
153 // Search for a context of the given type starting from the given context and
154 // moving toward the root of the tree.  If found, return its child that was
155 // found on the way there.
156 Context *find_context_above_by_parent_type (Context *where, SCM parent_type);
157
158 // Search for a context of the given type and ID starting from the given
159 // context and moving toward the leaves of the tree.  If the starting context
160 // matches, it is returned.  An empty ID matches any context of the given type.
161 Context *find_context_below (Context *where,
162                              SCM type_sym, const string &id);
163
164 // Search for a context of the given type and ID starting with the given
165 // context, then searching its descendants, then its parent's descendants, etc.
166 // An empty ID matches any context of the given type.
167 Context *find_context_near (Context *where,
168                             SCM type_sym, const string &id);
169
170 // Search for the top context (i.e. the ancestor with no parent) starting with
171 // the given context.
172 Context *find_top_context (Context *where);
173
174 bool melisma_busy (Context *);
175
176 Context *get_voice_to_lyrics (Context *lyrics);
177 Grob *get_current_note_head (Context *voice);
178 Grob *get_current_rest (Context *voice);
179
180 Moment measure_position (Context const *context);
181 Moment measure_position (Context const *context, Duration const *dur);
182 Rational measure_length (Context const *context);
183 int measure_number (Context const *context);
184
185 bool check_repeat_count_visibility (Context const *context, SCM count);
186
187 void set_context_property_on_children (Context *trans, SCM sym, SCM val);
188
189 /* Shorthand for creating and broadcasting stream events. */
190 #define send_stream_event(ctx, type, origin, ...)                       \
191   do {                                                                  \
192     SCM props[] = { __VA_ARGS__, 0 };                                   \
193     ctx->internal_send_stream_event (ly_symbol2scm (type), origin, props); \
194   } while (0)
195
196 SCM nested_property_alist (SCM alist, SCM prop_path, SCM value);
197 SCM nested_property (SCM alist, SCM prop_path, SCM fallback = SCM_EOL);
198 SCM nested_create_alist (SCM prop_path, SCM value);
199 SCM partial_list_copy (SCM alist, SCM tail, SCM newtail);
200 SCM assq_tail (SCM key, SCM alist, SCM alist_end);
201 SCM assv_tail (SCM key, SCM alist, SCM alist_end);
202 SCM assoc_tail (SCM key, SCM alist, SCM alist_end);
203 SCM evict_from_alist (SCM, SCM, SCM);
204 SCM nalist_to_alist (SCM nalist, int nested);
205 extern SCM ly_context_set_property_x_proc;
206 extern SCM ly_context_unset_property_proc;
207 extern SCM ly_context_matched_pop_property_proc;
208
209 #endif /* CONTEXT_HH */