BEAST - Free Software Audio Synthesizer and Tracker  0.9.2
glib-extra.hh
Go to the documentation of this file.
1  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
2 #ifndef __SFI_GLIB_EXTRA_H__
3 #define __SFI_GLIB_EXTRA_H__
4 
5 #include <glib.h>
6 #include <glib-object.h>
7 #include <rapicorn-core.hh> // for Rapicorn::string_format
8 
9 G_BEGIN_DECLS
10 
11 #if (GLIB_SIZEOF_LONG > 4)
12 #define G_HASH_LONG(l) ((l) + ((l) >> 32))
13 #else
14 #define G_HASH_LONG(l) (l)
15 #endif
16 #if (GLIB_SIZEOF_VOID_P > 4)
17 #define G_HASH_POINTER(p) ((guint32) (((gsize) (p)) + (((gsize) (p)) >> 32)))
18 #else
19 #define G_HASH_POINTER(p) ((guint32) (gsize) (p))
20 #endif
21 /* Provide a string identifying the current function, non-concatenatable */
22 #ifndef G_STRFUNC
23 # if defined (__GNUC__)
24 # define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
25 # elif defined (G_HAVE_ISO_VARARGS)
26 # define G_STRFUNC ((const char*) (__func__))
27 # elif
28 # define G_STRFUNC ((const char*) ("???"))
29 # endif
30 #endif
31 
32 
33 /* --- provide (historic) aliases --- */
34 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
35  g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
36 } G_STMT_END
37 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
38  g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
39 } G_STMT_END
40 
41 
42 /* --- abandon typesafety for some frequently used functions --- */
43 #ifndef __cplusplus
44 #define g_object_notify(o,s) g_object_notify ((gpointer) o, s)
45 #define g_object_get_qdata(o,q) g_object_get_qdata ((gpointer) o, q)
46 #define g_object_set_qdata(o,q,d) g_object_set_qdata ((gpointer) o, q, d)
47 #define g_object_set_qdata_full(o,q,d,f) g_object_set_qdata_full ((gpointer) o, q, d, (gpointer) f)
48 #define g_object_steal_qdata(o,q) g_object_steal_qdata ((gpointer) o, q)
49 #define g_object_get_data(o,k) g_object_get_data ((gpointer) o, k)
50 #define g_object_set_data(o,k,d) g_object_set_data ((gpointer) o, k, d)
51 #define g_object_set_data_full(o,k,d,f) g_object_set_data_full ((gpointer) o, k, d, (gpointer) f)
52 #define g_object_steal_data(o,k) g_object_steal_data ((gpointer) o, k)
53 #endif /* !__cplusplus */
54 void g_object_disconnect_any (gpointer object,
55  gpointer function,
56  gpointer data); /* workaorund for g_object_disconnect() */
57 
58 // == printf variants ==
59 #define g_intern_format(...) g_intern_string (Rapicorn::string_format (__VA_ARGS__).c_str())
60 #define g_string_add_format(gstr, ...) g_string_append (gstr, Rapicorn::string_format (__VA_ARGS__).c_str())
61 #define g_strdup_format(...) g_strdup (Rapicorn::string_format (__VA_ARGS__).c_str())
62 
63 /* --- string functions --- */
64 const gchar* g_printf_find_localised_directive (const gchar *format);
65 gchar** g_straddv (gchar **str_array,
66  const gchar *new_str);
67 gchar** g_strslistv (GSList *slist);
68 guint g_strlenv (gchar **str_array);
69 gchar* g_strdup_stripped (const gchar *string);
70 gchar* g_strdup_rstrip (const gchar *string);
71 gchar* g_strdup_lstrip (const gchar *string);
72 
73 const gchar* g_intern_strconcat (const gchar *first_string,
74  ...) G_GNUC_NULL_TERMINATED;
75 
76 GString* g_string_prefix_lines (GString *gstring,
77  const gchar *pstr);
78 
79 
80 /* --- string options --- */
81 gchar* g_option_concat (const gchar *first_option,
82  ...) G_GNUC_NULL_TERMINATED;
83 gboolean g_option_check (const gchar *option_string,
84  const gchar *option);
85 gchar* g_option_get (const gchar *option_string,
86  const gchar *option);
87 
88 
89 /* --- GParamSpec extensions --- */
90 void g_param_spec_set_options (GParamSpec *pspec,
91  const gchar *options);
92 void g_param_spec_add_option (GParamSpec *pspec,
93  const gchar *option,
94  const gchar *value);
95 gboolean g_param_spec_check_option (GParamSpec *pspec,
96  const gchar *option);
97 gboolean g_param_spec_provides_options (GParamSpec *pspec,
98  const gchar *options);
99 const gchar* g_param_spec_get_options (GParamSpec *pspec);
100 void g_param_spec_set_istepping (GParamSpec *pspec,
101  guint64 stepping);
102 guint64 g_param_spec_get_istepping (GParamSpec *pspec);
103 void g_param_spec_set_fstepping (GParamSpec *pspec,
104  gdouble stepping);
105 gdouble g_param_spec_get_fstepping (GParamSpec *pspec);
106 void g_param_spec_set_log_scale (GParamSpec *pspec,
107  gdouble center,
108  gdouble base,
109  gdouble n_steps);
110 gboolean g_param_spec_get_log_scale (GParamSpec *pspec,
111  gdouble *center,
112  gdouble *base,
113  gdouble *n_steps);
114 
115 
116 /* --- list extensions --- */
117 gpointer g_slist_pop_head (GSList **slist_p);
118 gpointer g_list_pop_head (GList **list_p);
119 GSList* g_slist_append_uniq (GSList *slist,
120  gpointer data);
121 void g_slist_free_deep (GSList *slist,
122  GDestroyNotify data_destroy);
123 void g_list_free_deep (GList *list,
124  GDestroyNotify data_destroy);
125 
126 
127 /* --- name conversions --- */
128 gchar* g_type_name_to_cname (const gchar *type_name);
129 gchar* g_type_name_to_sname (const gchar *type_name);
130 gchar* g_type_name_to_cupper (const gchar *type_name);
131 gchar* g_type_name_to_type_macro (const gchar *type_name);
132 
133 
134 /* --- simple main loop source --- */
135 typedef gboolean (*GSourcePending) (gpointer data,
136  gint *timeout);
137 typedef void (*GSourceDispatch) (gpointer data);
138 GSource* g_source_simple (gint priority,
139  GSourcePending pending,
140  GSourceDispatch dispatch,
141  gpointer data,
142  GDestroyNotify destroy,
143  GPollFD *first_pfd,
144  ...);
145 
146 
147 /* --- bit matrix --- */
148 typedef struct {
149  guint32 width, height;
150  guint32 bits[1]; /* flexible array */
151 } GBitMatrix;
152 
153 static inline GBitMatrix*
154 g_bit_matrix_new (guint width,
155  guint height)
156 {
157  GBitMatrix *matrix = (GBitMatrix*) g_new0 (guint32, MAX ((width * height + 31) / 32, 1) + 2);
158  matrix->width = width;
159  matrix->height = height;
160  return matrix;
161 }
162 
163 static inline void
164 g_bit_matrix_change (GBitMatrix *matrix,
165  guint x,
166  guint y,
167  gboolean bit_set)
168 {
169  guint32 cons, index, shift;
170  g_return_if_fail (matrix && x < matrix->width && y < matrix->height);
171  cons = y * matrix->width + x;
172  index = cons >> 5; /* / 32 */
173  shift = cons & 0x1f; /* % 32 */
174  if (bit_set)
175  matrix->bits[index] |= 1 << shift;
176  else
177  matrix->bits[index] &= ~(1 << shift);
178 }
179 
180 #define g_bit_matrix_set(matrix,x,y) g_bit_matrix_change (matrix, x, y, TRUE)
181 #define g_bit_matrix_unset(matrix,x,y) g_bit_matrix_change (matrix, x, y, FALSE)
182 
183 static inline guint32
184 g_bit_matrix_peek (GBitMatrix *matrix,
185  guint x,
186  guint y)
187 {
188  guint32 cons = y * matrix->width + x;
189  guint32 index = cons >> 5; /* / 32 */
190  guint32 shift = cons & 0x1f; /* % 32 */
191  return matrix->bits[index] & (1 << shift);
192 }
193 
194 static inline gboolean
195 g_bit_matrix_test (GBitMatrix *matrix,
196  guint x,
197  guint y)
198 {
199  if (x < matrix->width && y < matrix->height)
200  return g_bit_matrix_peek (matrix, x, y) != 0;
201  else
202  return 0;
203 }
204 
205 static inline void
206 g_bit_matrix_free (GBitMatrix *matrix)
207 {
208  g_free (matrix);
209 }
210 
211 
212 /* --- predicate idle --- */
213 guint g_predicate_idle_add (GSourceFunc predicate,
214  GSourceFunc function,
215  gpointer data);
216 guint g_predicate_idle_add_full (gint priority,
217  GSourceFunc predicate,
218  GSourceFunc function,
219  gpointer data,
220  GDestroyNotify notify);
221 
222 
223 /* --- unix signal queue --- */
224 #if 0
225 typedef gboolean (*GUSignalFunc) (gint8 usignal,
226  gpointer data);
227 guint g_usignal_add (gint8 usignal,
228  GUSignalFunc function,
229  gpointer data);
230 guint g_usignal_add_full (gint priority,
231  gint8 usignal,
232  GUSignalFunc function,
233  gpointer data,
234  GDestroyNotify destroy);
235 void g_usignal_notify (gint8 usignal);
236 #endif
237 
238 
239 /* --- GType boilerplate --- */
240 #ifndef G_DEFINE_DATA_TYPE // GTKFIX: add this to glib?
241 #define G_DEFINE_DATA_TYPE(TN, t_n, T_P) G_DEFINE_DATA_TYPE_EXTENDED (TN, t_n, T_P, GTypeFlags (0), {})
242 #define G_DEFINE_DATA_TYPE_WITH_CODE(TN, t_n, T_P, _C_) G_DEFINE_DATA_TYPE_EXTENDED (TN, t_n, T_P, GTypeFlags (0), _C_)
243 #define G_DEFINE_ABSTRACT_DATA_TYPE(TN, t_n, T_P) G_DEFINE_DATA_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, {})
244 #define G_DEFINE_ABSTRACT_DATA_TYPE_WITH_CODE(TN, t_n, T_P, _C_) G_DEFINE_DATA_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, _C_)
245 #endif /* !G_DEFINE_DATA_TYPE */
246 #ifndef G_DEFINE_DATA_TYPE_EXTENDED // GTKFIX: add this to glib?
247 #define G_DEFINE_DATA_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
248 \
249 static void type_name##_init (TypeName *self, \
250  TypeName##Class *klass); \
251 static void type_name##_class_init (TypeName##Class *klass, \
252  gpointer class_data); \
253 static gpointer type_name##_parent_class = NULL; \
254 static void type_name##_class_intern_init (gpointer klass, \
255  gpointer class_data) \
256 { \
257  type_name##_parent_class = g_type_class_peek_parent (klass); \
258  type_name##_class_init ((TypeName##Class*) klass, class_data); \
259 } \
260 \
261 GType \
262 type_name##_get_type (void) \
263 { \
264  static GType g_define_type_id = 0; \
265  if (G_UNLIKELY (g_define_type_id == 0)) \
266  { \
267  static const GTypeInfo g_define_type_info = { \
268  sizeof (TypeName##Class), \
269  (GBaseInitFunc) NULL, \
270  (GBaseFinalizeFunc) NULL, \
271  (GClassInitFunc) type_name##_class_intern_init, \
272  (GClassFinalizeFunc) NULL, \
273  NULL, /* class_data */ \
274  sizeof (TypeName), \
275  0, /* n_preallocs */ \
276  (GInstanceInitFunc) type_name##_init, \
277  }; \
278  g_define_type_id = g_type_register_static (TYPE_PARENT, #TypeName, &g_define_type_info, flags); \
279  { CODE ; } \
280  } \
281  return g_define_type_id; \
282 }
283 #endif /* !G_DEFINE_DATA_TYPE */
284 
285 
286 /* --- GScanner --- */
287 GScanner* g_scanner_new64 (const GScannerConfig *config_templ);
288 #ifndef G_DISABLE_DEPRECATED
289 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
290  g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
291 } G_STMT_END
292 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
293  g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
294 } G_STMT_END
295 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
296  g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
297 } G_STMT_END
298 #define g_scanner_freeze_symbol_table(scanner) ((void)0)
299 #define g_scanner_thaw_symbol_table(scanner) ((void)0)
300 #endif /* G_DISABLE_DEPRECATED */
301 
302 // == BSE_INSTALLPATH ==
303 // See also configure.ac, this function is here because all of sfi, bse and beast include this file.
304 enum BseInstallpathType {
305  BSE_INSTALLPATH_INCLUDES = 1,
306  BSE_INSTALLPATH_BINDIR,
307  BSE_INSTALLPATH_LOCALEBASE,
308  BSE_INSTALLPATH_LADSPA,
309  BSE_INSTALLPATH_PKGDOCDIR,
310  BSE_INSTALLPATH_USER_DATA,
311  BSE_INSTALLPATH_PKGLIBDIR,
312  BSE_INSTALLPATH_PKGLIBDIR_PLUGINS,
313  BSE_INSTALLPATH_PKGLIBDIR_DRIVERS,
314  BSE_INSTALLPATH_PKGDATADIR,
315  BSE_INSTALLPATH_PKGDATADIR_DEMO,
316  BSE_INSTALLPATH_PKGDATADIR_SAMPLES,
317  BSE_INSTALLPATH_PKGDATADIR_EFFECTS,
318  BSE_INSTALLPATH_PKGDATADIR_INSTRUMENTS,
319  BSE_INSTALLPATH_PKGDATADIR_SCRIPTS,
320  BSE_INSTALLPATH_PKGDATADIR_IMAGES,
321  BSE_INSTALLPATH_PKGDATADIR_KEYS,
322  BSE_INSTALLPATH_PKGDATADIR_SKINS,
323 };
325 std::string bse_installpath (BseInstallpathType installpath_type);
326 
329 
330 G_END_DECLS
331 
332 // == Flags Enumeration Operators in C++ ==
333 #ifdef __cplusplus
334 constexpr GParamFlags operator& (GParamFlags s1, GParamFlags s2) { return GParamFlags (s1 & (long long unsigned) s2); }
335 inline GParamFlags& operator&= (GParamFlags &s1, GParamFlags s2) { s1 = s1 & s2; return s1; }
336 constexpr GParamFlags operator| (GParamFlags s1, GParamFlags s2) { return GParamFlags (s1 | (long long unsigned) s2); }
337 inline GParamFlags& operator|= (GParamFlags &s1, GParamFlags s2) { s1 = s1 | s2; return s1; }
338 constexpr GParamFlags operator~ (GParamFlags s1) { return GParamFlags (~(long long unsigned) s1); }
339 constexpr GSignalMatchType operator& (GSignalMatchType s1, GSignalMatchType s2) { return GSignalMatchType (s1 & (long long unsigned) s2); }
340 inline GSignalMatchType& operator&= (GSignalMatchType &s1, GSignalMatchType s2) { s1 = s1 & s2; return s1; }
341 constexpr GSignalMatchType operator| (GSignalMatchType s1, GSignalMatchType s2) { return GSignalMatchType (s1 | (long long unsigned) s2); }
342 inline GSignalMatchType& operator|= (GSignalMatchType &s1, GSignalMatchType s2) { s1 = s1 | s2; return s1; }
343 constexpr GSignalMatchType operator~ (GSignalMatchType s1) { return GSignalMatchType (~(long long unsigned) s1); }
344 constexpr GSignalFlags operator& (GSignalFlags s1, GSignalFlags s2) { return GSignalFlags (s1 & (long long unsigned) s2); }
345 inline GSignalFlags& operator&= (GSignalFlags &s1, GSignalFlags s2) { s1 = s1 & s2; return s1; }
346 constexpr GSignalFlags operator| (GSignalFlags s1, GSignalFlags s2) { return GSignalFlags (s1 | (long long unsigned) s2); }
347 inline GSignalFlags& operator|= (GSignalFlags &s1, GSignalFlags s2) { s1 = s1 | s2; return s1; }
348 constexpr GSignalFlags operator~ (GSignalFlags s1) { return GSignalFlags (~(long long unsigned) s1); }
349 constexpr GConnectFlags operator& (GConnectFlags s1, GConnectFlags s2) { return GConnectFlags (s1 & (long long unsigned) s2); }
350 inline GConnectFlags& operator&= (GConnectFlags &s1, GConnectFlags s2) { s1 = s1 & s2; return s1; }
351 constexpr GConnectFlags operator| (GConnectFlags s1, GConnectFlags s2) { return GConnectFlags (s1 | (long long unsigned) s2); }
352 inline GConnectFlags& operator|= (GConnectFlags &s1, GConnectFlags s2) { s1 = s1 | s2; return s1; }
353 constexpr GConnectFlags operator~ (GConnectFlags s1) { return GConnectFlags (~(long long unsigned) s1); }
354 #endif // __cplusplus
355 #endif /* __SFI_GLIB_EXTRA_H__ */
STL class.
std::string bse_version()
Provide a string containing the BSE library version number.
Definition: glib-extra.cc:1071
std::string bse_installpath(BseInstallpathType installpath_type)
Provide installation directories and searchpaths for various types of data.
Definition: glib-extra.cc:1044
Definition: glib-extra.hh:148