[glibmm] Variant: Add cast_dynamic<> specializations for strings.



commit 3851639d919a9f779171648b7cbb8d1e74307204
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Mar 28 14:18:59 2011 +0200

    Variant: Add cast_dynamic<> specializations for strings.
    
    * glib/src/variant.[hg|ccg]: Add cast_dynamic() specializations for
    Vector<std::string> and Vector<Glib::ustring>, because this type seems
    appropriate for multiple types. For instance, see
    http://library.gnome.org/devel/glib/unstable/glib-GVariant.html#g-variant-get-string
    Thanks to Povietkin Konstantin.

 ChangeLog            |   10 +++++++++
 glib/src/variant.ccg |   51 +++++++++++++++++++++++++++++++++++++++++++++++++-
 glib/src/variant.hg  |    6 +++++
 3 files changed, 66 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d92c7dd..8963d7f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-03-28  Murray Cumming  <murrayc murrayc com>
+
+	Variant: Add cast_dynamic<> specializations for strings.
+
+	* glib/src/variant.[hg|ccg]: Add cast_dynamic() specializations for 
+	Vector<std::string> and Vector<Glib::ustring>, because this type seems 
+	appropriate for multiple types. For instance, see 
+	http://library.gnome.org/devel/glib/unstable/glib-GVariant.html#g-variant-get-string
+	Thanks to Povietkin Konstantin.
+
 2.27.99.1:
 
 2011-03-27  Murray Cumming  <murrayc murrayc com>
diff --git a/glib/src/variant.ccg b/glib/src/variant.ccg
index df93dcb..c41bfb5 100644
--- a/glib/src/variant.ccg
+++ b/glib/src/variant.ccg
@@ -95,7 +95,7 @@ template<>
 VariantContainerBase VariantBase::cast_dynamic<VariantContainerBase>(const VariantBase& v)
 throw(std::bad_cast)
 {
-  if(v.gobj() == NULL)
+  if(!v.gobj())
   {
     return VariantContainerBase();
   }
@@ -182,6 +182,30 @@ Glib::ustring Variant<Glib::ustring>::get() const
   return Glib::ustring(g_variant_get_string(gobject_, 0));
 }
 
+// Variant<std::string> makes sense for multiple types.
+// See http://library.gnome.org/devel/glib/unstable/glib-GVariant.html#g-variant-get-string
+template<>
+Variant<Glib::ustring> VariantBase::cast_dynamic< Variant<Glib::ustring> >(const VariantBase& v)
+throw(std::bad_cast)
+{
+  if(!v.gobj())
+  {
+    return Variant<Glib::ustring>();
+  }
+
+  const VariantType vtype = v.get_type();
+  if( vtype.equal(VariantTypeString) ||
+      vtype.equal(VariantTypeObjectPath) ||
+      vtype.equal(VariantTypeSignature) )
+  {
+    return Variant<Glib::ustring>(const_cast<GVariant*>(v.gobj()), true);
+  }
+  else
+  {
+    throw std::bad_cast();
+  }
+}
+
 // static
 const VariantType& Variant<std::string>::variant_type()
 {
@@ -201,6 +225,31 @@ Variant<std::string>::create(const std::string& data)
   return result;
 }
 
+// Variant<std::string> makes sense for multiple types.
+// See http://library.gnome.org/devel/glib/unstable/glib-GVariant.html#g-variant-get-string
+template<>
+Variant<std::string> VariantBase::cast_dynamic< Variant<std::string> >(const VariantBase& v)
+throw(std::bad_cast)
+{
+  if(!v.gobj())
+  {
+    return Variant<std::string>();
+  }
+
+  const VariantType vtype = v.get_type();
+  if( vtype.equal(VariantTypeString) ||
+      vtype.equal(VariantTypeByteString) ||  
+      vtype.equal(VariantTypeObjectPath) ||
+      vtype.equal(VariantTypeSignature) )
+  {
+    return Variant<std::string>(const_cast<GVariant*>(v.gobj()), true);
+  }
+  else
+  {
+    throw std::bad_cast();
+  }
+}
+
 typedef std::vector<Glib::ustring> type_vec_ustring;
 
 // static
diff --git a/glib/src/variant.hg b/glib/src/variant.hg
index 22c5d7a..ddaaf63 100644
--- a/glib/src/variant.hg
+++ b/glib/src/variant.hg
@@ -230,6 +230,7 @@ public:
    */
   static void create_object_path(VariantStringBase& output,
     const std::string& object_path);
+  _IGNORE(g_variant_new_object_path)
 
   _WRAP_METHOD(static bool is_object_path(const std::string& string), g_variant_is_object_path)
 
@@ -243,6 +244,7 @@ public:
    */
   static void create_signature(VariantStringBase& output,
     const std::string& object_path);
+  _IGNORE(g_variant_new_signature)
 
   _WRAP_METHOD(static bool is_signature(const std::string& string), g_variant_is_signature)
 };
@@ -498,6 +500,10 @@ public:
    _IGNORE(g_variant_dup_bytestring)
 };
 
+template<>
+Variant<std::string> VariantBase::cast_dynamic< Variant<std::string> >(const VariantBase& v)
+throw(std::bad_cast);
+
 /** Specialization of Variant containing a dictionary entry.  See also
  * Variant< std::map<K, V> >.
  * @newin{2,28}



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