[glibmm/glibmm-2-52] Variant: Use own get_n_children() instead of C API



commit 79dc3e624426a2ff1bf69c09058cb9927237d617
Author: Daniel Boles <dboles src gnome org>
Date:   Mon May 29 23:03:34 2017 +0100

    Variant: Use own get_n_children() instead of C API
    
    This looks much less ugly.

 glib/src/variant.ccg |   14 +++++---------
 glib/src/variant.hg  |    6 ++----
 2 files changed, 7 insertions(+), 13 deletions(-)
---
diff --git a/glib/src/variant.ccg b/glib/src/variant.ccg
index 9c47dea..ed88877 100644
--- a/glib/src/variant.ccg
+++ b/glib/src/variant.ccg
@@ -184,7 +184,7 @@ VariantContainerBase::create_maybe(const VariantType& child_type, const VariantB
 void
 VariantContainerBase::get_child(VariantBase& child, gsize index) const
 {
-  if (index >= g_variant_n_children(gobject_))
+  if (index >= get_n_children())
     throw std::out_of_range("VariantContainerBase::get_child(): Index out of bounds.");
 
   GVariant* const gvariant = g_variant_get_child_value(gobject_, index);
@@ -466,7 +466,7 @@ Variant<type_vec_ustring>::create(const type_vec_ustring& data)
 Glib::ustring
 Variant<type_vec_ustring>::get_child(gsize index) const
 {
-  if (index >= g_variant_n_children(const_cast<GVariant*>(gobj())))
+  if (index >= get_n_children())
     throw std::out_of_range(
       "Variant< std::vector<Glib::ustring> >::get_child(): Index out of bounds.");
 
@@ -482,9 +482,7 @@ Variant<type_vec_ustring>::get() const
   // but the Variant can alternatively hold an array of object paths or DBus type signatures.
   type_vec_ustring result;
 
-  gsize n_children = g_variant_n_children(const_cast<GVariant*>(gobj()));
-
-  for (gsize i = 0; i < n_children; i++)
+  for (gsize i = 0, n_children = get_n_children(); i < n_children; ++i)
   {
     GVariant* gvariant = g_variant_get_child_value(const_cast<GVariant*>(gobj()), i);
     result.emplace_back(Glib::Variant<Glib::ustring>(gvariant).get());
@@ -564,7 +562,7 @@ Variant<type_vec_string>::create_from_object_paths(const type_vec_string& data)
 std::string
 Variant<type_vec_string>::get_child(gsize index) const
 {
-  if (index >= g_variant_n_children(const_cast<GVariant*>(gobj())))
+  if (index >= get_n_children())
     throw std::out_of_range(
       "Variant< std::vector<std::string> >::get_child(): Index out of bounds.");
 
@@ -581,9 +579,7 @@ Variant<type_vec_string>::get() const
   // signatures.
   type_vec_string result;
 
-  gsize n_children = g_variant_n_children(const_cast<GVariant*>(gobj()));
-
-  for (gsize i = 0; i < n_children; i++)
+  for (gsize i = 0, n_children = get_n_children(); i < n_children; ++i)
   {
     GVariant* gvariant = g_variant_get_child_value(const_cast<GVariant*>(gobj()), i);
     result.emplace_back(Glib::Variant<std::string>(gvariant).get());
diff --git a/glib/src/variant.hg b/glib/src/variant.hg
index de10195..39c63f1 100644
--- a/glib/src/variant.hg
+++ b/glib/src/variant.hg
@@ -1125,7 +1125,7 @@ Variant< std::vector<T> >::create(const std::vector<T>& data)
 template<class T>
 T Variant< std::vector<T> >::get_child(gsize index) const
 {
-  if(index >= g_variant_n_children(const_cast<GVariant*>(gobj())))
+  if (index >= get_n_children())
     throw std::out_of_range(
       "Variant< std::vector<T> >::get_child(): Index out of bounds.");
 
@@ -1143,9 +1143,7 @@ std::vector<T> Variant< std::vector<T> >::get() const
 {
   std::vector<T> result;
 
-  gsize n_children = g_variant_n_children(const_cast<GVariant*>(gobj()));
-
-  for(gsize i = 0; i < n_children; i++)
+  for (gsize i = 0, n_children = get_n_children(); i < n_children; ++i)
   {
     Glib::Variant<T> variant;
 


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