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