[couchdb-glib] Added StructField object to deal with objects in JSON documents



commit 0f556737641e1037590ee623c11d967836d600bd
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Fri Jun 12 13:55:42 2009 +0200

    Added StructField object to deal with objects in JSON documents

 couchdb-glib/couchdb-document.c |    7 ++
 couchdb-glib/couchdb-glib.h     |    4 +
 couchdb-glib/couchdb-types.c    |  161 +++++++++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-types.h    |   29 ++++++-
 couchdb-glib/utils.h            |    8 ++
 5 files changed, 206 insertions(+), 3 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 587b532..d4117c6 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -241,6 +241,13 @@ couchdb_document_set_string_field (CouchDBDocument *document, const char *field,
 				       value);
 }
 
+CouchDBStructField *
+couchdb_document_get_struct_field (CouchDBDocument *document, const char *field)
+{
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+	g_return_val_if_fail (field != NULL, NULL);
+}
+
 char *
 couchdb_document_to_string (CouchDBDocument *document)
 {
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index 2d1064a..10b57ae 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -89,6 +89,10 @@ gdouble          couchdb_document_get_double_field (CouchDBDocument *document, c
 void             couchdb_document_set_double_field (CouchDBDocument *document, const char *field, gdouble value);
 const char      *couchdb_document_get_string_field (CouchDBDocument *document, const char *field);
 void             couchdb_document_set_string_field (CouchDBDocument *document, const char *field, const char *value);
+CouchDBStructField *couchdb_document_get_struct_field (CouchDBDocument *document, const char *field);
+void             couchdb_document_set_struct_field (CouchDBDocument *document,
+						    const char *field,
+						    CouchDBStructField *value);
 
 char            *couchdb_document_to_string (CouchDBDocument *document);
 
diff --git a/couchdb-glib/couchdb-types.c b/couchdb-glib/couchdb-types.c
index 2a0ff4d..ede6bb5 100644
--- a/couchdb-glib/couchdb-types.c
+++ b/couchdb-glib/couchdb-types.c
@@ -211,3 +211,164 @@ couchdb_document_info_get_revision (CouchDBDocumentInfo *doc_info)
 
 	return (const char *) doc_info->revision;
 }
+
+/*
+ * CouchDBStructField
+ */
+GType
+couchdb_struct_field_get_type (void)
+{
+	static GType object_type = 0;
+
+	if (G_UNLIKELY (!object_type))
+		object_type = g_boxed_type_register_static (g_intern_static_string ("CouchDBStructField"),
+							    (GBoxedCopyFunc) couchdb_struct_field_ref,
+							    (GBoxedFreeFunc) couchdb_struct_field_unref);
+
+	return object_type;
+}
+
+CouchDBStructField *
+couchdb_struct_field_new (void)
+{
+	CouchDBStructField *sf;
+
+	sf = g_slice_new (CouchDBStructField);
+	sf->ref_count = 1;
+	sf->json_object = json_object_new ();
+
+	return sf;
+}
+
+CouchDBStructField *
+couchdb_struct_field_new_from_json_object (JsonObject *json_object)
+{
+	CouchDBStructField *sf;
+
+	sf = g_slice_new (CouchDBStructField);
+	sf->ref_count = 1;
+	sf->json_object = json_object_ref (json_object);
+
+	return sf;
+}
+
+CouchDBStructField *
+couchdb_struct_field_ref (CouchDBStructField *sf)
+{
+	g_return_val_if_fail (sf != NULL, NULL);
+	g_return_val_if_fail (sf->ref_count > 0, NULL);
+
+	g_atomic_int_exchange_and_add (&sf->ref_count, 1);
+
+	return sf;
+}
+
+void
+couchdb_struct_field_unref (CouchDBStructField *sf)
+{
+	gint old_ref;
+
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (sf->ref_count > 0);
+
+	old_ref = g_atomic_int_get (&sf->ref_count);
+	if (old_ref > 1)
+		g_atomic_int_compare_and_exchange (&sf->ref_count, old_ref, old_ref - 1);
+	else {
+		json_object_unref (sf->json_object);
+		g_slice_free (CouchDBStructField, sf);
+	}
+}
+
+gboolean
+couchdb_struct_field_get_boolean_field (CouchDBStructField *sf, const char *field)
+{
+	g_return_val_if_fail (sf != NULL, 0);
+	g_return_val_if_fail (field != NULL, 0);
+
+	return json_object_get_boolean_member (sf->json_object, field);
+}
+
+void
+couchdb_struct_field_set_boolean_field (CouchDBStructField *sf, const char *field, gboolean value)
+{
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (field != NULL);
+
+	json_object_set_boolean_member (sf->json_object, field, value);
+}
+
+gdouble
+couchdb_struct_field_get_double_field (CouchDBStructField *sf, const char *field)
+{
+	g_return_val_if_fail (sf != NULL, 0);
+	g_return_val_if_fail (field != NULL, 0);
+
+	return json_object_get_double_member (sf->json_object, field);
+}
+
+void
+couchdb_struct_field_set_double_field (CouchDBStructField *sf, const char *field, gdouble value)
+{
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (field != NULL);
+
+	json_object_set_double_member (sf->json_object, field, value);
+}
+
+gint
+couchdb_struct_field_get_int_field (CouchDBStructField *sf, const char *field)
+{
+	g_return_val_if_fail (sf != NULL, 0);
+	g_return_val_if_fail (field != NULL, 0);
+
+	return json_object_get_int_member (sf->json_object, field);
+}
+
+void
+couchdb_struct_field_set_int_field (CouchDBStructField *sf, const char *field, gint value)
+{
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (field != NULL);
+
+	json_object_set_int_member (sf->json_object, field, value);
+}
+
+const char *
+couchdb_struct_field_get_string_field (CouchDBStructField *sf, const char *field)
+{
+	g_return_val_if_fail (sf != NULL, NULL);
+	g_return_val_if_fail (field != NULL, NULL);
+
+	return json_object_get_string_member (sf->json_object, field);
+}
+
+void
+couchdb_struct_field_set_string_field (CouchDBStructField *sf, const char *field, const char *value)
+{
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (field != NULL);
+	g_return_if_fail (value != NULL);
+
+	json_object_set_string_member (sf->json_object, field, value);
+}
+
+CouchDBStructField *
+couchdb_struct_field_get_struct_field (CouchDBStructField *sf, const char *field)
+{
+	g_return_val_if_fail (sf != NULL, NULL);
+	g_return_val_if_fail (field != NULL, NULL);
+
+	return couchdb_struct_field_new_from_json_object (
+		json_object_get_object_member (sf->json_object, field));
+}
+
+void
+couchdb_struct_field_set_struct_field (CouchDBStructField *sf, const char *field, CouchDBStructField *value)
+{
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (field != NULL);
+	g_return_if_fail (value != NULL);
+
+	json_object_set_object_member (sf->json_object, field, json_object_ref (value->json_object));
+}
diff --git a/couchdb-glib/couchdb-types.h b/couchdb-glib/couchdb-types.h
index f9aadb6..9e27b3d 100644
--- a/couchdb-glib/couchdb-types.h
+++ b/couchdb-glib/couchdb-types.h
@@ -26,13 +26,13 @@
 
 #define COUCHDB_TYPE_DATABASE_INFO (couchdb_database_info_get_type ())
 #define COUCHDB_TYPE_DOCUMENT_INFO (couchdb_document_info_get_type ())
-
-typedef struct _CouchDBDatabaseInfo CouchDBDatabaseInfo;
-typedef struct _CouchDBDocumentInfo CouchDBDocumentInfo;
+#define COUCHDB_TYPE_STRUCT_FIELD  (couchdb_struct_field_get_type ())
 
 /*
  * CouchDBDatabaseInfo
  */
+typedef struct _CouchDBDatabaseInfo CouchDBDatabaseInfo;
+
 GType                couchdb_database_info_get_type (void);
 CouchDBDatabaseInfo *couchdb_database_info_ref (CouchDBDatabaseInfo *dbinfo);
 void                 couchdb_database_info_unref (CouchDBDatabaseInfo *dbinfo);
@@ -47,6 +47,8 @@ gint                 couchdb_database_info_get_disk_size (CouchDBDatabaseInfo *d
 /*
  * CouchDBDocumentInfo
  */
+typedef struct _CouchDBDocumentInfo CouchDBDocumentInfo;
+
 GType                couchdb_document_info_get_type (void);
 CouchDBDocumentInfo *couchdb_document_info_ref (CouchDBDocumentInfo *doc_info);
 void                 couchdb_document_info_unref (CouchDBDocumentInfo *doc_info);
@@ -54,4 +56,25 @@ void                 couchdb_document_info_unref (CouchDBDocumentInfo *doc_info)
 const char          *couchdb_document_info_get_docid (CouchDBDocumentInfo *doc_info);
 const char          *couchdb_document_info_get_revision (CouchDBDocumentInfo *doc_info);
 
+/*
+ * CouchDBStructField
+ */
+typedef struct _CouchDBStructField CouchDBStructField;
+
+GType               couchdb_struct_field_get_type (void);
+CouchDBStructField *couchdb_struct_field_new (void);
+CouchDBStructField *couchdb_struct_field_ref (CouchDBStructField *sf);
+void                couchdb_struct_field_unref (CouchDBStructField *sf);
+
+gboolean            couchdb_struct_field_get_boolean_field (CouchDBStructField *sf, const char *field);
+void                couchdb_struct_field_set_boolean_field (CouchDBStructField *sf, const char *field, gboolean value);
+gdouble             couchdb_struct_field_get_double_field (CouchDBStructField *sf, const char *field);
+void                couchdb_struct_field_set_double_field (CouchDBStructField *sf, const char *field, gdouble value);
+gint                couchdb_struct_field_get_int_field (CouchDBStructField *sf, const char *field);
+void                couchdb_struct_field_set_int_field (CouchDBStructField *sf, const char *field, gint value);
+const char         *couchdb_struct_field_get_string_field (CouchDBStructField *sf, const char *field);
+void                couchdb_struct_field_set_string_field (CouchDBStructField *sf, const char *field, const char *value);
+CouchDBStructField *couchdb_struct_field_get_struct_field (CouchDBStructField *sf, const char *field);
+void                couchdb_struct_field_set_struct_field (CouchDBStructField *sf, const char *field, CouchDBStructField *value);
+
 #endif
diff --git a/couchdb-glib/utils.h b/couchdb-glib/utils.h
index ba6b17f..1ea6db8 100644
--- a/couchdb-glib/utils.h
+++ b/couchdb-glib/utils.h
@@ -59,6 +59,14 @@ struct _CouchDBDocumentInfo {
 
 CouchDBDocumentInfo *couchdb_document_info_new (const char *docid, const char *revision);
 
+struct _CouchDBStructField {
+	gint ref_count;
+
+	JsonObject *json_object;
+};
+
+CouchDBStructField *couchdb_struct_field_new_from_json_object (JsonObject *json_object);
+
 /*
  * Utility functions
  */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]