← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/feature-setvar-typed into lp:zorba

 

Paul J. Lucas has proposed merging lp:~zorba-coders/zorba/feature-setvar-typed into lp:zorba.

Commit message:
Added make_iterator() that's more reasonable than ItemSequence (which should eventually be deprecated and removed since it's rather pointless).

Requested reviews:
  Paul J. Lucas (paul-lucas)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/feature-setvar-typed/+merge/217830

Added make_iterator() that's more reasonable than ItemSequence (which should eventually be deprecated and removed since it's rather pointless).
-- 
https://code.launchpad.net/~zorba-coders/zorba/feature-setvar-typed/+merge/217830
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'bin/zorbacmd.cpp'
--- bin/zorbacmd.cpp	2014-04-29 23:20:42 +0000
+++ bin/zorbacmd.cpp	2014-04-30 21:33:35 +0000
@@ -39,7 +39,6 @@
 #include <zorba/store_manager.h>
 #include <zorba/uri_resolvers.h>
 #include <zorba/util/fs_util.h>
-#include <zorba/vector_item_sequence.h>
 #include <zorba/xquery_exception.h>
 #include <zorba/xquery_functions.h>
 #include <zorba/zorba.h>
@@ -281,8 +280,7 @@
               break;
             ++i;
           } // while
-          VectorItemSequence var_seq( vars );
-          dctx->setVariable( var_name, var_seq.getIterator(), true );
+          dctx->setVariable( var_name, make_iterator( vars ), true );
         }
       }
     }

=== modified file 'include/zorba/iterator.h'
--- include/zorba/iterator.h	2014-01-15 02:07:22 +0000
+++ include/zorba/iterator.h	2014-04-30 21:33:35 +0000
@@ -16,12 +16,17 @@
 #ifndef ZORBA_ITERATOR_API_H
 #define ZORBA_ITERATOR_API_H
 
-#include <zorba/config.h>
+// standard
+#include <cassert>
+
+// Zorba
 #include <zorba/api_shared_types.h>
-#include <zorba/item_sequence.h>
+#include <zorba/internal/type_traits.h>
 
 namespace zorba {
 
+///////////////////////////////////////////////////////////////////////////////
+
 /**
  * \brief Interface for an Iterator over a sequence of items.
  *
@@ -96,7 +101,64 @@
   skip(int64_t count);
 };
 
-
-} /* namespace zorba */
+///////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Creates an \c Iterator over some container type of \c Item, e.g.,
+ * <code>vector&lt;Item&gt;</code>.
+ *
+ * @param container The container to create the \c Iterator over.
+ * @param copy If \c false, \a container is swapped with an internal one thus
+ * clearing \a container; if \c true, a copy of \a container is made instead.
+ * @return Returns a new \c Iterator over \a container.
+ */
+template<class ContainerType>
+typename std::enable_if<
+  std::is_same<typename ContainerType::value_type,Item>::value,Iterator_t>::type
+make_iterator( ContainerType &container, bool copy = false ) {
+
+  struct iterator_impl : Iterator {
+    iterator_impl( ContainerType &container, bool copy ) : open_( false ) {
+      if ( copy )
+        container_ = container;
+      else
+        container_.swap( container );
+    }
+
+    void close() {
+      assert( open_ );
+      open_ = false;
+    }
+
+    bool isOpen() const {
+      return open_;
+    }
+
+    bool next( Item &result ) {
+      assert( open_ );
+      if ( pos_ == container_.end() )
+        return false;
+      result = *pos_;
+      ++pos_;
+      return true;
+    }
+
+    void open() {
+      assert( !open_ );
+      pos_ = container_.begin();
+      open_ = true;
+    }
+
+    ContainerType container_;
+    bool open_;
+    typename ContainerType::const_iterator pos_;
+  };
+
+  return Iterator_t( new iterator_impl( container, copy ) );
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+} // namespace zorba
 #endif
 /* vim:set et sw=2 ts=2: */


Follow ups