[glibmm] Variant: Added Variant<VariantBase>::get().
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glibmm] Variant: Added Variant<VariantBase>::get().
- Date: Wed, 16 Mar 2011 10:25:32 +0000 (UTC)
commit 2f2de9f5802bb722c86951398ca3ff501c990282
Author: Yannick Guesnet <yannick guesnet univ-rouen fr>
Date: Wed Mar 16 10:56:28 2011 +0100
Variant: Added Variant<VariantBase>::get().
* glib/src/variant.[ccg|hg]: Added Variant<VariantBase>::get().
* tests/glibmm_variant/main.cc: Add some tests.
This is useful because Variant<VariantBase> can be manipulate as other
variants of type Variant<T>. This allow, for example, to define variants
of type Variant<std::map<ustring, VariantBase> >.
ChangeLog | 11 +++++++++++
glib/src/variant.ccg | 5 +++++
glib/src/variant.hg | 2 +-
tests/glibmm_variant/main.cc | 37 +++++++++++++++++++++++++++++++++++++
4 files changed, 54 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3467618..a835295 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2011-03-16 Yannick Guesnet <Yannick Guesnet univ-rouen fr>
+
+ Variant: Added Variant<VariantBase>::get().
+
+ * glib/src/variant.[ccg|hg]: Added Variant<VariantBase>::get().
+ * tests/glibmm_variant/main.cc: Add some tests.
+
+ This is useful because Variant<VariantBase> can be manipulate as other
+ variants of type Variant<T>. This allow, for example, to define variants
+ of type Variant<std::map<ustring, VariantBase> >.
+
2011-03-14 Yannick Guesnet <Yannick Guesnet univ-rouen fr>
Variant: Add a cast operator.
diff --git a/glib/src/variant.ccg b/glib/src/variant.ccg
index c8dc242..f1c30ac 100644
--- a/glib/src/variant.ccg
+++ b/glib/src/variant.ccg
@@ -152,6 +152,11 @@ Variant<VariantBase> Variant<VariantBase>::create(const VariantBase& data)
return result;
}
+VariantBase Variant<VariantBase>::get() const
+{
+ return VariantBase(g_variant_get_variant(gobject_));
+}
+
// static
const VariantType& Variant<Glib::ustring>::variant_type()
{
diff --git a/glib/src/variant.hg b/glib/src/variant.hg
index d1e7d6c..f9606d3 100644
--- a/glib/src/variant.hg
+++ b/glib/src/variant.hg
@@ -345,7 +345,7 @@ public:
static Variant<VariantBase> create(const Glib::VariantBase& data);
_IGNORE(g_variant_new_variant)
- // The parent's class get() method suffices to get the child variant.
+ VariantBase get() const;
_IGNORE(g_variant_get_variant)
};
diff --git a/tests/glibmm_variant/main.cc b/tests/glibmm_variant/main.cc
index 1f463e4..e84dd75 100644
--- a/tests/glibmm_variant/main.cc
+++ b/tests/glibmm_variant/main.cc
@@ -157,4 +157,41 @@ static void test_dynamic_cast()
catch (const std::bad_cast& e)
{
}
+
+ // A variant of type a{sv}
+ typedef std::map<Glib::ustring, Glib::VariantBase> type_map_sv;
+ typedef Glib::Variant<type_map_sv> type_dict_sv;
+ g_assert((type_dict_sv::variant_type().get_string()) == "a{sv}");
+
+ type_dict_sv var_map;
+ type_map_sv map;
+ Glib::Variant<Glib::ustring> var_string =
+ Glib::Variant<Glib::ustring>::create("test variant");
+ map["test key"] = var_string;
+ var_map = type_dict_sv::create(map);
+ g_assert(var_map.get_type_string() == "a{sv}");
+
+ Glib::VariantBase& ref_var_base = var_map;
+ type_dict_sv var_map_cast = Glib::VariantBase::cast_dynamic<type_dict_sv>(ref_var_base);
+
+ try
+ {
+ Glib::Variant<std::map<Glib::ustring, Glib::ustring> > var_wrong_map =
+ Glib::VariantBase::cast_dynamic<Glib::Variant<std::map<Glib::ustring, Glib::ustring> > >(ref_var_base);
+ g_assert_not_reached();
+ }
+ catch(const std::bad_cast& e)
+ {
+ }
+
+ type_map_sv get_map = var_map_cast.get();
+ var_string = Glib::VariantBase::cast_dynamic<Glib::Variant< Glib::ustring > >(get_map["test key"]);
+ g_assert(var_string.get() == "test variant");
+
+ // A variant of type v
+ Glib::Variant< Glib::VariantBase > var_v = Glib::Variant< Glib::VariantBase >::create(var_string);
+ g_assert(var_v.get_type_string() == "v");
+ Glib::Variant< Glib::ustring > var_s2 =
+ Glib::VariantBase::cast_dynamic<Glib::Variant< Glib::ustring > >(var_v.get());
+ g_assert(var_s2.get() == "test variant");
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]