[glibmm] Variant test: Use the dictionary variant classes in the test.



commit 685d61b793b977ef2b89a6877178b8f2bc958f8b
Author: José Alburquerque <jaalburqu svn gnome org>
Date:   Tue Dec 28 19:42:34 2010 -0500

    Variant test: Use the dictionary variant classes in the test.
    
    	* glib/src/variant.{ccg,hg} (get_iter): Make all the get_iter()
    	methods in the variant container classes const.
    	(Variant< std::pair<K, V> >::get):
    	(Variant< std::map<K, V> >::lookup):
    	(std::map<K, V> Variant< std::map<K, V> >::get): Correct the getting
    	of a std::pair<> from the Variant<> and the getting of children from
    	the parent VariantContainerBase class.
    	(Variant< std::map<K, V> >::create): Create the dictionary entries as
    	Variants and then use g_variant_builder_add_value() to add them
    	instead of g_variant_add() which is easier.
    
    	* tests/glibmm_variant/main.cc: Use the updated dictionary classes in
    	the test.

 ChangeLog                    |   18 +++++++++++
 glib/src/variant.ccg         |    4 +-
 glib/src/variant.hg          |   45 +++++++++++++++++-----------
 tests/glibmm_variant/main.cc |   66 +++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 112 insertions(+), 21 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index bc8a243..f4235bb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2010-12-28  José Alburquerque  <jaalburqu svn gnome org>
+
+	Variant test: Use the dictionary variant classes in the test.
+
+	* glib/src/variant.{ccg,hg} (get_iter): Make all the get_iter()
+	methods in the variant container classes const.
+	(Variant< std::pair<K, V> >::get):
+	(Variant< std::map<K, V> >::lookup):
+	(std::map<K, V> Variant< std::map<K, V> >::get): Correct the getting
+	of a std::pair<> from the Variant<> and the getting of children from
+	the parent VariantContainerBase class.
+	(Variant< std::map<K, V> >::create): Create the dictionary entries as
+	Variants and then use g_variant_builder_add_value() to add them
+	instead of g_variant_add() which is easier.
+
+	* tests/glibmm_variant/main.cc: Use the updated dictionary classes in
+	the test.
+
 2010-12-26  José Alburquerque  <jaalburqu svn gnome org>
 
 	Variant: Add dictionary entry and dictionary specializations.
diff --git a/glib/src/variant.ccg b/glib/src/variant.ccg
index 72b0741..e58246e 100644
--- a/glib/src/variant.ccg
+++ b/glib/src/variant.ccg
@@ -226,7 +226,7 @@ type_vec_ustring Variant<type_vec_ustring>::get() const
   return result;
 }
 
-VariantIter Variant<type_vec_ustring>::get_iter()
+VariantIter Variant<type_vec_ustring>::get_iter() const
 {
   // Get the variant type of the elements.
   VariantType element_variant_type = Variant<Glib::ustring>::variant_type();
@@ -312,7 +312,7 @@ type_vec_string Variant<type_vec_string>::get() const
   return result;
 }
 
-VariantIter Variant<type_vec_string>::get_iter()
+VariantIter Variant<type_vec_string>::get_iter() const
 {
   // Get the variant type of the elements.
   VariantType element_variant_type = Variant<std::string>::variant_type();
diff --git a/glib/src/variant.hg b/glib/src/variant.hg
index 6009ae0..d0ccdd5 100644
--- a/glib/src/variant.hg
+++ b/glib/src/variant.hg
@@ -523,7 +523,7 @@ public:
    * @return the VaraintIter.
    * @newin{2,28}
    */
-  VariantIter get_iter();
+  VariantIter get_iter() const;
 };
 
 /** Specialization of Glib::Variant containing an array of UTF-8 capable
@@ -592,7 +592,7 @@ public:
    * @return the VaraintIter.
    * @newin{2,28}
    */
-  VariantIter get_iter();
+  VariantIter get_iter() const;
 };
 
 /** Specialization of Glib::Variant containing an array of non-UTF-8 strings
@@ -661,7 +661,7 @@ public:
    * @return the VaraintIter.
    * @newin{2,28}
    */
-  VariantIter get_iter();
+  VariantIter get_iter() const;
 }; 
 
 /** Specialization of Glib::Variant containing a dictionary (a map of (key,
@@ -736,7 +736,7 @@ public:
    * @return the VaraintIter.
    * @newin{2,28}
    */
-  VariantIter get_iter();
+  VariantIter get_iter() const;
 };
 
 } // namespace Glib
@@ -777,7 +777,7 @@ const VariantType& Variant< std::pair<K, V> >::variant_type()
 
 template<class K, class V>
 Variant< std::pair<K, V> >
