[glibmm] Variant: Added some methods.



commit 7d45142dfc6c25b34532c8dffef58b79546d70bc
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Jul 22 22:10:07 2010 +0200

    Variant: Added some methods.
    
    * glib/src/variant.[hg|ccg]: ValueBase: Wrap some simple functions
    with _WRAP_METHOD().
    * glib/src/variant_basictypes.h.m4: Syntax changes, and make the castitem
    constructor explicit.
    * tools/m4/convert_gio.m4: Added necessary conversion.
    
    We need to decide how to use this in get*() methods and add some tests.

 ChangeLog                        |   12 +++++++
 examples/settings/settings.cc    |   68 ++++++++++++++++++++++---------------
 gio/src/settings.ccg             |   10 +++++
 gio/src/settings.hg              |    6 +++-
 glib/src/variant.ccg             |    1 +
 glib/src/variant.hg              |   35 +++++++++++++++++--
 glib/src/variant_basictypes.h.m4 |   10 ++++--
 tools/m4/convert_gio.m4          |    1 +
 8 files changed, 107 insertions(+), 36 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3563c84..2ad82ee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2010-07-22  Murray Cumming  <murrayc murrayc com>
 
+	Variant: Added some methods.
+
+	* glib/src/variant.[hg|ccg]: ValueBase: Wrap some simple functions 
+	with _WRAP_METHOD().
+	* glib/src/variant_basictypes.h.m4: Syntax changes, and make the castitem 
+	constructor explicit.
+	* tools/m4/convert_gio.m4: Added necessary conversion.
+	
+	We need to decide how to use this in get*() methods and add some tests.
+
+2010-07-22  Murray Cumming  <murrayc murrayc com>
+
 	giomm: DBusMessage: Added several  methods.
 
 	* gio/src/dbusmessage.hg: Wrapped the simple functions.
diff --git a/examples/settings/settings.cc b/examples/settings/settings.cc
index a42e106..b7d6228 100644
--- a/examples/settings/settings.cc
+++ b/examples/settings/settings.cc
@@ -27,42 +27,54 @@ const char *const INT_KEY = "test-int";
 
 static void on_key_changed(const Glib::ustring& key, const Glib::RefPtr<Gio::Settings>& settings)
 {
-    std::cout << Glib::ustring::compose("'%1' changed\n", key);
-    if (key.raw() == STRING_KEY)
-        std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
-                                            key, settings->get_string(key));
-    else if (key.raw() == INT_KEY)
-        std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
-                                            key, settings->get_int(key));
-    else
-        std::cerr << "Unknown key\n";
+  std::cout << Glib::ustring::compose("'%1' changed\n", key);
+  if (key.raw() == STRING_KEY)
+  {
+    Glib::ustring str = settings->get_string(key);
+    std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
+                      key, str);
+                      
+    //Or:
+    Glib::Variant<Glib::ustring> variant = settings->get_value(key);
+    str = variant.get();
+    std::cout << Glib::ustring::compose("New value, via Variant, of '%1': '%2'\n",
+                      key, str);
+    
+  }
+  else if (key.raw() == INT_KEY)
+  {
+    std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
+                      key, settings->get_int(key));
+  }
+  else
+    std::cerr << "Unknown key\n";
 }
 
 int main(int, char**)
 {
-    std::locale::global(std::locale(""));
-    Gio::init();
+  std::locale::global(std::locale(""));
+  Gio::init();
 
-    // this is only a demo so we don't want to rely on an installed schema.
-    // Instead we set some environment variables that allow us to test things
-    // from the source directory.  We need to strip off the .libs/ directory
-    // first (thus the '..').  Generally you would install your schemas to the system schema
-    // directory
-    Glib::setenv("GSETTINGS_SCHEMA_DIR", ".", true);
-    Glib::setenv("GSETTINGS_BACKEND", "memory", true);
+  // this is only a demo so we don't want to rely on an installed schema.
+  // Instead we set some environment variables that allow us to test things
+  // from the source directory.  We need to strip off the .libs/ directory
+  // first (thus the '..').  Generally you would install your schemas to the system schema
+  // directory
+  Glib::setenv("GSETTINGS_SCHEMA_DIR", ".", true);
+  Glib::setenv("GSETTINGS_BACKEND", "memory", true);
 
-    const Glib::RefPtr<Gio::Settings> settings =
-        Gio::Settings::create("org.gtkmm.demo");
+  const Glib::RefPtr<Gio::Settings> settings =
+    Gio::Settings::create("org.gtkmm.demo");
 
-    settings->signal_changed().connect(sigc::bind(sigc::ptr_fun(&on_key_changed), settings));
+  settings->signal_changed().connect(sigc::bind(sigc::ptr_fun(&on_key_changed), settings));
 
-    std::cout << Glib::ustring::compose("Initial value of '%1': '%2'\n",
-                                        STRING_KEY, settings->get_string(STRING_KEY));
-    settings->set_string(STRING_KEY, "Hoopoe");
+  std::cout << Glib::ustring::compose("Initial value of '%1': '%2'\n",
+                    STRING_KEY, settings->get_string(STRING_KEY));
+  settings->set_string(STRING_KEY, "Hoopoe");
 
-    std::cout << Glib::ustring::compose("Initial value of '%1': '%2'\n",
-                                        INT_KEY, settings->get_int(INT_KEY));
-    settings->set_int(INT_KEY, 18);
+  std::cout << Glib::ustring::compose("Initial value of '%1': '%2'\n",
+                    INT_KEY, settings->get_int(INT_KEY));
+  settings->set_int(INT_KEY, 18);
 
-    return 0;
+  return 0;
 }
diff --git a/gio/src/settings.ccg b/gio/src/settings.ccg
index 342b70f..de32cd9 100644
--- a/gio/src/settings.ccg
+++ b/gio/src/settings.ccg
@@ -3,6 +3,16 @@
 namespace Gio
 {
 
+void Settings::get_value(const Glib::ustring& key, Glib::VariantBase& value)
+{
+  GVariant* const g_value = g_settings_get_value(gobj(), key.c_str());
+  if(!g_value)
+    return;
+  
+  g_value_copy(
+  value.init(g_value);
+}
+
 void Settings::bind(const Glib::ustring& key,
                     const Glib::PropertyProxy_Base& property_proxy,
                     SettingsBindFlags flags)
diff --git a/gio/src/settings.hg b/gio/src/settings.hg
index 9b3e457..d06b55a 100644
--- a/gio/src/settings.hg
+++ b/gio/src/settings.hg
@@ -53,7 +53,11 @@ public:
   // FIXME: implement the GVariant stuff
 
   _WRAP_METHOD(bool set_value(const Glib::ustring& key, const Glib::VariantBase& value),  g_settings_set_value)
-  _WRAP_METHOD(Glib::VariantBase get_value(const Glib::ustring& key), g_settings_get_value)
+  
+  /** TODO: Documentation.
+   */
+  void get_value(const Glib::ustring& key, Glib::VariantBase& value);
+  _IGNORE(g_settings_get_value);
 
   _WRAP_METHOD(int get_int(const Glib::ustring& key) const, g_settings_get_int)
   _WRAP_METHOD(void set_int(const Glib::ustring& key, int value), g_settings_set_int)
diff --git a/glib/src/variant.ccg b/glib/src/variant.ccg
index 1d16e4c..9edbd4c 100644
--- a/glib/src/variant.ccg
+++ b/glib/src/variant.ccg
@@ -16,6 +16,7 @@
  */
 
 #include <glibmm/variant.h>
+#include <glibmm/utility.h>
 #include <glib/gvariant.h>
 
 namespace Glib
diff --git a/glib/src/variant.hg b/glib/src/variant.hg
index eba1763..b84c50b 100644
--- a/glib/src/variant.hg
+++ b/glib/src/variant.hg
@@ -46,6 +46,19 @@ public:
 
   _WRAP_METHOD(bool is_container() const, g_variant_is_container)
   _WRAP_METHOD(GVariantClass classify() const, g_variant_classify)
+  
+  _WRAP_METHOD(Glib::ustring print(bool type_annotate = false) const, g_variant_print)
+  _IGNORE(g_variant_print_string);
+
+  _IGNORE(g_variant_hash)
+  
+  #m4 _CONVERSION(`const VariantBase&',`gconstpointer',`const_cast<GVariant*>(($3).gobj())')
+  _WRAP_METHOD(guint hash() const, g_variant_hash)
+  _WRAP_METHOD(bool equal(const VariantBase& other) const, g_variant_equal)
+
+  _WRAP_METHOD(VariantBase get_normal_form() const, g_variant_get_normal_form)
+  _WRAP_METHOD(bool is_normal_form() const, g_variant_is_normal_form)
+  _WRAP_METHOD(VariantBase byteswap() const, g_variant_byteswap)
 };
 
 /** Template class from which other Glib::Variant<> specializations derive.
@@ -73,10 +86,17 @@ _IGNORE(g_variant_get_type)
 template <>
 class Variant<VariantBase> : public VariantBase
 {
+public:
   typedef GVariant* CType;
 
-  Variant<VariantBase>() : VariantBase() { }
-  Variant<VariantBase>(GVariant* castitem) : VariantBase(castitem) { }
+  Variant<VariantBase>()
+  : VariantBase()
+  {}
+  
+  explicit Variant<VariantBase>(GVariant* castitem)
+  : VariantBase(castitem)
+  {}
+  
   static const GVariantType* variant_type() G_GNUC_CONST;
 
   //This must have a create() method because otherwise it would be a copy constructor.
@@ -94,10 +114,17 @@ class Variant<VariantBase> : public VariantBase
 template <>
 class Variant<Glib::ustring> : public VariantBase
 {
+public:
   typedef char* CType;
 
-  Variant<Glib::ustring>() : VariantBase() { }
-  Variant<Glib::ustring>(GVariant* castitem) : VariantBase(castitem) { }
+  Variant<Glib::ustring>()
+  : VariantBase()
+  {}
+  
+  explicit Variant<Glib::ustring>(GVariant* castitem)
+  : VariantBase(castitem)
+  {}
+  
   static const GVariantType* variant_type() G_GNUC_CONST;
   static Variant<Glib::ustring> create(const Glib::ustring& data);
 
diff --git a/glib/src/variant_basictypes.h.m4 b/glib/src/variant_basictypes.h.m4
index a7504f0..84847e9 100644
--- a/glib/src/variant_basictypes.h.m4
+++ b/glib/src/variant_basictypes.h.m4
@@ -39,12 +39,16 @@ public:
   typedef g$2 CType;
 
   /// Default constructor.
-  Variant<$1>() : VariantBase() { }
+  Variant<$1>()
+  : VariantBase()
+  {}
 
   /** GVariant constructor.
    * @param castitem The GVariant to wrap.
    */
-  Variant<$1>(GVariant* castitem) : VariantBase(castitem) { }
+  explicit Variant<$1>(GVariant* castitem)
+  : VariantBase(castitem)
+  {}
 
   /** Gets the GVariantType.
    * @return The GVariantType.
@@ -65,7 +69,7 @@ public:
 ])
 
 divert[]dnl
-// This is a generated file, do not edit.  Generated from __file__
+// This is a generated file. Do not edit it.  Generated from __file__
 
 #ifndef DOXYGEN_SHOULD_SKIP_THIS
 #ifndef _GLIBMM_VARIANT_H_INCLUDE_VARIANT_BASICTYPES_H
diff --git a/tools/m4/convert_gio.m4 b/tools/m4/convert_gio.m4
index 403b19d..30694f2 100644
--- a/tools/m4/convert_gio.m4
+++ b/tools/m4/convert_gio.m4
@@ -148,6 +148,7 @@ _CONVERSION(`const Glib::RefPtr<SettingsBackend>&',`GSettingsBackend*',__CONVERT
 
 
 _CONVERSION(`GVariant*',`Glib::VariantBase',`Glib::wrap($3, true)')
+_CONVERSION(`GVariant*',`VariantBase',`Glib::wrap($3, true)')
 _CONVERSION(`const Glib::VariantBase&',`GVariant*',`const_cast<GVariant*>(($3).gobj())')
 
 #Socket



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