grim/gplate

Parents e4b947c95fb9
Children 0dca6bccdacb
Moved tag adding/removing to the library, this should be cleaned up at some point, but i'm looking for functionality right now, nothing more ;)
--- a/gplate/gplate-errors.h Thu Feb 07 19:01:28 2008 -0600
+++ b/gplate/gplate-errors.h Fri Feb 08 00:11:26 2008 -0600
@@ -39,6 +39,8 @@
GPLATE_ERROR_LIBRARY_FUNCTION_BOUND,
GPLATE_ERROR_LIBRARY_FUNCTION_NOT_BOUND,
GPLATE_ERROR_LIBRARY_FUNCTION_NOT_BOUND_TO_TAG,
+ GPLATE_ERROR_LIBRARY_TAG_EXISTS = 450,
+ GPLATE_ERROR_LIBRARY_TAG_NOT_FOUND,
GPLATE_ERROR_UNKNOWN = 1 << 31,
} GPlateErrors;
--- a/gplate/gplate-library.c Thu Feb 07 19:01:28 2008 -0600
+++ b/gplate/gplate-library.c Fri Feb 08 00:11:26 2008 -0600
@@ -35,7 +35,8 @@
/******************************************************************************
* Globals
*****************************************************************************/
-static GHashTable *library = NULL;
+static GHashTable *functions = NULL;
+static GHashTable *tags = NULL;
/******************************************************************************
* GPlateLibraryData API
@@ -60,11 +61,15 @@
*****************************************************************************/
static inline void
gplate_library_lazy_init(void) {
- if(G_UNLIKELY(library == NULL)) {
- library =
+ if(G_UNLIKELY(functions == NULL)) {
+ functions =
g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)gplate_library_data_free);
}
+
+ if(G_UNLIKELY(tags == NULL)) {
+ tags = g_hash_table_new(g_direct_hash, g_direct_equal);
+ }
}
static inline gboolean
@@ -121,7 +126,7 @@
if(!gplate_library_check_type(function, GPLATE_TYPE_FUNCTION, error))
return FALSE;
- data = g_hash_table_lookup(library, name);
+ data = g_hash_table_lookup(functions, name);
if(data) {
/* if the function is already registered with the same name and
* function, return TRUE and jump out.
@@ -142,7 +147,7 @@
data = gplate_library_data_new(function, G_TYPE_INVALID);
- g_hash_table_insert(library, g_strdup(name), data);
+ g_hash_table_insert(functions, g_strdup(name), data);
return TRUE;
}
@@ -162,7 +167,7 @@
g_return_val_if_fail(name, FALSE);
- ret = g_hash_table_remove(library, name);
+ ret = g_hash_table_remove(functions, name);
if(!ret && error) {
*error = g_error_new(GPLATE_DOMAIN,
@@ -175,6 +180,72 @@
}
/**
+ * gplate_library_add_tag:
+ * @tag: The #GPlateTag's GType to add.
+ * @error: The return address for any errors.
+ *
+ * Adds a tag to the library.
+ *
+ * Tags must be added to the library via this function for a template to
+ * handle them.
+ *
+ * Return Value: TRUE on success, FALSE on failure with @error set.
+ */
+gboolean
+gplate_library_add_tag(GType tag, GError **error) {
+ gpointer data;
+
+ gplate_library_lazy_init();
+
+ if(!gplate_library_check_type(tag, GPLATE_TYPE_TAG, error))
+ return FALSE;
+
+ data = g_hash_table_lookup(tags, GSIZE_TO_POINTER(tag));
+ if(data) {
+ if(error) {
+ *error = g_error_new(GPLATE_DOMAIN,
+ GPLATE_ERROR_LIBRARY_TAG_EXISTS,
+ "Tag '%s' is already in the library",
+ g_type_name(tag));
+ }
+
+ return FALSE;
+ }
+
+ data = GSIZE_TO_POINTER(tag);
+
+ g_hash_table_insert(tags, data, data);
+
+ return TRUE;
+}
+
+gboolean
+gplate_library_remove_tag(GType tag, GError **error) {
+ gpointer data;
+
+ gplate_library_lazy_init();
+
+ if(!gplate_library_check_type(tag, GPLATE_TYPE_TAG, error))
+ return FALSE;
+
+ data = g_hash_table_lookup(tags, GSIZE_TO_POINTER(tag));
+ if(!data) {
+ if(error) {
+ *error = g_error_new(GPLATE_DOMAIN,
+ GPLATE_ERROR_LIBRARY_TAG_NOT_FOUND,
+ "Tag '%s' is not in the library.",
+ g_type_name(tag));
+ }
+
+ return FALSE;
+ }
+
+ g_hash_table_remove(tags, GSIZE_TO_POINTER(tag));
+
+ return TRUE;
+}
+
+/**
* gplate_library_bind_function:
* @name: The name of the function.
* @tag: The #GType of the tag.
@@ -195,7 +266,7 @@
if(!gplate_library_check_type(tag, GPLATE_TYPE_TAG, error))
return FALSE;
- data = g_hash_table_lookup(library, name);
+ data = g_hash_table_lookup(functions, name);
if(!data) {
if(error) {
*error = g_error_new(GPLATE_DOMAIN,
@@ -233,7 +304,7 @@
gplate_library_lazy_init();
- data = g_hash_table_lookup(library, name);
+ data = g_hash_table_lookup(functions, name);
if(!data) {
if(error) {
*error = g_error_new(GPLATE_DOMAIN,
@@ -318,7 +389,7 @@
g_return_val_if_fail(name, FALSE);
- data = g_hash_table_lookup(library, name);
+ data = g_hash_table_lookup(functions, name);
if(!data) {
if(error) {
*error = g_error_new(GPLATE_DOMAIN,
--- a/gplate/gplate-library.h Thu Feb 07 19:01:28 2008 -0600
+++ b/gplate/gplate-library.h Fri Feb 08 00:11:26 2008 -0600
@@ -29,13 +29,15 @@
gboolean gplate_library_add_function(const gchar *name, GType function, GError **error);
gboolean gplate_library_remove_function(const gchar *name, GError **error);
+gboolean gplate_library_add_tag(GType tag, GError **error);
+gboolean gplate_library_remove_tag(GType tag, GError **error);
+
gboolean gplate_library_bind_function(const gchar *name, GType tag, GError **error);
gboolean gplate_library_unbind_function(const gchar *name, GError **error);
gboolean gplate_library_add_bound_function(const gchar *name, GType function, GType tag, GError **error);
gboolean gplate_library_lookup_function(const gchar *name, GType *function, GType *tag, GError **error);
-
GType gplate_library_lookup_function_for_tag(const gchar *name, GType tag, GError **error);
G_END_DECLS