-Variant< std::pair<K, V> >::create(const  std::pair<K, V>& data)
+Variant< std::pair<K, V> >::create(const std::pair<K, V>& data)
 {
   Variant<K> key = Variant<K>::create(data.first);
   Variant<V> value = Variant<V>::create(data.second);
@@ -794,10 +794,13 @@ Variant< std::pair<K, V> >::create(const  std::pair<K, V>& data)
 template<class K, class V>
 std::pair<K, V> Variant< std::pair<K, V> >::get() const
 {
-  // Get the key and the value (which are the first and second elements of
-  // this VariantBaseContainer respectively).
-  Variant<K> key = get(0);
-  Variant<V> value = get(1);
+  // Get the key (the first element of the this VariantContainerBase).
+  Variant<K> key;
+  VariantContainerBase::get(key, 0);
+
+  // Get the value (the second element of the this VariantContainerBase).
+  Variant<V> value;
+  VariantContainerBase::get(value, 1);
 
   std::pair<K, V> result(key.get(), value.get());
 
@@ -877,7 +880,7 @@ std::vector<T> Variant< std::vector<T> >::get() const
 }
 
 template<class T>
-VariantIter Variant< std::vector<T> >::get_iter()
+VariantIter Variant< std::vector<T> >::get_iter() const
 {
   // Get the variant type of the elements.
   VariantType element_variant_type = Variant<T>::variant_type();
@@ -921,10 +924,12 @@ Variant< std::map<K, V> >::create(const std::map<K, V>& data)
 
   // Add the elements of the vector into the builder.
   for(typename std::map<K, V>::const_iterator iter = data.begin();
-    iter < data.end(); iter++)
+    iter != data.end(); iter++)
   {
-    g_variant_builder_add(builder,
-      reinterpret_cast<gchar*>(element_variant_type.gobj()), *iter);
+    Variant< std::pair<K, V> > dict_entry =
+      Variant< std::pair<K, V> >::create(*iter);
+
+    g_variant_builder_add_value(builder, dict_entry.gobj());
   }
 
   // Create the variant using the builder.
@@ -958,9 +963,13 @@ bool Variant< std::map<K, V> >::lookup(const K& key, V& value) const
 
   while(iter.next_value(entry))
   {
-    if(entry.first == key)
-      value = entry.second;
-    return true;
+    std::pair<K, V> element = entry.get();
+
+    if(element.first == key)
+    {
+      value = element.second;
+      return true;
+    }
   }
 
   return false;
@@ -975,14 +984,14 @@ std::map<K, V> Variant< std::map<K, V> >::get() const
 
   while(iter.next_value(entry))
   {
-    result.insert(std::pair<K, V>(entry.first, entry.second));
+    result.insert(entry.get());
   }
 
   return result;
 }
 
 template<class K, class V>
-VariantIter Variant< std::map<K, V> >::get_iter()
+VariantIter Variant< std::map<K, V> >::get_iter() const
 {
   // Get the variant type of the elements.
   VariantType element_variant_type =
diff --git a/tests/glibmm_variant/main.cc b/tests/glibmm_variant/main.cc
index 71d3bbd..9a69491 100644
--- a/tests/glibmm_variant/main.cc
+++ b/tests/glibmm_variant/main.cc
@@ -32,9 +32,73 @@ int main(int, char**)
     "variant are " << integers_variant.get_iter().get_n_children() <<
     '.' << std::endl;
 
-  int index = 4;
+  unsigned index = 4;
   std::cout << "Element number " << index + 1 << " in the copy is " <<
     integers_variant.get(index) << '.' << std::endl;
 
+  std::cout << std::endl;
+
+  typedef std::pair<Glib::ustring, Glib::ustring> TypeDictEntry;
+
+  TypeDictEntry dict_entry("A key", "A value");
+
+  std::cout << "The original dictionary entry is (" << dict_entry.first <<
+    ", " << dict_entry.second << ")." << std::endl;
+
+  Glib::Variant<TypeDictEntry> dict_entry_variant =
+    Glib::Variant<TypeDictEntry>::create(dict_entry);
+
+  TypeDictEntry copy_entry = dict_entry_variant.get();
+
+  std::cout << "The copy dictionary entry is (" << copy_entry.first <<
+    ", " << copy_entry.second << ")." << std::endl;
+
+  std::cout << std::endl;
+
+  typedef std::map<unsigned, Glib::ustring> TypeDict;
+
+  TypeDict orig_dict;
+
+  for(unsigned i = 0; i < 10; i++)
+  {
+    std::string x_repeated(i, 'x');
+    orig_dict.insert(std::pair<unsigned, Glib::ustring>(i, x_repeated));
+  }
+
+  std::cout << "The original dictionary:" << std::endl;
+
+  for(unsigned i = 0; i < orig_dict.size(); i++)
+  {
+    std::cout << "(" << i << ", " << orig_dict[i] << ")." << std::endl;
+  }
+
+  Glib::Variant<TypeDict> orig_dict_variant =
+    Glib::Variant<TypeDict>::create(orig_dict);
+
+  TypeDict dict_copy = orig_dict_variant.get();
+
+  std::cout << "The copy of the dictionary:" << std::endl;
+
+  for(unsigned i = 0; i < dict_copy.size(); i++)
+  {
+    std::cout << "(" << i << ", " << dict_copy[i] << ")." << std::endl;
+  }
+
+  index = 3;
+
+  std::pair<unsigned, Glib::ustring> a_pair = orig_dict_variant.get(index);
+
+  std::cout << "Element number " << index + 1 << " in the variant is: (" <<
+    a_pair.first << ", " << a_pair.second << ")." << std::endl;
+
+
+  Glib::ustring value;
+
+  if(orig_dict_variant.lookup(index, value))
+  {
+    std::cout << "The x's of element number " << index + 1 <<
+      " in the variant are: " << value << '.' << std::endl;
+  }
+
   return 0;
 }



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