[couchdb-glib] Add CouchdbArrayField object



commit 1ae73365ca4cd4c7b0870644a45ce9244285745b
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Wed Feb 10 18:11:49 2010 +0100

    Add CouchdbArrayField object

 couchdb-glib/Makefile.am             |    3 +
 couchdb-glib/couchdb-array-field.c   |  228 ++++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-array-field.h   |   48 +++++++
 couchdb-glib/couchdb-glib.h          |    1 +
 couchdb-glib/couchdb-types.h         |    1 +
 doc/reference/couchdb-glib-docs.sgml |    1 +
 6 files changed, 282 insertions(+), 0 deletions(-)
---
diff --git a/couchdb-glib/Makefile.am b/couchdb-glib/Makefile.am
index 187dc41..553d980 100644
--- a/couchdb-glib/Makefile.am
+++ b/couchdb-glib/Makefile.am
@@ -25,6 +25,7 @@ couchdb-marshal.c: couchdb-marshal.list $(GLIB_GENMARSHAL)
 	$(GLIB_GENMARSHAL) $< --body --prefix=_couchdb_marshal > $@
 
 libcouchdb_glib_1_0_la_headers =	\
+	couchdb-array-field.h		\
 	couchdb-credentials.h		\
 	couchdb-database-info.h		\
 	couchdb-document.h		\
@@ -37,6 +38,7 @@ libcouchdb_glib_1_0_la_headers =	\
 	utils.h
 
 libcouchdb_glib_1_0_la_sources =	\
+	couchdb-array-field.c		\
 	couchdb-credentials.c		\
 	couchdb-database-info.c		\
 	couchdb-document.c		\
@@ -64,6 +66,7 @@ libcouchdb_glib_1_0_la_LDFLAGS =	\
 
 hdir = $(includedir)/couchdb-glib-1.0
 h_DATA = 				\
+	couchdb-array-field.h		\
 	couchdb-credentials.h		\
 	couchdb-database-info.h		\
 	couchdb-document.h		\
