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