grim/gplate

Parents 05bb9395cf2e
Children 66a8580d5c65
Added 2 new functions: gplate_library_[gs]et_default_function_for_tag
--- a/gplate/gplate-library.c Thu Feb 14 03:48:40 2008 -0600
+++ b/gplate/gplate-library.c Sun Feb 17 03:22:52 2008 -0600
@@ -36,6 +36,7 @@
GType tag;
gchar *prefix;
gchar *suffix;
+ GType default_function;
} GPlateTagData;
/******************************************************************************
@@ -122,6 +123,7 @@
data->tag = tag;
data->prefix = (prefix) ? g_strdup(prefix) : NULL;
data->suffix = (suffix) ? g_strdup(suffix) : NULL;
+ data->default_function = G_TYPE_INVALID;
g_type_class_unref(klass);
@@ -602,6 +604,94 @@
return default_tag;
}
+/**
+ * gplate_library_set_default_function_for_tag:
+ * @name: The name of the function.
+ * @tag: The #GType of the tag.
+ * @error: The return address for any errors.
+ *
+ * Sets the default function of @tag to @name.
+ *
+ * A default function is used when the contents of a tag do not match any
+ * functions that are registered to the tag.
+ *
+ * For example, the default function of #GPlateCommentTag is
+ * #GPlateNoopFunction.
+ *
+ * Return Value: TRUE on success, otherwise FALSE with @error set.
+ */
+gboolean
+gplate_library_set_default_function_for_tag(const gchar *name, GType tag,
+ GError **error)
+{
+ GPlateTagData *tdata = NULL;
+ GType function = G_TYPE_INVALID;
+
+ g_return_val_if_fail(name, FALSE);
+
+ gplate_library_lazy_init();
+
+ tdata = g_hash_table_lookup(tags, GSIZE_TO_POINTER(tag));
+ if(!tdata) {
+ if(error) {
+ *error = g_error_new(GPLATE_DOMAIN,
+ GPLATE_ERROR_LIBRARY_TAG_NOT_FOUND,
+ "tag '%s' is not registered",
+ g_type_name(tag));
+ }
+
+ return FALSE;
+ }
+
+ function = gplate_library_lookup_function_for_tag(name, tag, error);
+ if(function == G_TYPE_INVALID) {
+ /* error should already be set from
+ * gplate_library_lookup_function_for_tag
+ */
+ return FALSE;
+ }
+
+ tdata->default_function = function;
+
+ return TRUE;
+}
+
+/**
+ * gplate_library_get_default_function_for_tag:
+ * @tag: The #GPlateTag.
+ * @error: The return address for any errors.
+ *
+ * Get's the default function for @tag.
+ *
+ * Return Value: On success, the #GType of the function is returned.
+ * On failure, #G_TYPE_INVALID is returned and @error is set.
+ *
+ * However, if a tag does not have a default function,
+ * #G_TYPE_INVALID will be returned. So it is recommended to
+ * pass @error and check if it's set before assuming a return
+ * value of #G_TYPE_INVALID is an error.
+ */
+GType
+gplate_library_get_default_function_for_tag(GType tag, GError **error) {
+ GPlateTagData *tdata = NULL;
+
+ gplate_library_lazy_init();
+
+ tdata = g_hash_table_lookup(tags, GSIZE_TO_POINTER(tag));
+ if(!tdata) {
+ if(error) {
+ *error = g_error_new(GPLATE_DOMAIN,
+ GPLATE_ERROR_LIBRARY_TAG_NOT_FOUND,
+ "tag '%s' is not registered",
+ g_type_name(tag));
+ }
+
+ return G_TYPE_INVALID;
+ }
+
+ return tdata->default_function;
+}
+
/******************************************************************************
* For each stuff
*****************************************************************************/
--- a/gplate/gplate-library.h Thu Feb 14 03:48:40 2008 -0600
+++ b/gplate/gplate-library.h Sun Feb 17 03:22:52 2008 -0600
@@ -44,6 +44,9 @@
gboolean gplate_library_lookup_tag(GType tag, gchar **prefix, gchar **suffix, GError **error);
GType gplate_library_get_default_tag(void);
+gboolean gplate_library_set_default_function_for_tag(const gchar *name, GType tag, GError **error);
+GType gplate_library_get_default_function_for_tag(GType tag, GError **error);
+
void gplate_library_tags_foreach(GPlateLibraryTagsForeachFunc func, gpointer data);
G_END_DECLS