diff options
| author | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-01-04 09:13:46 +0000 |
|---|---|---|
| committer | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-01-04 09:13:46 +0000 |
| commit | bb2f640231f5f88cb819cb761be2f30d90a34f00 (patch) | |
| tree | 2e72dfaa8084e67354dafc530e71a1e7838d45ff | |
| parent | 1e21b80fcd678cf3d073609e693b3435efbb84a2 (diff) | |
Core: in utils.h, add a macros, fix existing
* lasso/utils.h:
- add macro lasso_ref(object), if object is not null, call
g_object_ref on it, and return the value, otherwise do nothing and
return NULL.
- make a better reporting of bad object release
- change format type for __LINE__ and dest arguments in
lasso_release_gobject warning display.
- add a lasso_check_non_empty_string macro
- add new macro to extract a specific node type from a list of
GObject objects.
- use xmlStrdup not g_strdup for lasso_assign_xml_string
- add lasso_list_add_gstrv and lasso_check_good_rc
- add macro lasso_list_get_first_child
- add inline function to test empty string
- change macro lasso_check_non_empty_string to use the new inline
function and go to cleanup
- fix lasso_check_non_empty_string macro
* lasso/utils.c:
- add lasso_gobject_is_of_type returns 0 if first parameters is a
gobject whose GType is equal to the second parameter, and 1
otherwise.
| -rw-r--r-- | lasso/utils.c | 22 | ||||
| -rw-r--r-- | lasso/utils.h | 57 |
2 files changed, 76 insertions, 3 deletions
diff --git a/lasso/utils.c b/lasso/utils.c index 59051800..fa012b49 100644 --- a/lasso/utils.c +++ b/lasso/utils.c @@ -91,3 +91,25 @@ lasso_safe_prefix_string(const gchar *str, gsize length) g_string_free(output, FALSE); return ret; } + +/** + * lasso_gobject_is_of_type: + * @a: a #GObject object + * @b: a #GType value + * + * Return true if object @a is of type @b. + * + * Return value: whether object @a is of type @b. + */ +int +lasso_gobject_is_of_type(GObject *a, GType b) +{ + GType typeid = (GType)b; + + if (a && G_IS_OBJECT(a)) { + return G_OBJECT_TYPE(G_OBJECT(a)) == typeid ? 0 : 1; + } + return 1; +} + + diff --git a/lasso/utils.h b/lasso/utils.h index ac2bcecf..d6836f41 100644 --- a/lasso/utils.h +++ b/lasso/utils.h @@ -26,6 +26,7 @@ #define __LASSO_UTILS_H__ #include <glib.h> +#include <glib-object.h> #include "debug.h" #include "./backward_comp.h" @@ -53,6 +54,8 @@ #define lasso_private_data(object) ((object)->private_data) +#define lasso_ref(object) ((object) != NULL ? g_object_ref(object) : NULL) + /* Freeing */ #define lasso_release(dest) \ { \ @@ -81,7 +84,7 @@ if (G_IS_OBJECT(dest) || dest == NULL) { \ lasso_release_full(dest, g_object_unref); \ } else { \ - g_critical("Trying to unref a non GObject pointer dest=%s", #dest); \ + g_critical("Trying to unref a non GObject pointer file=%s:%u pointerbybname=%s pointer=%p", __FILE__, __LINE__, #dest, dest); \ } \ } @@ -153,7 +156,7 @@ #define lasso_assign_xml_string(dest,src) \ { \ - xmlChar *__tmp = g_strdup(src); \ + xmlChar *__tmp = xmlStrdup(src); \ lasso_release_xml_string(dest); \ dest = __tmp; \ } @@ -196,7 +199,7 @@ { \ GList *__tmp = (src); \ lasso_release_gobject_list(dest); \ - dest = __tmp; \ + dest = (GList*)__tmp; \ } #define lasso_assign_list_of_gobjects(dest, src) \ @@ -300,6 +303,15 @@ lasso_list_add_non_null(dest, __tmp_src); \ } +#define lasso_list_add_gstrv(dest, src) \ + { \ + GList **__tmp_dest = &(dest); \ + const char **__iter = (const char**)(src); \ + while (__iter && *__iter) { \ + lasso_list_add_string(*__tmp_dest, *__iter); \ + } \ + } + /* Pointer ownership transfer */ #define lasso_transfer_full(dest, src, kind) \ {\ @@ -341,6 +353,23 @@ #define lasso_null_param(name) \ g_return_val_if_fail(name != NULL, LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ); +inline static gboolean +lasso_is_empty_string(const char *str) { + return ((str) == NULL || (str)[0] == '\0'); +} + +/** + * lasso_check_non_empty_string: + * @str: a char pointer + * + * Check that @str is non-NULL and not empty, otherwise jump to cleanup and return + * LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ. + */ +#define lasso_check_non_empty_string(str) \ + goto_cleanup_if_fail_with_rc(! lasso_is_empty_string(str), \ + LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ); + + /** * The following macros are made to create some formalism for function's cleanup code. * @@ -410,6 +439,18 @@ } \ } +/** + * check_good_rc: + * @what: a call to a function returning a lasso error code + * + * Check if return code is 0, if not store it in rc and jump to cleanup label. + */ +#define lasso_check_good_rc(what) \ + { \ + int __rc = (what);\ + goto_cleanup_if_fail_with_rc(__rc == 0, __rc); \ + } + #define lasso_mem_debug(who, what, where) \ { \ if (lasso_flag_memory_debug) \ @@ -439,6 +480,9 @@ #define lasso_foreach_full_end() \ } } +#define lasso_list_get_first_child(list) \ + ((list) ? (list)->data : NULL) + /* Declare type of element in a container */ #ifndef OFTYPE #define OFTYPE(x) @@ -446,5 +490,12 @@ /* Get a printable extract for error messages */ char* lasso_safe_prefix_string(const char *str, gsize length); +int lasso_gobject_is_of_type(GObject *a, GType b); + +/* Get first node of this type in a list */ +/* ex: lasso_extract_node (LassoNode, LASSO_TYPE_NODE, list) */ +#define lasso_extract_gobject_from_list(type, gobjecttype, list) \ + (type*) g_list_find_custom(list, \ + (gconstpointer)gobjecttype, (GCompareFunc)lasso_gobject_is_of_type); #endif /* __LASSO_UTILS_H__ */ |
