[glibmm] Add Glib::ValueArray with usage test.
- From: José Alburquerque <jaalburqu src gnome org>
- To: svn-commits-list gnome org
- Subject: [glibmm] Add Glib::ValueArray with usage test.
- Date: Mon, 22 Jun 2009 18:03:02 -0400 (EDT)
commit 2065c9cd2cf978f0fbafb6e184e5d6223fcf3704
Author: José Alburquerque <jaalburqu svn gnome org>
Date: Mon Jun 22 17:54:00 2009 -0400
Add Glib::ValueArray with usage test.
configure.ac | 1 +
glib/glibmm.h | 1 +
glib/src/Makefile_list_of_hg.am_fragment | 3 +-
glib/src/valuearray.ccg | 99 ++++++++++++++++++++++++++++++
glib/src/valuearray.hg | 84 +++++++++++++++++++++++++
tests/Makefile.am | 3 +-
tests/glibmm_valuearray/Makefile.am | 4 +
tests/glibmm_valuearray/main.cc | 77 +++++++++++++++++++++++
8 files changed, 270 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index c1413f4..248fb1e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -317,6 +317,7 @@ AC_CONFIG_FILES([
tests/glibmm_nodetree/Makefile
tests/glibmm_date/Makefile
tests/glibmm_ustring_compose/Makefile
+ tests/glibmm_valuearray/Makefile
tests/giomm_simple/Makefile
tests/giomm_ioerror/Makefile
diff --git a/glib/glibmm.h b/glib/glibmm.h
index e6f9b36..fa5f57f 100644
--- a/glib/glibmm.h
+++ b/glib/glibmm.h
@@ -71,6 +71,7 @@
#include <glibmm/uriutils.h>
#include <glibmm/ustring.h>
#include <glibmm/value.h>
+#include <glibmm/valuearray.h>
#include <glibmm/wrap.h>
#endif /* _GLIBMM_H */
diff --git a/glib/src/Makefile_list_of_hg.am_fragment b/glib/src/Makefile_list_of_hg.am_fragment
index 37863c8..8d48fbc 100644
--- a/glib/src/Makefile_list_of_hg.am_fragment
+++ b/glib/src/Makefile_list_of_hg.am_fragment
@@ -7,7 +7,8 @@ files_posix_hg =
files_win32_hg =
files_general_hg = checksum.hg convert.hg date.hg fileutils.hg iochannel.hg keyfile.hg markup.hg \
module.hg optioncontext.hg optionentry.hg optiongroup.hg regex.hg \
- shell.hg spawn.hg thread.hg nodetree.hg unicode.hg uriutils.hg
+ shell.hg spawn.hg thread.hg nodetree.hg unicode.hg uriutils.hg \
+ valuearray.hg
files_general_deprecated_hg =
include $(top_srcdir)/build_shared/Makefile_build_gensrc.am_fragment
diff --git a/glib/src/valuearray.ccg b/glib/src/valuearray.ccg
new file mode 100644
index 0000000..b8d0a49
--- /dev/null
+++ b/glib/src/valuearray.ccg
@@ -0,0 +1,99 @@
+/* $Id$ */
+
+/* Copyright (C) 2002-2009 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <glibmm/exceptionhandler.h>
+static int ValueArray_Compare_glibmm_callback(gconstpointer a,
+ gconstpointer b, gpointer user_data)
+{
+ Glib::ValueArray::SlotCompare* the_slot =
+ static_cast<Glib::ValueArray::SlotCompare*>(user_data);
+
+ #ifdef GLIBMM_EXCEPTIONS_ENABLED
+ try
+ {
+ #endif //GLIBMM_EXCEPTIONS_ENABLED
+ return (*the_slot)(*reinterpret_cast<const Glib::ValueBase*>(a),
+ *reinterpret_cast<const Glib::ValueBase*>(b));
+ #ifdef GLIBMM_EXCEPTIONS_ENABLED
+ }
+ catch(...)
+ {
+ Glib::exception_handlers_invoke();
+ }
+ #endif //GLIBMM_EXCEPTIONS_ENABLED
+
+ return 0;
+}
+
+namespace Glib
+{
+
+ValueArray::ValueArray() : gobject_(g_value_array_new(0))
+{}
+
+ValueArray::ValueArray(int n_preallocated) :
+ gobject_(g_value_array_new(n_preallocated))
+{}
+
+bool ValueArray::get_nth(guint index, Glib::ValueBase& value)
+{
+ GValue* const g_value = g_value_array_get_nth(gobj(), index);
+
+ if(g_value)
+ {
+ value.init(g_value);
+ return true;
+ }
+ else
+ return false;
+}
+
+Glib::ValueArray& ValueArray::append(const Glib::ValueBase& value)
+{
+ g_value_array_append(gobj(), value.gobj());
+ return *this;
+}
+
+Glib::ValueArray& ValueArray::prepend(const Glib::ValueBase& value)
+{
+ g_value_array_prepend(gobj(), value.gobj());
+ return *this;
+}
+
+Glib::ValueArray& ValueArray::insert(guint index, const Glib::ValueBase& value)
+{
+ g_value_array_insert(gobj(), index, value.gobj());
+ return *this;
+}
+
+Glib::ValueArray& ValueArray::remove(guint index)
+{
+ g_value_array_remove(gobj(), index);
+ return *this;
+}
+
+Glib::ValueArray& ValueArray::sort(const SlotCompare& compare_func)
+{
+ SlotCompare slot_copy(compare_func);
+ g_value_array_sort_with_data(gobj(), &ValueArray_Compare_glibmm_callback,
+ &slot_copy);
+ return *this;
+}
+
+} // Glib namespace
diff --git a/glib/src/valuearray.hg b/glib/src/valuearray.hg
new file mode 100644
index 0000000..d540b61
--- /dev/null
+++ b/glib/src/valuearray.hg
@@ -0,0 +1,84 @@
+/* $Id$ */
+
+/* Copyright (C) 2002-2009 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+_DEFS(glibmm,glib)
+
+#include <glib-object.h>
+#include <glibmm/value.h>
+#include <sigc++/functors/slot.h>
+
+namespace Glib
+{
+
+/** A container structure to maintain an array of generic values.
+ * The prime purpose of a ValueArray is for it to be used as an object property
+ * that holds an array of values. A ValueArray wraps an array of ValueBase
+ * elements.
+ *
+ * @newin2p22
+ */
+class ValueArray
+{
+ _CLASS_BOXEDTYPE(ValueArray, GValueArray, NONE, g_value_array_copy, g_value_array_free)
+ _IGNORE(g_value_array_copy, g_value_array_free)
+ _CUSTOM_DEFAULT_CTOR
+
+public:
+ /** For example,
+ * int on_compare(const Glib::ValueBase& v1, const Glib::ValueBase& v2);.
+ * The compare function should return -1 if v1 < v2, 0 if v1 == v2, and 1 if
+ * v1 > v2.
+ */
+ typedef sigc::slot<int, const Glib::ValueBase&, const Glib::ValueBase&> SlotCompare;
+
+ /** Default constructor. Constructs a new array with no pre-allocation.
+ */
+ ValueArray();
+
+ /** Constructs a new array with pre-allocation.
+ */
+ ValueArray(int n_preallocated);
+
+ /** Return the value at @a index contained in the value array.
+ * @param index Index of the value of interest.
+ * @param value An uninitialized ValueBase in which to store the result. If
+ * the get is successful, @a value will be valid, otherwise it will remain
+ * uninitialized.
+ * @return whether the get was successful or not.
+ */
+ bool get_nth(guint index, Glib::ValueBase& value);
+ _IGNORE(g_value_array_get_nth)
+
+ _WRAP_METHOD_DOCS_ONLY(g_value_array_append)
+ Glib::ValueArray& append(const Glib::ValueBase& value);
+
+ _WRAP_METHOD_DOCS_ONLY(g_value_array_prepend)
+ Glib::ValueArray& prepend(const Glib::ValueBase& value);
+
+ _WRAP_METHOD_DOCS_ONLY(g_value_array_insert)
+ Glib::ValueArray& insert(guint index, const Glib::ValueBase& value);
+
+ _WRAP_METHOD_DOCS_ONLY(g_value_array_remove)
+ Glib::ValueArray& remove(guint index);
+
+ _WRAP_METHOD_DOCS_ONLY(g_value_array_sort)
+ Glib::ValueArray& sort(const SlotCompare& compare_func);
+};
+
+} //namespace Glib
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 1d2feba..a29eb79 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,5 @@
-test_dirs = glibmm_value glibmm_nodetree glibmm_date glibmm_ustring_compose giomm_simple giomm_ioerror
+test_dirs = glibmm_value glibmm_nodetree glibmm_date glibmm_valuearray \
+ glibmm_ustring_compose giomm_simple giomm_ioerror
SUBDIRS = $(test_dirs)
EXTRA_DIST = Makefile.am_fragment
diff --git a/tests/glibmm_valuearray/Makefile.am b/tests/glibmm_valuearray/Makefile.am
new file mode 100644
index 0000000..6cd6fff
--- /dev/null
+++ b/tests/glibmm_valuearray/Makefile.am
@@ -0,0 +1,4 @@
+include $(top_srcdir)/tests/Makefile.am_fragment
+
+noinst_PROGRAMS = test
+test_SOURCES = main.cc
diff --git a/tests/glibmm_valuearray/main.cc b/tests/glibmm_valuearray/main.cc
new file mode 100644
index 0000000..78bf33d
--- /dev/null
+++ b/tests/glibmm_valuearray/main.cc
@@ -0,0 +1,77 @@
+#include <glibmm.h>
+#include <iostream>
+
+#define VALUES 10
+
+int on_compare(const Glib::ValueBase& v1, const Glib::ValueBase& v2)
+{
+ const Glib::Value<int> intVal1 = static_cast< const Glib::Value<int>& >(v1);
+ const Glib::Value<int> intVal2 = static_cast< const Glib::Value<int>& >(v2);
+
+ int int1 = intVal1.get();
+ int int2 = intVal2.get();
+
+ if(int1 < int2)
+ return -1;
+ else if(int1 == int2)
+ return 0;
+ else
+ return 1;
+}
+
+int main(int, char**)
+{
+ Glib::init();
+
+ Glib::Value<int> value[VALUES];
+ Glib::ValueArray array;
+
+ for(int i = 0; i < VALUES; i++)
+ {
+ value[i].init(Glib::Value<int>::value_type());
+ value[i].set(i + 1); // (i + 1) ==> Set to natural counting numbers.
+ array.prepend(value[i]);
+ }
+
+ std::cout << "Array members before sorting:" << std::endl;
+
+ for(int i = 0; i < VALUES; i++)
+ {
+ Glib::ValueBase value;
+
+ if(!array.get_nth(i, value))
+ {
+ std::cerr << "Error getting element " << i << " of value array." <<
+ std::endl;
+ break;
+ }
+
+ Glib::Value<int> int_val = static_cast< Glib::Value<int>& >(value);
+ std::cout << int_val.get() << " ";
+ }
+ std::cout << std::endl; // End of line for list of array elements.
+
+ // Sort array and remove last element:
+ array.sort(sigc::ptr_fun(&on_compare)).remove(VALUES - 1);
+
+ std::cout << "Array members after sorting without last element:" <<
+ std::endl;
+
+ for(int i = 0; i < VALUES - 1; i++)
+ {
+ Glib::ValueBase value;
+
+ if(!array.get_nth(i, value))
+ {
+ std::cerr << "Error getting element " << i << " of value array." <<
+ std::endl;
+ break;
+ }
+
+ Glib::Value<int> int_val = static_cast< Glib::Value<int>& >(value);
+ std::cout << int_val.get() << " ";
+ }
+ std::cout << std::endl; // End of line for list of array elements.
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]