BEAST/BSE - Better Audio System and Sound Engine  0.8.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
glib-extra.hh
Go to the documentation of this file.
00001  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
00002 #ifndef __SFI_GLIB_EXTRA_H__
00003 #define __SFI_GLIB_EXTRA_H__
00004 
00005 #include <glib.h>
00006 #include <glib-object.h>
00007 #include <rapicorn-core.hh>     // for Rapicorn::string_format
00008 
00009 G_BEGIN_DECLS
00010 
00011 #if (GLIB_SIZEOF_LONG > 4)
00012 #define G_HASH_LONG(l)  ((l) + ((l) >> 32))
00013 #else
00014 #define G_HASH_LONG(l)  (l)
00015 #endif
00016 #if (GLIB_SIZEOF_VOID_P > 4)
00017 #define G_HASH_POINTER(p)       ((guint32) (((gsize) (p)) + (((gsize) (p)) >> 32)))
00018 #else
00019 #define G_HASH_POINTER(p)       ((guint32) (gsize) (p))
00020 #endif
00021 /* Provide a string identifying the current function, non-concatenatable */
00022 #ifndef G_STRFUNC
00023 #  if defined (__GNUC__)
00024 #    define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
00025 #  elif defined (G_HAVE_ISO_VARARGS)
00026 #    define G_STRFUNC     ((const char*) (__func__))
00027 #  elif
00028 #    define G_STRFUNC     ((const char*) ("???"))
00029 #  endif
00030 #endif
00031 
00032 
00033 /* --- provide (historic) aliases --- */
00034 #define g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
00035   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
00036 } G_STMT_END
00037 #define g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
00038   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
00039 } G_STMT_END
00040 
00041 
00042 /* --- abandon typesafety for some frequently used functions --- */
00043 #ifndef __cplusplus
00044 #define g_object_notify(o,s)              g_object_notify ((gpointer) o, s)
00045 #define g_object_get_qdata(o,q)           g_object_get_qdata ((gpointer) o, q)
00046 #define g_object_set_qdata(o,q,d)         g_object_set_qdata ((gpointer) o, q, d)
00047 #define g_object_set_qdata_full(o,q,d,f)  g_object_set_qdata_full ((gpointer) o, q, d, (gpointer) f)
00048 #define g_object_steal_qdata(o,q)         g_object_steal_qdata ((gpointer) o, q)
00049 #define g_object_get_data(o,k)            g_object_get_data ((gpointer) o, k)
00050 #define g_object_set_data(o,k,d)          g_object_set_data ((gpointer) o, k, d)
00051 #define g_object_set_data_full(o,k,d,f)   g_object_set_data_full ((gpointer) o, k, d, (gpointer) f)
00052 #define g_object_steal_data(o,k)          g_object_steal_data ((gpointer) o, k)
00053 #endif  /* !__cplusplus */
00054 void g_object_disconnect_any (gpointer object,
00055                               gpointer function,
00056                               gpointer data); /* workaorund for g_object_disconnect() */
00057 
00058 // == printf variants ==
00059 #define g_intern_format(...)            g_intern_string (Rapicorn::string_format (__VA_ARGS__).c_str())
00060 #define g_string_add_format(gstr, ...)  g_string_append (gstr, Rapicorn::string_format (__VA_ARGS__).c_str())
00061 #define g_strdup_format(...)            g_strdup (Rapicorn::string_format (__VA_ARGS__).c_str())
00062 
00063 /* --- string functions --- */
00064 const gchar*    g_printf_find_localised_directive (const gchar *format);
00065 gchar**         g_straddv         (gchar        **str_array,
00066                                    const gchar   *new_str);
00067 gchar**         g_strslistv       (GSList        *slist);
00068 guint           g_strlenv         (gchar        **str_array);
00069 gchar*          g_strdup_stripped (const gchar   *string);
00070 gchar*          g_strdup_rstrip   (const gchar   *string);
00071 gchar*          g_strdup_lstrip   (const gchar   *string);
00072 
00073 const gchar*    g_intern_strconcat      (const gchar   *first_string,
00074                                          ...) G_GNUC_NULL_TERMINATED;
00075 
00076 gchar*          g_path_concat     (const gchar   *first_path,
00077                                    ...) G_GNUC_NULL_TERMINATED;
00078 GString*        g_string_prefix_lines (GString     *gstring,
00079                                        const gchar *pstr);
00080 
00081 
00082 /* --- string options --- */
00083 gchar*          g_option_concat   (const gchar   *first_option,
00084                                    ...) G_GNUC_NULL_TERMINATED;
00085 gboolean        g_option_check    (const gchar   *option_string,
00086                                    const gchar   *option);
00087 gchar*          g_option_get      (const gchar   *option_string,
00088                                    const gchar   *option);
00089 
00090 
00091 /* --- GParamSpec extensions --- */
00092 void         g_param_spec_set_options      (GParamSpec  *pspec,
00093                                             const gchar *options);
00094 void         g_param_spec_add_option       (GParamSpec  *pspec,
00095                                             const gchar *option,
00096                                             const gchar *value);
00097 gboolean     g_param_spec_check_option     (GParamSpec  *pspec,
00098                                             const gchar *option);
00099 gboolean     g_param_spec_provides_options (GParamSpec  *pspec,
00100                                             const gchar *options);
00101 const gchar* g_param_spec_get_options      (GParamSpec  *pspec);
00102 void         g_param_spec_set_istepping    (GParamSpec  *pspec,
00103                                             guint64      stepping);
00104 guint64      g_param_spec_get_istepping    (GParamSpec  *pspec);
00105 void         g_param_spec_set_fstepping    (GParamSpec  *pspec,
00106                                             gdouble      stepping);
00107 gdouble      g_param_spec_get_fstepping    (GParamSpec  *pspec);
00108 void         g_param_spec_set_log_scale    (GParamSpec  *pspec,
00109                                             gdouble      center,
00110                                             gdouble      base,
00111                                             gdouble      n_steps);
00112 gboolean     g_param_spec_get_log_scale    (GParamSpec  *pspec,
00113                                             gdouble     *center,
00114                                             gdouble     *base,
00115                                             gdouble     *n_steps);
00116 
00117 
00118 /* --- list extensions --- */
00119 gpointer        g_slist_pop_head        (GSList      **slist_p);
00120 gpointer        g_list_pop_head         (GList       **list_p);
00121 GSList*         g_slist_append_uniq     (GSList       *slist,
00122                                          gpointer      data);
00123 void            g_slist_free_deep       (GSList       *slist,
00124                                          GDestroyNotify data_destroy);
00125 void            g_list_free_deep        (GList         *list,
00126                                          GDestroyNotify data_destroy);
00127 
00128 
00129 /* --- name conversions --- */
00130 gchar*  g_type_name_to_cname            (const gchar    *type_name);
00131 gchar*  g_type_name_to_sname            (const gchar    *type_name);
00132 gchar*  g_type_name_to_cupper           (const gchar    *type_name);
00133 gchar*  g_type_name_to_type_macro       (const gchar    *type_name);
00134 
00135 
00136 /* --- simple main loop source --- */
00137 typedef gboolean (*GSourcePending)  (gpointer    data,
00138                                      gint       *timeout);
00139 typedef void     (*GSourceDispatch) (gpointer    data);
00140 GSource*        g_source_simple (gint            priority,
00141                                  GSourcePending  pending,
00142                                  GSourceDispatch dispatch,
00143                                  gpointer        data,
00144                                  GDestroyNotify  destroy,
00145                                  GPollFD        *first_pfd,
00146                                  ...);
00147 
00148 
00149 /* --- bit matrix --- */
00150 typedef struct {
00151   guint32 width, height;
00152   guint32 bits[1]; /* flexible array */
00153 } GBitMatrix;
00154 
00155 static inline GBitMatrix*
00156 g_bit_matrix_new (guint           width,
00157                   guint           height)
00158 {
00159   GBitMatrix *matrix = (GBitMatrix*) g_new0 (guint32, MAX ((width * height + 31) / 32, 1) + 2);
00160   matrix->width = width;
00161   matrix->height = height;
00162   return matrix;
00163 }
00164 
00165 static inline void
00166 g_bit_matrix_change (GBitMatrix     *matrix,
00167                      guint           x,
00168                      guint           y,
00169                      gboolean        bit_set)
00170 {
00171   guint32 cons, index, shift;
00172   g_return_if_fail (matrix && x < matrix->width && y < matrix->height);
00173   cons = y * matrix->width + x;
00174   index = cons >> 5; /* / 32 */
00175   shift = cons & 0x1f;  /* % 32 */
00176   if (bit_set)
00177     matrix->bits[index] |= 1 << shift;
00178   else
00179     matrix->bits[index] &= ~(1 << shift);
00180 }
00181 
00182 #define g_bit_matrix_set(matrix,x,y)    g_bit_matrix_change (matrix, x, y, TRUE)
00183 #define g_bit_matrix_unset(matrix,x,y)  g_bit_matrix_change (matrix, x, y, FALSE)
00184 
00185 static inline guint32
00186 g_bit_matrix_peek (GBitMatrix     *matrix,
00187                    guint           x,
00188                    guint           y)
00189 {
00190   guint32 cons = y * matrix->width + x;
00191   guint32 index = cons >> 5; /* / 32 */
00192   guint32 shift = cons & 0x1f;  /* % 32 */
00193   return matrix->bits[index] & (1 << shift);
00194 }
00195 
00196 static inline gboolean
00197 g_bit_matrix_test (GBitMatrix *matrix,
00198                    guint       x,
00199                    guint       y)
00200 {
00201   if (x < matrix->width && y < matrix->height)
00202     return g_bit_matrix_peek (matrix, x, y) != 0;
00203   else
00204     return 0;
00205 }
00206 
00207 static inline void
00208 g_bit_matrix_free (GBitMatrix *matrix)
00209 {
00210   g_free (matrix);
00211 }
00212 
00213 
00214 /* --- predicate idle --- */
00215 guint g_predicate_idle_add      (GSourceFunc     predicate,
00216                                  GSourceFunc     function,
00217                                  gpointer        data);
00218 guint g_predicate_idle_add_full (gint            priority,
00219                                  GSourceFunc     predicate,
00220                                  GSourceFunc     function,
00221                                  gpointer        data,
00222                                  GDestroyNotify  notify);
00223 
00224 
00225 /* --- unix signal queue --- */
00226 #if 0
00227 typedef gboolean (*GUSignalFunc) (gint8          usignal,
00228                                   gpointer       data);
00229 guint   g_usignal_add            (gint8          usignal,
00230                                   GUSignalFunc   function,
00231                                   gpointer       data);
00232 guint   g_usignal_add_full       (gint           priority,
00233                                   gint8          usignal,
00234                                   GUSignalFunc   function,
00235                                   gpointer       data,
00236                                   GDestroyNotify destroy);
00237 void    g_usignal_notify         (gint8          usignal);
00238 #endif
00239 
00240 
00241 /* --- GType boilerplate --- */
00242 #ifndef G_DEFINE_DATA_TYPE              // GTKFIX: add this to glib?
00243 #define G_DEFINE_DATA_TYPE(TN, t_n, T_P)                         G_DEFINE_DATA_TYPE_EXTENDED (TN, t_n, T_P, GTypeFlags (0), {})
00244 #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_)
00245 #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, {})
00246 #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_)
00247 #endif /* !G_DEFINE_DATA_TYPE */
00248 #ifndef G_DEFINE_DATA_TYPE_EXTENDED     // GTKFIX: add this to glib?
00249 #define G_DEFINE_DATA_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
00250 \
00251 static void     type_name##_init              (TypeName        *self, \
00252                                                TypeName##Class *klass); \
00253 static void     type_name##_class_init        (TypeName##Class *klass, \
00254                                                gpointer         class_data); \
00255 static gpointer type_name##_parent_class = NULL; \
00256 static void     type_name##_class_intern_init (gpointer klass, \
00257                                                gpointer class_data) \
00258 { \
00259   type_name##_parent_class = g_type_class_peek_parent (klass); \
00260   type_name##_class_init ((TypeName##Class*) klass, class_data); \
00261 } \
00262 \
00263 GType \
00264 type_name##_get_type (void) \
00265 { \
00266   static GType g_define_type_id = 0; \
00267   if (G_UNLIKELY (g_define_type_id == 0)) \
00268     { \
00269       static const GTypeInfo g_define_type_info = { \
00270         sizeof (TypeName##Class), \
00271         (GBaseInitFunc) NULL, \
00272         (GBaseFinalizeFunc) NULL, \
00273         (GClassInitFunc) type_name##_class_intern_init, \
00274         (GClassFinalizeFunc) NULL, \
00275         NULL,   /* class_data */ \
00276         sizeof (TypeName), \
00277         0,      /* n_preallocs */ \
00278         (GInstanceInitFunc) type_name##_init, \
00279       }; \
00280       g_define_type_id = g_type_register_static (TYPE_PARENT, #TypeName, &g_define_type_info, flags); \
00281       { CODE ; } \
00282     } \
00283   return g_define_type_id; \
00284 }
00285 #endif /* !G_DEFINE_DATA_TYPE */
00286 
00287 
00288 /* --- GScanner --- */
00289 GScanner*       g_scanner_new64                 (const GScannerConfig *config_templ);
00290 #ifndef G_DISABLE_DEPRECATED
00291 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
00292   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
00293 } G_STMT_END
00294 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
00295   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
00296 } G_STMT_END
00297 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
00298   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
00299 } G_STMT_END
00300 #define g_scanner_freeze_symbol_table(scanner) ((void)0)
00301 #define g_scanner_thaw_symbol_table(scanner) ((void)0)
00302 #endif /* G_DISABLE_DEPRECATED */
00303 
00304 
00305 G_END_DECLS
00306 
00307 // == Flags Enumeration Operators in C++ ==
00308 #ifdef __cplusplus
00309 constexpr GParamFlags  operator&  (GParamFlags  s1, GParamFlags s2) { return GParamFlags (s1 & (long long unsigned) s2); }
00310 inline    GParamFlags& operator&= (GParamFlags &s1, GParamFlags s2) { s1 = s1 & s2; return s1; }
00311 constexpr GParamFlags  operator|  (GParamFlags  s1, GParamFlags s2) { return GParamFlags (s1 | (long long unsigned) s2); }
00312 inline    GParamFlags& operator|= (GParamFlags &s1, GParamFlags s2) { s1 = s1 | s2; return s1; }
00313 constexpr GParamFlags  operator~  (GParamFlags  s1)                 { return GParamFlags (~(long long unsigned) s1); }
00314 constexpr GSignalMatchType  operator&  (GSignalMatchType  s1, GSignalMatchType s2) { return GSignalMatchType (s1 & (long long unsigned) s2); }
00315 inline    GSignalMatchType& operator&= (GSignalMatchType &s1, GSignalMatchType s2) { s1 = s1 & s2; return s1; }
00316 constexpr GSignalMatchType  operator|  (GSignalMatchType  s1, GSignalMatchType s2) { return GSignalMatchType (s1 | (long long unsigned) s2); }
00317 inline    GSignalMatchType& operator|= (GSignalMatchType &s1, GSignalMatchType s2) { s1 = s1 | s2; return s1; }
00318 constexpr GSignalMatchType  operator~  (GSignalMatchType  s1)                 { return GSignalMatchType (~(long long unsigned) s1); }
00319 constexpr GSignalFlags  operator&  (GSignalFlags  s1, GSignalFlags s2) { return GSignalFlags (s1 & (long long unsigned) s2); }
00320 inline    GSignalFlags& operator&= (GSignalFlags &s1, GSignalFlags s2) { s1 = s1 & s2; return s1; }
00321 constexpr GSignalFlags  operator|  (GSignalFlags  s1, GSignalFlags s2) { return GSignalFlags (s1 | (long long unsigned) s2); }
00322 inline    GSignalFlags& operator|= (GSignalFlags &s1, GSignalFlags s2) { s1 = s1 | s2; return s1; }
00323 constexpr GSignalFlags  operator~  (GSignalFlags  s1)                 { return GSignalFlags (~(long long unsigned) s1); }
00324 constexpr GConnectFlags  operator&  (GConnectFlags  s1, GConnectFlags s2) { return GConnectFlags (s1 & (long long unsigned) s2); }
00325 inline    GConnectFlags& operator&= (GConnectFlags &s1, GConnectFlags s2) { s1 = s1 & s2; return s1; }
00326 constexpr GConnectFlags  operator|  (GConnectFlags  s1, GConnectFlags s2) { return GConnectFlags (s1 | (long long unsigned) s2); }
00327 inline    GConnectFlags& operator|= (GConnectFlags &s1, GConnectFlags s2) { s1 = s1 | s2; return s1; }
00328 constexpr GConnectFlags  operator~  (GConnectFlags  s1)                 { return GConnectFlags (~(long long unsigned) s1); }
00329 #endif // __cplusplus
00330 #endif /* __SFI_GLIB_EXTRA_H__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines