[glibmm] Variant<>: Make this usable as an ouput parameter.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glibmm] Variant<>: Make this usable as an ouput parameter.
- Date: Thu, 22 Jul 2010 20:40:20 +0000 (UTC)
commit 96c1f18cd3b695c925553d0ccda47da7c1d56d8c
Author: Murray Cumming <murrayc murrayc com>
Date: Thu Jul 22 22:40:12 2010 +0200
Variant<>: Make this usable as an ouput parameter.
* glib/src/variant.[hg|ccg]: VariantBase: Added init(), like Value::init().
This just changes the gobject_, with appropriate unref and ref.
* gio/src/settings.[hg|ccg]: get_value(): Use an output parameter for the
VariantBase, instead of a return value, as we do for Value, allowing the
caller to use a derived (templated) type.
* examples/settings/settings.cc: Show how Variant<> can be used.
ChangeLog | 11 +++++++++++
examples/settings/settings.cc | 9 ++++++++-
gio/src/settings.ccg | 9 +++++++++
gio/src/settings.hg | 6 +++++-
glib/src/variant.ccg | 10 ++++++++++
glib/src/variant.hg | 4 ++--
6 files changed, 45 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index df636cb..a1a78f6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
2010-07-22 Murray Cumming <murrayc murrayc com>
+ Variant<>: Make this usable as an ouput parameter.
+
+ * glib/src/variant.[hg|ccg]: VariantBase: Added init(), like Value::init().
+ This just changes the gobject_, with appropriate unref and ref.
+ * gio/src/settings.[hg|ccg]: get_value(): Use an output parameter for the
+ VariantBase, instead of a return value, as we do for Value, allowing the
+ caller to use a derived (templated) type.
+ * examples/settings/settings.cc: Show how Variant<> can be used.
+
+2010-07-22 Murray Cumming <murrayc murrayc com>
+
Settings example: Minor change.
* examples/settings/settings.cc: Remove the unnecessary use of
diff --git a/examples/settings/settings.cc b/examples/settings/settings.cc
index 5f8cbfa..b7ce0ca 100644
--- a/examples/settings/settings.cc
+++ b/examples/settings/settings.cc
@@ -32,7 +32,14 @@ static void on_key_changed(const Glib::ustring& key, const Glib::RefPtr<Gio::Set
{
Glib::ustring str = settings->get_string(key);
std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
- key, str);
+ key, str);
+
+ //Or:
+ Glib::Variant<Glib::ustring> variant;
+ settings->get_value(key, variant);
+ str = variant.get();
+ std::cout << Glib::ustring::compose("New value, via variant, of '%1': '%2'\n",
+ key, str);
}
else if (key == INT_KEY)
{
diff --git a/gio/src/settings.ccg b/gio/src/settings.ccg
index 342b70f..feaba2a 100644
--- a/gio/src/settings.ccg
+++ b/gio/src/settings.ccg
@@ -3,6 +3,15 @@
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;
+
+ value.init(g_value, false /* don't take a reference */);
+}
+
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 9edbd4c..6f1a683 100644
--- a/glib/src/variant.ccg
+++ b/glib/src/variant.ccg
@@ -25,6 +25,16 @@ namespace Glib
/****************** Specializations ***********************************/
+void VariantBase::init(const GVariant* cobject, bool take_a_reference)
+{
+ if(gobject_)
+ g_variant_unref(gobject_);
+
+ gobject_ = const_cast<GVariant*>(cobject);
+ if(take_a_reference)
+ g_variant_ref(gobject_);
+}
+
// static
const GVariantType* Variant<VariantBase>::variant_type()
{
diff --git a/glib/src/variant.hg b/glib/src/variant.hg
index b84c50b..83306ab 100644
--- a/glib/src/variant.hg
+++ b/glib/src/variant.hg
@@ -44,13 +44,13 @@ class VariantBase
g_variant_get, g_variant_get_va)
public:
+ void init(const GVariant* cobject, bool take_a_reference = false);
+
_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)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]