diff --git a/couchdb-glib/couchdb-array-field.c b/couchdb-glib/couchdb-array-field.c
new file mode 100644
index 0000000..404187c
--- /dev/null
+++ b/couchdb-glib/couchdb-array-field.c
@@ -0,0 +1,228 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009-2010 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <json-glib/json-glib.h>
+#include "couchdb-array-field.h"
+#include "couchdb-struct-field.h"
+
+struct _CouchdbArrayField {
+	gint ref_count;
+	JsonArray *json_array;
+};
+
+GType
+couchdb_array_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 ("CouchdbArrayField"),
+							    (GBoxedCopyFunc) couchdb_array_field_ref,
+							    (GBoxedFreeFunc) couchdb_array_field_unref);
+
+	return object_type;
+}
+
+/**
+ * couchdb_array_field_new:
+ *
+ * Create a new array field object.
+ *
+ * Return value: A newly-created #CouchdbArrayField object.
+ */
+CouchdbArrayField *
+couchdb_array_field_new (void)
+{
+	CouchdbArrayField *array;
+
+	array = g_slice_new (CouchdbArrayField);
+	array->ref_count = 1;
+	array->json_array = json_array_new ();
+
+	return array;
+}
+
+/**
+ * couchdb_array_field_ref:
+ * @array: A #CouchdbArrayField object
+ *
+ * Increments reference count of a #CouchdbArrayField object.
+ *
+ * Return value: A pointer to the referenced object.
+ */
+CouchdbArrayField *
+couchdb_array_field_ref (CouchdbArrayField *array)
+{
+	g_return_val_if_fail (array != NULL, NULL);
+	g_return_val_if_fail (array->ref_count > 0, NULL);
+
+	g_atomic_int_exchange_and_add (&array->ref_count, 1);
+
+	return array;
+}
+
+/**
+ * couchdb_array_field_unref:
+ * @array: A #CouchdbArrayField object
+ *
+ * Decrements reference count of a #CouchdbArrayField object. When
+ * the reference count is equal to 0, the object will be destroyed and
+ * the memory it uses freed.
+ */
+void
+couchdb_array_field_unref (CouchdbArrayField *array)
+{
+	gint old_ref;
+
+	g_return_if_fail (array != NULL);
+	g_return_if_fail (array->ref_count > 0);
+
+	old_ref = g_atomic_int_get (&array->ref_count);
+	if (old_ref > 1)
+		g_atomic_int_compare_and_exchange (&array->ref_count, old_ref, old_ref - 1);
+	else {
+		json_array_unref (array->json_array);
+		g_slice_free (CouchdbArrayField, array);
+	}
+}
+
+/**
+ * couchdb_array_field_get_length:
+ * @array: A #CouchdbArrayField object
+ *
+ * Get the number of elements on the given #CouchdbArrayField object.
+ *
+ * Return value: Number of elements in the given array.
+ */
+guint
+couchdb_array_field_get_length (CouchdbArrayField *array)
+{
+	g_return_val_if_fail (array != NULL, 0);
+
+	return json_array_get_length (array->json_array);
+}
+
+/**
+ * couchdb_array_field_add_array_element:
+ * @array: A #CouchdbArrayField object
+ * @value: Value to be added
+ *
+ * Add a new element of type array to the given array.
+ */
+void
+couchdb_array_field_add_array_element (CouchdbArrayField *array, const CouchdbArrayField *value)
+{
+	g_return_if_fail (array != NULL);
+
+	json_array_add_array_element (array->json_array, json_array_ref (value->json_array));
+}
+
+/**
+ * couchdb_array_field_add_boolean_element:
+ * @array: A #CouchdbArrayField object
+ * @value: Value to be added
+ *
+ * Add a new element of type boolean to the given array.
+ */
+void
+couchdb_array_field_add_boolean_element (CouchdbArrayField *array, gboolean value)
+{
+	g_return_if_fail (array != NULL);
+
+	json_array_add_boolean_element (array->json_array, value);
+}
+
+/**
+ * couchdb_array_field_add_int_element:
+ * @array: A #CouchdbArrayField object
+ * @value: Value to be added
+ *
+ * Add a new element of type integer to the given array.
+ */
+void
+couchdb_array_field_add_int_element (CouchdbArrayField *array, gint value)
+{
+	g_return_if_fail (array != NULL);
+
+	json_array_add_int_element (array->json_array, value);
+}
+
+/**
+ * couchdb_array_field_add_double_element:
+ * @array: A #CouchdbArrayField object
+ * @value: Value to be added
+ *
+ * Add a new element of type double to the given array.
+ */
+void
+couchdb_array_field_add_double_element (CouchdbArrayField *array, gdouble value)
+{
+	g_return_if_fail (array != NULL);
+
+	json_array_add_double_element (array->json_array, value);
+}
+
+/**
+ * couchdb_array_field_add_string_element:
+ * @array: A #CouchdbArrayField object
+ * @value: Value to be added
+ *
+ * Add a new element of type string to the given array.
+ */
+void
+couchdb_array_field_add_string_element (CouchdbArrayField *array, const gchar *value)
+{
+	g_return_if_fail (array != NULL);
+
+	json_array_add_string_element (array->json_array, value);
+}
+
+/**
+ * couchdb_array_field_add_struct_element:
+ * @array: A #CouchdbArrayField object
+ * @value: Value to be added
+ *
+ * Add a new element of type struct to the given array.
+ */
+void
+couchdb_array_field_add_struct_element (CouchdbArrayField *array, const CouchdbStructField *value)
+{
+	g_return_if_fail (array != NULL);
+
+	json_array_add_object_element (
+		array->json_array,
+		json_object_ref (couchdb_struct_field_get_json_object ((CouchdbStructField *) value)));
+}
+
+/**
+ * couchdb_array_field_remove_element:
+ * @array: A #CouchdbArrayField object
+ * @index: Position of the element to remove
+ *
+ * Remove an element from the given #CouchdbArrayField object.
+ */
+void
+couchdb_array_field_remove_element (CouchdbArrayField *array, guint index)
+{
+	g_return_if_fail (array != NULL);
+
+	json_array_remove_element (array->json_array, index);
+}
diff --git a/couchdb-glib/couchdb-array-field.h b/couchdb-glib/couchdb-array-field.h
new file mode 100644
index 0000000..168cb69
--- /dev/null
+++ b/couchdb-glib/couchdb-array-field.h
@@ -0,0 +1,48 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009-2010 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __COUCHDB_ARRAY_FIELD_H__
+#define __COUCHDB_ARRAY_FIELD_H__
+
+#include <glib-object.h>
+#include "couchdb-types.h"
+
+G_BEGIN_DECLS
+
+#define COUCHDB_TYPE_ARRAY_FIELD (couchdb_array_field_get_type ())
+
+GType              couchdb_array_field_get_type (void);
+CouchdbArrayField *couchdb_array_field_new (void);
+CouchdbArrayField *couchdb_array_field_ref (CouchdbArrayField *array);
+void               couchdb_array_field_unref (CouchdbArrayField *array);
+
+guint              couchdb_array_field_get_length (CouchdbArrayField *array);
+void               couchdb_array_field_add_array_element (CouchdbArrayField *array, const CouchdbArrayField *value);
+void               couchdb_array_field_add_boolean_element (CouchdbArrayField *array, gboolean value);
+void               couchdb_array_field_add_double_element (CouchdbArrayField *array, gdouble value);
+void               couchdb_array_field_add_int_element (CouchdbArrayField *array, gint value);
+void               couchdb_array_field_add_string_element (CouchdbArrayField *array, const gchar *value);
+void               couchdb_array_field_add_struct_element (CouchdbArrayField *array, const CouchdbStructField *value);
+void               couchdb_array_field_remove_element (CouchdbArrayField *array, guint index);
+
+G_END_DECLS
+
+#endif
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index b3dead3..20674a8 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -25,6 +25,7 @@
 #define __COUCHDB_GLIB_H__
 
 #include <couchdb-types.h>
+#include <couchdb-array-field.h>
 #include <couchdb-credentials.h>
 #include <couchdb-database-info.h>
 #include <couchdb-document.h>
diff --git a/couchdb-glib/couchdb-types.h b/couchdb-glib/couchdb-types.h
index abb84b0..546601a 100644
--- a/couchdb-glib/couchdb-types.h
+++ b/couchdb-glib/couchdb-types.h
@@ -28,6 +28,7 @@
 
 G_BEGIN_DECLS
 
+typedef struct _CouchdbArrayField CouchdbArrayField;
 typedef struct _CouchdbDocument CouchdbDocument;
 typedef struct _CouchdbDatabaseInfo CouchdbDatabaseInfo;
 typedef struct _CouchdbDocumentInfo CouchdbDocumentInfo;
diff --git a/doc/reference/couchdb-glib-docs.sgml b/doc/reference/couchdb-glib-docs.sgml
index 08d3582..16b3eb9 100644
--- a/doc/reference/couchdb-glib-docs.sgml
+++ b/doc/reference/couchdb-glib-docs.sgml
@@ -13,6 +13,7 @@
     <xi:include href="xml/couchdb-document.xml"/>
     <xi:include href="xml/couchdb-document-info.xml"/>
     <xi:include href="xml/couchdb-database-info.xml"/>
+    <xi:include href="xml/couchdb-array-field.xml"/>
     <xi:include href="xml/couchdb-struct-field.xml"/>
   </chapter>
   <chapter>



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