[geary/wip/search-columns-714494: 2/2] Add to_gee_iterable



commit d1006e77d79be2c43c6f61173acc030c013577bc
Author: Charles Lindsay <chaz yorba org>
Date:   Fri Dec 13 16:07:16 2013 -0800

    Add to_gee_iterable
    
    My thinking here is that we don't want to make use of Geary.Iterable in
    any interfaces (in the API sense, not the Vala sense), but we want to
    stick with standard Gee classes.  I think in the past we've often used
    generic Collections to say "we don't care how these are stored, we just
    need to iterate over them".  I propose using Gee.Iterable to mean that
    same thing, with the added guarantee that we only iterate over them
    once.  We can keep using Collections the same way we always have for
    anything we need to iterate over more than once, or for things we need
    to know the size of ahead of time, etc.
    
    Toward that end, where Geary.traverse() is a convenient way to get a
    Geary.Iterable from a Gee.Iterable, to_gee_iterable() is a convenient
    way to get a Gee.Iterable from a Geary.Iterable.  It's so lightweight it
    shouldn't be an issue to occasionally translate into/out of
    Gee.Iterables at API boundaries.

 src/engine/util/util-iterable.vala |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)
---
diff --git a/src/engine/util/util-iterable.vala b/src/engine/util/util-iterable.vala
index 4c5ad05..4eb004a 100644
--- a/src/engine/util/util-iterable.vala
+++ b/src/engine/util/util-iterable.vala
@@ -5,6 +5,9 @@
  */
 
 namespace Geary {
+    /**
+     * Take a Gee object and return a Geary.Iterable for convenience.
+     */
     public Geary.Iterable<G> traverse<G>(Gee.Iterable<G> i) {
         return new Geary.Iterable<G>(i.iterator());
     }
@@ -21,6 +24,31 @@ namespace Geary {
  */
 
 public class Geary.Iterable<G> : BaseObject {
+    /**
+     * A private class that lets us take a Geary.Iterable and convert it back
+     * into a Gee.Iterable.
+     */
+    private class GeeIterable<G> : Gee.Traversable<G>, Gee.Iterable<G>, BaseObject {
+        private Gee.Iterator<G> i;
+        
+        public GeeIterable(Gee.Iterator<G> iterator) {
+            i = iterator;
+        }
+        
+        public Gee.Iterator<G> iterator() {
+            return i;
+        }
+        
+        // Unfortunately necessary for Gee.Traversable.
+        public virtual bool @foreach(Gee.ForallFunc<G> f) {
+            foreach (G g in this) {
+                if (!f(g))
+                    return false;
+            }
+            return true;
+        }
+    }
+
     private Gee.Iterator<G> i;
     
     public Iterable(Gee.Iterator<G> iterator) {
@@ -100,6 +128,14 @@ public class Geary.Iterable<G> : BaseObject {
         return count;
     }
     
+    /**
+     * The resulting Gee.Iterable comes with the same caveat that you may only
+     * iterate over it once.
+     */
+    public Gee.Iterable<G> to_gee_iterable() {
+        return new GeeIterable<G>(i);
+    }
+    
     public Gee.Collection<G> add_all_to(Gee.Collection<G> c) {
         while (i.next())
             c.add(i  get());


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