BEAST/BSE - Better Audio System and Sound Engine  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 gchar* g_path_concat (const gchar *first_path,
77  ...) G_GNUC_NULL_TERMINATED;
78 GString* g_string_prefix_lines (GString *gstring,
79  const gchar *pstr);
80 
81 
82 /* --- string options --- */
83 gchar* g_option_concat (const gchar *first_option,
84  ...) G_GNUC_NULL_TERMINATED;
85 gboolean g_option_check (const gchar *option_string,
86  const gchar *option);
87 gchar* g_option_get (const gchar *option_string,
88  const gchar *option);
89 
90 
91 /* --- GParamSpec extensions --- */
92 void g_param_spec_set_options (GParamSpec *pspec,
93  const gchar *options);
94 void g_param_spec_add_option (GParamSpec *pspec,
95  const gchar *option,
96  const gchar *value);
97 gboolean g_param_spec_check_option (GParamSpec *pspec,
98  const gchar *option);
99 gboolean g_param_spec_provides_options (GParamSpec *pspec,
100  const gchar *options);
101 const gchar* g_param_spec_get_options (GParamSpec *pspec);
102 void g_param_spec_set_istepping (GParamSpec *pspec,
103  guint64 stepping);
104 guint64 g_param_spec_get_istepping (GParamSpec *pspec);
105 void g_param_spec_set_fstepping (GParamSpec *pspec,
106  gdouble stepping);
107 gdouble g_param_spec_get_fstepping (GParamSpec *pspec);
108 void g_param_spec_set_log_scale (GParamSpec *pspec,
109  gdouble center,
110  gdouble base,
111  gdouble n_steps);
112 gboolean g_param_spec_get_log_scale (GParamSpec *pspec,
113  gdouble *center,
114  gdouble *base,
115  gdouble *n_steps);
116 
117 
118 /* --- list extensions --- */
119 gpointer g_slist_pop_head (GSList **slist_p);
120 gpointer g_list_pop_head (GList **list_p);
121 GSList* g_slist_append_uniq (GSList *slist,
122  gpointer data);
123 void g_slist_free_deep (GSList *slist,
124  GDestroyNotify data_destroy);
125 void g_list_free_deep (GList *list,
126  GDestroyNotify data_destroy);
127 
128 
129 /* --- name conversions --- */
130 gchar* g_type_name_to_cname (const gchar *type_name);
131 gchar* g_type_name_to_sname (const gchar *type_name);
132 gchar* g_type_name_to_cupper (const gchar *type_name);
133 gchar* g_type_name_to_type_macro (const gchar *type_name);
134 
135 
136 /* --- simple main loop source --- */
137 typedef gboolean (*GSourcePending) (gpointer data,
138  gint *timeout);
139 typedef void (*GSourceDispatch) (gpointer data);
140 GSource* g_source_simple (gint priority,
141  GSourcePending pending,
142  GSourceDispatch dispatch,
143  gpointer data,
144  GDestroyNotify destroy,
145  GPollFD *first_pfd,
146  ...);
147 
148 
149 /* --- bit matrix --- */
150 typedef struct {
151  guint32 width, height;
152  guint32 bits[1]; /* flexible array */
153 } GBitMatrix;
154 
155 static inline GBitMatrix*
156 g_bit_matrix_new (guint width,
157  guint height)
158 {
159  GBitMatrix *matrix = (GBitMatrix*) g_new0 (guint32, MAX ((width * height + 31) / 32, 1) + 2);
160  matrix->width = width;
161  matrix->height = height;
162  return matrix;
163 }
164 
165 static inline void
166 g_bit_matrix_change (GBitMatrix *matrix,
167  guint x,
168  guint y,
169  gboolean bit_set)
170 {
171  guint32 cons, index, shift;
172  g_return_if_fail (matrix && x < matrix->width && y < matrix->height);
173  cons = y * matrix->width + x;
174  index = cons >> 5; /* / 32 */
175  shift = cons & 0x1f; /* % 32 */
176  if (bit_set)
177  matrix->bits[index] |= 1 << shift;
178  else
179  matrix->bits[index] &= ~(1 << shift);
180 }
181 
182 #define g_bit_matrix_set(matrix,x,y) g_bit_matrix_change (matrix, x, y, TRUE)
183 #define g_bit_matrix_unset(matrix,x,y) g_bit_matrix_change (matrix, x, y, FALSE)
184 
185 static inline guint32
186 g_bit_matrix_peek (GBitMatrix *matrix,
187  guint x,
188  guint y)
189 {
190  guint32 cons = y * matrix->width + x;
191  guint32 index = cons >> 5; /* / 32 */
192  guint32 shift = cons & 0x1f; /* % 32 */
193  return matrix->bits[index] & (1 << shift);
194 }
195 
196 static inline gboolean
197 g_bit_matrix_test (GBitMatrix *matrix,
198  guint x,
199  guint y)
200 {
201  if (x < matrix->width && y < matrix->height)
202  return g_bit_matrix_peek (matrix, x, y) != 0;
203  else
204  return 0;
205 }
206 
207 static inline void
208 g_bit_matrix_free (GBitMatrix *matrix)
209 {
210  g_free (matrix);
211 }
212 
213 
214 /* --- predicate idle --- */
215 guint g_predicate_idle_add (GSourceFunc predicate,
216  GSourceFunc function,
217  gpointer data);
218 guint g_predicate_idle_add_full (gint priority,
219  GSourceFunc predicate,
220  GSourceFunc function,
221  gpointer data,
222  GDestroyNotify notify);
223 
224 
225 /* --- unix signal queue --- */
226 #if 0
227 typedef gboolean (*GUSignalFunc) (gint8 usignal,
228  gpointer data);
229 guint g_usignal_add (gint8 usignal,
230  GUSignalFunc function,
231  gpointer data);
232 guint g_usignal_add_full (gint priority,
233  gint8 usignal,
234  GUSignalFunc function,
235  gpointer data,
236  GDestroyNotify destroy);
237 void g_usignal_notify (gint8 usignal);
238 #endif
239 
240 
241 /* --- GType boilerplate --- */
242 #ifndef G_DEFINE_DATA_TYPE // GTKFIX: add this to glib?
243 #define G_DEFINE_DATA_TYPE(TN, t_n, T_P) G_DEFINE_DATA_TYPE_EXTENDED (TN, t_n, T_P, GTypeFlags (0), {})
244 #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_)
245 #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, {})
246 #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_)
247 #endif /* !G_DEFINE_DATA_TYPE */
248 #ifndef G_DEFINE_DATA_TYPE_EXTENDED // GTKFIX: add this to glib?
249 #define G_DEFINE_DATA_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
250 \
251 static void type_name##_init (TypeName *self, \
252  TypeName##Class *klass); \
253 static void type_name##_class_init (TypeName##Class *klass, \
254  gpointer class_data); \
255 static gpointer type_name##_parent_class = NULL; \
256 static void type_name##_class_intern_init (gpointer klass, \
257  gpointer class_data) \
258 { \
259  type_name##_parent_class = g_type_class_peek_parent (klass); \
260  type_name##_class_init ((TypeName##Class*) klass, class_data); \
261 } \
262 \
263 GType \
264 type_name##_get_type (void) \
265 { \
266  static GType g_define_type_id = 0; \
267  if (G_UNLIKELY (g_define_type_id == 0)) \
268  { \
269  static const GTypeInfo g_define_type_info = { \
270  sizeof (TypeName##Class), \
271  (GBaseInitFunc) NULL, \
272  (GBaseFinalizeFunc) NULL, \
273  (GClassInitFunc) type_name##_class_intern_init, \
274  (GClassFinalizeFunc) NULL, \
275  NULL, /* class_data */ \
276  sizeof (TypeName), \
277  0, /* n_preallocs */ \
278  (GInstanceInitFunc) type_name##_init, \
279  }; \
280  g_define_type_id = g_type_register_static (TYPE_PARENT, #TypeName, &g_define_type_info, flags); \
281  { CODE ; } \
282  } \
283  return g_define_type_id; \
284 }
285 #endif /* !G_DEFINE_DATA_TYPE */
286 
287 
288 /* --- GScanner --- */
289 GScanner* g_scanner_new64 (const GScannerConfig *config_templ);
290 #ifndef G_DISABLE_DEPRECATED
291 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
292  g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
293 } G_STMT_END
294 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
295  g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
296 } G_STMT_END
297 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
298  g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
299 } G_STMT_END
300 #define g_scanner_freeze_symbol_table(scanner) ((void)0)
301 #define g_scanner_thaw_symbol_table(scanner) ((void)0)
302 #endif /* G_DISABLE_DEPRECATED */
303 
304 
305 G_END_DECLS
306 
307 // == Flags Enumeration Operators in C++ ==
308 #ifdef __cplusplus
309 constexpr GParamFlags operator& (GParamFlags s1, GParamFlags s2) { return GParamFlags (s1 & (long long unsigned) s2); }
310 inline GParamFlags& operator&= (GParamFlags &s1, GParamFlags s2) { s1 = s1 & s2; return s1; }
311 constexpr GParamFlags operator| (GParamFlags s1, GParamFlags s2) { return GParamFlags (s1 | (long long unsigned) s2); }
312 inline GParamFlags& operator|= (GParamFlags &s1, GParamFlags s2) { s1 = s1 | s2; return s1; }
313 constexpr GParamFlags operator~ (GParamFlags s1) { return GParamFlags (~(long long unsigned) s1); }
314 constexpr GSignalMatchType operator& (GSignalMatchType s1, GSignalMatchType s2) { return GSignalMatchType (s1 & (long long unsigned) s2); }
315 inline GSignalMatchType& operator&= (GSignalMatchType &s1, GSignalMatchType s2) { s1 = s1 & s2; return s1; }
316 constexpr GSignalMatchType operator| (GSignalMatchType s1, GSignalMatchType s2) { return GSignalMatchType (s1 | (long long unsigned) s2); }
317 inline GSignalMatchType& operator|= (GSignalMatchType &s1, GSignalMatchType s2) { s1 = s1 | s2; return s1; }
318 constexpr GSignalMatchType operator~ (GSignalMatchType s1) { return GSignalMatchType (~(long long unsigned) s1); }
319 constexpr GSignalFlags operator& (GSignalFlags s1, GSignalFlags s2) { return GSignalFlags (s1 & (long long unsigned) s2); }
320 inline GSignalFlags& operator&= (GSignalFlags &s1, GSignalFlags s2) { s1 = s1 & s2; return s1; }
321 constexpr GSignalFlags operator| (GSignalFlags s1, GSignalFlags s2) { return GSignalFlags (s1 | (long long unsigned) s2); }
322 inline GSignalFlags& operator|= (GSignalFlags &s1, GSignalFlags s2) { s1 = s1 | s2; return s1; }
323 constexpr GSignalFlags operator~ (GSignalFlags s1) { return GSignalFlags (~(long long unsigned) s1); }
324 constexpr GConnectFlags operator& (GConnectFlags s1, GConnectFlags s2) { return GConnectFlags (s1 & (long long unsigned) s2); }
325 inline GConnectFlags& operator&= (GConnectFlags &s1, GConnectFlags s2) { s1 = s1 & s2; return s1; }
326 constexpr GConnectFlags operator| (GConnectFlags s1, GConnectFlags s2) { return GConnectFlags (s1 | (long long unsigned) s2); }
327 inline GConnectFlags& operator|= (GConnectFlags &s1, GConnectFlags s2) { s1 = s1 | s2; return s1; }
328 constexpr GConnectFlags operator~ (GConnectFlags s1) { return GConnectFlags (~(long long unsigned) s1); }
329 #endif // __cplusplus
330 #endif /* __SFI_GLIB_EXTRA_H__ */
int width() const
int height() const
Definition: glib-extra.hh:150